blob: 2e960485f47c0903fa9ea142dd8a85e6782c6723 [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";
97 case Del:
98 return "Del";
99 case AugLoad:
100 return "AugLoad";
101 case AugStore:
102 return "AugStore";
103 case Param:
104 return "Param";
105 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700106 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500107 }
108}
109
110static int
111validate_arguments(arguments_ty args)
112{
113 if (!validate_args(args->args))
114 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700115 if (args->vararg && args->vararg->annotation
116 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500117 return 0;
118 }
119 if (!validate_args(args->kwonlyargs))
120 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100121 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700122 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500123 return 0;
124 }
125 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
126 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
127 return 0;
128 }
129 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
130 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
131 "kw_defaults on arguments");
132 return 0;
133 }
134 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
135}
136
137static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100138validate_constant(PyObject *value)
139{
140 if (value == Py_None || value == Py_Ellipsis)
141 return 1;
142
143 if (PyLong_CheckExact(value)
144 || PyFloat_CheckExact(value)
145 || PyComplex_CheckExact(value)
146 || PyBool_Check(value)
147 || PyUnicode_CheckExact(value)
148 || PyBytes_CheckExact(value))
149 return 1;
150
151 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
152 PyObject *it;
153
154 it = PyObject_GetIter(value);
155 if (it == NULL)
156 return 0;
157
158 while (1) {
159 PyObject *item = PyIter_Next(it);
160 if (item == NULL) {
161 if (PyErr_Occurred()) {
162 Py_DECREF(it);
163 return 0;
164 }
165 break;
166 }
167
168 if (!validate_constant(item)) {
169 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 return 0;
172 }
Victor Stinner726f6902016-01-27 00:11:47 +0100173 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100174 }
175
176 Py_DECREF(it);
177 return 1;
178 }
179
180 return 0;
181}
182
183static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500184validate_expr(expr_ty exp, expr_context_ty ctx)
185{
186 int check_ctx = 1;
187 expr_context_ty actual_ctx;
188
189 /* First check expression context. */
190 switch (exp->kind) {
191 case Attribute_kind:
192 actual_ctx = exp->v.Attribute.ctx;
193 break;
194 case Subscript_kind:
195 actual_ctx = exp->v.Subscript.ctx;
196 break;
197 case Starred_kind:
198 actual_ctx = exp->v.Starred.ctx;
199 break;
200 case Name_kind:
201 actual_ctx = exp->v.Name.ctx;
202 break;
203 case List_kind:
204 actual_ctx = exp->v.List.ctx;
205 break;
206 case Tuple_kind:
207 actual_ctx = exp->v.Tuple.ctx;
208 break;
209 default:
210 if (ctx != Load) {
211 PyErr_Format(PyExc_ValueError, "expression which can't be "
212 "assigned to in %s context", expr_context_name(ctx));
213 return 0;
214 }
215 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100216 /* set actual_ctx to prevent gcc warning */
217 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500218 }
219 if (check_ctx && actual_ctx != ctx) {
220 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
221 expr_context_name(ctx), expr_context_name(actual_ctx));
222 return 0;
223 }
224
225 /* Now validate expression. */
226 switch (exp->kind) {
227 case BoolOp_kind:
228 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
229 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
230 return 0;
231 }
232 return validate_exprs(exp->v.BoolOp.values, Load, 0);
233 case BinOp_kind:
234 return validate_expr(exp->v.BinOp.left, Load) &&
235 validate_expr(exp->v.BinOp.right, Load);
236 case UnaryOp_kind:
237 return validate_expr(exp->v.UnaryOp.operand, Load);
238 case Lambda_kind:
239 return validate_arguments(exp->v.Lambda.args) &&
240 validate_expr(exp->v.Lambda.body, Load);
241 case IfExp_kind:
242 return validate_expr(exp->v.IfExp.test, Load) &&
243 validate_expr(exp->v.IfExp.body, Load) &&
244 validate_expr(exp->v.IfExp.orelse, Load);
245 case Dict_kind:
246 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
247 PyErr_SetString(PyExc_ValueError,
248 "Dict doesn't have the same number of keys as values");
249 return 0;
250 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400251 /* null_ok=1 for keys expressions to allow dict unpacking to work in
252 dict literals, i.e. ``{**{a:b}}`` */
253 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
254 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500255 case Set_kind:
256 return validate_exprs(exp->v.Set.elts, Load, 0);
257#define COMP(NAME) \
258 case NAME ## _kind: \
259 return validate_comprehension(exp->v.NAME.generators) && \
260 validate_expr(exp->v.NAME.elt, Load);
261 COMP(ListComp)
262 COMP(SetComp)
263 COMP(GeneratorExp)
264#undef COMP
265 case DictComp_kind:
266 return validate_comprehension(exp->v.DictComp.generators) &&
267 validate_expr(exp->v.DictComp.key, Load) &&
268 validate_expr(exp->v.DictComp.value, Load);
269 case Yield_kind:
270 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500271 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000272 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400273 case Await_kind:
274 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500275 case Compare_kind:
276 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
277 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
278 return 0;
279 }
280 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
281 asdl_seq_LEN(exp->v.Compare.ops)) {
282 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
283 "of comparators and operands");
284 return 0;
285 }
286 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
287 validate_expr(exp->v.Compare.left, Load);
288 case Call_kind:
289 return validate_expr(exp->v.Call.func, Load) &&
290 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400291 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100292 case Constant_kind:
293 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100294 PyErr_Format(PyExc_TypeError,
295 "got an invalid type in Constant: %s",
296 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100297 return 0;
298 }
299 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400300 case JoinedStr_kind:
301 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
302 case FormattedValue_kind:
303 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
304 return 0;
305 if (exp->v.FormattedValue.format_spec)
306 return validate_expr(exp->v.FormattedValue.format_spec, Load);
307 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500308 case Attribute_kind:
309 return validate_expr(exp->v.Attribute.value, Load);
310 case Subscript_kind:
311 return validate_slice(exp->v.Subscript.slice) &&
312 validate_expr(exp->v.Subscript.value, Load);
313 case Starred_kind:
314 return validate_expr(exp->v.Starred.value, ctx);
315 case List_kind:
316 return validate_exprs(exp->v.List.elts, ctx, 0);
317 case Tuple_kind:
318 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300319 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500320 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500321 return 1;
322 default:
323 PyErr_SetString(PyExc_SystemError, "unexpected expression");
324 return 0;
325 }
326}
327
328static int
329validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
330{
331 if (asdl_seq_LEN(seq))
332 return 1;
333 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
334 return 0;
335}
336
337static int
338validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
339{
340 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
341 validate_exprs(targets, ctx, 0);
342}
343
344static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300345validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348}
349
350static int
351validate_stmt(stmt_ty stmt)
352{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100353 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500354 switch (stmt->kind) {
355 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300356 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500357 validate_arguments(stmt->v.FunctionDef.args) &&
358 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
359 (!stmt->v.FunctionDef.returns ||
360 validate_expr(stmt->v.FunctionDef.returns, Load));
361 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300362 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500363 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
364 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400365 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500366 case Return_kind:
367 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
368 case Delete_kind:
369 return validate_assignlist(stmt->v.Delete.targets, Del);
370 case Assign_kind:
371 return validate_assignlist(stmt->v.Assign.targets, Store) &&
372 validate_expr(stmt->v.Assign.value, Load);
373 case AugAssign_kind:
374 return validate_expr(stmt->v.AugAssign.target, Store) &&
375 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700376 case AnnAssign_kind:
377 if (stmt->v.AnnAssign.target->kind != Name_kind &&
378 stmt->v.AnnAssign.simple) {
379 PyErr_SetString(PyExc_TypeError,
380 "AnnAssign with simple non-Name target");
381 return 0;
382 }
383 return validate_expr(stmt->v.AnnAssign.target, Store) &&
384 (!stmt->v.AnnAssign.value ||
385 validate_expr(stmt->v.AnnAssign.value, Load)) &&
386 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500387 case For_kind:
388 return validate_expr(stmt->v.For.target, Store) &&
389 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300390 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500391 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400392 case AsyncFor_kind:
393 return validate_expr(stmt->v.AsyncFor.target, Store) &&
394 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300395 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400396 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500397 case While_kind:
398 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300399 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500400 validate_stmts(stmt->v.While.orelse);
401 case If_kind:
402 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300403 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500404 validate_stmts(stmt->v.If.orelse);
405 case With_kind:
406 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
407 return 0;
408 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
409 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
410 if (!validate_expr(item->context_expr, Load) ||
411 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
412 return 0;
413 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300414 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400415 case AsyncWith_kind:
416 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
417 return 0;
418 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
419 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
420 if (!validate_expr(item->context_expr, Load) ||
421 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
422 return 0;
423 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300424 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500425 case Raise_kind:
426 if (stmt->v.Raise.exc) {
427 return validate_expr(stmt->v.Raise.exc, Load) &&
428 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
429 }
430 if (stmt->v.Raise.cause) {
431 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
432 return 0;
433 }
434 return 1;
435 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300436 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500437 return 0;
438 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
439 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
440 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
441 return 0;
442 }
443 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
444 asdl_seq_LEN(stmt->v.Try.orelse)) {
445 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
446 return 0;
447 }
448 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
449 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
450 if ((handler->v.ExceptHandler.type &&
451 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300452 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500453 return 0;
454 }
455 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
456 validate_stmts(stmt->v.Try.finalbody)) &&
457 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
458 validate_stmts(stmt->v.Try.orelse));
459 case Assert_kind:
460 return validate_expr(stmt->v.Assert.test, Load) &&
461 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
462 case Import_kind:
463 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
464 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300465 if (stmt->v.ImportFrom.level < 0) {
466 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500467 return 0;
468 }
469 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
470 case Global_kind:
471 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
472 case Nonlocal_kind:
473 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
474 case Expr_kind:
475 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400476 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300477 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400478 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
479 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
480 (!stmt->v.AsyncFunctionDef.returns ||
481 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500482 case Pass_kind:
483 case Break_kind:
484 case Continue_kind:
485 return 1;
486 default:
487 PyErr_SetString(PyExc_SystemError, "unexpected statement");
488 return 0;
489 }
490}
491
492static int
493validate_stmts(asdl_seq *seq)
494{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100495 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500496 for (i = 0; i < asdl_seq_LEN(seq); i++) {
497 stmt_ty stmt = asdl_seq_GET(seq, i);
498 if (stmt) {
499 if (!validate_stmt(stmt))
500 return 0;
501 }
502 else {
503 PyErr_SetString(PyExc_ValueError,
504 "None disallowed in statement list");
505 return 0;
506 }
507 }
508 return 1;
509}
510
511static int
512validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
513{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100514 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500515 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
516 expr_ty expr = asdl_seq_GET(exprs, i);
517 if (expr) {
518 if (!validate_expr(expr, ctx))
519 return 0;
520 }
521 else if (!null_ok) {
522 PyErr_SetString(PyExc_ValueError,
523 "None disallowed in expression list");
524 return 0;
525 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100526
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500527 }
528 return 1;
529}
530
531int
532PyAST_Validate(mod_ty mod)
533{
534 int res = 0;
535
536 switch (mod->kind) {
537 case Module_kind:
538 res = validate_stmts(mod->v.Module.body);
539 break;
540 case Interactive_kind:
541 res = validate_stmts(mod->v.Interactive.body);
542 break;
543 case Expression_kind:
544 res = validate_expr(mod->v.Expression.body, Load);
545 break;
546 case Suite_kind:
547 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
548 break;
549 default:
550 PyErr_SetString(PyExc_SystemError, "impossible module node");
551 res = 0;
552 break;
553 }
554 return res;
555}
556
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500557/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500558#include "grammar.h"
559#include "parsetok.h"
560#include "graminit.h"
561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562/* Data structure used internally */
563struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400564 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200565 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500566 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567};
568
569static asdl_seq *seq_for_testlist(struct compiling *, const node *);
570static expr_ty ast_for_expr(struct compiling *, const node *);
571static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300572static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000573static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
574 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000575static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000576static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577
guoci90fc8982018-09-11 17:45:45 -0400578static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
579static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200582static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000583 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000585static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400586static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000587static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Nick Coghlan650f0d02007-04-15 12:05:43 +0000589#define COMP_GENEXP 0
590#define COMP_LISTCOMP 1
591#define COMP_SETCOMP 2
592
Benjamin Peterson55e00432012-01-16 17:22:31 -0500593static int
594init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000595{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
597 if (!m)
598 return 0;
599 c->c_normalize = PyObject_GetAttrString(m, "normalize");
600 Py_DECREF(m);
601 if (!c->c_normalize)
602 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500603 return 1;
604}
605
606static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400607new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500608{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400609 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500610 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000611 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500612 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500613 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000614 /* Check whether there are non-ASCII characters in the
615 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500616 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300618 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500619 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500620 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200621 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500622 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300623 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
624 if (form == NULL) {
625 Py_DECREF(id);
626 return NULL;
627 }
628 PyObject *args[2] = {form, id};
629 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500630 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200631 if (!id2)
632 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300633 if (!PyUnicode_Check(id2)) {
634 PyErr_Format(PyExc_TypeError,
635 "unicodedata.normalize() must return a string, not "
636 "%.200s",
637 Py_TYPE(id2)->tp_name);
638 Py_DECREF(id2);
639 return NULL;
640 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200641 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000642 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000643 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200644 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
645 Py_DECREF(id);
646 return NULL;
647 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000648 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649}
650
Benjamin Peterson55e00432012-01-16 17:22:31 -0500651#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200654ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400656 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200657 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200659 va_start(va, errmsg);
660 errstr = PyUnicode_FromFormatV(errmsg, va);
661 va_end(va);
662 if (!errstr) {
663 return 0;
664 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200665 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000667 Py_INCREF(Py_None);
668 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 }
Ammar Askar025eb982018-09-24 17:12:49 -0400670 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200671 if (!tmp) {
672 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400673 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000674 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000675 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 Py_DECREF(errstr);
677 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400678 if (value) {
679 PyErr_SetObject(PyExc_SyntaxError, value);
680 Py_DECREF(value);
681 }
682 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683}
684
685/* num_stmts() returns number of contained statements.
686
687 Use this routine to determine how big a sequence is needed for
688 the statements in a parse tree. Its raison d'etre is this bit of
689 grammar:
690
691 stmt: simple_stmt | compound_stmt
692 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
693
694 A simple_stmt can contain multiple small_stmt elements joined
695 by semicolons. If the arg is a simple_stmt, the number of
696 small_stmt elements is returned.
697*/
698
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800699static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800700new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800701{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800702 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800703 if (res == NULL)
704 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800705 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
706 Py_DECREF(res);
707 return NULL;
708 }
709 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800710}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800711#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713static int
714num_stmts(const node *n)
715{
716 int i, l;
717 node *ch;
718
719 switch (TYPE(n)) {
720 case single_input:
721 if (TYPE(CHILD(n, 0)) == NEWLINE)
722 return 0;
723 else
724 return num_stmts(CHILD(n, 0));
725 case file_input:
726 l = 0;
727 for (i = 0; i < NCH(n); i++) {
728 ch = CHILD(n, i);
729 if (TYPE(ch) == stmt)
730 l += num_stmts(ch);
731 }
732 return l;
733 case stmt:
734 return num_stmts(CHILD(n, 0));
735 case compound_stmt:
736 return 1;
737 case simple_stmt:
738 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
739 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800740 case func_body_suite:
741 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
742 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 if (NCH(n) == 1)
744 return num_stmts(CHILD(n, 0));
745 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800746 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800748 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
749 i += 2;
750 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 l += num_stmts(CHILD(n, i));
752 return l;
753 }
754 default: {
755 char buf[128];
756
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000757 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 TYPE(n), NCH(n));
759 Py_FatalError(buf);
760 }
761 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700762 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763}
764
765/* Transform the CST rooted at node * to the appropriate AST
766*/
767
768mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200769PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
770 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000772 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800774 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 stmt_ty s;
776 node *ch;
777 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500778 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800779 asdl_seq *argtypes = NULL;
780 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400782 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200783 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400784 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800785 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800786
787 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789
Jeremy Hyltona8293132006-02-28 17:58:27 +0000790 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 switch (TYPE(n)) {
792 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200793 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500795 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 for (i = 0; i < NCH(n) - 1; i++) {
797 ch = CHILD(n, i);
798 if (TYPE(ch) == NEWLINE)
799 continue;
800 REQ(ch, stmt);
801 num = num_stmts(ch);
802 if (num == 1) {
803 s = ast_for_stmt(&c, ch);
804 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000806 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808 else {
809 ch = CHILD(ch, 0);
810 REQ(ch, simple_stmt);
811 for (j = 0; j < num; j++) {
812 s = ast_for_stmt(&c, CHILD(ch, j * 2));
813 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500814 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000815 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 }
817 }
818 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800819
820 /* Type ignores are stored under the ENDMARKER in file_input. */
821 ch = CHILD(n, NCH(n) - 1);
822 REQ(ch, ENDMARKER);
823 num = NCH(ch);
824 type_ignores = _Py_asdl_seq_new(num, arena);
825 if (!type_ignores)
826 goto out;
827
828 for (i = 0; i < num; i++) {
829 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
830 if (!ti)
831 goto out;
832 asdl_seq_SET(type_ignores, i, ti);
833 }
834
835 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500836 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 case eval_input: {
838 expr_ty testlist_ast;
839
Nick Coghlan650f0d02007-04-15 12:05:43 +0000840 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000841 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 goto out;
844 res = Expression(testlist_ast, arena);
845 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 }
847 case single_input:
848 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200849 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500851 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000852 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000853 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000855 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500856 goto out;
857 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
859 else {
860 n = CHILD(n, 0);
861 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200862 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500864 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000866 s = ast_for_stmt(&c, n);
867 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500868 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 asdl_seq_SET(stmts, 0, s);
870 }
871 else {
872 /* Only a simple_stmt can contain multiple statements. */
873 REQ(n, simple_stmt);
874 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 if (TYPE(CHILD(n, i)) == NEWLINE)
876 break;
877 s = ast_for_stmt(&c, CHILD(n, i));
878 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500879 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 asdl_seq_SET(stmts, i / 2, s);
881 }
882 }
883
Benjamin Peterson55e00432012-01-16 17:22:31 -0500884 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500886 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800887 case func_type_input:
888 n = CHILD(n, 0);
889 REQ(n, func_type);
890
891 if (TYPE(CHILD(n, 1)) == typelist) {
892 ch = CHILD(n, 1);
893 /* this is overly permissive -- we don't pay any attention to
894 * stars on the args -- just parse them into an ordered list */
895 num = 0;
896 for (i = 0; i < NCH(ch); i++) {
897 if (TYPE(CHILD(ch, i)) == test) {
898 num++;
899 }
900 }
901
902 argtypes = _Py_asdl_seq_new(num, arena);
903 if (!argtypes)
904 goto out;
905
906 j = 0;
907 for (i = 0; i < NCH(ch); i++) {
908 if (TYPE(CHILD(ch, i)) == test) {
909 arg = ast_for_expr(&c, CHILD(ch, i));
910 if (!arg)
911 goto out;
912 asdl_seq_SET(argtypes, j++, arg);
913 }
914 }
915 }
916 else {
917 argtypes = _Py_asdl_seq_new(0, arena);
918 if (!argtypes)
919 goto out;
920 }
921
922 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
923 if (!ret)
924 goto out;
925 res = FunctionType(argtypes, ret, arena);
926 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000928 PyErr_Format(PyExc_SystemError,
929 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500930 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500932 out:
933 if (c.c_normalize) {
934 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500935 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500936 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937}
938
Victor Stinner14e461d2013-08-26 22:28:21 +0200939mod_ty
940PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
941 PyArena *arena)
942{
943 mod_ty mod;
944 PyObject *filename;
945 filename = PyUnicode_DecodeFSDefault(filename_str);
946 if (filename == NULL)
947 return NULL;
948 mod = PyAST_FromNodeObject(n, flags, filename, arena);
949 Py_DECREF(filename);
950 return mod;
951
952}
953
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
955*/
956
957static operator_ty
958get_operator(const node *n)
959{
960 switch (TYPE(n)) {
961 case VBAR:
962 return BitOr;
963 case CIRCUMFLEX:
964 return BitXor;
965 case AMPER:
966 return BitAnd;
967 case LEFTSHIFT:
968 return LShift;
969 case RIGHTSHIFT:
970 return RShift;
971 case PLUS:
972 return Add;
973 case MINUS:
974 return Sub;
975 case STAR:
976 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400977 case AT:
978 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 case SLASH:
980 return Div;
981 case DOUBLESLASH:
982 return FloorDiv;
983 case PERCENT:
984 return Mod;
985 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000986 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 }
988}
989
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200990static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000991 "None",
992 "True",
993 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200994 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000995 NULL,
996};
997
998static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400999forbidden_name(struct compiling *c, identifier name, const node *n,
1000 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001001{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001002 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001003 const char * const *p = FORBIDDEN;
1004 if (!full_checks) {
1005 /* In most cases, the parser will protect True, False, and None
1006 from being assign to. */
1007 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001008 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001009 for (; *p; p++) {
1010 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1011 ast_error(c, n, "cannot assign to %U", name);
1012 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001013 }
1014 }
1015 return 0;
1016}
1017
Serhiy Storchakab619b092018-11-27 09:40:29 +02001018static expr_ty
1019copy_location(expr_ty e, const node *n)
1020{
1021 if (e) {
1022 e->lineno = LINENO(n);
1023 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001024 e->end_lineno = n->n_end_lineno;
1025 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001026 }
1027 return e;
1028}
1029
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001030static const char *
1031get_expr_name(expr_ty e)
1032{
1033 switch (e->kind) {
1034 case Attribute_kind:
1035 return "attribute";
1036 case Subscript_kind:
1037 return "subscript";
1038 case Starred_kind:
1039 return "starred";
1040 case Name_kind:
1041 return "name";
1042 case List_kind:
1043 return "list";
1044 case Tuple_kind:
1045 return "tuple";
1046 case Lambda_kind:
1047 return "lambda";
1048 case Call_kind:
1049 return "function call";
1050 case BoolOp_kind:
1051 case BinOp_kind:
1052 case UnaryOp_kind:
1053 return "operator";
1054 case GeneratorExp_kind:
1055 return "generator expression";
1056 case Yield_kind:
1057 case YieldFrom_kind:
1058 return "yield expression";
1059 case Await_kind:
1060 return "await expression";
1061 case ListComp_kind:
1062 return "list comprehension";
1063 case SetComp_kind:
1064 return "set comprehension";
1065 case DictComp_kind:
1066 return "dict comprehension";
1067 case Dict_kind:
1068 return "dict display";
1069 case Set_kind:
1070 return "set display";
1071 case JoinedStr_kind:
1072 case FormattedValue_kind:
1073 return "f-string expression";
1074 case Constant_kind: {
1075 PyObject *value = e->v.Constant.value;
1076 if (value == Py_None) {
1077 return "None";
1078 }
1079 if (value == Py_False) {
1080 return "False";
1081 }
1082 if (value == Py_True) {
1083 return "True";
1084 }
1085 if (value == Py_Ellipsis) {
1086 return "Ellipsis";
1087 }
1088 return "literal";
1089 }
1090 case Compare_kind:
1091 return "comparison";
1092 case IfExp_kind:
1093 return "conditional expression";
1094 case NamedExpr_kind:
1095 return "named expression";
1096 default:
1097 PyErr_Format(PyExc_SystemError,
1098 "unexpected expression in assignment %d (line %d)",
1099 e->kind, e->lineno);
1100 return NULL;
1101 }
1102}
1103
Jeremy Hyltona8293132006-02-28 17:58:27 +00001104/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105
1106 Only sets context for expr kinds that "can appear in assignment context"
1107 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1108 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109*/
1110
1111static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001112set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113{
1114 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001115
1116 /* The ast defines augmented store and load contexts, but the
1117 implementation here doesn't actually use them. The code may be
1118 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001119 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001120 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001121 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001122 */
1123 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124
1125 switch (e->kind) {
1126 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001127 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001128 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001129 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001130 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001132 e->v.Subscript.ctx = ctx;
1133 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001134 case Starred_kind:
1135 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001136 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001137 return 0;
1138 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001140 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001141 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001142 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001143 }
1144 e->v.Name.ctx = ctx;
1145 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001147 e->v.List.ctx = ctx;
1148 s = e->v.List.elts;
1149 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001151 e->v.Tuple.ctx = ctx;
1152 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001153 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001154 default: {
1155 const char *expr_name = get_expr_name(e);
1156 if (expr_name != NULL) {
1157 ast_error(c, n, "cannot %s %s",
1158 ctx == Store ? "assign to" : "delete",
1159 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001160 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001161 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001162 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001163 }
1164
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 */
1168 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001169 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170
Thomas Wouters89f507f2006-12-13 04:49:30 +00001171 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001172 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001173 return 0;
1174 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 }
1176 return 1;
1177}
1178
1179static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001180ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181{
1182 REQ(n, augassign);
1183 n = CHILD(n, 0);
1184 switch (STR(n)[0]) {
1185 case '+':
1186 return Add;
1187 case '-':
1188 return Sub;
1189 case '/':
1190 if (STR(n)[1] == '/')
1191 return FloorDiv;
1192 else
1193 return Div;
1194 case '%':
1195 return Mod;
1196 case '<':
1197 return LShift;
1198 case '>':
1199 return RShift;
1200 case '&':
1201 return BitAnd;
1202 case '^':
1203 return BitXor;
1204 case '|':
1205 return BitOr;
1206 case '*':
1207 if (STR(n)[1] == '*')
1208 return Pow;
1209 else
1210 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001211 case '@':
1212 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001214 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001215 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 }
1217}
1218
1219static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001220ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001222 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 |'is' 'not'
1224 */
1225 REQ(n, comp_op);
1226 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001227 n = CHILD(n, 0);
1228 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 case LESS:
1230 return Lt;
1231 case GREATER:
1232 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001233 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 return Eq;
1235 case LESSEQUAL:
1236 return LtE;
1237 case GREATEREQUAL:
1238 return GtE;
1239 case NOTEQUAL:
1240 return NotEq;
1241 case NAME:
1242 if (strcmp(STR(n), "in") == 0)
1243 return In;
1244 if (strcmp(STR(n), "is") == 0)
1245 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001246 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001248 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001251 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 }
1253 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001254 /* handle "not in" and "is not" */
1255 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 case NAME:
1257 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1258 return NotIn;
1259 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1260 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001261 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001263 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001265 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001266 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 }
Neal Norwitz79792652005-11-14 04:25:03 +00001268 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271}
1272
1273static asdl_seq *
1274seq_for_testlist(struct compiling *c, const node *n)
1275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001277 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1278 */
Armin Rigo31441302005-10-21 12:57:31 +00001279 asdl_seq *seq;
1280 expr_ty expression;
1281 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001282 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001284 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 if (!seq)
1286 return NULL;
1287
1288 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001290 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291
Benjamin Peterson4905e802009-09-27 02:43:28 +00001292 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001293 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295
1296 assert(i / 2 < seq->size);
1297 asdl_seq_SET(seq, i / 2, expression);
1298 }
1299 return seq;
1300}
1301
Neal Norwitzc1505362006-12-28 06:47:50 +00001302static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001303ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001304{
1305 identifier name;
1306 expr_ty annotation = NULL;
1307 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001308 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001309
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001310 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001311 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001312 name = NEW_IDENTIFIER(ch);
1313 if (!name)
1314 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001315 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001316 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001317
1318 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1319 annotation = ast_for_expr(c, CHILD(n, 2));
1320 if (!annotation)
1321 return NULL;
1322 }
1323
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001324 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001325 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001326 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001327 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001328 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329}
1330
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331/* returns -1 if failed to handle keyword only arguments
1332 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001333 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001334 ^^^
1335 start pointing here
1336 */
1337static int
1338handle_keywordonly_args(struct compiling *c, const node *n, int start,
1339 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1340{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001341 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001342 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001343 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001344 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001345 int i = start;
1346 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001347
1348 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001349 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001350 return -1;
1351 }
1352 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001353 while (i < NCH(n)) {
1354 ch = CHILD(n, i);
1355 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001356 case vfpdef:
1357 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001358 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001359 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001360 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001361 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 asdl_seq_SET(kwdefaults, j, expression);
1363 i += 2; /* '=' and test */
1364 }
1365 else { /* setting NULL if no default value exists */
1366 asdl_seq_SET(kwdefaults, j, NULL);
1367 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001368 if (NCH(ch) == 3) {
1369 /* ch is NAME ':' test */
1370 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001371 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001372 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001373 }
1374 else {
1375 annotation = NULL;
1376 }
1377 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001378 argname = NEW_IDENTIFIER(ch);
1379 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001381 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001382 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001383 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001384 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001385 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001386 if (!arg)
1387 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001388 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001389 i += 1; /* the name */
1390 if (TYPE(CHILD(n, i)) == COMMA)
1391 i += 1; /* the comma, if present */
1392 break;
1393 case TYPE_COMMENT:
1394 /* arg will be equal to the last argument processed */
1395 arg->type_comment = NEW_TYPE_COMMENT(ch);
1396 if (!arg->type_comment)
1397 goto error;
1398 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001399 break;
1400 case DOUBLESTAR:
1401 return i;
1402 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001403 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 goto error;
1405 }
1406 }
1407 return i;
1408 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001410}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411
Jeremy Hyltona8293132006-02-28 17:58:27 +00001412/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413
1414static arguments_ty
1415ast_for_arguments(struct compiling *c, const node *n)
1416{
Neal Norwitzc1505362006-12-28 06:47:50 +00001417 /* This function handles both typedargslist (function definition)
1418 and varargslist (lambda definition).
1419
1420 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001421 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1422 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1423 | '**' tfpdef [',']]]
1424 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1425 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001426 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001427 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1428 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1429 | '**' vfpdef [',']]]
1430 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1431 | '**' vfpdef [',']
1432 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001433 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001436 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1437 int nposdefaults = 0, found_default = 0;
1438 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001439 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001440 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 node *ch;
1442
1443 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001444 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001445 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001446 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001448 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449
Jeremy Hyltone921e022008-07-17 16:37:17 +00001450 /* First count the number of positional args & defaults. The
1451 variable i is the loop index for this for loop and the next.
1452 The next loop picks up where the first leaves off.
1453 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001455 ch = CHILD(n, i);
1456 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001457 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001458 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001459 if (i < NCH(n) && /* skip argument following star */
1460 (TYPE(CHILD(n, i)) == tfpdef ||
1461 TYPE(CHILD(n, i)) == vfpdef)) {
1462 i++;
1463 }
1464 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001465 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001466 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001467 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001468 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001471 defaults for keyword only args */
1472 for ( ; i < NCH(n); ++i) {
1473 ch = CHILD(n, i);
1474 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001475 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001476 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001477 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001478 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001479 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001480 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001481 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001482 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001483 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001485 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001486 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001487 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001489 since we set NULL as default for keyword only argument w/o default
1490 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001491 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001492 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001494 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001495
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001496 /* tfpdef: NAME [':' test]
1497 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 */
1499 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001500 j = 0; /* index for defaults */
1501 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 ch = CHILD(n, i);
1504 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001505 case tfpdef:
1506 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1508 anything other than EQUAL or a comma? */
1509 /* XXX Should NCH(n) check be made a separate check? */
1510 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001511 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1512 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001513 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001514 assert(posdefaults != NULL);
1515 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001517 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001519 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001520 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001521 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001522 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001523 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001524 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001525 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001526 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001527 asdl_seq_SET(posargs, k++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001528 i += 1; /* the name */
1529 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1530 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 break;
1532 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001533 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001534 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1535 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001536 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001537 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001538 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001539 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001540 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001541 if (TYPE(ch) == COMMA) {
1542 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001543 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001544
1545 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1546 ast_error(c, CHILD(n, i),
1547 "bare * has associated type comment");
1548 return NULL;
1549 }
1550
Guido van Rossum4f72a782006-10-27 23:31:49 +00001551 res = handle_keywordonly_args(c, n, i,
1552 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001553 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001554 i = res; /* res has new position to process */
1555 }
1556 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001557 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001558 if (!vararg)
1559 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001560
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001561 i += 2; /* the star and the name */
1562 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1563 i += 1; /* the comma, if present */
1564
1565 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1566 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1567 if (!vararg->type_comment)
1568 return NULL;
1569 i += 1;
1570 }
1571
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001572 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1573 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001574 int res = 0;
1575 res = handle_keywordonly_args(c, n, i,
1576 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001577 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001578 i = res; /* res has new position to process */
1579 }
1580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 break;
1582 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001583 ch = CHILD(n, i+1); /* tfpdef */
1584 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001585 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001586 if (!kwarg)
1587 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001588 i += 2; /* the double star and the name */
1589 if (TYPE(CHILD(n, i)) == COMMA)
1590 i += 1; /* the comma, if present */
1591 break;
1592 case TYPE_COMMENT:
1593 assert(i);
1594
1595 if (kwarg)
1596 arg = kwarg;
1597
1598 /* arg will be equal to the last argument processed */
1599 arg->type_comment = NEW_TYPE_COMMENT(ch);
1600 if (!arg->type_comment)
1601 return NULL;
1602 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 break;
1604 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001605 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 "unexpected node in varargslist: %d @ %d",
1607 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001608 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001609 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001611 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612}
1613
1614static expr_ty
1615ast_for_dotted_name(struct compiling *c, const node *n)
1616{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001617 expr_ty e;
1618 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001619 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001621 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622
1623 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001624
1625 lineno = LINENO(n);
1626 col_offset = n->n_col_offset;
1627
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001628 ch = CHILD(n, 0);
1629 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001631 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001632 e = Name(id, Load, lineno, col_offset,
1633 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001635 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636
1637 for (i = 2; i < NCH(n); i+=2) {
1638 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001639 if (!id)
1640 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001641 e = Attribute(e, id, Load, lineno, col_offset,
1642 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001643 if (!e)
1644 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 }
1646
1647 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648}
1649
1650static expr_ty
1651ast_for_decorator(struct compiling *c, const node *n)
1652{
1653 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1654 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001655 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001658 REQ(CHILD(n, 0), AT);
1659 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1662 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001663 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001666 d = name_expr;
1667 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 }
1669 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001670 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001671 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001672 if (!d)
1673 return NULL;
1674 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 }
1676 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001677 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001678 if (!d)
1679 return NULL;
1680 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 }
1682
1683 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684}
1685
1686static asdl_seq*
1687ast_for_decorators(struct compiling *c, const node *n)
1688{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001689 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001690 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001694 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 if (!decorator_seq)
1696 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001699 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001700 if (!d)
1701 return NULL;
1702 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 }
1704 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705}
1706
1707static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001708ast_for_funcdef_impl(struct compiling *c, const node *n0,
1709 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001711 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001712 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001713 identifier name;
1714 arguments_ty args;
1715 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001716 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001717 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001718 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001719 node *tc;
1720 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721
1722 REQ(n, funcdef);
1723
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 name = NEW_IDENTIFIER(CHILD(n, name_i));
1725 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001726 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001727 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001728 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1730 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001731 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001732 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1733 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1734 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001735 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001736 name_i += 2;
1737 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001738 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1739 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1740 if (!type_comment)
1741 return NULL;
1742 name_i += 1;
1743 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001744 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001746 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001747 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001749 if (NCH(CHILD(n, name_i + 3)) > 1) {
1750 /* Check if the suite has a type comment in it. */
1751 tc = CHILD(CHILD(n, name_i + 3), 1);
1752
1753 if (TYPE(tc) == TYPE_COMMENT) {
1754 if (type_comment != NULL) {
1755 ast_error(c, n, "Cannot have two type comments on def");
1756 return NULL;
1757 }
1758 type_comment = NEW_TYPE_COMMENT(tc);
1759 if (!type_comment)
1760 return NULL;
1761 }
1762 }
1763
Yury Selivanov75445082015-05-11 22:57:16 -04001764 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001765 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001766 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001767 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001768 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001769 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001770}
1771
1772static stmt_ty
1773ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1774{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001775 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001776 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001777 REQ(CHILD(n, 0), NAME);
1778 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001779 REQ(CHILD(n, 1), funcdef);
1780
guoci90fc8982018-09-11 17:45:45 -04001781 return ast_for_funcdef_impl(c, n, decorator_seq,
1782 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001783}
1784
1785static stmt_ty
1786ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1787{
1788 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1789 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001790 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001791}
1792
1793
1794static stmt_ty
1795ast_for_async_stmt(struct compiling *c, const node *n)
1796{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001797 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001798 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001799 REQ(CHILD(n, 0), NAME);
1800 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001801
1802 switch (TYPE(CHILD(n, 1))) {
1803 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001804 return ast_for_funcdef_impl(c, n, NULL,
1805 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001806 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001807 return ast_for_with_stmt(c, n,
1808 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001809
1810 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001811 return ast_for_for_stmt(c, n,
1812 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001813
1814 default:
1815 PyErr_Format(PyExc_SystemError,
1816 "invalid async stament: %s",
1817 STR(CHILD(n, 1)));
1818 return NULL;
1819 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820}
1821
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001822static stmt_ty
1823ast_for_decorated(struct compiling *c, const node *n)
1824{
Yury Selivanov75445082015-05-11 22:57:16 -04001825 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001826 stmt_ty thing = NULL;
1827 asdl_seq *decorator_seq = NULL;
1828
1829 REQ(n, decorated);
1830
1831 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1832 if (!decorator_seq)
1833 return NULL;
1834
1835 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001836 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001838
1839 if (TYPE(CHILD(n, 1)) == funcdef) {
1840 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1841 } else if (TYPE(CHILD(n, 1)) == classdef) {
1842 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001843 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1844 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001845 }
1846 return thing;
1847}
1848
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001850ast_for_namedexpr(struct compiling *c, const node *n)
1851{
1852 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1853 ['else' ':' suite]
1854 namedexpr_test: test [':=' test]
1855 argument: ( test [comp_for] |
1856 test ':=' test |
1857 test '=' test |
1858 '**' test |
1859 '*' test )
1860 */
1861 expr_ty target, value;
1862
1863 target = ast_for_expr(c, CHILD(n, 0));
1864 if (!target)
1865 return NULL;
1866
1867 value = ast_for_expr(c, CHILD(n, 2));
1868 if (!value)
1869 return NULL;
1870
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001871 if (target->kind != Name_kind) {
1872 const char *expr_name = get_expr_name(target);
1873 if (expr_name != NULL) {
1874 ast_error(c, n, "cannot use named assignment with %s", expr_name);
1875 }
1876 return NULL;
1877 }
1878
1879 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001880 return NULL;
1881
1882 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1883 n->n_end_col_offset, c->c_arena);
1884}
1885
1886static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887ast_for_lambdef(struct compiling *c, const node *n)
1888{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001889 /* lambdef: 'lambda' [varargslist] ':' test
1890 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 arguments_ty args;
1892 expr_ty expression;
1893
1894 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001895 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 if (!args)
1897 return NULL;
1898 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001899 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 }
1902 else {
1903 args = ast_for_arguments(c, CHILD(n, 1));
1904 if (!args)
1905 return NULL;
1906 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001907 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 }
1910
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001911 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1912 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913}
1914
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001915static expr_ty
1916ast_for_ifexpr(struct compiling *c, const node *n)
1917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001919 expr_ty expression, body, orelse;
1920
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001921 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001922 body = ast_for_expr(c, CHILD(n, 0));
1923 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001924 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001925 expression = ast_for_expr(c, CHILD(n, 2));
1926 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001927 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001928 orelse = ast_for_expr(c, CHILD(n, 4));
1929 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001930 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001931 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001932 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001933 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001934}
1935
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001937 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938
Nick Coghlan650f0d02007-04-15 12:05:43 +00001939 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940*/
1941
1942static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001943count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001945 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946
Guido van Rossumd8faa362007-04-27 19:54:29 +00001947 count_comp_for:
1948 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001949 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001950 if (NCH(n) == 2) {
1951 REQ(CHILD(n, 0), NAME);
1952 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1953 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001954 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001955 else if (NCH(n) == 1) {
1956 n = CHILD(n, 0);
1957 }
1958 else {
1959 goto error;
1960 }
1961 if (NCH(n) == (5)) {
1962 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001963 }
1964 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001965 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001966 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001967 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001968 REQ(n, comp_iter);
1969 n = CHILD(n, 0);
1970 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001971 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001972 else if (TYPE(n) == comp_if) {
1973 if (NCH(n) == 3) {
1974 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001975 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001976 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001977 else
1978 return n_fors;
1979 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001980
Jelle Zijlstraac317702017-10-05 20:24:46 -07001981 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001982 /* Should never be reached */
1983 PyErr_SetString(PyExc_SystemError,
1984 "logic error in count_comp_fors");
1985 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986}
1987
Nick Coghlan650f0d02007-04-15 12:05:43 +00001988/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989
Nick Coghlan650f0d02007-04-15 12:05:43 +00001990 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991*/
1992
1993static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001994count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001996 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997
Guido van Rossumd8faa362007-04-27 19:54:29 +00001998 while (1) {
1999 REQ(n, comp_iter);
2000 if (TYPE(CHILD(n, 0)) == comp_for)
2001 return n_ifs;
2002 n = CHILD(n, 0);
2003 REQ(n, comp_if);
2004 n_ifs++;
2005 if (NCH(n) == 2)
2006 return n_ifs;
2007 n = CHILD(n, 2);
2008 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009}
2010
Guido van Rossum992d4a32007-07-11 13:09:30 +00002011static asdl_seq *
2012ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002015 asdl_seq *comps;
2016
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002017 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 if (n_fors == -1)
2019 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002020
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002021 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002022 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002024
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002026 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002027 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002028 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002029 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002030 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002031 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032
Guido van Rossum992d4a32007-07-11 13:09:30 +00002033 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034
Jelle Zijlstraac317702017-10-05 20:24:46 -07002035 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002036 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002037 REQ(CHILD(n, 0), NAME);
2038 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
2039 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002040 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002041 else {
2042 sync_n = CHILD(n, 0);
2043 }
2044 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002045
Jelle Zijlstraac317702017-10-05 20:24:46 -07002046 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002047 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002048 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002050 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002051 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002053
Thomas Wouters89f507f2006-12-13 04:49:30 +00002054 /* Check the # of children rather than the length of t, since
2055 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002056 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002057 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002058 comp = comprehension(first, expression, NULL,
2059 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002061 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2062 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2063 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002064 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002065 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002067
Jelle Zijlstraac317702017-10-05 20:24:46 -07002068 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 int j, n_ifs;
2070 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071
Jelle Zijlstraac317702017-10-05 20:24:46 -07002072 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002073 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002074 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002076
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002077 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002078 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002082 REQ(n, comp_iter);
2083 n = CHILD(n, 0);
2084 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085
Guido van Rossum992d4a32007-07-11 13:09:30 +00002086 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002087 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002088 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002089 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002090 if (NCH(n) == 3)
2091 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002093 /* on exit, must guarantee that n is a comp_for */
2094 if (TYPE(n) == comp_iter)
2095 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002096 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002098 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002100 return comps;
2101}
2102
2103static expr_ty
2104ast_for_itercomp(struct compiling *c, const node *n, int type)
2105{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002106 /* testlist_comp: (test|star_expr)
2107 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002108 expr_ty elt;
2109 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002110 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111
Guido van Rossum992d4a32007-07-11 13:09:30 +00002112 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002114 ch = CHILD(n, 0);
2115 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002116 if (!elt)
2117 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002118 if (elt->kind == Starred_kind) {
2119 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2120 return NULL;
2121 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122
Guido van Rossum992d4a32007-07-11 13:09:30 +00002123 comps = ast_for_comprehension(c, CHILD(n, 1));
2124 if (!comps)
2125 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002126
2127 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002128 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2129 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002130 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002131 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2132 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002133 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002134 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2135 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002136 else
2137 /* Should never happen */
2138 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139}
2140
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002141/* Fills in the key, value pair corresponding to the dict element. In case
2142 * of an unpacking, key is NULL. *i is advanced by the number of ast
2143 * elements. Iff successful, nonzero is returned.
2144 */
2145static int
2146ast_for_dictelement(struct compiling *c, const node *n, int *i,
2147 expr_ty *key, expr_ty *value)
2148{
2149 expr_ty expression;
2150 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2151 assert(NCH(n) - *i >= 2);
2152
2153 expression = ast_for_expr(c, CHILD(n, *i + 1));
2154 if (!expression)
2155 return 0;
2156 *key = NULL;
2157 *value = expression;
2158
2159 *i += 2;
2160 }
2161 else {
2162 assert(NCH(n) - *i >= 3);
2163
2164 expression = ast_for_expr(c, CHILD(n, *i));
2165 if (!expression)
2166 return 0;
2167 *key = expression;
2168
2169 REQ(CHILD(n, *i + 1), COLON);
2170
2171 expression = ast_for_expr(c, CHILD(n, *i + 2));
2172 if (!expression)
2173 return 0;
2174 *value = expression;
2175
2176 *i += 3;
2177 }
2178 return 1;
2179}
2180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002182ast_for_dictcomp(struct compiling *c, const node *n)
2183{
2184 expr_ty key, value;
2185 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002186 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002188 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002189 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002190 assert(key);
2191 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002193 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002194 if (!comps)
2195 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002197 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2198 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002199}
2200
2201static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002202ast_for_dictdisplay(struct compiling *c, const node *n)
2203{
2204 int i;
2205 int j;
2206 int size;
2207 asdl_seq *keys, *values;
2208
2209 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2210 keys = _Py_asdl_seq_new(size, c->c_arena);
2211 if (!keys)
2212 return NULL;
2213
2214 values = _Py_asdl_seq_new(size, c->c_arena);
2215 if (!values)
2216 return NULL;
2217
2218 j = 0;
2219 for (i = 0; i < NCH(n); i++) {
2220 expr_ty key, value;
2221
2222 if (!ast_for_dictelement(c, n, &i, &key, &value))
2223 return NULL;
2224 asdl_seq_SET(keys, j, key);
2225 asdl_seq_SET(values, j, value);
2226
2227 j++;
2228 }
2229 keys->size = j;
2230 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002231 return Dict(keys, values, LINENO(n), n->n_col_offset,
2232 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002233}
2234
2235static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002236ast_for_genexp(struct compiling *c, const node *n)
2237{
2238 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002239 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002240}
2241
2242static expr_ty
2243ast_for_listcomp(struct compiling *c, const node *n)
2244{
2245 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002246 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002247}
2248
2249static expr_ty
2250ast_for_setcomp(struct compiling *c, const node *n)
2251{
2252 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002253 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002254}
2255
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002256static expr_ty
2257ast_for_setdisplay(struct compiling *c, const node *n)
2258{
2259 int i;
2260 int size;
2261 asdl_seq *elts;
2262
2263 assert(TYPE(n) == (dictorsetmaker));
2264 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2265 elts = _Py_asdl_seq_new(size, c->c_arena);
2266 if (!elts)
2267 return NULL;
2268 for (i = 0; i < NCH(n); i += 2) {
2269 expr_ty expression;
2270 expression = ast_for_expr(c, CHILD(n, i));
2271 if (!expression)
2272 return NULL;
2273 asdl_seq_SET(elts, i / 2, expression);
2274 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002275 return Set(elts, LINENO(n), n->n_col_offset,
2276 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002277}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002278
2279static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280ast_for_atom(struct compiling *c, const node *n)
2281{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002282 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2283 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002284 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 */
2286 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002289 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002290 PyObject *name;
2291 const char *s = STR(ch);
2292 size_t len = strlen(s);
2293 if (len >= 4 && len <= 5) {
2294 if (!strcmp(s, "None"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002295 return Constant(Py_None, LINENO(n), n->n_col_offset,
2296 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002297 if (!strcmp(s, "True"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002298 return Constant(Py_True, LINENO(n), n->n_col_offset,
2299 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002300 if (!strcmp(s, "False"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002301 return Constant(Py_False, LINENO(n), n->n_col_offset,
2302 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002303 }
2304 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002305 if (!name)
2306 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002307 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002308 return Name(name, Load, LINENO(n), n->n_col_offset,
2309 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002310 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002312 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002313 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002314 const char *errtype = NULL;
2315 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2316 errtype = "unicode error";
2317 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2318 errtype = "value error";
2319 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002320 PyObject *type, *value, *tback, *errstr;
2321 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002322 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002323 if (errstr) {
2324 ast_error(c, n, "(%s) %U", errtype, errstr);
2325 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002326 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002327 else {
2328 PyErr_Clear();
2329 ast_error(c, n, "(%s) unknown error", errtype);
2330 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002331 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002332 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002333 Py_XDECREF(tback);
2334 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002335 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002336 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002337 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338 }
2339 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002340 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002341 if (!pynum)
2342 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002343
Victor Stinner43d81952013-07-17 00:57:58 +02002344 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2345 Py_DECREF(pynum);
2346 return NULL;
2347 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002348 return Constant(pynum, LINENO(n), n->n_col_offset,
2349 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 }
Georg Brandldde00282007-03-18 19:01:53 +00002351 case ELLIPSIS: /* Ellipsis */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002352 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset,
2353 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002355 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356
Thomas Wouters89f507f2006-12-13 04:49:30 +00002357 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002358 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2359 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360
Thomas Wouters89f507f2006-12-13 04:49:30 +00002361 if (TYPE(ch) == yield_expr)
2362 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002365 if (NCH(ch) == 1) {
2366 return ast_for_testlist(c, ch);
2367 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002368
Serhiy Storchakab619b092018-11-27 09:40:29 +02002369 if (TYPE(CHILD(ch, 1)) == comp_for) {
2370 return copy_location(ast_for_genexp(c, ch), n);
2371 }
2372 else {
2373 return copy_location(ast_for_testlist(c, ch), n);
2374 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002376 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377
Thomas Wouters89f507f2006-12-13 04:49:30 +00002378 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002379 return List(NULL, Load, LINENO(n), n->n_col_offset,
2380 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381
Nick Coghlan650f0d02007-04-15 12:05:43 +00002382 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002383 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2384 asdl_seq *elts = seq_for_testlist(c, ch);
2385 if (!elts)
2386 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002387
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002388 return List(elts, Load, LINENO(n), n->n_col_offset,
2389 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002390 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002391 else {
2392 return copy_location(ast_for_listcomp(c, ch), n);
2393 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002395 /* dictorsetmaker: ( ((test ':' test | '**' test)
2396 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2397 * ((test | '*' test)
2398 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002399 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002400 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002401 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002402 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002403 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2404 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002405 }
2406 else {
2407 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2408 if (NCH(ch) == 1 ||
2409 (NCH(ch) > 1 &&
2410 TYPE(CHILD(ch, 1)) == COMMA)) {
2411 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002412 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002413 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002414 else if (NCH(ch) > 1 &&
2415 TYPE(CHILD(ch, 1)) == comp_for) {
2416 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002417 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002418 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002419 else if (NCH(ch) > 3 - is_dict &&
2420 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2421 /* It's a dictionary comprehension. */
2422 if (is_dict) {
2423 ast_error(c, n, "dict unpacking cannot be used in "
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002424 "dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002425 return NULL;
2426 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002427 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002428 }
2429 else {
2430 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002431 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002432 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002433 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002437 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2438 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439 }
2440}
2441
2442static slice_ty
2443ast_for_slice(struct compiling *c, const node *n)
2444{
2445 node *ch;
2446 expr_ty lower = NULL, upper = NULL, step = NULL;
2447
2448 REQ(n, subscript);
2449
2450 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002451 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 sliceop: ':' [test]
2453 */
2454 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 if (NCH(n) == 1 && TYPE(ch) == test) {
2456 /* 'step' variable hold no significance in terms of being used over
2457 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 if (!step)
2460 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461
Thomas Wouters89f507f2006-12-13 04:49:30 +00002462 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 }
2464
2465 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002466 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 if (!lower)
2468 return NULL;
2469 }
2470
2471 /* If there's an upper bound it's in the second or third position. */
2472 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002473 if (NCH(n) > 1) {
2474 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475
Thomas Wouters89f507f2006-12-13 04:49:30 +00002476 if (TYPE(n2) == test) {
2477 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 if (!upper)
2479 return NULL;
2480 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002483 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484
Thomas Wouters89f507f2006-12-13 04:49:30 +00002485 if (TYPE(n2) == test) {
2486 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 if (!upper)
2488 return NULL;
2489 }
2490 }
2491
2492 ch = CHILD(n, NCH(n) - 1);
2493 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002494 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002495 ch = CHILD(ch, 1);
2496 if (TYPE(ch) == test) {
2497 step = ast_for_expr(c, ch);
2498 if (!step)
2499 return NULL;
2500 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 }
2502 }
2503
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002504 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505}
2506
2507static expr_ty
2508ast_for_binop(struct compiling *c, const node *n)
2509{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002510 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002512 BinOp(BinOp(A, op, B), op, C).
2513 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514
Guido van Rossumd8faa362007-04-27 19:54:29 +00002515 int i, nops;
2516 expr_ty expr1, expr2, result;
2517 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518
Guido van Rossumd8faa362007-04-27 19:54:29 +00002519 expr1 = ast_for_expr(c, CHILD(n, 0));
2520 if (!expr1)
2521 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522
Guido van Rossumd8faa362007-04-27 19:54:29 +00002523 expr2 = ast_for_expr(c, CHILD(n, 2));
2524 if (!expr2)
2525 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526
Guido van Rossumd8faa362007-04-27 19:54:29 +00002527 newoperator = get_operator(CHILD(n, 1));
2528 if (!newoperator)
2529 return NULL;
2530
2531 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002532 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002533 c->c_arena);
2534 if (!result)
2535 return NULL;
2536
2537 nops = (NCH(n) - 1) / 2;
2538 for (i = 1; i < nops; i++) {
2539 expr_ty tmp_result, tmp;
2540 const node* next_oper = CHILD(n, i * 2 + 1);
2541
2542 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002543 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 return NULL;
2545
Guido van Rossumd8faa362007-04-27 19:54:29 +00002546 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2547 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 return NULL;
2549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002551 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002552 CHILD(n, i * 2 + 2)->n_end_lineno,
2553 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002554 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002556 return NULL;
2557 result = tmp_result;
2558 }
2559 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560}
2561
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002562static expr_ty
2563ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002566 subscriptlist: subscript (',' subscript)* [',']
2567 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2568 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002569 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002570 REQ(n, trailer);
2571 if (TYPE(CHILD(n, 0)) == LPAR) {
2572 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002573 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2574 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002575 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002576 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002577 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002578 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002579 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2580 if (!attr_id)
2581 return NULL;
2582 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002583 LINENO(n), n->n_col_offset,
2584 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002585 }
2586 else {
2587 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002588 REQ(CHILD(n, 2), RSQB);
2589 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002590 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002591 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2592 if (!slc)
2593 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002594 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002595 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002596 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002597 }
2598 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002600 by treating the sequence as a tuple literal if there are
2601 no slice features.
2602 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002603 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002604 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002605 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002606 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002607 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002608 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002609 if (!slices)
2610 return NULL;
2611 for (j = 0; j < NCH(n); j += 2) {
2612 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002613 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002614 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002615 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002616 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002617 asdl_seq_SET(slices, j / 2, slc);
2618 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002619 if (!simple) {
2620 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002621 Load, LINENO(n), n->n_col_offset,
2622 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002623 }
2624 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002625 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002626 if (!elts)
2627 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002628 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2629 slc = (slice_ty)asdl_seq_GET(slices, j);
2630 assert(slc->kind == Index_kind && slc->v.Index.value);
2631 asdl_seq_SET(elts, j, slc->v.Index.value);
2632 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002633 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2634 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002635 if (!e)
2636 return NULL;
2637 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002638 Load, LINENO(n), n->n_col_offset,
2639 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002640 }
2641 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002642}
2643
2644static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002645ast_for_factor(struct compiling *c, const node *n)
2646{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002647 expr_ty expression;
2648
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002649 expression = ast_for_expr(c, CHILD(n, 1));
2650 if (!expression)
2651 return NULL;
2652
2653 switch (TYPE(CHILD(n, 0))) {
2654 case PLUS:
2655 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002656 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002657 c->c_arena);
2658 case MINUS:
2659 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002660 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002661 c->c_arena);
2662 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002663 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2664 n->n_end_lineno, n->n_end_col_offset,
2665 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002666 }
2667 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2668 TYPE(CHILD(n, 0)));
2669 return NULL;
2670}
2671
2672static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002673ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002674{
Yury Selivanov75445082015-05-11 22:57:16 -04002675 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002676 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002677
2678 REQ(n, atom_expr);
2679 nch = NCH(n);
2680
Jelle Zijlstraac317702017-10-05 20:24:46 -07002681 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002682 start = 1;
2683 assert(nch > 1);
2684 }
2685
2686 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002687 if (!e)
2688 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002689 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002690 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002691 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002692 return Await(e, LINENO(n), n->n_col_offset,
2693 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002694 }
2695
2696 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002697 node *ch = CHILD(n, i);
2698 if (TYPE(ch) != trailer)
2699 break;
2700 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002701 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002702 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002703 tmp->lineno = e->lineno;
2704 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002705 e = tmp;
2706 }
Yury Selivanov75445082015-05-11 22:57:16 -04002707
2708 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002709 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002710 return Await(e, LINENO(n), n->n_col_offset,
2711 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002712 }
2713 else {
2714 return e;
2715 }
2716}
2717
2718static expr_ty
2719ast_for_power(struct compiling *c, const node *n)
2720{
2721 /* power: atom trailer* ('**' factor)*
2722 */
2723 expr_ty e;
2724 REQ(n, power);
2725 e = ast_for_atom_expr(c, CHILD(n, 0));
2726 if (!e)
2727 return NULL;
2728 if (NCH(n) == 1)
2729 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002730 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2731 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002732 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002733 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002734 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2735 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002736 }
2737 return e;
2738}
2739
Guido van Rossum0368b722007-05-11 16:50:42 +00002740static expr_ty
2741ast_for_starred(struct compiling *c, const node *n)
2742{
2743 expr_ty tmp;
2744 REQ(n, star_expr);
2745
2746 tmp = ast_for_expr(c, CHILD(n, 1));
2747 if (!tmp)
2748 return NULL;
2749
2750 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002751 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2752 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002753}
2754
2755
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756/* Do not name a variable 'expr'! Will cause a compile error.
2757*/
2758
2759static expr_ty
2760ast_for_expr(struct compiling *c, const node *n)
2761{
2762 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002763 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002764 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002765 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 and_test: not_test ('and' not_test)*
2768 not_test: 'not' not_test | comparison
2769 comparison: expr (comp_op expr)*
2770 expr: xor_expr ('|' xor_expr)*
2771 xor_expr: and_expr ('^' and_expr)*
2772 and_expr: shift_expr ('&' shift_expr)*
2773 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2774 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002775 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002777 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002778 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002779 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 */
2781
2782 asdl_seq *seq;
2783 int i;
2784
2785 loop:
2786 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002787 case namedexpr_test:
2788 if (NCH(n) == 3)
2789 return ast_for_namedexpr(c, n);
2790 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002792 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002793 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002794 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002796 else if (NCH(n) > 1)
2797 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002798 /* Fallthrough */
2799 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 case and_test:
2801 if (NCH(n) == 1) {
2802 n = CHILD(n, 0);
2803 goto loop;
2804 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002805 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 if (!seq)
2807 return NULL;
2808 for (i = 0; i < NCH(n); i += 2) {
2809 expr_ty e = ast_for_expr(c, CHILD(n, i));
2810 if (!e)
2811 return NULL;
2812 asdl_seq_SET(seq, i / 2, e);
2813 }
2814 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002815 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002816 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002817 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002818 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002819 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2820 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 case not_test:
2822 if (NCH(n) == 1) {
2823 n = CHILD(n, 0);
2824 goto loop;
2825 }
2826 else {
2827 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2828 if (!expression)
2829 return NULL;
2830
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002831 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002832 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002833 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 }
2835 case comparison:
2836 if (NCH(n) == 1) {
2837 n = CHILD(n, 0);
2838 goto loop;
2839 }
2840 else {
2841 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002842 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002843 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002844 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 if (!ops)
2846 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002847 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 return NULL;
2850 }
2851 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002852 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002854 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002855 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002857 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858
2859 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002860 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002862 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002864 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 asdl_seq_SET(cmps, i / 2, expression);
2866 }
2867 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002868 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002870 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002872 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2873 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 }
2875 break;
2876
Guido van Rossum0368b722007-05-11 16:50:42 +00002877 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 /* The next five cases all handle BinOps. The main body of code
2880 is the same in each case, but the switch turned inside out to
2881 reuse the code for each type of operator.
2882 */
2883 case expr:
2884 case xor_expr:
2885 case and_expr:
2886 case shift_expr:
2887 case arith_expr:
2888 case term:
2889 if (NCH(n) == 1) {
2890 n = CHILD(n, 0);
2891 goto loop;
2892 }
2893 return ast_for_binop(c, n);
2894 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002895 node *an = NULL;
2896 node *en = NULL;
2897 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002898 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002899 if (NCH(n) > 1)
2900 an = CHILD(n, 1); /* yield_arg */
2901 if (an) {
2902 en = CHILD(an, NCH(an) - 1);
2903 if (NCH(an) == 2) {
2904 is_from = 1;
2905 exp = ast_for_expr(c, en);
2906 }
2907 else
2908 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 if (!exp)
2910 return NULL;
2911 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002912 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002913 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2914 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2915 return Yield(exp, LINENO(n), n->n_col_offset,
2916 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002917 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002918 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 if (NCH(n) == 1) {
2920 n = CHILD(n, 0);
2921 goto loop;
2922 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002923 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002924 case power:
2925 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002927 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002928 return NULL;
2929 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002930 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 return NULL;
2932}
2933
2934static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002935ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002936 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937{
2938 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002939 arglist: argument (',' argument)* [',']
2940 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941 */
2942
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002943 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002944 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002945 asdl_seq *args;
2946 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947
2948 REQ(n, arglist);
2949
2950 nargs = 0;
2951 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002953 node *ch = CHILD(n, i);
2954 if (TYPE(ch) == argument) {
2955 if (NCH(ch) == 1)
2956 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002957 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2958 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002959 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002960 ast_error(c, ch, "invalid syntax");
2961 return NULL;
2962 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002963 if (NCH(n) > 1) {
2964 ast_error(c, ch, "Generator expression must be parenthesized");
2965 return NULL;
2966 }
2967 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002968 else if (TYPE(CHILD(ch, 0)) == STAR)
2969 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002970 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2971 nargs++;
2972 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002974 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002975 nkeywords++;
2976 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002978
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002979 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002981 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002982 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002984 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002985
2986 nargs = 0; /* positional arguments + iterable argument unpackings */
2987 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2988 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002990 node *ch = CHILD(n, i);
2991 if (TYPE(ch) == argument) {
2992 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002993 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002994 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002995 /* a positional argument */
2996 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002997 if (ndoublestars) {
2998 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002999 "positional argument follows "
3000 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003001 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003002 else {
3003 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003004 "positional argument follows "
3005 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003006 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003007 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003008 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003009 e = ast_for_expr(c, chch);
3010 if (!e)
3011 return NULL;
3012 asdl_seq_SET(args, nargs++, e);
3013 }
3014 else if (TYPE(chch) == STAR) {
3015 /* an iterable argument unpacking */
3016 expr_ty starred;
3017 if (ndoublestars) {
3018 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003019 "iterable argument unpacking follows "
3020 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003021 return NULL;
3022 }
3023 e = ast_for_expr(c, CHILD(ch, 1));
3024 if (!e)
3025 return NULL;
3026 starred = Starred(e, Load, LINENO(chch),
3027 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003028 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003029 c->c_arena);
3030 if (!starred)
3031 return NULL;
3032 asdl_seq_SET(args, nargs++, starred);
3033
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003034 }
3035 else if (TYPE(chch) == DOUBLESTAR) {
3036 /* a keyword argument unpacking */
3037 keyword_ty kw;
3038 i++;
3039 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003041 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003042 kw = keyword(NULL, e, c->c_arena);
3043 asdl_seq_SET(keywords, nkeywords++, kw);
3044 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003046 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003047 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003048 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003050 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003051 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003053 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3054 /* treat colon equal as positional argument */
3055 if (nkeywords) {
3056 if (ndoublestars) {
3057 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003058 "positional argument follows "
3059 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003060 }
3061 else {
3062 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003063 "positional argument follows "
3064 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003065 }
3066 return NULL;
3067 }
3068 e = ast_for_namedexpr(c, ch);
3069 if (!e)
3070 return NULL;
3071 asdl_seq_SET(args, nargs++, e);
3072 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003073 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003074 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003075 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003076 identifier key, tmp;
3077 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003079 // To remain LL(1), the grammar accepts any test (basically, any
3080 // expression) in the keyword slot of a call site. So, we need
3081 // to manually enforce that the keyword is a NAME here.
3082 static const int name_tree[] = {
3083 test,
3084 or_test,
3085 and_test,
3086 not_test,
3087 comparison,
3088 expr,
3089 xor_expr,
3090 and_expr,
3091 shift_expr,
3092 arith_expr,
3093 term,
3094 factor,
3095 power,
3096 atom_expr,
3097 atom,
3098 0,
3099 };
3100 node *expr_node = chch;
3101 for (int i = 0; name_tree[i]; i++) {
3102 if (TYPE(expr_node) != name_tree[i])
3103 break;
3104 if (NCH(expr_node) != 1)
3105 break;
3106 expr_node = CHILD(expr_node, 0);
3107 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003108 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003109 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003110 "expression cannot contain assignment, "
3111 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003112 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003113 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003114 key = new_identifier(STR(expr_node), c);
3115 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003116 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003118 if (forbidden_name(c, key, chch, 1)) {
3119 return NULL;
3120 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003121 for (k = 0; k < nkeywords; k++) {
3122 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003123 if (tmp && !PyUnicode_Compare(tmp, key)) {
3124 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003125 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003126 return NULL;
3127 }
3128 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003129 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003131 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003132 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003134 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003135 asdl_seq_SET(keywords, nkeywords++, kw);
3136 }
3137 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 }
3139
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003140 return Call(func, args, keywords, func->lineno, func->col_offset,
3141 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142}
3143
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003145ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003147 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003148 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003150 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003151 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003152 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003153 }
3154 else {
3155 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003156 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003157 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003159 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 else {
3161 asdl_seq *tmp = seq_for_testlist(c, n);
3162 if (!tmp)
3163 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003164 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3165 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003167}
3168
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169static stmt_ty
3170ast_for_expr_stmt(struct compiling *c, const node *n)
3171{
3172 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003173 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003174 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3175 annassign: ':' test ['=' (yield_expr|testlist)]
3176 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3177 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3178 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003179 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003181 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003183 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003184 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 if (!e)
3186 return NULL;
3187
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003188 return Expr(e, LINENO(n), n->n_col_offset,
3189 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 }
3191 else if (TYPE(CHILD(n, 1)) == augassign) {
3192 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003193 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003194 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195
Thomas Wouters89f507f2006-12-13 04:49:30 +00003196 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 if (!expr1)
3198 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003199 if(!set_context(c, expr1, Store, ch))
3200 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003201 /* set_context checks that most expressions are not the left side.
3202 Augmented assignments can only have a name, a subscript, or an
3203 attribute on the left, though, so we have to explicitly check for
3204 those. */
3205 switch (expr1->kind) {
3206 case Name_kind:
3207 case Attribute_kind:
3208 case Subscript_kind:
3209 break;
3210 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003211 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003212 return NULL;
3213 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214
Thomas Wouters89f507f2006-12-13 04:49:30 +00003215 ch = CHILD(n, 2);
3216 if (TYPE(ch) == testlist)
3217 expr2 = ast_for_testlist(c, ch);
3218 else
3219 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003220 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221 return NULL;
3222
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003223 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003224 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225 return NULL;
3226
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003227 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3228 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003230 else if (TYPE(CHILD(n, 1)) == annassign) {
3231 expr_ty expr1, expr2, expr3;
3232 node *ch = CHILD(n, 0);
3233 node *deep, *ann = CHILD(n, 1);
3234 int simple = 1;
3235
3236 /* we keep track of parens to qualify (x) as expression not name */
3237 deep = ch;
3238 while (NCH(deep) == 1) {
3239 deep = CHILD(deep, 0);
3240 }
3241 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3242 simple = 0;
3243 }
3244 expr1 = ast_for_testlist(c, ch);
3245 if (!expr1) {
3246 return NULL;
3247 }
3248 switch (expr1->kind) {
3249 case Name_kind:
3250 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3251 return NULL;
3252 }
3253 expr1->v.Name.ctx = Store;
3254 break;
3255 case Attribute_kind:
3256 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3257 return NULL;
3258 }
3259 expr1->v.Attribute.ctx = Store;
3260 break;
3261 case Subscript_kind:
3262 expr1->v.Subscript.ctx = Store;
3263 break;
3264 case List_kind:
3265 ast_error(c, ch,
3266 "only single target (not list) can be annotated");
3267 return NULL;
3268 case Tuple_kind:
3269 ast_error(c, ch,
3270 "only single target (not tuple) can be annotated");
3271 return NULL;
3272 default:
3273 ast_error(c, ch,
3274 "illegal target for annotation");
3275 return NULL;
3276 }
3277
3278 if (expr1->kind != Name_kind) {
3279 simple = 0;
3280 }
3281 ch = CHILD(ann, 1);
3282 expr2 = ast_for_expr(c, ch);
3283 if (!expr2) {
3284 return NULL;
3285 }
3286 if (NCH(ann) == 2) {
3287 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003288 LINENO(n), n->n_col_offset,
3289 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003290 }
3291 else {
3292 ch = CHILD(ann, 3);
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003293 if (TYPE(ch) == testlist) {
3294 expr3 = ast_for_testlist(c, ch);
3295 }
3296 else {
3297 expr3 = ast_for_expr(c, ch);
3298 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003299 if (!expr3) {
3300 return NULL;
3301 }
3302 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003303 LINENO(n), n->n_col_offset,
3304 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003305 }
3306 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003308 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003309 asdl_seq *targets;
3310 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003312 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 /* a normal assignment */
3315 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003316
3317 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3318 nch_minus_type = num - has_type_comment;
3319
3320 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 if (!targets)
3322 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003323 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003324 expr_ty e;
3325 node *ch = CHILD(n, i);
3326 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003327 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003328 return NULL;
3329 }
3330 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003332 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003334 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003335 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003336 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 asdl_seq_SET(targets, i / 2, e);
3339 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003340 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003341 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003342 expression = ast_for_testlist(c, value);
3343 else
3344 expression = ast_for_expr(c, value);
3345 if (!expression)
3346 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003347 if (has_type_comment) {
3348 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3349 if (!type_comment)
3350 return NULL;
3351 }
3352 else
3353 type_comment = NULL;
3354 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003355 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357}
3358
Benjamin Peterson78565b22009-06-28 19:19:51 +00003359
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003361ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362{
3363 asdl_seq *seq;
3364 int i;
3365 expr_ty e;
3366
3367 REQ(n, exprlist);
3368
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003369 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003371 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 e = ast_for_expr(c, CHILD(n, i));
3374 if (!e)
3375 return NULL;
3376 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003377 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003378 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379 }
3380 return seq;
3381}
3382
3383static stmt_ty
3384ast_for_del_stmt(struct compiling *c, const node *n)
3385{
3386 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 /* del_stmt: 'del' exprlist */
3389 REQ(n, del_stmt);
3390
3391 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3392 if (!expr_list)
3393 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003394 return Delete(expr_list, LINENO(n), n->n_col_offset,
3395 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396}
3397
3398static stmt_ty
3399ast_for_flow_stmt(struct compiling *c, const node *n)
3400{
3401 /*
3402 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3403 | yield_stmt
3404 break_stmt: 'break'
3405 continue_stmt: 'continue'
3406 return_stmt: 'return' [testlist]
3407 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003408 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409 raise_stmt: 'raise' [test [',' test [',' test]]]
3410 */
3411 node *ch;
3412
3413 REQ(n, flow_stmt);
3414 ch = CHILD(n, 0);
3415 switch (TYPE(ch)) {
3416 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003417 return Break(LINENO(n), n->n_col_offset,
3418 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003420 return Continue(LINENO(n), n->n_col_offset,
3421 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003423 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3424 if (!exp)
3425 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003426 return Expr(exp, LINENO(n), n->n_col_offset,
3427 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428 }
3429 case return_stmt:
3430 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003431 return Return(NULL, LINENO(n), n->n_col_offset,
3432 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003434 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435 if (!expression)
3436 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003437 return Return(expression, LINENO(n), n->n_col_offset,
3438 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 }
3440 case raise_stmt:
3441 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003442 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3443 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003444 else if (NCH(ch) >= 2) {
3445 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3447 if (!expression)
3448 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003449 if (NCH(ch) == 4) {
3450 cause = ast_for_expr(c, CHILD(ch, 3));
3451 if (!cause)
3452 return NULL;
3453 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003454 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3455 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 }
Stefan Krahf432a322017-08-21 13:09:59 +02003457 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003459 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 "unexpected flow_stmt: %d", TYPE(ch));
3461 return NULL;
3462 }
3463}
3464
3465static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003466alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467{
3468 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003469 import_as_name: NAME ['as' NAME]
3470 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 dotted_name: NAME ('.' NAME)*
3472 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003473 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003474
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 loop:
3476 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003477 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003478 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003479 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003480 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003481 if (!name)
3482 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003483 if (NCH(n) == 3) {
3484 node *str_node = CHILD(n, 2);
3485 str = NEW_IDENTIFIER(str_node);
3486 if (!str)
3487 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003488 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003489 return NULL;
3490 }
3491 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003492 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003493 return NULL;
3494 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003495 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 case dotted_as_name:
3498 if (NCH(n) == 1) {
3499 n = CHILD(n, 0);
3500 goto loop;
3501 }
3502 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003503 node *asname_node = CHILD(n, 2);
3504 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003505 if (!a)
3506 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003508 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003509 if (!a->asname)
3510 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003511 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 return a;
3514 }
3515 break;
3516 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003517 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003518 node *name_node = CHILD(n, 0);
3519 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003520 if (!name)
3521 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003522 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003523 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003524 return alias(name, NULL, c->c_arena);
3525 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526 else {
3527 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003528 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003529 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532
3533 len = 0;
3534 for (i = 0; i < NCH(n); i += 2)
3535 /* length of string plus one for the dot */
3536 len += strlen(STR(CHILD(n, i))) + 1;
3537 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003538 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 if (!str)
3540 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003541 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 if (!s)
3543 return NULL;
3544 for (i = 0; i < NCH(n); i += 2) {
3545 char *sch = STR(CHILD(n, i));
3546 strcpy(s, STR(CHILD(n, i)));
3547 s += strlen(sch);
3548 *s++ = '.';
3549 }
3550 --s;
3551 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3553 PyBytes_GET_SIZE(str),
3554 NULL);
3555 Py_DECREF(str);
3556 if (!uni)
3557 return NULL;
3558 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003559 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003560 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3561 Py_DECREF(str);
3562 return NULL;
3563 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003564 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565 }
3566 break;
3567 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003568 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003569 if (!str)
3570 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003571 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3572 Py_DECREF(str);
3573 return NULL;
3574 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003575 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003577 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578 "unexpected import name: %d", TYPE(n));
3579 return NULL;
3580 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003581
3582 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583 return NULL;
3584}
3585
3586static stmt_ty
3587ast_for_import_stmt(struct compiling *c, const node *n)
3588{
3589 /*
3590 import_stmt: import_name | import_from
3591 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003592 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3593 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003595 int lineno;
3596 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597 int i;
3598 asdl_seq *aliases;
3599
3600 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003601 lineno = LINENO(n);
3602 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003604 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003606 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003607 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003608 if (!aliases)
3609 return NULL;
3610 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003611 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003612 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003614 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003616 // Even though n is modified above, the end position is not changed
3617 return Import(aliases, lineno, col_offset,
3618 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003620 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003622 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003623 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003624 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003625 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003627 /* Count the number of dots (for relative imports) and check for the
3628 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003629 for (idx = 1; idx < NCH(n); idx++) {
3630 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003631 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3632 if (!mod)
3633 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003634 idx++;
3635 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003636 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003638 ndots += 3;
3639 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003640 } else if (TYPE(CHILD(n, idx)) != DOT) {
3641 break;
3642 }
3643 ndots++;
3644 }
3645 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003646 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003647 case STAR:
3648 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003649 n = CHILD(n, idx);
3650 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003651 break;
3652 case LPAR:
3653 /* from ... import (x, y, z) */
3654 n = CHILD(n, idx + 1);
3655 n_children = NCH(n);
3656 break;
3657 case import_as_names:
3658 /* from ... import x, y, z */
3659 n = CHILD(n, idx);
3660 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003661 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003662 ast_error(c, n,
3663 "trailing comma not allowed without"
3664 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 return NULL;
3666 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003667 break;
3668 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003669 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003670 return NULL;
3671 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003673 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003674 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676
3677 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003678 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003679 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003680 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003682 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003684 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003685 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003686 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003687 if (!import_alias)
3688 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003689 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003690 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003692 if (mod != NULL)
3693 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003694 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003695 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003696 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697 }
Neal Norwitz79792652005-11-14 04:25:03 +00003698 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 "unknown import statement: starts with command '%s'",
3700 STR(CHILD(n, 0)));
3701 return NULL;
3702}
3703
3704static stmt_ty
3705ast_for_global_stmt(struct compiling *c, const node *n)
3706{
3707 /* global_stmt: 'global' NAME (',' NAME)* */
3708 identifier name;
3709 asdl_seq *s;
3710 int i;
3711
3712 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003713 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003717 name = NEW_IDENTIFIER(CHILD(n, i));
3718 if (!name)
3719 return NULL;
3720 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003722 return Global(s, LINENO(n), n->n_col_offset,
3723 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724}
3725
3726static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003727ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3728{
3729 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3730 identifier name;
3731 asdl_seq *s;
3732 int i;
3733
3734 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003735 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003736 if (!s)
3737 return NULL;
3738 for (i = 1; i < NCH(n); i += 2) {
3739 name = NEW_IDENTIFIER(CHILD(n, i));
3740 if (!name)
3741 return NULL;
3742 asdl_seq_SET(s, i / 2, name);
3743 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003744 return Nonlocal(s, LINENO(n), n->n_col_offset,
3745 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003746}
3747
3748static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749ast_for_assert_stmt(struct compiling *c, const node *n)
3750{
3751 /* assert_stmt: 'assert' test [',' test] */
3752 REQ(n, assert_stmt);
3753 if (NCH(n) == 2) {
3754 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3755 if (!expression)
3756 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003757 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3758 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003759 }
3760 else if (NCH(n) == 4) {
3761 expr_ty expr1, expr2;
3762
3763 expr1 = ast_for_expr(c, CHILD(n, 1));
3764 if (!expr1)
3765 return NULL;
3766 expr2 = ast_for_expr(c, CHILD(n, 3));
3767 if (!expr2)
3768 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003770 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3771 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 }
Neal Norwitz79792652005-11-14 04:25:03 +00003773 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 "improper number of parts to 'assert' statement: %d",
3775 NCH(n));
3776 return NULL;
3777}
3778
3779static asdl_seq *
3780ast_for_suite(struct compiling *c, const node *n)
3781{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003782 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003783 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 stmt_ty s;
3785 int i, total, num, end, pos = 0;
3786 node *ch;
3787
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003788 if (TYPE(n) != func_body_suite) {
3789 REQ(n, suite);
3790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791
3792 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003793 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003795 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003797 n = CHILD(n, 0);
3798 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003800 */
3801 end = NCH(n) - 1;
3802 if (TYPE(CHILD(n, end - 1)) == SEMI)
3803 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003805 for (i = 0; i < end; i += 2) {
3806 ch = CHILD(n, i);
3807 s = ast_for_stmt(c, ch);
3808 if (!s)
3809 return NULL;
3810 asdl_seq_SET(seq, pos++, s);
3811 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 }
3813 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003814 i = 2;
3815 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3816 i += 2;
3817 REQ(CHILD(n, 2), NEWLINE);
3818 }
3819
3820 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003821 ch = CHILD(n, i);
3822 REQ(ch, stmt);
3823 num = num_stmts(ch);
3824 if (num == 1) {
3825 /* small_stmt or compound_stmt with only one child */
3826 s = ast_for_stmt(c, ch);
3827 if (!s)
3828 return NULL;
3829 asdl_seq_SET(seq, pos++, s);
3830 }
3831 else {
3832 int j;
3833 ch = CHILD(ch, 0);
3834 REQ(ch, simple_stmt);
3835 for (j = 0; j < NCH(ch); j += 2) {
3836 /* statement terminates with a semi-colon ';' */
3837 if (NCH(CHILD(ch, j)) == 0) {
3838 assert((j + 1) == NCH(ch));
3839 break;
3840 }
3841 s = ast_for_stmt(c, CHILD(ch, j));
3842 if (!s)
3843 return NULL;
3844 asdl_seq_SET(seq, pos++, s);
3845 }
3846 }
3847 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 }
3849 assert(pos == seq->size);
3850 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851}
3852
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003853static void
3854get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3855{
Pablo Galindo46a97922019-02-19 22:51:53 +00003856 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003857 // There must be no empty suites.
3858 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003859 stmt_ty last = asdl_seq_GET(s, tot - 1);
3860 *end_lineno = last->end_lineno;
3861 *end_col_offset = last->end_col_offset;
3862}
3863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864static stmt_ty
3865ast_for_if_stmt(struct compiling *c, const node *n)
3866{
3867 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3868 ['else' ':' suite]
3869 */
3870 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003871 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003872
3873 REQ(n, if_stmt);
3874
3875 if (NCH(n) == 4) {
3876 expr_ty expression;
3877 asdl_seq *suite_seq;
3878
3879 expression = ast_for_expr(c, CHILD(n, 1));
3880 if (!expression)
3881 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003883 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003885 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886
Guido van Rossumd8faa362007-04-27 19:54:29 +00003887 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003888 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891 s = STR(CHILD(n, 4));
3892 /* s[2], the third character in the string, will be
3893 's' for el_s_e, or
3894 'i' for el_i_f
3895 */
3896 if (s[2] == 's') {
3897 expr_ty expression;
3898 asdl_seq *seq1, *seq2;
3899
3900 expression = ast_for_expr(c, CHILD(n, 1));
3901 if (!expression)
3902 return NULL;
3903 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003904 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 return NULL;
3906 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003907 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003909 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910
Guido van Rossumd8faa362007-04-27 19:54:29 +00003911 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003912 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 }
3914 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003915 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003916 expr_ty expression;
3917 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003918 asdl_seq *orelse = NULL;
3919 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920 /* must reference the child n_elif+1 since 'else' token is third,
3921 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003922 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3923 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3924 has_else = 1;
3925 n_elif -= 3;
3926 }
3927 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928
Thomas Wouters89f507f2006-12-13 04:49:30 +00003929 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003930 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003932 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003933 if (!orelse)
3934 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003936 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003938 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3939 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003941 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3942 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003944 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 asdl_seq_SET(orelse, 0,
3947 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003948 LINENO(CHILD(n, NCH(n) - 6)),
3949 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003950 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003951 /* the just-created orelse handled the last elif */
3952 n_elif--;
3953 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954
Thomas Wouters89f507f2006-12-13 04:49:30 +00003955 for (i = 0; i < n_elif; i++) {
3956 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003957 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003958 if (!newobj)
3959 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003961 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003964 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003967 if (orelse != NULL) {
3968 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3969 } else {
3970 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3971 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003972 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003974 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003975 CHILD(n, off)->n_col_offset,
3976 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003977 orelse = newobj;
3978 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003979 expression = ast_for_expr(c, CHILD(n, 1));
3980 if (!expression)
3981 return NULL;
3982 suite_seq = ast_for_suite(c, CHILD(n, 3));
3983 if (!suite_seq)
3984 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003985 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003986 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003987 LINENO(n), n->n_col_offset,
3988 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003990
3991 PyErr_Format(PyExc_SystemError,
3992 "unexpected token in 'if' statement: %s", s);
3993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003994}
3995
3996static stmt_ty
3997ast_for_while_stmt(struct compiling *c, const node *n)
3998{
3999 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4000 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004001 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002
4003 if (NCH(n) == 4) {
4004 expr_ty expression;
4005 asdl_seq *suite_seq;
4006
4007 expression = ast_for_expr(c, CHILD(n, 1));
4008 if (!expression)
4009 return NULL;
4010 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004011 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004013 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4014 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4015 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 }
4017 else if (NCH(n) == 7) {
4018 expr_ty expression;
4019 asdl_seq *seq1, *seq2;
4020
4021 expression = ast_for_expr(c, CHILD(n, 1));
4022 if (!expression)
4023 return NULL;
4024 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004025 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026 return NULL;
4027 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004028 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004030 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004032 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4033 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004035
4036 PyErr_Format(PyExc_SystemError,
4037 "wrong number of tokens for 'while' statement: %d",
4038 NCH(n));
4039 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040}
4041
4042static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004043ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004044{
guoci90fc8982018-09-11 17:45:45 -04004045 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004046 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004048 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004049 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004050 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004051 int has_type_comment;
4052 string type_comment;
4053 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054 REQ(n, for_stmt);
4055
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004056 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4057
4058 if (NCH(n) == 9 + has_type_comment) {
4059 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060 if (!seq)
4061 return NULL;
4062 }
4063
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004064 node_target = CHILD(n, 1);
4065 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004066 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004068 /* Check the # of children rather than the length of _target, since
4069 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004070 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004071 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004072 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004074 target = Tuple(_target, Store, first->lineno, first->col_offset,
4075 node_target->n_end_lineno, node_target->n_end_col_offset,
4076 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004077
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004078 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004079 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004081 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004082 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083 return NULL;
4084
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004085 if (seq != NULL) {
4086 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4087 } else {
4088 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4089 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004090
4091 if (has_type_comment) {
4092 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4093 if (!type_comment)
4094 return NULL;
4095 }
4096 else
4097 type_comment = NULL;
4098
Yury Selivanov75445082015-05-11 22:57:16 -04004099 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004100 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004101 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004102 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004103 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004104 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004105 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004106 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004107}
4108
4109static excepthandler_ty
4110ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4111{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004112 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004113 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004114 REQ(exc, except_clause);
4115 REQ(body, suite);
4116
4117 if (NCH(exc) == 1) {
4118 asdl_seq *suite_seq = ast_for_suite(c, body);
4119 if (!suite_seq)
4120 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004121 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122
Neal Norwitzad74aa82008-03-31 05:14:30 +00004123 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004124 exc->n_col_offset,
4125 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126 }
4127 else if (NCH(exc) == 2) {
4128 expr_ty expression;
4129 asdl_seq *suite_seq;
4130
4131 expression = ast_for_expr(c, CHILD(exc, 1));
4132 if (!expression)
4133 return NULL;
4134 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004135 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004137 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004138
Neal Norwitzad74aa82008-03-31 05:14:30 +00004139 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004140 exc->n_col_offset,
4141 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142 }
4143 else if (NCH(exc) == 4) {
4144 asdl_seq *suite_seq;
4145 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004146 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004147 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004149 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004150 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004152 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153 return NULL;
4154 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004155 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004156 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004157 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004158
Neal Norwitzad74aa82008-03-31 05:14:30 +00004159 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004160 exc->n_col_offset,
4161 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004162 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004163
4164 PyErr_Format(PyExc_SystemError,
4165 "wrong number of children for 'except' clause: %d",
4166 NCH(exc));
4167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004168}
4169
4170static stmt_ty
4171ast_for_try_stmt(struct compiling *c, const node *n)
4172{
Neal Norwitzf599f422005-12-17 21:33:47 +00004173 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004174 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004175 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004176 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178 REQ(n, try_stmt);
4179
Neal Norwitzf599f422005-12-17 21:33:47 +00004180 body = ast_for_suite(c, CHILD(n, 2));
4181 if (body == NULL)
4182 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004183
Neal Norwitzf599f422005-12-17 21:33:47 +00004184 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4185 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4186 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4187 /* we can assume it's an "else",
4188 because nch >= 9 for try-else-finally and
4189 it would otherwise have a type of except_clause */
4190 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4191 if (orelse == NULL)
4192 return NULL;
4193 n_except--;
4194 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195
Neal Norwitzf599f422005-12-17 21:33:47 +00004196 finally = ast_for_suite(c, CHILD(n, nch - 1));
4197 if (finally == NULL)
4198 return NULL;
4199 n_except--;
4200 }
4201 else {
4202 /* we can assume it's an "else",
4203 otherwise it would have a type of except_clause */
4204 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4205 if (orelse == NULL)
4206 return NULL;
4207 n_except--;
4208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004209 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004210 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004211 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004212 return NULL;
4213 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214
Neal Norwitzf599f422005-12-17 21:33:47 +00004215 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004216 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004217 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004218 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004219 if (handlers == NULL)
4220 return NULL;
4221
4222 for (i = 0; i < n_except; i++) {
4223 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4224 CHILD(n, 5 + i * 3));
4225 if (!e)
4226 return NULL;
4227 asdl_seq_SET(handlers, i, e);
4228 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004229 }
4230
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004231 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004232 if (finally != NULL) {
4233 // finally is always last
4234 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4235 } else if (orelse != NULL) {
4236 // otherwise else is last
4237 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4238 } else {
4239 // inline the get_last_end_pos logic due to layout mismatch
4240 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4241 end_lineno = last_handler->end_lineno;
4242 end_col_offset = last_handler->end_col_offset;
4243 }
4244 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4245 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004246}
4247
Georg Brandl0c315622009-05-25 21:10:36 +00004248/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004249static withitem_ty
4250ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004251{
4252 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004253
Georg Brandl0c315622009-05-25 21:10:36 +00004254 REQ(n, with_item);
4255 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004256 if (!context_expr)
4257 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004258 if (NCH(n) == 3) {
4259 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004260
4261 if (!optional_vars) {
4262 return NULL;
4263 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004264 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004265 return NULL;
4266 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004267 }
4268
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004269 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004270}
4271
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004272/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004273static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004274ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004275{
guoci90fc8982018-09-11 17:45:45 -04004276 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004277 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004278 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004279 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004280
4281 REQ(n, with_stmt);
4282
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004283 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4284 nch_minus_type = NCH(n) - has_type_comment;
4285
4286 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004287 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004288 if (!items)
4289 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004290 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004291 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4292 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004293 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004294 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004295 }
4296
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004297 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4298 if (!body)
4299 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004300 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004301
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004302 if (has_type_comment) {
4303 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4304 if (!type_comment)
4305 return NULL;
4306 }
4307 else
4308 type_comment = NULL;
4309
Yury Selivanov75445082015-05-11 22:57:16 -04004310 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004311 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004312 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004313 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004314 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004315 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004316}
4317
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004318static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004319ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004320{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004321 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004322 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004323 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004324 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004325 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004326
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004327 REQ(n, classdef);
4328
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004329 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004330 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004331 if (!s)
4332 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004333 get_last_end_pos(s, &end_lineno, &end_col_offset);
4334
Benjamin Peterson30760062008-11-25 04:02:28 +00004335 classname = NEW_IDENTIFIER(CHILD(n, 1));
4336 if (!classname)
4337 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004338 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004339 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004340 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004341 LINENO(n), n->n_col_offset,
4342 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004343 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004344
4345 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004346 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004347 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004348 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004349 get_last_end_pos(s, &end_lineno, &end_col_offset);
4350
Benjamin Peterson30760062008-11-25 04:02:28 +00004351 classname = NEW_IDENTIFIER(CHILD(n, 1));
4352 if (!classname)
4353 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004354 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004355 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004356 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004357 LINENO(n), n->n_col_offset,
4358 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004359 }
4360
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004361 /* class NAME '(' arglist ')' ':' suite */
4362 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004363 {
4364 PyObject *dummy_name;
4365 expr_ty dummy;
4366 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4367 if (!dummy_name)
4368 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004369 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4370 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4371 c->c_arena);
4372 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004373 if (!call)
4374 return NULL;
4375 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004376 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004377 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004378 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004379 get_last_end_pos(s, &end_lineno, &end_col_offset);
4380
Benjamin Peterson30760062008-11-25 04:02:28 +00004381 classname = NEW_IDENTIFIER(CHILD(n, 1));
4382 if (!classname)
4383 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004384 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004385 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004386
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004387 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004388 decorator_seq, LINENO(n), n->n_col_offset,
4389 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004390}
4391
4392static stmt_ty
4393ast_for_stmt(struct compiling *c, const node *n)
4394{
4395 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004396 assert(NCH(n) == 1);
4397 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004398 }
4399 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004400 assert(num_stmts(n) == 1);
4401 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004402 }
4403 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004404 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004405 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4406 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004407 */
4408 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004409 case expr_stmt:
4410 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004411 case del_stmt:
4412 return ast_for_del_stmt(c, n);
4413 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004414 return Pass(LINENO(n), n->n_col_offset,
4415 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004416 case flow_stmt:
4417 return ast_for_flow_stmt(c, n);
4418 case import_stmt:
4419 return ast_for_import_stmt(c, n);
4420 case global_stmt:
4421 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004422 case nonlocal_stmt:
4423 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004424 case assert_stmt:
4425 return ast_for_assert_stmt(c, n);
4426 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004427 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004428 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4429 TYPE(n), NCH(n));
4430 return NULL;
4431 }
4432 }
4433 else {
4434 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004435 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004436 */
4437 node *ch = CHILD(n, 0);
4438 REQ(n, compound_stmt);
4439 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004440 case if_stmt:
4441 return ast_for_if_stmt(c, ch);
4442 case while_stmt:
4443 return ast_for_while_stmt(c, ch);
4444 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004445 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004446 case try_stmt:
4447 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004448 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004449 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004450 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004451 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004452 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004453 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 case decorated:
4455 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004456 case async_stmt:
4457 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004458 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004459 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004460 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461 TYPE(n), NCH(n));
4462 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004463 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004464 }
4465}
4466
4467static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004468parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004469{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004470 const char *end;
4471 long x;
4472 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004473 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004474 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004475
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004476 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004477 errno = 0;
4478 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004479 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004480 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004481 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004482 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004483 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004484 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004485 }
4486 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004487 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004488 if (*end == '\0') {
4489 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004490 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004491 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004492 }
4493 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004494 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004495 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004496 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4497 if (compl.imag == -1.0 && PyErr_Occurred())
4498 return NULL;
4499 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004500 }
4501 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004502 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004503 dx = PyOS_string_to_double(s, NULL, NULL);
4504 if (dx == -1.0 && PyErr_Occurred())
4505 return NULL;
4506 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004508}
4509
4510static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004511parsenumber(struct compiling *c, const char *s)
4512{
4513 char *dup, *end;
4514 PyObject *res = NULL;
4515
4516 assert(s != NULL);
4517
4518 if (strchr(s, '_') == NULL) {
4519 return parsenumber_raw(c, s);
4520 }
4521 /* Create a duplicate without underscores. */
4522 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004523 if (dup == NULL) {
4524 return PyErr_NoMemory();
4525 }
Brett Cannona721aba2016-09-09 14:57:09 -07004526 end = dup;
4527 for (; *s; s++) {
4528 if (*s != '_') {
4529 *end++ = *s;
4530 }
4531 }
4532 *end = '\0';
4533 res = parsenumber_raw(c, dup);
4534 PyMem_Free(dup);
4535 return res;
4536}
4537
4538static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004539decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004540{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004541 const char *s, *t;
4542 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004543 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4544 while (s < end && (*s & 0x80)) s++;
4545 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004546 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004547}
4548
Eric V. Smith56466482016-10-31 14:46:26 -04004549static int
4550warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004551 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004552{
4553 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4554 first_invalid_escape_char);
4555 if (msg == NULL) {
4556 return -1;
4557 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004558 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004559 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004560 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004561 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004562 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004563 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004564 to get a more accurate error report */
4565 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004566 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004567 }
4568 Py_DECREF(msg);
4569 return -1;
4570 }
4571 Py_DECREF(msg);
4572 return 0;
4573}
4574
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004575static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004576decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4577 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004578{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004579 PyObject *v, *u;
4580 char *buf;
4581 char *p;
4582 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004583
Benjamin Peterson202803a2016-02-25 22:34:45 -08004584 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004585 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004586 return NULL;
4587 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4588 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4589 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4590 if (u == NULL)
4591 return NULL;
4592 p = buf = PyBytes_AsString(u);
4593 end = s + len;
4594 while (s < end) {
4595 if (*s == '\\') {
4596 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004597 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004598 strcpy(p, "u005c");
4599 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004600 if (s >= end)
4601 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004602 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004603 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004604 if (*s & 0x80) { /* XXX inefficient */
4605 PyObject *w;
4606 int kind;
4607 void *data;
4608 Py_ssize_t len, i;
4609 w = decode_utf8(c, &s, end);
4610 if (w == NULL) {
4611 Py_DECREF(u);
4612 return NULL;
4613 }
4614 kind = PyUnicode_KIND(w);
4615 data = PyUnicode_DATA(w);
4616 len = PyUnicode_GET_LENGTH(w);
4617 for (i = 0; i < len; i++) {
4618 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4619 sprintf(p, "\\U%08x", chr);
4620 p += 10;
4621 }
4622 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004623 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004624 Py_DECREF(w);
4625 } else {
4626 *p++ = *s++;
4627 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004628 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004629 len = p - buf;
4630 s = buf;
4631
Eric V. Smith56466482016-10-31 14:46:26 -04004632 const char *first_invalid_escape;
4633 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4634
4635 if (v != NULL && first_invalid_escape != NULL) {
4636 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4637 /* We have not decref u before because first_invalid_escape points
4638 inside u. */
4639 Py_XDECREF(u);
4640 Py_DECREF(v);
4641 return NULL;
4642 }
4643 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004644 Py_XDECREF(u);
4645 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004646}
4647
Eric V. Smith56466482016-10-31 14:46:26 -04004648static PyObject *
4649decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4650 size_t len)
4651{
4652 const char *first_invalid_escape;
4653 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4654 &first_invalid_escape);
4655 if (result == NULL)
4656 return NULL;
4657
4658 if (first_invalid_escape != NULL) {
4659 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4660 Py_DECREF(result);
4661 return NULL;
4662 }
4663 }
4664 return result;
4665}
4666
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004667/* Shift locations for the given node and all its children by adding `lineno`
4668 and `col_offset` to existing locations. */
4669static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4670{
4671 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004672 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004673 for (int i = 0; i < NCH(n); ++i) {
4674 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4675 /* Shifting column offsets unnecessary if there's been newlines. */
4676 col_offset = 0;
4677 }
4678 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4679 }
4680 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004681 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004682}
4683
4684/* Fix locations for the given node and its children.
4685
4686 `parent` is the enclosing node.
4687 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004688 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004689*/
4690static void
4691fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4692{
4693 char *substr = NULL;
4694 char *start;
4695 int lines = LINENO(parent) - 1;
4696 int cols = parent->n_col_offset;
4697 /* Find the full fstring to fix location information in `n`. */
4698 while (parent && parent->n_type != STRING)
4699 parent = parent->n_child;
4700 if (parent && parent->n_str) {
4701 substr = strstr(parent->n_str, expr_str);
4702 if (substr) {
4703 start = substr;
4704 while (start > parent->n_str) {
4705 if (start[0] == '\n')
4706 break;
4707 start--;
4708 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004709 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004710 /* adjust the start based on the number of newlines encountered
4711 before the f-string expression */
4712 for (char* p = parent->n_str; p < substr; p++) {
4713 if (*p == '\n') {
4714 lines++;
4715 }
4716 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004717 }
4718 }
4719 fstring_shift_node_locations(n, lines, cols);
4720}
4721
Eric V. Smith451d0e32016-09-09 21:56:20 -04004722/* Compile this expression in to an expr_ty. Add parens around the
4723 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004724static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004725fstring_compile_expr(const char *expr_start, const char *expr_end,
4726 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004727
Eric V. Smith235a6f02015-09-19 14:51:32 -04004728{
4729 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004730 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004731 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004732 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004733 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004734 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004735
Eric V. Smith1d44c412015-09-23 07:49:00 -04004736 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004737 assert(*(expr_start-1) == '{');
4738 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004739
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004740 /* If the substring is all whitespace, it's an error. We need to catch this
4741 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4742 because turning the expression '' in to '()' would go from being invalid
4743 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004744 for (s = expr_start; s != expr_end; s++) {
4745 char c = *s;
4746 /* The Python parser ignores only the following whitespace
4747 characters (\r already is converted to \n). */
4748 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004749 break;
4750 }
4751 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004752 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004753 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004754 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004755 }
4756
Eric V. Smith451d0e32016-09-09 21:56:20 -04004757 len = expr_end - expr_start;
4758 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4759 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004760 if (str == NULL) {
4761 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004762 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004763 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004764
Eric V. Smith451d0e32016-09-09 21:56:20 -04004765 str[0] = '(';
4766 memcpy(str+1, expr_start, len);
4767 str[len+1] = ')';
4768 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004769
4770 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004771 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4772 Py_eval_input, 0);
4773 if (!mod_n) {
4774 PyMem_RawFree(str);
4775 return NULL;
4776 }
4777 /* Reuse str to find the correct column offset. */
4778 str[0] = '{';
4779 str[len+1] = '}';
4780 fstring_fix_node_location(n, mod_n, str);
4781 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004782 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004783 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004784 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004785 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004786 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004787}
4788
4789/* Return -1 on error.
4790
4791 Return 0 if we reached the end of the literal.
4792
4793 Return 1 if we haven't reached the end of the literal, but we want
4794 the caller to process the literal up to this point. Used for
4795 doubled braces.
4796*/
4797static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004798fstring_find_literal(const char **str, const char *end, int raw,
4799 PyObject **literal, int recurse_lvl,
4800 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004801{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004802 /* Get any literal string. It ends when we hit an un-doubled left
4803 brace (which isn't part of a unicode name escape such as
4804 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004805
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004806 const char *s = *str;
4807 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004808 int result = 0;
4809
Eric V. Smith235a6f02015-09-19 14:51:32 -04004810 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004811 while (s < end) {
4812 char ch = *s++;
4813 if (!raw && ch == '\\' && s < end) {
4814 ch = *s++;
4815 if (ch == 'N') {
4816 if (s < end && *s++ == '{') {
4817 while (s < end && *s++ != '}') {
4818 }
4819 continue;
4820 }
4821 break;
4822 }
4823 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4824 return -1;
4825 }
4826 }
4827 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004828 /* Check for doubled braces, but only at the top level. If
4829 we checked at every level, then f'{0:{3}}' would fail
4830 with the two closing braces. */
4831 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004832 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004833 /* We're going to tell the caller that the literal ends
4834 here, but that they should continue scanning. But also
4835 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004836 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004837 result = 1;
4838 goto done;
4839 }
4840
4841 /* Where a single '{' is the start of a new expression, a
4842 single '}' is not allowed. */
4843 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004844 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004845 ast_error(c, n, "f-string: single '}' is not allowed");
4846 return -1;
4847 }
4848 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004849 /* We're either at a '{', which means we're starting another
4850 expression; or a '}', which means we're at the end of this
4851 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004852 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004853 break;
4854 }
4855 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004856 *str = s;
4857 assert(s <= end);
4858 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004859done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004860 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004861 if (raw)
4862 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004863 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004864 NULL, NULL);
4865 else
Eric V. Smith56466482016-10-31 14:46:26 -04004866 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004867 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868 if (!*literal)
4869 return -1;
4870 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004871 return result;
4872}
4873
4874/* Forward declaration because parsing is recursive. */
4875static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004876fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004877 struct compiling *c, const node *n);
4878
Eric V. Smith451d0e32016-09-09 21:56:20 -04004879/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004880 expression (so it must be a '{'). Returns the FormattedValue node,
4881 which includes the expression, conversion character, and
4882 format_spec expression.
4883
4884 Note that I don't do a perfect job here: I don't make sure that a
4885 closing brace doesn't match an opening paren, for example. It
4886 doesn't need to error on all invalid expressions, just correctly
4887 find the end of all valid ones. Any errors inside the expression
4888 will be caught when we parse it later. */
4889static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004890fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004891 expr_ty *expression, struct compiling *c, const node *n)
4892{
4893 /* Return -1 on error, else 0. */
4894
Eric V. Smith451d0e32016-09-09 21:56:20 -04004895 const char *expr_start;
4896 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897 expr_ty simple_expression;
4898 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004899 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900
4901 /* 0 if we're not in a string, else the quote char we're trying to
4902 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004903 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004904
4905 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4906 int string_type = 0;
4907
4908 /* Keep track of nesting level for braces/parens/brackets in
4909 expressions. */
4910 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004911 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004912
4913 /* Can only nest one level deep. */
4914 if (recurse_lvl >= 2) {
4915 ast_error(c, n, "f-string: expressions nested too deeply");
4916 return -1;
4917 }
4918
4919 /* The first char must be a left brace, or we wouldn't have gotten
4920 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004921 assert(**str == '{');
4922 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004923
Eric V. Smith451d0e32016-09-09 21:56:20 -04004924 expr_start = *str;
4925 for (; *str < end; (*str)++) {
4926 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927
4928 /* Loop invariants. */
4929 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004930 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004931 if (quote_char)
4932 assert(string_type == 1 || string_type == 3);
4933 else
4934 assert(string_type == 0);
4935
Eric V. Smith451d0e32016-09-09 21:56:20 -04004936 ch = **str;
4937 /* Nowhere inside an expression is a backslash allowed. */
4938 if (ch == '\\') {
4939 /* Error: can't include a backslash character, inside
4940 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004941 ast_error(c, n,
4942 "f-string expression part "
4943 "cannot include a backslash");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004944 return -1;
4945 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004946 if (quote_char) {
4947 /* We're inside a string. See if we're at the end. */
4948 /* This code needs to implement the same non-error logic
4949 as tok_get from tokenizer.c, at the letter_quote
4950 label. To actually share that code would be a
4951 nightmare. But, it's unlikely to change and is small,
4952 so duplicate it here. Note we don't need to catch all
4953 of the errors, since they'll be caught when parsing the
4954 expression. We just need to match the non-error
4955 cases. Thus we can ignore \n in single-quoted strings,
4956 for example. Or non-terminated strings. */
4957 if (ch == quote_char) {
4958 /* Does this match the string_type (single or triple
4959 quoted)? */
4960 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004961 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004962 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004963 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004964 string_type = 0;
4965 quote_char = 0;
4966 continue;
4967 }
4968 } else {
4969 /* We're at the end of a normal string. */
4970 quote_char = 0;
4971 string_type = 0;
4972 continue;
4973 }
4974 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004975 } else if (ch == '\'' || ch == '"') {
4976 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004977 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004978 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004979 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004980 } else {
4981 /* Start of a normal string. */
4982 string_type = 1;
4983 }
4984 /* Start looking for the end of the string. */
4985 quote_char = ch;
4986 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004987 if (nested_depth >= MAXLEVEL) {
4988 ast_error(c, n, "f-string: too many nested parenthesis");
4989 return -1;
4990 }
4991 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004992 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 } else if (ch == '#') {
4994 /* Error: can't include a comment character, inside parens
4995 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004996 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 return -1;
4998 } else if (nested_depth == 0 &&
4999 (ch == '!' || ch == ':' || ch == '}')) {
5000 /* First, test for the special case of "!=". Since '=' is
5001 not an allowed conversion character, nothing is lost in
5002 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005003 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005004 /* This isn't a conversion character, just continue. */
5005 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005006 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005007 /* Normal way out of this loop. */
5008 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005009 } else if (ch == ']' || ch == '}' || ch == ')') {
5010 if (!nested_depth) {
5011 ast_error(c, n, "f-string: unmatched '%c'", ch);
5012 return -1;
5013 }
5014 nested_depth--;
5015 int opening = parenstack[nested_depth];
5016 if (!((opening == '(' && ch == ')') ||
5017 (opening == '[' && ch == ']') ||
5018 (opening == '{' && ch == '}')))
5019 {
5020 ast_error(c, n,
5021 "f-string: closing parenthesis '%c' "
5022 "does not match opening parenthesis '%c'",
5023 ch, opening);
5024 return -1;
5025 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 } else {
5027 /* Just consume this char and loop around. */
5028 }
5029 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005030 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005031 /* If we leave this loop in a string or with mismatched parens, we
5032 don't care. We'll get a syntax error when compiling the
5033 expression. But, we can produce a better error message, so
5034 let's just do that.*/
5035 if (quote_char) {
5036 ast_error(c, n, "f-string: unterminated string");
5037 return -1;
5038 }
5039 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005040 int opening = parenstack[nested_depth - 1];
5041 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005042 return -1;
5043 }
5044
Eric V. Smith451d0e32016-09-09 21:56:20 -04005045 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005046 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005047
5048 /* Compile the expression as soon as possible, so we show errors
5049 related to the expression before errors related to the
5050 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005051 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005052 if (!simple_expression)
5053 return -1;
5054
5055 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005056 if (**str == '!') {
5057 *str += 1;
5058 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005059 goto unexpected_end_of_string;
5060
Eric V. Smith451d0e32016-09-09 21:56:20 -04005061 conversion = **str;
5062 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063
5064 /* Validate the conversion. */
5065 if (!(conversion == 's' || conversion == 'r'
5066 || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005067 ast_error(c, n,
5068 "f-string: invalid conversion character: "
5069 "expected 's', 'r', or 'a'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005070 return -1;
5071 }
5072 }
5073
5074 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005075 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005076 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005077 if (**str == ':') {
5078 *str += 1;
5079 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005080 goto unexpected_end_of_string;
5081
5082 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005083 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005084 if (!format_spec)
5085 return -1;
5086 }
5087
Eric V. Smith451d0e32016-09-09 21:56:20 -04005088 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005089 goto unexpected_end_of_string;
5090
5091 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005092 assert(*str < end);
5093 assert(**str == '}');
5094 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005095
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 /* And now create the FormattedValue node that represents this
5097 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005098 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005099 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005100 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005101 c->c_arena);
5102 if (!*expression)
5103 return -1;
5104
5105 return 0;
5106
5107unexpected_end_of_string:
5108 ast_error(c, n, "f-string: expecting '}'");
5109 return -1;
5110}
5111
5112/* Return -1 on error.
5113
5114 Return 0 if we have a literal (possible zero length) and an
5115 expression (zero length if at the end of the string.
5116
5117 Return 1 if we have a literal, but no expression, and we want the
5118 caller to call us again. This is used to deal with doubled
5119 braces.
5120
5121 When called multiple times on the string 'a{{b{0}c', this function
5122 will return:
5123
5124 1. the literal 'a{' with no expression, and a return value
5125 of 1. Despite the fact that there's no expression, the return
5126 value of 1 means we're not finished yet.
5127
5128 2. the literal 'b' and the expression '0', with a return value of
5129 0. The fact that there's an expression means we're not finished.
5130
5131 3. literal 'c' with no expression and a return value of 0. The
5132 combination of the return value of 0 with no expression means
5133 we're finished.
5134*/
5135static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005136fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5137 int recurse_lvl, PyObject **literal,
5138 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005139 struct compiling *c, const node *n)
5140{
5141 int result;
5142
5143 assert(*literal == NULL && *expression == NULL);
5144
5145 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005146 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 if (result < 0)
5148 goto error;
5149
5150 assert(result == 0 || result == 1);
5151
5152 if (result == 1)
5153 /* We have a literal, but don't look at the expression. */
5154 return 1;
5155
Eric V. Smith451d0e32016-09-09 21:56:20 -04005156 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005157 /* We're at the end of the string or the end of a nested
5158 f-string: no expression. The top-level error case where we
5159 expect to be at the end of the string but we're at a '}' is
5160 handled later. */
5161 return 0;
5162
5163 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005164 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005165
Eric V. Smith451d0e32016-09-09 21:56:20 -04005166 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005167 goto error;
5168
5169 return 0;
5170
5171error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005172 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005173 return -1;
5174}
5175
5176#define EXPRLIST_N_CACHED 64
5177
5178typedef struct {
5179 /* Incrementally build an array of expr_ty, so be used in an
5180 asdl_seq. Cache some small but reasonably sized number of
5181 expr_ty's, and then after that start dynamically allocating,
5182 doubling the number allocated each time. Note that the f-string
5183 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005184 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185 fast as you add exressions in an f-string. */
5186
5187 Py_ssize_t allocated; /* Number we've allocated. */
5188 Py_ssize_t size; /* Number we've used. */
5189 expr_ty *p; /* Pointer to the memory we're actually
5190 using. Will point to 'data' until we
5191 start dynamically allocating. */
5192 expr_ty data[EXPRLIST_N_CACHED];
5193} ExprList;
5194
5195#ifdef NDEBUG
5196#define ExprList_check_invariants(l)
5197#else
5198static void
5199ExprList_check_invariants(ExprList *l)
5200{
5201 /* Check our invariants. Make sure this object is "live", and
5202 hasn't been deallocated. */
5203 assert(l->size >= 0);
5204 assert(l->p != NULL);
5205 if (l->size <= EXPRLIST_N_CACHED)
5206 assert(l->data == l->p);
5207}
5208#endif
5209
5210static void
5211ExprList_Init(ExprList *l)
5212{
5213 l->allocated = EXPRLIST_N_CACHED;
5214 l->size = 0;
5215
5216 /* Until we start allocating dynamically, p points to data. */
5217 l->p = l->data;
5218
5219 ExprList_check_invariants(l);
5220}
5221
5222static int
5223ExprList_Append(ExprList *l, expr_ty exp)
5224{
5225 ExprList_check_invariants(l);
5226 if (l->size >= l->allocated) {
5227 /* We need to alloc (or realloc) the memory. */
5228 Py_ssize_t new_size = l->allocated * 2;
5229
5230 /* See if we've ever allocated anything dynamically. */
5231 if (l->p == l->data) {
5232 Py_ssize_t i;
5233 /* We're still using the cached data. Switch to
5234 alloc-ing. */
5235 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5236 if (!l->p)
5237 return -1;
5238 /* Copy the cached data into the new buffer. */
5239 for (i = 0; i < l->size; i++)
5240 l->p[i] = l->data[i];
5241 } else {
5242 /* Just realloc. */
5243 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5244 if (!tmp) {
5245 PyMem_RawFree(l->p);
5246 l->p = NULL;
5247 return -1;
5248 }
5249 l->p = tmp;
5250 }
5251
5252 l->allocated = new_size;
5253 assert(l->allocated == 2 * l->size);
5254 }
5255
5256 l->p[l->size++] = exp;
5257
5258 ExprList_check_invariants(l);
5259 return 0;
5260}
5261
5262static void
5263ExprList_Dealloc(ExprList *l)
5264{
5265 ExprList_check_invariants(l);
5266
5267 /* If there's been an error, or we've never dynamically allocated,
5268 do nothing. */
5269 if (!l->p || l->p == l->data) {
5270 /* Do nothing. */
5271 } else {
5272 /* We have dynamically allocated. Free the memory. */
5273 PyMem_RawFree(l->p);
5274 }
5275 l->p = NULL;
5276 l->size = -1;
5277}
5278
5279static asdl_seq *
5280ExprList_Finish(ExprList *l, PyArena *arena)
5281{
5282 asdl_seq *seq;
5283
5284 ExprList_check_invariants(l);
5285
5286 /* Allocate the asdl_seq and copy the expressions in to it. */
5287 seq = _Py_asdl_seq_new(l->size, arena);
5288 if (seq) {
5289 Py_ssize_t i;
5290 for (i = 0; i < l->size; i++)
5291 asdl_seq_SET(seq, i, l->p[i]);
5292 }
5293 ExprList_Dealloc(l);
5294 return seq;
5295}
5296
5297/* The FstringParser is designed to add a mix of strings and
5298 f-strings, and concat them together as needed. Ultimately, it
5299 generates an expr_ty. */
5300typedef struct {
5301 PyObject *last_str;
5302 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005303 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005304} FstringParser;
5305
5306#ifdef NDEBUG
5307#define FstringParser_check_invariants(state)
5308#else
5309static void
5310FstringParser_check_invariants(FstringParser *state)
5311{
5312 if (state->last_str)
5313 assert(PyUnicode_CheckExact(state->last_str));
5314 ExprList_check_invariants(&state->expr_list);
5315}
5316#endif
5317
5318static void
5319FstringParser_Init(FstringParser *state)
5320{
5321 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005322 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005323 ExprList_Init(&state->expr_list);
5324 FstringParser_check_invariants(state);
5325}
5326
5327static void
5328FstringParser_Dealloc(FstringParser *state)
5329{
5330 FstringParser_check_invariants(state);
5331
5332 Py_XDECREF(state->last_str);
5333 ExprList_Dealloc(&state->expr_list);
5334}
5335
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005336/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005337static expr_ty
5338make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5339{
5340 PyObject *s = *str;
5341 *str = NULL;
5342 assert(PyUnicode_CheckExact(s));
5343 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5344 Py_DECREF(s);
5345 return NULL;
5346 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005347 return Constant(s, LINENO(n), n->n_col_offset,
5348 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005349}
5350
5351/* Add a non-f-string (that is, a regular literal string). str is
5352 decref'd. */
5353static int
5354FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5355{
5356 FstringParser_check_invariants(state);
5357
5358 assert(PyUnicode_CheckExact(str));
5359
5360 if (PyUnicode_GET_LENGTH(str) == 0) {
5361 Py_DECREF(str);
5362 return 0;
5363 }
5364
5365 if (!state->last_str) {
5366 /* We didn't have a string before, so just remember this one. */
5367 state->last_str = str;
5368 } else {
5369 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005370 PyUnicode_AppendAndDel(&state->last_str, str);
5371 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005372 return -1;
5373 }
5374 FstringParser_check_invariants(state);
5375 return 0;
5376}
5377
Eric V. Smith451d0e32016-09-09 21:56:20 -04005378/* Parse an f-string. The f-string is in *str to end, with no
5379 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005380static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005381FstringParser_ConcatFstring(FstringParser *state, const char **str,
5382 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005383 struct compiling *c, const node *n)
5384{
5385 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005386 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005387
5388 /* Parse the f-string. */
5389 while (1) {
5390 PyObject *literal = NULL;
5391 expr_ty expression = NULL;
5392
5393 /* If there's a zero length literal in front of the
5394 expression, literal will be NULL. If we're at the end of
5395 the f-string, expression will be NULL (unless result == 1,
5396 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005397 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005398 &literal, &expression,
5399 c, n);
5400 if (result < 0)
5401 return -1;
5402
5403 /* Add the literal, if any. */
5404 if (!literal) {
5405 /* Do nothing. Just leave last_str alone (and possibly
5406 NULL). */
5407 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005408 /* Note that the literal can be zero length, if the
5409 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005410 state->last_str = literal;
5411 literal = NULL;
5412 } else {
5413 /* We have a literal, concatenate it. */
5414 assert(PyUnicode_GET_LENGTH(literal) != 0);
5415 if (FstringParser_ConcatAndDel(state, literal) < 0)
5416 return -1;
5417 literal = NULL;
5418 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005419
5420 /* We've dealt with the literal now. It can't be leaked on further
5421 errors. */
5422 assert(literal == NULL);
5423
5424 /* See if we should just loop around to get the next literal
5425 and expression, while ignoring the expression this
5426 time. This is used for un-doubling braces, as an
5427 optimization. */
5428 if (result == 1)
5429 continue;
5430
5431 if (!expression)
5432 /* We're done with this f-string. */
5433 break;
5434
5435 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005436 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005437 if (!state->last_str) {
5438 /* Do nothing. No previous literal. */
5439 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005440 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005441 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5442 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5443 return -1;
5444 }
5445
5446 if (ExprList_Append(&state->expr_list, expression) < 0)
5447 return -1;
5448 }
5449
Eric V. Smith235a6f02015-09-19 14:51:32 -04005450 /* If recurse_lvl is zero, then we must be at the end of the
5451 string. Otherwise, we must be at a right brace. */
5452
Eric V. Smith451d0e32016-09-09 21:56:20 -04005453 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005454 ast_error(c, n, "f-string: unexpected end of string");
5455 return -1;
5456 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005457 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005458 ast_error(c, n, "f-string: expecting '}'");
5459 return -1;
5460 }
5461
5462 FstringParser_check_invariants(state);
5463 return 0;
5464}
5465
5466/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005467 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005468static expr_ty
5469FstringParser_Finish(FstringParser *state, struct compiling *c,
5470 const node *n)
5471{
5472 asdl_seq *seq;
5473
5474 FstringParser_check_invariants(state);
5475
5476 /* If we're just a constant string with no expressions, return
5477 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005478 if (!state->fmode) {
5479 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005480 if (!state->last_str) {
5481 /* Create a zero length string. */
5482 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5483 if (!state->last_str)
5484 goto error;
5485 }
5486 return make_str_node_and_del(&state->last_str, c, n);
5487 }
5488
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005489 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005490 last node in our expression list. */
5491 if (state->last_str) {
5492 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5493 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5494 goto error;
5495 }
5496 /* This has already been freed. */
5497 assert(state->last_str == NULL);
5498
5499 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5500 if (!seq)
5501 goto error;
5502
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005503 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5504 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005505
5506error:
5507 FstringParser_Dealloc(state);
5508 return NULL;
5509}
5510
Eric V. Smith451d0e32016-09-09 21:56:20 -04005511/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5512 at end, parse it into an expr_ty. Return NULL on error. Adjust
5513 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005514static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005515fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005516 struct compiling *c, const node *n)
5517{
5518 FstringParser state;
5519
5520 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005521 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005522 c, n) < 0) {
5523 FstringParser_Dealloc(&state);
5524 return NULL;
5525 }
5526
5527 return FstringParser_Finish(&state, c, n);
5528}
5529
5530/* n is a Python string literal, including the bracketing quote
5531 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005532 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005533 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005534 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5535 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005536*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005537static int
5538parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5539 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005540{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005541 size_t len;
5542 const char *s = STR(n);
5543 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005544 int fmode = 0;
5545 *bytesmode = 0;
5546 *rawmode = 0;
5547 *result = NULL;
5548 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005549 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005550 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005551 if (quote == 'b' || quote == 'B') {
5552 quote = *++s;
5553 *bytesmode = 1;
5554 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005555 else if (quote == 'u' || quote == 'U') {
5556 quote = *++s;
5557 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005558 else if (quote == 'r' || quote == 'R') {
5559 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005560 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005561 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005562 else if (quote == 'f' || quote == 'F') {
5563 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005564 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005565 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005566 else {
5567 break;
5568 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005569 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005570 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005571 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005572 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005573 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005574 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005575 if (quote != '\'' && quote != '\"') {
5576 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005577 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005578 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005579 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005580 s++;
5581 len = strlen(s);
5582 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005583 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005584 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005585 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005586 }
5587 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005588 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005589 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005590 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005591 }
5592 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005593 /* A triple quoted string. We've already skipped one quote at
5594 the start and one at the end of the string. Now skip the
5595 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005596 s += 2;
5597 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005598 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005599 if (s[--len] != quote || s[--len] != quote) {
5600 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005601 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005602 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005603 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005604
Eric V. Smith451d0e32016-09-09 21:56:20 -04005605 if (fmode) {
5606 /* Just return the bytes. The caller will parse the resulting
5607 string. */
5608 *fstr = s;
5609 *fstrlen = len;
5610 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005611 }
5612
Eric V. Smith451d0e32016-09-09 21:56:20 -04005613 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005614 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005615 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005616 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005617 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005618 const char *ch;
5619 for (ch = s; *ch; ch++) {
5620 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005621 ast_error(c, n,
5622 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005623 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005624 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005625 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005626 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005627 if (*rawmode)
5628 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005629 else
Eric V. Smith56466482016-10-31 14:46:26 -04005630 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005631 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005632 if (*rawmode)
5633 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005634 else
Eric V. Smith56466482016-10-31 14:46:26 -04005635 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005636 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005637 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005638}
5639
Eric V. Smith235a6f02015-09-19 14:51:32 -04005640/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5641 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005642 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005643 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005644 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005645 node if there's just an f-string (with no leading or trailing
5646 literals), or a JoinedStr node if there are multiple f-strings or
5647 any literals involved. */
5648static expr_ty
5649parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005650{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005651 int bytesmode = 0;
5652 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005653 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005654
5655 FstringParser state;
5656 FstringParser_Init(&state);
5657
5658 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005659 int this_bytesmode;
5660 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005661 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005662 const char *fstr;
5663 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005664
5665 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005666 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5667 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005668 goto error;
5669
5670 /* Check that we're not mixing bytes with unicode. */
5671 if (i != 0 && bytesmode != this_bytesmode) {
5672 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005673 /* s is NULL if the current string part is an f-string. */
5674 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005675 goto error;
5676 }
5677 bytesmode = this_bytesmode;
5678
Eric V. Smith451d0e32016-09-09 21:56:20 -04005679 if (fstr != NULL) {
5680 int result;
5681 assert(s == NULL && !bytesmode);
5682 /* This is an f-string. Parse and concatenate it. */
5683 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5684 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005685 if (result < 0)
5686 goto error;
5687 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005688 /* A string or byte string. */
5689 assert(s != NULL && fstr == NULL);
5690
Eric V. Smith451d0e32016-09-09 21:56:20 -04005691 assert(bytesmode ? PyBytes_CheckExact(s) :
5692 PyUnicode_CheckExact(s));
5693
Eric V. Smith451d0e32016-09-09 21:56:20 -04005694 if (bytesmode) {
5695 /* For bytes, concat as we go. */
5696 if (i == 0) {
5697 /* First time, just remember this value. */
5698 bytes_str = s;
5699 } else {
5700 PyBytes_ConcatAndDel(&bytes_str, s);
5701 if (!bytes_str)
5702 goto error;
5703 }
5704 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005705 /* This is a regular string. Concatenate it. */
5706 if (FstringParser_ConcatAndDel(&state, s) < 0)
5707 goto error;
5708 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005709 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005710 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005711 if (bytesmode) {
5712 /* Just return the bytes object and we're done. */
5713 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5714 goto error;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005715 return Constant(bytes_str, LINENO(n), n->n_col_offset,
5716 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005717 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005718
Eric V. Smith235a6f02015-09-19 14:51:32 -04005719 /* We're not a bytes string, bytes_str should never have been set. */
5720 assert(bytes_str == NULL);
5721
5722 return FstringParser_Finish(&state, c, n);
5723
5724error:
5725 Py_XDECREF(bytes_str);
5726 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005727 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005728}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005729
5730PyObject *
5731_PyAST_GetDocString(asdl_seq *body)
5732{
5733 if (!asdl_seq_LEN(body)) {
5734 return NULL;
5735 }
5736 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5737 if (st->kind != Expr_kind) {
5738 return NULL;
5739 }
5740 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005741 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5742 return e->v.Constant.value;
5743 }
5744 return NULL;
5745}