blob: 62ee60aa9ff6ceb60b25d7011f1c2b07ac29f373 [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
Benjamin Peterson832bfe22011-08-09 16:15:04 -050043validate_keywords(asdl_seq *keywords)
44{
Victor Stinner4d73ae72018-11-22 14:45:16 +010045 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050046 for (i = 0; i < asdl_seq_LEN(keywords); i++)
47 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
48 return 0;
49 return 1;
50}
51
52static int
53validate_args(asdl_seq *args)
54{
Victor Stinner4d73ae72018-11-22 14:45:16 +010055 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050056 for (i = 0; i < asdl_seq_LEN(args); i++) {
57 arg_ty arg = asdl_seq_GET(args, i);
58 if (arg->annotation && !validate_expr(arg->annotation, Load))
59 return 0;
60 }
61 return 1;
62}
63
64static const char *
65expr_context_name(expr_context_ty ctx)
66{
67 switch (ctx) {
68 case Load:
69 return "Load";
70 case Store:
71 return "Store";
72 case Del:
73 return "Del";
74 case AugLoad:
75 return "AugLoad";
76 case AugStore:
77 return "AugStore";
78 case Param:
79 return "Param";
80 default:
Barry Warsawb2e57942017-09-14 18:13:16 -070081 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -050082 }
83}
84
85static int
86validate_arguments(arguments_ty args)
87{
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010088 if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050089 return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010090 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -070091 if (args->vararg && args->vararg->annotation
92 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050093 return 0;
94 }
95 if (!validate_args(args->kwonlyargs))
96 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +010097 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -070098 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050099 return 0;
100 }
Pablo Galindo2f58a842019-05-31 14:09:49 +0100101 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500102 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
103 return 0;
104 }
105 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
106 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
107 "kw_defaults on arguments");
108 return 0;
109 }
110 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
111}
112
113static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100114validate_constant(PyObject *value)
115{
116 if (value == Py_None || value == Py_Ellipsis)
117 return 1;
118
119 if (PyLong_CheckExact(value)
120 || PyFloat_CheckExact(value)
121 || PyComplex_CheckExact(value)
122 || PyBool_Check(value)
123 || PyUnicode_CheckExact(value)
124 || PyBytes_CheckExact(value))
125 return 1;
126
127 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
128 PyObject *it;
129
130 it = PyObject_GetIter(value);
131 if (it == NULL)
132 return 0;
133
134 while (1) {
135 PyObject *item = PyIter_Next(it);
136 if (item == NULL) {
137 if (PyErr_Occurred()) {
138 Py_DECREF(it);
139 return 0;
140 }
141 break;
142 }
143
144 if (!validate_constant(item)) {
145 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100146 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100147 return 0;
148 }
Victor Stinner726f6902016-01-27 00:11:47 +0100149 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100150 }
151
152 Py_DECREF(it);
153 return 1;
154 }
155
156 return 0;
157}
158
159static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500160validate_expr(expr_ty exp, expr_context_ty ctx)
161{
162 int check_ctx = 1;
163 expr_context_ty actual_ctx;
164
165 /* First check expression context. */
166 switch (exp->kind) {
167 case Attribute_kind:
168 actual_ctx = exp->v.Attribute.ctx;
169 break;
170 case Subscript_kind:
171 actual_ctx = exp->v.Subscript.ctx;
172 break;
173 case Starred_kind:
174 actual_ctx = exp->v.Starred.ctx;
175 break;
176 case Name_kind:
177 actual_ctx = exp->v.Name.ctx;
178 break;
179 case List_kind:
180 actual_ctx = exp->v.List.ctx;
181 break;
182 case Tuple_kind:
183 actual_ctx = exp->v.Tuple.ctx;
184 break;
185 default:
186 if (ctx != Load) {
187 PyErr_Format(PyExc_ValueError, "expression which can't be "
188 "assigned to in %s context", expr_context_name(ctx));
189 return 0;
190 }
191 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100192 /* set actual_ctx to prevent gcc warning */
193 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500194 }
195 if (check_ctx && actual_ctx != ctx) {
196 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
197 expr_context_name(ctx), expr_context_name(actual_ctx));
198 return 0;
199 }
200
201 /* Now validate expression. */
202 switch (exp->kind) {
203 case BoolOp_kind:
204 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
205 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
206 return 0;
207 }
208 return validate_exprs(exp->v.BoolOp.values, Load, 0);
209 case BinOp_kind:
210 return validate_expr(exp->v.BinOp.left, Load) &&
211 validate_expr(exp->v.BinOp.right, Load);
212 case UnaryOp_kind:
213 return validate_expr(exp->v.UnaryOp.operand, Load);
214 case Lambda_kind:
215 return validate_arguments(exp->v.Lambda.args) &&
216 validate_expr(exp->v.Lambda.body, Load);
217 case IfExp_kind:
218 return validate_expr(exp->v.IfExp.test, Load) &&
219 validate_expr(exp->v.IfExp.body, Load) &&
220 validate_expr(exp->v.IfExp.orelse, Load);
221 case Dict_kind:
222 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
223 PyErr_SetString(PyExc_ValueError,
224 "Dict doesn't have the same number of keys as values");
225 return 0;
226 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400227 /* null_ok=1 for keys expressions to allow dict unpacking to work in
228 dict literals, i.e. ``{**{a:b}}`` */
229 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
230 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500231 case Set_kind:
232 return validate_exprs(exp->v.Set.elts, Load, 0);
233#define COMP(NAME) \
234 case NAME ## _kind: \
235 return validate_comprehension(exp->v.NAME.generators) && \
236 validate_expr(exp->v.NAME.elt, Load);
237 COMP(ListComp)
238 COMP(SetComp)
239 COMP(GeneratorExp)
240#undef COMP
241 case DictComp_kind:
242 return validate_comprehension(exp->v.DictComp.generators) &&
243 validate_expr(exp->v.DictComp.key, Load) &&
244 validate_expr(exp->v.DictComp.value, Load);
245 case Yield_kind:
246 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500247 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000248 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400249 case Await_kind:
250 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500251 case Compare_kind:
252 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
253 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
254 return 0;
255 }
256 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
257 asdl_seq_LEN(exp->v.Compare.ops)) {
258 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
259 "of comparators and operands");
260 return 0;
261 }
262 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
263 validate_expr(exp->v.Compare.left, Load);
264 case Call_kind:
265 return validate_expr(exp->v.Call.func, Load) &&
266 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400267 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100268 case Constant_kind:
269 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100270 PyErr_Format(PyExc_TypeError,
271 "got an invalid type in Constant: %s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700272 _PyType_Name(Py_TYPE(exp->v.Constant.value)));
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100273 return 0;
274 }
275 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400276 case JoinedStr_kind:
277 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
278 case FormattedValue_kind:
279 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
280 return 0;
281 if (exp->v.FormattedValue.format_spec)
282 return validate_expr(exp->v.FormattedValue.format_spec, Load);
283 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500284 case Attribute_kind:
285 return validate_expr(exp->v.Attribute.value, Load);
286 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200287 return validate_expr(exp->v.Subscript.slice, Load) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500288 validate_expr(exp->v.Subscript.value, Load);
289 case Starred_kind:
290 return validate_expr(exp->v.Starred.value, ctx);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200291 case Slice_kind:
292 return (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) &&
293 (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) &&
294 (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500295 case List_kind:
296 return validate_exprs(exp->v.List.elts, ctx, 0);
297 case Tuple_kind:
298 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000299 case NamedExpr_kind:
300 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300301 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500302 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500303 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500304 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000305 PyErr_SetString(PyExc_SystemError, "unexpected expression");
306 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500307}
308
309static int
310validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
311{
312 if (asdl_seq_LEN(seq))
313 return 1;
314 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
315 return 0;
316}
317
318static int
319validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
320{
321 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
322 validate_exprs(targets, ctx, 0);
323}
324
325static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300326validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500327{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300328 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500329}
330
331static int
332validate_stmt(stmt_ty stmt)
333{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100334 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500335 switch (stmt->kind) {
336 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300337 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500338 validate_arguments(stmt->v.FunctionDef.args) &&
339 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
340 (!stmt->v.FunctionDef.returns ||
341 validate_expr(stmt->v.FunctionDef.returns, Load));
342 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300343 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
345 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400346 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500347 case Return_kind:
348 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
349 case Delete_kind:
350 return validate_assignlist(stmt->v.Delete.targets, Del);
351 case Assign_kind:
352 return validate_assignlist(stmt->v.Assign.targets, Store) &&
353 validate_expr(stmt->v.Assign.value, Load);
354 case AugAssign_kind:
355 return validate_expr(stmt->v.AugAssign.target, Store) &&
356 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700357 case AnnAssign_kind:
358 if (stmt->v.AnnAssign.target->kind != Name_kind &&
359 stmt->v.AnnAssign.simple) {
360 PyErr_SetString(PyExc_TypeError,
361 "AnnAssign with simple non-Name target");
362 return 0;
363 }
364 return validate_expr(stmt->v.AnnAssign.target, Store) &&
365 (!stmt->v.AnnAssign.value ||
366 validate_expr(stmt->v.AnnAssign.value, Load)) &&
367 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 case For_kind:
369 return validate_expr(stmt->v.For.target, Store) &&
370 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300371 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500372 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400373 case AsyncFor_kind:
374 return validate_expr(stmt->v.AsyncFor.target, Store) &&
375 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300376 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400377 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500378 case While_kind:
379 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300380 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500381 validate_stmts(stmt->v.While.orelse);
382 case If_kind:
383 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300384 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500385 validate_stmts(stmt->v.If.orelse);
386 case With_kind:
387 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
388 return 0;
389 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
390 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
391 if (!validate_expr(item->context_expr, Load) ||
392 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
393 return 0;
394 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300395 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400396 case AsyncWith_kind:
397 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
398 return 0;
399 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
400 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
401 if (!validate_expr(item->context_expr, Load) ||
402 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
403 return 0;
404 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300405 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 case Raise_kind:
407 if (stmt->v.Raise.exc) {
408 return validate_expr(stmt->v.Raise.exc, Load) &&
409 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
410 }
411 if (stmt->v.Raise.cause) {
412 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
413 return 0;
414 }
415 return 1;
416 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300417 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500418 return 0;
419 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
420 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
421 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
422 return 0;
423 }
424 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
425 asdl_seq_LEN(stmt->v.Try.orelse)) {
426 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
427 return 0;
428 }
429 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
430 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
431 if ((handler->v.ExceptHandler.type &&
432 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300433 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500434 return 0;
435 }
436 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
437 validate_stmts(stmt->v.Try.finalbody)) &&
438 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
439 validate_stmts(stmt->v.Try.orelse));
440 case Assert_kind:
441 return validate_expr(stmt->v.Assert.test, Load) &&
442 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
443 case Import_kind:
444 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
445 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300446 if (stmt->v.ImportFrom.level < 0) {
447 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500448 return 0;
449 }
450 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
451 case Global_kind:
452 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
453 case Nonlocal_kind:
454 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
455 case Expr_kind:
456 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400457 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300458 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400459 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
460 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
461 (!stmt->v.AsyncFunctionDef.returns ||
462 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500463 case Pass_kind:
464 case Break_kind:
465 case Continue_kind:
466 return 1;
467 default:
468 PyErr_SetString(PyExc_SystemError, "unexpected statement");
469 return 0;
470 }
471}
472
473static int
474validate_stmts(asdl_seq *seq)
475{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100476 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500477 for (i = 0; i < asdl_seq_LEN(seq); i++) {
478 stmt_ty stmt = asdl_seq_GET(seq, i);
479 if (stmt) {
480 if (!validate_stmt(stmt))
481 return 0;
482 }
483 else {
484 PyErr_SetString(PyExc_ValueError,
485 "None disallowed in statement list");
486 return 0;
487 }
488 }
489 return 1;
490}
491
492static int
493validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
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(exprs); i++) {
497 expr_ty expr = asdl_seq_GET(exprs, i);
498 if (expr) {
499 if (!validate_expr(expr, ctx))
500 return 0;
501 }
502 else if (!null_ok) {
503 PyErr_SetString(PyExc_ValueError,
504 "None disallowed in expression list");
505 return 0;
506 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100507
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500508 }
509 return 1;
510}
511
512int
513PyAST_Validate(mod_ty mod)
514{
515 int res = 0;
516
517 switch (mod->kind) {
518 case Module_kind:
519 res = validate_stmts(mod->v.Module.body);
520 break;
521 case Interactive_kind:
522 res = validate_stmts(mod->v.Interactive.body);
523 break;
524 case Expression_kind:
525 res = validate_expr(mod->v.Expression.body, Load);
526 break;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500527 default:
528 PyErr_SetString(PyExc_SystemError, "impossible module node");
529 res = 0;
530 break;
531 }
532 return res;
533}
534
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500535/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500536#include "grammar.h"
537#include "parsetok.h"
538#include "graminit.h"
539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540/* Data structure used internally */
541struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400542 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200543 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500544 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800545 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546};
547
548static asdl_seq *seq_for_testlist(struct compiling *, const node *);
549static expr_ty ast_for_expr(struct compiling *, const node *);
550static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300551static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000552static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
553 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000554static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000555static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556
guoci90fc8982018-09-11 17:45:45 -0400557static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
558static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200561static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +0200562 const node *, const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000564static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400565static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000566static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567
Nick Coghlan650f0d02007-04-15 12:05:43 +0000568#define COMP_GENEXP 0
569#define COMP_LISTCOMP 1
570#define COMP_SETCOMP 2
571
Benjamin Peterson55e00432012-01-16 17:22:31 -0500572static int
573init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000574{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500575 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
576 if (!m)
577 return 0;
578 c->c_normalize = PyObject_GetAttrString(m, "normalize");
579 Py_DECREF(m);
580 if (!c->c_normalize)
581 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500582 return 1;
583}
584
585static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400586new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500587{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400588 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500589 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000590 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500591 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500592 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000593 /* Check whether there are non-ASCII characters in the
594 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500595 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200596 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500597 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500598 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200599 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500600 }
Dino Viehland5b172c22019-09-11 08:47:17 -0700601 PyObject *form = PyUnicode_InternFromString("NFKC");
Oren Milman7dc46d82017-09-30 20:16:24 +0300602 if (form == NULL) {
603 Py_DECREF(id);
604 return NULL;
605 }
606 PyObject *args[2] = {form, id};
607 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500608 Py_DECREF(id);
Dino Viehland8d88e8c2019-09-12 15:38:13 +0100609 Py_DECREF(form);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200610 if (!id2)
611 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300612 if (!PyUnicode_Check(id2)) {
613 PyErr_Format(PyExc_TypeError,
614 "unicodedata.normalize() must return a string, not "
615 "%.200s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700616 _PyType_Name(Py_TYPE(id2)));
Oren Milman7dc46d82017-09-30 20:16:24 +0300617 Py_DECREF(id2);
618 return NULL;
619 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200620 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000621 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000622 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200623 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
624 Py_DECREF(id);
625 return NULL;
626 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000627 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628}
629
Benjamin Peterson55e00432012-01-16 17:22:31 -0500630#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200633ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400635 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200636 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200638 va_start(va, errmsg);
639 errstr = PyUnicode_FromFormatV(errmsg, va);
640 va_end(va);
641 if (!errstr) {
642 return 0;
643 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200644 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000646 Py_INCREF(Py_None);
647 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 }
Ammar Askar025eb982018-09-24 17:12:49 -0400649 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200650 if (!tmp) {
651 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400652 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000653 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 Py_DECREF(errstr);
656 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400657 if (value) {
658 PyErr_SetObject(PyExc_SyntaxError, value);
659 Py_DECREF(value);
660 }
661 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662}
663
664/* num_stmts() returns number of contained statements.
665
666 Use this routine to determine how big a sequence is needed for
667 the statements in a parse tree. Its raison d'etre is this bit of
668 grammar:
669
670 stmt: simple_stmt | compound_stmt
671 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
672
673 A simple_stmt can contain multiple small_stmt elements joined
674 by semicolons. If the arg is a simple_stmt, the number of
675 small_stmt elements is returned.
676*/
677
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800678static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800679new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800680{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800681 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800682 if (res == NULL)
683 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800684 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
685 Py_DECREF(res);
686 return NULL;
687 }
688 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800689}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800690#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800691
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692static int
693num_stmts(const node *n)
694{
695 int i, l;
696 node *ch;
697
698 switch (TYPE(n)) {
699 case single_input:
700 if (TYPE(CHILD(n, 0)) == NEWLINE)
701 return 0;
702 else
703 return num_stmts(CHILD(n, 0));
704 case file_input:
705 l = 0;
706 for (i = 0; i < NCH(n); i++) {
707 ch = CHILD(n, i);
708 if (TYPE(ch) == stmt)
709 l += num_stmts(ch);
710 }
711 return l;
712 case stmt:
713 return num_stmts(CHILD(n, 0));
714 case compound_stmt:
715 return 1;
716 case simple_stmt:
717 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
718 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800719 case func_body_suite:
720 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
721 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 if (NCH(n) == 1)
723 return num_stmts(CHILD(n, 0));
724 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800725 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800727 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
728 i += 2;
729 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 l += num_stmts(CHILD(n, i));
731 return l;
732 }
733 default: {
734 char buf[128];
735
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000736 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 TYPE(n), NCH(n));
738 Py_FatalError(buf);
739 }
740 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700741 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742}
743
744/* Transform the CST rooted at node * to the appropriate AST
745*/
746
747mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200748PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
749 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000751 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800753 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 stmt_ty s;
755 node *ch;
756 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500757 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800758 asdl_seq *argtypes = NULL;
759 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400761 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200762 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400763 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800764 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700765 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800766
767 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769
Jeremy Hyltona8293132006-02-28 17:58:27 +0000770 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 switch (TYPE(n)) {
772 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200773 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500775 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 for (i = 0; i < NCH(n) - 1; i++) {
777 ch = CHILD(n, i);
778 if (TYPE(ch) == NEWLINE)
779 continue;
780 REQ(ch, stmt);
781 num = num_stmts(ch);
782 if (num == 1) {
783 s = ast_for_stmt(&c, ch);
784 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500785 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000786 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 }
788 else {
789 ch = CHILD(ch, 0);
790 REQ(ch, simple_stmt);
791 for (j = 0; j < num; j++) {
792 s = ast_for_stmt(&c, CHILD(ch, j * 2));
793 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500794 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000795 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 }
797 }
798 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800799
800 /* Type ignores are stored under the ENDMARKER in file_input. */
801 ch = CHILD(n, NCH(n) - 1);
802 REQ(ch, ENDMARKER);
803 num = NCH(ch);
804 type_ignores = _Py_asdl_seq_new(num, arena);
805 if (!type_ignores)
806 goto out;
807
808 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700809 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
810 if (!type_comment)
811 goto out;
812 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800813 if (!ti)
814 goto out;
815 asdl_seq_SET(type_ignores, i, ti);
816 }
817
818 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500819 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 case eval_input: {
821 expr_ty testlist_ast;
822
Nick Coghlan650f0d02007-04-15 12:05:43 +0000823 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000824 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500826 goto out;
827 res = Expression(testlist_ast, arena);
828 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 }
830 case single_input:
831 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200832 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500834 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000836 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000838 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500839 goto out;
840 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 }
842 else {
843 n = CHILD(n, 0);
844 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200845 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500847 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000849 s = ast_for_stmt(&c, n);
850 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500851 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 asdl_seq_SET(stmts, 0, s);
853 }
854 else {
855 /* Only a simple_stmt can contain multiple statements. */
856 REQ(n, simple_stmt);
857 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 if (TYPE(CHILD(n, i)) == NEWLINE)
859 break;
860 s = ast_for_stmt(&c, CHILD(n, i));
861 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500862 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 asdl_seq_SET(stmts, i / 2, s);
864 }
865 }
866
Benjamin Peterson55e00432012-01-16 17:22:31 -0500867 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500869 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800870 case func_type_input:
871 n = CHILD(n, 0);
872 REQ(n, func_type);
873
874 if (TYPE(CHILD(n, 1)) == typelist) {
875 ch = CHILD(n, 1);
876 /* this is overly permissive -- we don't pay any attention to
877 * stars on the args -- just parse them into an ordered list */
878 num = 0;
879 for (i = 0; i < NCH(ch); i++) {
880 if (TYPE(CHILD(ch, i)) == test) {
881 num++;
882 }
883 }
884
885 argtypes = _Py_asdl_seq_new(num, arena);
886 if (!argtypes)
887 goto out;
888
889 j = 0;
890 for (i = 0; i < NCH(ch); i++) {
891 if (TYPE(CHILD(ch, i)) == test) {
892 arg = ast_for_expr(&c, CHILD(ch, i));
893 if (!arg)
894 goto out;
895 asdl_seq_SET(argtypes, j++, arg);
896 }
897 }
898 }
899 else {
900 argtypes = _Py_asdl_seq_new(0, arena);
901 if (!argtypes)
902 goto out;
903 }
904
905 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
906 if (!ret)
907 goto out;
908 res = FunctionType(argtypes, ret, arena);
909 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000911 PyErr_Format(PyExc_SystemError,
912 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500913 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500915 out:
916 if (c.c_normalize) {
917 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500918 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500919 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920}
921
Victor Stinner14e461d2013-08-26 22:28:21 +0200922mod_ty
923PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
924 PyArena *arena)
925{
926 mod_ty mod;
927 PyObject *filename;
928 filename = PyUnicode_DecodeFSDefault(filename_str);
929 if (filename == NULL)
930 return NULL;
931 mod = PyAST_FromNodeObject(n, flags, filename, arena);
932 Py_DECREF(filename);
933 return mod;
934
935}
936
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
938*/
939
940static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800941get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942{
943 switch (TYPE(n)) {
944 case VBAR:
945 return BitOr;
946 case CIRCUMFLEX:
947 return BitXor;
948 case AMPER:
949 return BitAnd;
950 case LEFTSHIFT:
951 return LShift;
952 case RIGHTSHIFT:
953 return RShift;
954 case PLUS:
955 return Add;
956 case MINUS:
957 return Sub;
958 case STAR:
959 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400960 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800961 if (c->c_feature_version < 5) {
962 ast_error(c, n,
963 "The '@' operator is only supported in Python 3.5 and greater");
964 return (operator_ty)0;
965 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400966 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 case SLASH:
968 return Div;
969 case DOUBLESLASH:
970 return FloorDiv;
971 case PERCENT:
972 return Mod;
973 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000974 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 }
976}
977
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200978static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000979 "None",
980 "True",
981 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200982 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000983 NULL,
984};
985
986static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400987forbidden_name(struct compiling *c, identifier name, const node *n,
988 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000989{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000990 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200991 const char * const *p = FORBIDDEN;
992 if (!full_checks) {
993 /* In most cases, the parser will protect True, False, and None
994 from being assign to. */
995 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000996 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200997 for (; *p; p++) {
998 if (_PyUnicode_EqualToASCIIString(name, *p)) {
999 ast_error(c, n, "cannot assign to %U", name);
1000 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001001 }
1002 }
1003 return 0;
1004}
1005
Serhiy Storchakab619b092018-11-27 09:40:29 +02001006static expr_ty
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001007copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001008{
1009 if (e) {
1010 e->lineno = LINENO(n);
1011 e->col_offset = n->n_col_offset;
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001012 e->end_lineno = end->n_end_lineno;
1013 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001014 }
1015 return e;
1016}
1017
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001018static const char *
1019get_expr_name(expr_ty e)
1020{
1021 switch (e->kind) {
1022 case Attribute_kind:
1023 return "attribute";
1024 case Subscript_kind:
1025 return "subscript";
1026 case Starred_kind:
1027 return "starred";
1028 case Name_kind:
1029 return "name";
1030 case List_kind:
1031 return "list";
1032 case Tuple_kind:
1033 return "tuple";
1034 case Lambda_kind:
1035 return "lambda";
1036 case Call_kind:
1037 return "function call";
1038 case BoolOp_kind:
1039 case BinOp_kind:
1040 case UnaryOp_kind:
1041 return "operator";
1042 case GeneratorExp_kind:
1043 return "generator expression";
1044 case Yield_kind:
1045 case YieldFrom_kind:
1046 return "yield expression";
1047 case Await_kind:
1048 return "await expression";
1049 case ListComp_kind:
1050 return "list comprehension";
1051 case SetComp_kind:
1052 return "set comprehension";
1053 case DictComp_kind:
1054 return "dict comprehension";
1055 case Dict_kind:
1056 return "dict display";
1057 case Set_kind:
1058 return "set display";
1059 case JoinedStr_kind:
1060 case FormattedValue_kind:
1061 return "f-string expression";
1062 case Constant_kind: {
1063 PyObject *value = e->v.Constant.value;
1064 if (value == Py_None) {
1065 return "None";
1066 }
1067 if (value == Py_False) {
1068 return "False";
1069 }
1070 if (value == Py_True) {
1071 return "True";
1072 }
1073 if (value == Py_Ellipsis) {
1074 return "Ellipsis";
1075 }
1076 return "literal";
1077 }
1078 case Compare_kind:
1079 return "comparison";
1080 case IfExp_kind:
1081 return "conditional expression";
1082 case NamedExpr_kind:
1083 return "named expression";
1084 default:
1085 PyErr_Format(PyExc_SystemError,
1086 "unexpected expression in assignment %d (line %d)",
1087 e->kind, e->lineno);
1088 return NULL;
1089 }
1090}
1091
Jeremy Hyltona8293132006-02-28 17:58:27 +00001092/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093
1094 Only sets context for expr kinds that "can appear in assignment context"
1095 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1096 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097*/
1098
1099static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001100set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101{
1102 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001103
1104 /* The ast defines augmented store and load contexts, but the
1105 implementation here doesn't actually use them. The code may be
1106 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001107 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001108 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001109 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001110 */
1111 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112
1113 switch (e->kind) {
1114 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001115 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001116 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001117 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001118 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001120 e->v.Subscript.ctx = ctx;
1121 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001122 case Starred_kind:
1123 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001124 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001125 return 0;
1126 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001128 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001129 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001130 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001131 }
1132 e->v.Name.ctx = ctx;
1133 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001135 e->v.List.ctx = ctx;
1136 s = e->v.List.elts;
1137 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001139 e->v.Tuple.ctx = ctx;
1140 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001141 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001142 default: {
1143 const char *expr_name = get_expr_name(e);
1144 if (expr_name != NULL) {
1145 ast_error(c, n, "cannot %s %s",
1146 ctx == Store ? "assign to" : "delete",
1147 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001148 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001149 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001150 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001151 }
1152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 */
1156 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001157 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001160 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001161 return 0;
1162 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 }
1164 return 1;
1165}
1166
1167static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001168ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169{
1170 REQ(n, augassign);
1171 n = CHILD(n, 0);
1172 switch (STR(n)[0]) {
1173 case '+':
1174 return Add;
1175 case '-':
1176 return Sub;
1177 case '/':
1178 if (STR(n)[1] == '/')
1179 return FloorDiv;
1180 else
1181 return Div;
1182 case '%':
1183 return Mod;
1184 case '<':
1185 return LShift;
1186 case '>':
1187 return RShift;
1188 case '&':
1189 return BitAnd;
1190 case '^':
1191 return BitXor;
1192 case '|':
1193 return BitOr;
1194 case '*':
1195 if (STR(n)[1] == '*')
1196 return Pow;
1197 else
1198 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001199 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001200 if (c->c_feature_version < 5) {
1201 ast_error(c, n,
1202 "The '@' operator is only supported in Python 3.5 and greater");
1203 return (operator_ty)0;
1204 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001205 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001207 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001208 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 }
1210}
1211
1212static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001213ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001215 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 |'is' 'not'
1217 */
1218 REQ(n, comp_op);
1219 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001220 n = CHILD(n, 0);
1221 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 case LESS:
1223 return Lt;
1224 case GREATER:
1225 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001226 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 return Eq;
1228 case LESSEQUAL:
1229 return LtE;
1230 case GREATEREQUAL:
1231 return GtE;
1232 case NOTEQUAL:
1233 return NotEq;
1234 case NAME:
1235 if (strcmp(STR(n), "in") == 0)
1236 return In;
1237 if (strcmp(STR(n), "is") == 0)
1238 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001239 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001241 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001243 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001244 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 }
1246 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001247 /* handle "not in" and "is not" */
1248 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 case NAME:
1250 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1251 return NotIn;
1252 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1253 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001254 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001256 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001258 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 }
Neal Norwitz79792652005-11-14 04:25:03 +00001261 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001263 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264}
1265
1266static asdl_seq *
1267seq_for_testlist(struct compiling *c, const node *n)
1268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001270 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1271 */
Armin Rigo31441302005-10-21 12:57:31 +00001272 asdl_seq *seq;
1273 expr_ty expression;
1274 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001275 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001277 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 if (!seq)
1279 return NULL;
1280
1281 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001283 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284
Benjamin Peterson4905e802009-09-27 02:43:28 +00001285 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001286 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288
1289 assert(i / 2 < seq->size);
1290 asdl_seq_SET(seq, i / 2, expression);
1291 }
1292 return seq;
1293}
1294
Neal Norwitzc1505362006-12-28 06:47:50 +00001295static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001296ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001297{
1298 identifier name;
1299 expr_ty annotation = NULL;
1300 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001301 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001302
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001303 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001304 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001305 name = NEW_IDENTIFIER(ch);
1306 if (!name)
1307 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001308 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001309 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001310
1311 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1312 annotation = ast_for_expr(c, CHILD(n, 2));
1313 if (!annotation)
1314 return NULL;
1315 }
1316
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001317 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001318 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001319 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001320 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001321 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322}
1323
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324/* returns -1 if failed to handle keyword only arguments
1325 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001326 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 ^^^
1328 start pointing here
1329 */
1330static int
1331handle_keywordonly_args(struct compiling *c, const node *n, int start,
1332 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1333{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001334 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001337 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 int i = start;
1339 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001340
1341 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001342 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001343 return -1;
1344 }
1345 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 while (i < NCH(n)) {
1347 ch = CHILD(n, i);
1348 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349 case vfpdef:
1350 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001352 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001353 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001354 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001355 asdl_seq_SET(kwdefaults, j, expression);
1356 i += 2; /* '=' and test */
1357 }
1358 else { /* setting NULL if no default value exists */
1359 asdl_seq_SET(kwdefaults, j, NULL);
1360 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001361 if (NCH(ch) == 3) {
1362 /* ch is NAME ':' test */
1363 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001364 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001365 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001366 }
1367 else {
1368 annotation = NULL;
1369 }
1370 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001371 argname = NEW_IDENTIFIER(ch);
1372 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001374 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001375 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001376 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001377 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001378 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001379 if (!arg)
1380 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001381 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001382 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001383 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001384 i += 1; /* the comma, if present */
1385 break;
1386 case TYPE_COMMENT:
1387 /* arg will be equal to the last argument processed */
1388 arg->type_comment = NEW_TYPE_COMMENT(ch);
1389 if (!arg->type_comment)
1390 goto error;
1391 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 break;
1393 case DOUBLESTAR:
1394 return i;
1395 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001396 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 goto error;
1398 }
1399 }
1400 return i;
1401 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001403}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404
Jeremy Hyltona8293132006-02-28 17:58:27 +00001405/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406
1407static arguments_ty
1408ast_for_arguments(struct compiling *c, const node *n)
1409{
Neal Norwitzc1505362006-12-28 06:47:50 +00001410 /* This function handles both typedargslist (function definition)
1411 and varargslist (lambda definition).
1412
1413 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001414
1415 The following definition for typedarglist is equivalent to this set of rules:
1416
1417 arguments = argument (',' [TYPE_COMMENT] argument)*
1418 argument = tfpdef ['=' test]
1419 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1420 args = '*' [tfpdef]
1421 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1422 [TYPE_COMMENT] [kwargs]])
1423 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1424 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1425 [TYPE_COMMENT] [args_kwonly_kwargs]])
1426 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1427 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1428 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1429
1430 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1431 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1432 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1433 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1434 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1435 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1436 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1437 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1438 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1439 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1440 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1441 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1442 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1443 '**' tfpdef [','] [TYPE_COMMENT]))
1444
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001445 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001446
1447 The following definition for varargslist is equivalent to this set of rules:
1448
1449 arguments = argument (',' argument )*
1450 argument = vfpdef ['=' test]
1451 kwargs = '**' vfpdef [',']
1452 args = '*' [vfpdef]
1453 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1454 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1455 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1456 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1457 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1458 (vararglist_no_posonly)
1459
1460 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1461 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1462 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1463 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1464 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1465 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1466 [',']]] | '**' vfpdef [','])
1467
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001468 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001469
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001471 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001472 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001473 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001474 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001475 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 node *ch;
1477
1478 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001479 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001480 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001481 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001483 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484
Jeremy Hyltone921e022008-07-17 16:37:17 +00001485 /* First count the number of positional args & defaults. The
1486 variable i is the loop index for this for loop and the next.
1487 The next loop picks up where the first leaves off.
1488 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001490 ch = CHILD(n, i);
1491 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001492 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001493 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001494 if (i < NCH(n) && /* skip argument following star */
1495 (TYPE(CHILD(n, i)) == tfpdef ||
1496 TYPE(CHILD(n, i)) == vfpdef)) {
1497 i++;
1498 }
1499 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001500 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001501 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001502 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001504 if (TYPE(ch) == SLASH ) {
1505 nposonlyargs = nposargs;
1506 nposargs = 0;
1507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001510 defaults for keyword only args */
1511 for ( ; i < NCH(n); ++i) {
1512 ch = CHILD(n, i);
1513 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001514 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001515 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001516 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1517 if (!posonlyargs && nposonlyargs) {
1518 return NULL;
1519 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001520 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001521 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001522 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001523 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001524 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001525 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001526 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001528 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001529 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001530 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001532 since we set NULL as default for keyword only argument w/o default
1533 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001534 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001535 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001536 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001537 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001538
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001539 /* tfpdef: NAME [':' test]
1540 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 */
1542 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001543 j = 0; /* index for defaults */
1544 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001545 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001547 ch = CHILD(n, i);
1548 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001549 case tfpdef:
1550 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1552 anything other than EQUAL or a comma? */
1553 /* XXX Should NCH(n) check be made a separate check? */
1554 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001555 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1556 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001557 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001558 assert(posdefaults != NULL);
1559 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001561 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001563 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001564 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001565 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001566 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001567 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001568 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001569 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001570 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001571 if (l < nposonlyargs) {
1572 asdl_seq_SET(posonlyargs, l++, arg);
1573 } else {
1574 asdl_seq_SET(posargs, k++, arg);
1575 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001576 i += 1; /* the name */
1577 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1578 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001580 case SLASH:
1581 /* Advance the slash and the comma. If there are more names
1582 * after the slash there will be a comma so we are advancing
1583 * the correct number of nodes. If the slash is the last item,
1584 * we will be advancing an extra token but then * i > NCH(n)
1585 * and the enclosing while will finish correctly. */
1586 i += 2;
1587 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001589 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001590 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1591 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001592 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001593 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001594 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001595 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001596 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001597 if (TYPE(ch) == COMMA) {
1598 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001599 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001600
1601 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1602 ast_error(c, CHILD(n, i),
1603 "bare * has associated type comment");
1604 return NULL;
1605 }
1606
Guido van Rossum4f72a782006-10-27 23:31:49 +00001607 res = handle_keywordonly_args(c, n, i,
1608 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001609 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001610 i = res; /* res has new position to process */
1611 }
1612 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001613 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001614 if (!vararg)
1615 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001616
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001617 i += 2; /* the star and the name */
1618 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1619 i += 1; /* the comma, if present */
1620
1621 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1622 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1623 if (!vararg->type_comment)
1624 return NULL;
1625 i += 1;
1626 }
1627
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001628 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1629 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001630 int res = 0;
1631 res = handle_keywordonly_args(c, n, i,
1632 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001633 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001634 i = res; /* res has new position to process */
1635 }
1636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637 break;
1638 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001639 ch = CHILD(n, i+1); /* tfpdef */
1640 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001641 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001642 if (!kwarg)
1643 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001644 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001645 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001646 i += 1; /* the comma, if present */
1647 break;
1648 case TYPE_COMMENT:
1649 assert(i);
1650
1651 if (kwarg)
1652 arg = kwarg;
1653
1654 /* arg will be equal to the last argument processed */
1655 arg->type_comment = NEW_TYPE_COMMENT(ch);
1656 if (!arg->type_comment)
1657 return NULL;
1658 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 break;
1660 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001661 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 "unexpected node in varargslist: %d @ %d",
1663 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001664 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001665 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001667 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668}
1669
1670static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671ast_for_decorator(struct compiling *c, const node *n)
1672{
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001673 /* decorator: '@' namedexpr_test NEWLINE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001676 REQ(CHILD(n, 0), AT);
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001677 REQ(CHILD(n, 2), NEWLINE);
1678
1679 return ast_for_expr(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680}
1681
1682static asdl_seq*
1683ast_for_decorators(struct compiling *c, const node *n)
1684{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001685 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001686 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001690 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 if (!decorator_seq)
1692 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001695 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001696 if (!d)
1697 return NULL;
1698 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 }
1700 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701}
1702
1703static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001704ast_for_funcdef_impl(struct compiling *c, const node *n0,
1705 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001707 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001708 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001709 identifier name;
1710 arguments_ty args;
1711 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001712 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001713 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001714 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001715 node *tc;
1716 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717
Guido van Rossum495da292019-03-07 12:38:08 -08001718 if (is_async && c->c_feature_version < 5) {
1719 ast_error(c, n,
1720 "Async functions are only supported in Python 3.5 and greater");
1721 return NULL;
1722 }
1723
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 REQ(n, funcdef);
1725
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 name = NEW_IDENTIFIER(CHILD(n, name_i));
1727 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001728 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001729 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001730 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1732 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001733 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001734 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1735 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1736 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001737 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001738 name_i += 2;
1739 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001740 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1741 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1742 if (!type_comment)
1743 return NULL;
1744 name_i += 1;
1745 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001746 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001748 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001749 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001751 if (NCH(CHILD(n, name_i + 3)) > 1) {
1752 /* Check if the suite has a type comment in it. */
1753 tc = CHILD(CHILD(n, name_i + 3), 1);
1754
1755 if (TYPE(tc) == TYPE_COMMENT) {
1756 if (type_comment != NULL) {
1757 ast_error(c, n, "Cannot have two type comments on def");
1758 return NULL;
1759 }
1760 type_comment = NEW_TYPE_COMMENT(tc);
1761 if (!type_comment)
1762 return NULL;
1763 }
1764 }
1765
Yury Selivanov75445082015-05-11 22:57:16 -04001766 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001767 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001768 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001769 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001770 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001771 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001772}
1773
1774static stmt_ty
1775ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1776{
Guido van Rossum495da292019-03-07 12:38:08 -08001777 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001778 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001779 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001780 REQ(CHILD(n, 1), funcdef);
1781
guoci90fc8982018-09-11 17:45:45 -04001782 return ast_for_funcdef_impl(c, n, decorator_seq,
1783 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001784}
1785
1786static stmt_ty
1787ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1788{
1789 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1790 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001791 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001792}
1793
1794
1795static stmt_ty
1796ast_for_async_stmt(struct compiling *c, const node *n)
1797{
Guido van Rossum495da292019-03-07 12:38:08 -08001798 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001799 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001800 REQ(CHILD(n, 0), ASYNC);
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{
Guido van Rossumb08d3f72019-12-15 10:00:33 -08001852 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001853 argument: ( test [comp_for] |
1854 test ':=' test |
1855 test '=' test |
1856 '**' test |
1857 '*' test )
1858 */
1859 expr_ty target, value;
1860
1861 target = ast_for_expr(c, CHILD(n, 0));
1862 if (!target)
1863 return NULL;
1864
1865 value = ast_for_expr(c, CHILD(n, 2));
1866 if (!value)
1867 return NULL;
1868
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001869 if (target->kind != Name_kind) {
1870 const char *expr_name = get_expr_name(target);
1871 if (expr_name != NULL) {
Ned Batchelder37143a82019-12-31 21:40:58 -05001872 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001873 }
1874 return NULL;
1875 }
1876
1877 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001878 return NULL;
1879
1880 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1881 n->n_end_col_offset, c->c_arena);
1882}
1883
1884static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885ast_for_lambdef(struct compiling *c, const node *n)
1886{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001887 /* lambdef: 'lambda' [varargslist] ':' test
1888 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 arguments_ty args;
1890 expr_ty expression;
1891
1892 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001893 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 if (!args)
1895 return NULL;
1896 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001897 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 }
1900 else {
1901 args = ast_for_arguments(c, CHILD(n, 1));
1902 if (!args)
1903 return NULL;
1904 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001905 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 }
1908
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001909 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1910 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911}
1912
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001913static expr_ty
1914ast_for_ifexpr(struct compiling *c, const node *n)
1915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001917 expr_ty expression, body, orelse;
1918
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001919 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001920 body = ast_for_expr(c, CHILD(n, 0));
1921 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001922 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001923 expression = ast_for_expr(c, CHILD(n, 2));
1924 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001925 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001926 orelse = ast_for_expr(c, CHILD(n, 4));
1927 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001928 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001929 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001930 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001931 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001932}
1933
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001935 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936
Nick Coghlan650f0d02007-04-15 12:05:43 +00001937 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938*/
1939
1940static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001941count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001943 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944
Guido van Rossumd8faa362007-04-27 19:54:29 +00001945 count_comp_for:
1946 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001947 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001948 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001949 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001950 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001951 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001952 else if (NCH(n) == 1) {
1953 n = CHILD(n, 0);
1954 }
1955 else {
1956 goto error;
1957 }
1958 if (NCH(n) == (5)) {
1959 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001960 }
1961 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001962 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001963 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001964 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001965 REQ(n, comp_iter);
1966 n = CHILD(n, 0);
1967 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001968 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001969 else if (TYPE(n) == comp_if) {
1970 if (NCH(n) == 3) {
1971 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001972 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001973 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001974 else
1975 return n_fors;
1976 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001977
Jelle Zijlstraac317702017-10-05 20:24:46 -07001978 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001979 /* Should never be reached */
1980 PyErr_SetString(PyExc_SystemError,
1981 "logic error in count_comp_fors");
1982 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983}
1984
Nick Coghlan650f0d02007-04-15 12:05:43 +00001985/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986
Nick Coghlan650f0d02007-04-15 12:05:43 +00001987 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988*/
1989
1990static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001991count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001993 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994
Guido van Rossumd8faa362007-04-27 19:54:29 +00001995 while (1) {
1996 REQ(n, comp_iter);
1997 if (TYPE(CHILD(n, 0)) == comp_for)
1998 return n_ifs;
1999 n = CHILD(n, 0);
2000 REQ(n, comp_if);
2001 n_ifs++;
2002 if (NCH(n) == 2)
2003 return n_ifs;
2004 n = CHILD(n, 2);
2005 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006}
2007
Guido van Rossum992d4a32007-07-11 13:09:30 +00002008static asdl_seq *
2009ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002012 asdl_seq *comps;
2013
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002014 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 if (n_fors == -1)
2016 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002017
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002018 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002019 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002023 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002025 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002026 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002027 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002028 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029
Guido van Rossum992d4a32007-07-11 13:09:30 +00002030 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031
Jelle Zijlstraac317702017-10-05 20:24:46 -07002032 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002033 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002034 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002035 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002036 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002037 else {
2038 sync_n = CHILD(n, 0);
2039 }
2040 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002041
Guido van Rossum495da292019-03-07 12:38:08 -08002042 /* Async comprehensions only allowed in Python 3.6 and greater */
2043 if (is_async && c->c_feature_version < 6) {
2044 ast_error(c, n,
2045 "Async comprehensions are only supported in Python 3.6 and greater");
2046 return NULL;
2047 }
2048
Jelle Zijlstraac317702017-10-05 20:24:46 -07002049 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002050 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002051 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002053 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002054 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002056
Thomas Wouters89f507f2006-12-13 04:49:30 +00002057 /* Check the # of children rather than the length of t, since
2058 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002059 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002060 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002061 comp = comprehension(first, expression, NULL,
2062 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002064 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2065 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2066 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002067 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002068 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002070
Jelle Zijlstraac317702017-10-05 20:24:46 -07002071 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 int j, n_ifs;
2073 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074
Jelle Zijlstraac317702017-10-05 20:24:46 -07002075 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002076 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002077 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002079
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002080 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002081 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002085 REQ(n, comp_iter);
2086 n = CHILD(n, 0);
2087 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088
Guido van Rossum992d4a32007-07-11 13:09:30 +00002089 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002090 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002091 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002092 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002093 if (NCH(n) == 3)
2094 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002096 /* on exit, must guarantee that n is a comp_for */
2097 if (TYPE(n) == comp_iter)
2098 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002099 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002101 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002103 return comps;
2104}
2105
2106static expr_ty
2107ast_for_itercomp(struct compiling *c, const node *n, int type)
2108{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002109 /* testlist_comp: (test|star_expr)
2110 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002111 expr_ty elt;
2112 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002113 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114
Guido van Rossum992d4a32007-07-11 13:09:30 +00002115 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002117 ch = CHILD(n, 0);
2118 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002119 if (!elt)
2120 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002121 if (elt->kind == Starred_kind) {
2122 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2123 return NULL;
2124 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125
Guido van Rossum992d4a32007-07-11 13:09:30 +00002126 comps = ast_for_comprehension(c, CHILD(n, 1));
2127 if (!comps)
2128 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002129
2130 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002131 return GeneratorExp(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_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002134 return ListComp(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 if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002137 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2138 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002139 else
2140 /* Should never happen */
2141 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142}
2143
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002144/* Fills in the key, value pair corresponding to the dict element. In case
2145 * of an unpacking, key is NULL. *i is advanced by the number of ast
2146 * elements. Iff successful, nonzero is returned.
2147 */
2148static int
2149ast_for_dictelement(struct compiling *c, const node *n, int *i,
2150 expr_ty *key, expr_ty *value)
2151{
2152 expr_ty expression;
2153 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2154 assert(NCH(n) - *i >= 2);
2155
2156 expression = ast_for_expr(c, CHILD(n, *i + 1));
2157 if (!expression)
2158 return 0;
2159 *key = NULL;
2160 *value = expression;
2161
2162 *i += 2;
2163 }
2164 else {
2165 assert(NCH(n) - *i >= 3);
2166
2167 expression = ast_for_expr(c, CHILD(n, *i));
2168 if (!expression)
2169 return 0;
2170 *key = expression;
2171
2172 REQ(CHILD(n, *i + 1), COLON);
2173
2174 expression = ast_for_expr(c, CHILD(n, *i + 2));
2175 if (!expression)
2176 return 0;
2177 *value = expression;
2178
2179 *i += 3;
2180 }
2181 return 1;
2182}
2183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002185ast_for_dictcomp(struct compiling *c, const node *n)
2186{
2187 expr_ty key, value;
2188 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002189 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002192 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002193 assert(key);
2194 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002196 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002197 if (!comps)
2198 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002200 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2201 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002202}
2203
2204static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205ast_for_dictdisplay(struct compiling *c, const node *n)
2206{
2207 int i;
2208 int j;
2209 int size;
2210 asdl_seq *keys, *values;
2211
2212 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2213 keys = _Py_asdl_seq_new(size, c->c_arena);
2214 if (!keys)
2215 return NULL;
2216
2217 values = _Py_asdl_seq_new(size, c->c_arena);
2218 if (!values)
2219 return NULL;
2220
2221 j = 0;
2222 for (i = 0; i < NCH(n); i++) {
2223 expr_ty key, value;
2224
2225 if (!ast_for_dictelement(c, n, &i, &key, &value))
2226 return NULL;
2227 asdl_seq_SET(keys, j, key);
2228 asdl_seq_SET(values, j, value);
2229
2230 j++;
2231 }
2232 keys->size = j;
2233 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002234 return Dict(keys, values, LINENO(n), n->n_col_offset,
2235 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002236}
2237
2238static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002239ast_for_genexp(struct compiling *c, const node *n)
2240{
2241 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002242 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002243}
2244
2245static expr_ty
2246ast_for_listcomp(struct compiling *c, const node *n)
2247{
2248 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002249 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002250}
2251
2252static expr_ty
2253ast_for_setcomp(struct compiling *c, const node *n)
2254{
2255 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002256 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002257}
2258
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002259static expr_ty
2260ast_for_setdisplay(struct compiling *c, const node *n)
2261{
2262 int i;
2263 int size;
2264 asdl_seq *elts;
2265
2266 assert(TYPE(n) == (dictorsetmaker));
2267 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2268 elts = _Py_asdl_seq_new(size, c->c_arena);
2269 if (!elts)
2270 return NULL;
2271 for (i = 0; i < NCH(n); i += 2) {
2272 expr_ty expression;
2273 expression = ast_for_expr(c, CHILD(n, i));
2274 if (!expression)
2275 return NULL;
2276 asdl_seq_SET(elts, i / 2, expression);
2277 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002278 return Set(elts, LINENO(n), n->n_col_offset,
2279 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002280}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002281
2282static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283ast_for_atom(struct compiling *c, const node *n)
2284{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002285 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2286 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002287 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 */
2289 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002292 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002293 PyObject *name;
2294 const char *s = STR(ch);
2295 size_t len = strlen(s);
2296 if (len >= 4 && len <= 5) {
2297 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002298 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002299 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002300 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002301 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002302 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002303 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002304 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002305 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002306 }
2307 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002308 if (!name)
2309 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002310 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002311 return Name(name, Load, LINENO(n), n->n_col_offset,
2312 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002313 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002315 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002316 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002317 const char *errtype = NULL;
2318 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2319 errtype = "unicode error";
2320 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2321 errtype = "value error";
2322 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002323 PyObject *type, *value, *tback, *errstr;
2324 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002325 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002326 if (errstr) {
2327 ast_error(c, n, "(%s) %U", errtype, errstr);
2328 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002329 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002330 else {
2331 PyErr_Clear();
2332 ast_error(c, n, "(%s) unknown error", errtype);
2333 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002334 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002335 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002336 Py_XDECREF(tback);
2337 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002338 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002339 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002340 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 }
2342 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002343 PyObject *pynum;
2344 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2345 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2346 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2347 ast_error(c, ch,
2348 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2349 return NULL;
2350 }
2351 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002352 if (!pynum)
2353 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002354
Victor Stinner43d81952013-07-17 00:57:58 +02002355 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2356 Py_DECREF(pynum);
2357 return NULL;
2358 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002359 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002360 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361 }
Georg Brandldde00282007-03-18 19:01:53 +00002362 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002363 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002364 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002366 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367
Thomas Wouters89f507f2006-12-13 04:49:30 +00002368 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002369 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2370 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371
Thomas Wouters89f507f2006-12-13 04:49:30 +00002372 if (TYPE(ch) == yield_expr)
2373 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002376 if (NCH(ch) == 1) {
2377 return ast_for_testlist(c, ch);
2378 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002379
Serhiy Storchakab619b092018-11-27 09:40:29 +02002380 if (TYPE(CHILD(ch, 1)) == comp_for) {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002381 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002382 }
2383 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002384 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002385 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002387 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388
Thomas Wouters89f507f2006-12-13 04:49:30 +00002389 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002390 return List(NULL, Load, LINENO(n), n->n_col_offset,
2391 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392
Nick Coghlan650f0d02007-04-15 12:05:43 +00002393 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002394 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2395 asdl_seq *elts = seq_for_testlist(c, ch);
2396 if (!elts)
2397 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002398
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002399 return List(elts, Load, LINENO(n), n->n_col_offset,
2400 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002401 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002402 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002403 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002406 /* dictorsetmaker: ( ((test ':' test | '**' test)
2407 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2408 * ((test | '*' test)
2409 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002410 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002411 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002412 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002413 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002414 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2415 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002416 }
2417 else {
2418 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2419 if (NCH(ch) == 1 ||
2420 (NCH(ch) > 1 &&
2421 TYPE(CHILD(ch, 1)) == COMMA)) {
2422 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002423 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002424 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002425 else if (NCH(ch) > 1 &&
2426 TYPE(CHILD(ch, 1)) == comp_for) {
2427 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002428 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002429 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002430 else if (NCH(ch) > 3 - is_dict &&
2431 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2432 /* It's a dictionary comprehension. */
2433 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002434 ast_error(c, n,
2435 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002436 return NULL;
2437 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002438 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002439 }
2440 else {
2441 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002442 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002443 }
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002444 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002448 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2449 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 }
2451}
2452
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002453static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454ast_for_slice(struct compiling *c, const node *n)
2455{
2456 node *ch;
2457 expr_ty lower = NULL, upper = NULL, step = NULL;
2458
2459 REQ(n, subscript);
2460
2461 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002462 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 sliceop: ':' [test]
2464 */
2465 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 if (NCH(n) == 1 && TYPE(ch) == test) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002467 return ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 }
2469
2470 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002471 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 if (!lower)
2473 return NULL;
2474 }
2475
2476 /* If there's an upper bound it's in the second or third position. */
2477 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002478 if (NCH(n) > 1) {
2479 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480
Thomas Wouters89f507f2006-12-13 04:49:30 +00002481 if (TYPE(n2) == test) {
2482 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 if (!upper)
2484 return NULL;
2485 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002488 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489
Thomas Wouters89f507f2006-12-13 04:49:30 +00002490 if (TYPE(n2) == test) {
2491 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 if (!upper)
2493 return NULL;
2494 }
2495 }
2496
2497 ch = CHILD(n, NCH(n) - 1);
2498 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002499 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002500 ch = CHILD(ch, 1);
2501 if (TYPE(ch) == test) {
2502 step = ast_for_expr(c, ch);
2503 if (!step)
2504 return NULL;
2505 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 }
2507 }
2508
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002509 return Slice(lower, upper, step, LINENO(n), n->n_col_offset,
2510 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511}
2512
2513static expr_ty
2514ast_for_binop(struct compiling *c, const node *n)
2515{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002516 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002518 BinOp(BinOp(A, op, B), op, C).
2519 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520
Guido van Rossumd8faa362007-04-27 19:54:29 +00002521 int i, nops;
2522 expr_ty expr1, expr2, result;
2523 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524
Guido van Rossumd8faa362007-04-27 19:54:29 +00002525 expr1 = ast_for_expr(c, CHILD(n, 0));
2526 if (!expr1)
2527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528
Guido van Rossumd8faa362007-04-27 19:54:29 +00002529 expr2 = ast_for_expr(c, CHILD(n, 2));
2530 if (!expr2)
2531 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532
Guido van Rossum495da292019-03-07 12:38:08 -08002533 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002534 if (!newoperator)
2535 return NULL;
2536
2537 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002538 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002539 c->c_arena);
2540 if (!result)
2541 return NULL;
2542
2543 nops = (NCH(n) - 1) / 2;
2544 for (i = 1; i < nops; i++) {
2545 expr_ty tmp_result, tmp;
2546 const node* next_oper = CHILD(n, i * 2 + 1);
2547
Guido van Rossum495da292019-03-07 12:38:08 -08002548 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002549 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 return NULL;
2551
Guido van Rossumd8faa362007-04-27 19:54:29 +00002552 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2553 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 return NULL;
2555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002557 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002558 CHILD(n, i * 2 + 2)->n_end_lineno,
2559 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002560 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002562 return NULL;
2563 result = tmp_result;
2564 }
2565 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566}
2567
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002568static expr_ty
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002569ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002572 subscriptlist: subscript (',' subscript)* [',']
2573 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2574 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002575 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002576 REQ(n, trailer);
2577 if (TYPE(CHILD(n, 0)) == LPAR) {
2578 if (NCH(n) == 2)
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002579 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002580 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002581 else
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002582 return ast_for_call(c, CHILD(n, 1), left_expr,
2583 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002584 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002585 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002586 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2587 if (!attr_id)
2588 return NULL;
2589 return Attribute(left_expr, attr_id, Load,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002590 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002591 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002592 }
2593 else {
2594 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002595 REQ(CHILD(n, 2), RSQB);
2596 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002597 if (NCH(n) == 1) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002598 expr_ty slc = ast_for_slice(c, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002599 if (!slc)
2600 return NULL;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002601 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002602 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002603 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002604 }
2605 else {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002606 int j;
2607 expr_ty slc, e;
2608 asdl_seq *elts;
2609 elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2610 if (!elts)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002611 return NULL;
2612 for (j = 0; j < NCH(n); j += 2) {
2613 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002614 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002615 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002616 asdl_seq_SET(elts, j / 2, slc);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002617 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002618 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002619 n->n_end_lineno, n->n_end_col_offset,
2620 c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002621 if (!e)
2622 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002623 return Subscript(left_expr, e,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002624 Load, LINENO(start), start->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002625 n_copy->n_end_lineno, n_copy->n_end_col_offset,
2626 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002627 }
2628 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002629}
2630
2631static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002632ast_for_factor(struct compiling *c, const node *n)
2633{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002634 expr_ty expression;
2635
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002636 expression = ast_for_expr(c, CHILD(n, 1));
2637 if (!expression)
2638 return NULL;
2639
2640 switch (TYPE(CHILD(n, 0))) {
2641 case PLUS:
2642 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002643 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002644 c->c_arena);
2645 case MINUS:
2646 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002647 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002648 c->c_arena);
2649 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002650 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2651 n->n_end_lineno, n->n_end_col_offset,
2652 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002653 }
2654 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2655 TYPE(CHILD(n, 0)));
2656 return NULL;
2657}
2658
2659static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002660ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002661{
Yury Selivanov75445082015-05-11 22:57:16 -04002662 int i, nch, start = 0;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002663 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002664
2665 REQ(n, atom_expr);
2666 nch = NCH(n);
2667
Guido van Rossum495da292019-03-07 12:38:08 -08002668 if (TYPE(CHILD(n, 0)) == AWAIT) {
2669 if (c->c_feature_version < 5) {
2670 ast_error(c, n,
2671 "Await expressions are only supported in Python 3.5 and greater");
2672 return NULL;
2673 }
Yury Selivanov75445082015-05-11 22:57:16 -04002674 start = 1;
2675 assert(nch > 1);
2676 }
2677
2678 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002679 if (!e)
2680 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002681 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002682 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002683 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002684 return Await(e, LINENO(n), n->n_col_offset,
2685 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002686 }
2687
2688 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002689 node *ch = CHILD(n, i);
2690 if (TYPE(ch) != trailer)
2691 break;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002692 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2693 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002694 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002695 }
Yury Selivanov75445082015-05-11 22:57:16 -04002696
2697 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002698 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002699 return Await(e, LINENO(n), n->n_col_offset,
2700 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002701 }
2702 else {
2703 return e;
2704 }
2705}
2706
2707static expr_ty
2708ast_for_power(struct compiling *c, const node *n)
2709{
2710 /* power: atom trailer* ('**' factor)*
2711 */
2712 expr_ty e;
2713 REQ(n, power);
2714 e = ast_for_atom_expr(c, CHILD(n, 0));
2715 if (!e)
2716 return NULL;
2717 if (NCH(n) == 1)
2718 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002719 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2720 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002721 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002722 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002723 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2724 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002725 }
2726 return e;
2727}
2728
Guido van Rossum0368b722007-05-11 16:50:42 +00002729static expr_ty
2730ast_for_starred(struct compiling *c, const node *n)
2731{
2732 expr_ty tmp;
2733 REQ(n, star_expr);
2734
2735 tmp = ast_for_expr(c, CHILD(n, 1));
2736 if (!tmp)
2737 return NULL;
2738
2739 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002740 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2741 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002742}
2743
2744
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745/* Do not name a variable 'expr'! Will cause a compile error.
2746*/
2747
2748static expr_ty
2749ast_for_expr(struct compiling *c, const node *n)
2750{
2751 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002752 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002753 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002754 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 and_test: not_test ('and' not_test)*
2757 not_test: 'not' not_test | comparison
2758 comparison: expr (comp_op expr)*
2759 expr: xor_expr ('|' xor_expr)*
2760 xor_expr: and_expr ('^' and_expr)*
2761 and_expr: shift_expr ('&' shift_expr)*
2762 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2763 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002764 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002766 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002767 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002768 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 */
2770
2771 asdl_seq *seq;
2772 int i;
2773
2774 loop:
2775 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002776 case namedexpr_test:
2777 if (NCH(n) == 3)
2778 return ast_for_namedexpr(c, n);
2779 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002781 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002782 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002783 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002785 else if (NCH(n) > 1)
2786 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002787 /* Fallthrough */
2788 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 case and_test:
2790 if (NCH(n) == 1) {
2791 n = CHILD(n, 0);
2792 goto loop;
2793 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002794 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 if (!seq)
2796 return NULL;
2797 for (i = 0; i < NCH(n); i += 2) {
2798 expr_ty e = ast_for_expr(c, CHILD(n, i));
2799 if (!e)
2800 return NULL;
2801 asdl_seq_SET(seq, i / 2, e);
2802 }
2803 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002804 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002805 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002806 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002807 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002808 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2809 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 case not_test:
2811 if (NCH(n) == 1) {
2812 n = CHILD(n, 0);
2813 goto loop;
2814 }
2815 else {
2816 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2817 if (!expression)
2818 return NULL;
2819
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002820 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002821 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002822 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 }
2824 case comparison:
2825 if (NCH(n) == 1) {
2826 n = CHILD(n, 0);
2827 goto loop;
2828 }
2829 else {
2830 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002831 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002832 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002833 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 if (!ops)
2835 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002836 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838 return NULL;
2839 }
2840 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002841 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002843 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002844 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847
2848 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002849 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002851 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002853 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 asdl_seq_SET(cmps, i / 2, expression);
2855 }
2856 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002857 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002861 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2862 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864
Guido van Rossum0368b722007-05-11 16:50:42 +00002865 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 /* The next five cases all handle BinOps. The main body of code
2868 is the same in each case, but the switch turned inside out to
2869 reuse the code for each type of operator.
2870 */
2871 case expr:
2872 case xor_expr:
2873 case and_expr:
2874 case shift_expr:
2875 case arith_expr:
2876 case term:
2877 if (NCH(n) == 1) {
2878 n = CHILD(n, 0);
2879 goto loop;
2880 }
2881 return ast_for_binop(c, n);
2882 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002883 node *an = NULL;
2884 node *en = NULL;
2885 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002887 if (NCH(n) > 1)
2888 an = CHILD(n, 1); /* yield_arg */
2889 if (an) {
2890 en = CHILD(an, NCH(an) - 1);
2891 if (NCH(an) == 2) {
2892 is_from = 1;
2893 exp = ast_for_expr(c, en);
2894 }
2895 else
2896 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002897 if (!exp)
2898 return NULL;
2899 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002900 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002901 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2902 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2903 return Yield(exp, LINENO(n), n->n_col_offset,
2904 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002905 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002906 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 if (NCH(n) == 1) {
2908 n = CHILD(n, 0);
2909 goto loop;
2910 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002911 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002912 case power:
2913 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002915 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 return NULL;
2917 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002918 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 return NULL;
2920}
2921
2922static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002923ast_for_call(struct compiling *c, const node *n, expr_ty func,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002924 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925{
2926 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002927 arglist: argument (',' argument)* [',']
2928 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929 */
2930
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002931 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002932 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002933 asdl_seq *args;
2934 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935
2936 REQ(n, arglist);
2937
2938 nargs = 0;
2939 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002941 node *ch = CHILD(n, i);
2942 if (TYPE(ch) == argument) {
2943 if (NCH(ch) == 1)
2944 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002945 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2946 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002947 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002948 ast_error(c, ch, "invalid syntax");
2949 return NULL;
2950 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002951 if (NCH(n) > 1) {
2952 ast_error(c, ch, "Generator expression must be parenthesized");
2953 return NULL;
2954 }
2955 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002956 else if (TYPE(CHILD(ch, 0)) == STAR)
2957 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002958 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2959 nargs++;
2960 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002962 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002963 nkeywords++;
2964 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002967 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002969 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002970 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002972 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002973
2974 nargs = 0; /* positional arguments + iterable argument unpackings */
2975 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2976 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002978 node *ch = CHILD(n, i);
2979 if (TYPE(ch) == argument) {
2980 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002981 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002982 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002983 /* a positional argument */
2984 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002985 if (ndoublestars) {
2986 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002987 "positional argument follows "
2988 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002989 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002990 else {
2991 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002992 "positional argument follows "
2993 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002994 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002995 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002996 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002997 e = ast_for_expr(c, chch);
2998 if (!e)
2999 return NULL;
3000 asdl_seq_SET(args, nargs++, e);
3001 }
3002 else if (TYPE(chch) == STAR) {
3003 /* an iterable argument unpacking */
3004 expr_ty starred;
3005 if (ndoublestars) {
3006 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003007 "iterable argument unpacking follows "
3008 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003009 return NULL;
3010 }
3011 e = ast_for_expr(c, CHILD(ch, 1));
3012 if (!e)
3013 return NULL;
3014 starred = Starred(e, Load, LINENO(chch),
3015 chch->n_col_offset,
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +01003016 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003017 c->c_arena);
3018 if (!starred)
3019 return NULL;
3020 asdl_seq_SET(args, nargs++, starred);
3021
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003022 }
3023 else if (TYPE(chch) == DOUBLESTAR) {
3024 /* a keyword argument unpacking */
3025 keyword_ty kw;
3026 i++;
3027 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003029 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003030 kw = keyword(NULL, e, c->c_arena);
3031 asdl_seq_SET(keywords, nkeywords++, kw);
3032 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003034 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003035 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003036 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003038 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003039 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003041 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3042 /* treat colon equal as positional argument */
3043 if (nkeywords) {
3044 if (ndoublestars) {
3045 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003046 "positional argument follows "
3047 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003048 }
3049 else {
3050 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003051 "positional argument follows "
3052 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003053 }
3054 return NULL;
3055 }
3056 e = ast_for_namedexpr(c, ch);
3057 if (!e)
3058 return NULL;
3059 asdl_seq_SET(args, nargs++, e);
3060 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003061 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003062 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003063 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003064 identifier key, tmp;
3065 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003067 // To remain LL(1), the grammar accepts any test (basically, any
3068 // expression) in the keyword slot of a call site. So, we need
3069 // to manually enforce that the keyword is a NAME here.
3070 static const int name_tree[] = {
3071 test,
3072 or_test,
3073 and_test,
3074 not_test,
3075 comparison,
3076 expr,
3077 xor_expr,
3078 and_expr,
3079 shift_expr,
3080 arith_expr,
3081 term,
3082 factor,
3083 power,
3084 atom_expr,
3085 atom,
3086 0,
3087 };
3088 node *expr_node = chch;
3089 for (int i = 0; name_tree[i]; i++) {
3090 if (TYPE(expr_node) != name_tree[i])
3091 break;
3092 if (NCH(expr_node) != 1)
3093 break;
3094 expr_node = CHILD(expr_node, 0);
3095 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003096 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003097 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003098 "expression cannot contain assignment, "
3099 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003100 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003101 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003102 key = new_identifier(STR(expr_node), c);
3103 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003104 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003106 if (forbidden_name(c, key, chch, 1)) {
3107 return NULL;
3108 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003109 for (k = 0; k < nkeywords; k++) {
3110 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003111 if (tmp && !PyUnicode_Compare(tmp, key)) {
3112 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003113 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003114 return NULL;
3115 }
3116 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003117 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003119 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003120 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003122 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003123 asdl_seq_SET(keywords, nkeywords++, kw);
3124 }
3125 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 }
3127
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003128 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003129 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130}
3131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003133ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003135 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003136 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003138 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003139 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003140 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003141 }
3142 else {
3143 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003144 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003145 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003147 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 else {
3149 asdl_seq *tmp = seq_for_testlist(c, n);
3150 if (!tmp)
3151 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003152 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3153 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003155}
3156
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157static stmt_ty
3158ast_for_expr_stmt(struct compiling *c, const node *n)
3159{
3160 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003161 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003162 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3163 annassign: ':' test ['=' (yield_expr|testlist)]
3164 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3165 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3166 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003167 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003169 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003171 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003172 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173 if (!e)
3174 return NULL;
3175
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003176 return Expr(e, LINENO(n), n->n_col_offset,
3177 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178 }
3179 else if (TYPE(CHILD(n, 1)) == augassign) {
3180 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003181 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003182 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183
Thomas Wouters89f507f2006-12-13 04:49:30 +00003184 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 if (!expr1)
3186 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003187 if(!set_context(c, expr1, Store, ch))
3188 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003189 /* set_context checks that most expressions are not the left side.
3190 Augmented assignments can only have a name, a subscript, or an
3191 attribute on the left, though, so we have to explicitly check for
3192 those. */
3193 switch (expr1->kind) {
3194 case Name_kind:
3195 case Attribute_kind:
3196 case Subscript_kind:
3197 break;
3198 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003199 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003200 return NULL;
3201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202
Thomas Wouters89f507f2006-12-13 04:49:30 +00003203 ch = CHILD(n, 2);
3204 if (TYPE(ch) == testlist)
3205 expr2 = ast_for_testlist(c, ch);
3206 else
3207 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003208 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 return NULL;
3210
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003211 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003212 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213 return NULL;
3214
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003215 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3216 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003218 else if (TYPE(CHILD(n, 1)) == annassign) {
3219 expr_ty expr1, expr2, expr3;
3220 node *ch = CHILD(n, 0);
3221 node *deep, *ann = CHILD(n, 1);
3222 int simple = 1;
3223
Guido van Rossum495da292019-03-07 12:38:08 -08003224 /* AnnAssigns are only allowed in Python 3.6 or greater */
3225 if (c->c_feature_version < 6) {
3226 ast_error(c, ch,
3227 "Variable annotation syntax is only supported in Python 3.6 and greater");
3228 return NULL;
3229 }
3230
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003231 /* we keep track of parens to qualify (x) as expression not name */
3232 deep = ch;
3233 while (NCH(deep) == 1) {
3234 deep = CHILD(deep, 0);
3235 }
3236 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3237 simple = 0;
3238 }
3239 expr1 = ast_for_testlist(c, ch);
3240 if (!expr1) {
3241 return NULL;
3242 }
3243 switch (expr1->kind) {
3244 case Name_kind:
3245 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3246 return NULL;
3247 }
3248 expr1->v.Name.ctx = Store;
3249 break;
3250 case Attribute_kind:
3251 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3252 return NULL;
3253 }
3254 expr1->v.Attribute.ctx = Store;
3255 break;
3256 case Subscript_kind:
3257 expr1->v.Subscript.ctx = Store;
3258 break;
3259 case List_kind:
3260 ast_error(c, ch,
3261 "only single target (not list) can be annotated");
3262 return NULL;
3263 case Tuple_kind:
3264 ast_error(c, ch,
3265 "only single target (not tuple) can be annotated");
3266 return NULL;
3267 default:
3268 ast_error(c, ch,
3269 "illegal target for annotation");
3270 return NULL;
3271 }
3272
3273 if (expr1->kind != Name_kind) {
3274 simple = 0;
3275 }
3276 ch = CHILD(ann, 1);
3277 expr2 = ast_for_expr(c, ch);
3278 if (!expr2) {
3279 return NULL;
3280 }
3281 if (NCH(ann) == 2) {
3282 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003283 LINENO(n), n->n_col_offset,
3284 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003285 }
3286 else {
3287 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003288 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003289 expr3 = ast_for_testlist(c, ch);
3290 }
3291 else {
3292 expr3 = ast_for_expr(c, ch);
3293 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003294 if (!expr3) {
3295 return NULL;
3296 }
3297 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003298 LINENO(n), n->n_col_offset,
3299 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003300 }
3301 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003303 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003304 asdl_seq *targets;
3305 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003307 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308
Thomas Wouters89f507f2006-12-13 04:49:30 +00003309 /* a normal assignment */
3310 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003311
3312 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3313 nch_minus_type = num - has_type_comment;
3314
3315 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 if (!targets)
3317 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003318 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003319 expr_ty e;
3320 node *ch = CHILD(n, i);
3321 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003322 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 return NULL;
3324 }
3325 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003327 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003329 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003330 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003331 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 asdl_seq_SET(targets, i / 2, e);
3334 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003335 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003336 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003337 expression = ast_for_testlist(c, value);
3338 else
3339 expression = ast_for_expr(c, value);
3340 if (!expression)
3341 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003342 if (has_type_comment) {
3343 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3344 if (!type_comment)
3345 return NULL;
3346 }
3347 else
3348 type_comment = NULL;
3349 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003350 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352}
3353
Benjamin Peterson78565b22009-06-28 19:19:51 +00003354
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003356ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357{
3358 asdl_seq *seq;
3359 int i;
3360 expr_ty e;
3361
3362 REQ(n, exprlist);
3363
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003364 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003366 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003368 e = ast_for_expr(c, CHILD(n, i));
3369 if (!e)
3370 return NULL;
3371 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003372 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374 }
3375 return seq;
3376}
3377
3378static stmt_ty
3379ast_for_del_stmt(struct compiling *c, const node *n)
3380{
3381 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 /* del_stmt: 'del' exprlist */
3384 REQ(n, del_stmt);
3385
3386 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3387 if (!expr_list)
3388 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003389 return Delete(expr_list, LINENO(n), n->n_col_offset,
3390 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391}
3392
3393static stmt_ty
3394ast_for_flow_stmt(struct compiling *c, const node *n)
3395{
3396 /*
3397 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3398 | yield_stmt
3399 break_stmt: 'break'
3400 continue_stmt: 'continue'
3401 return_stmt: 'return' [testlist]
3402 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003403 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404 raise_stmt: 'raise' [test [',' test [',' test]]]
3405 */
3406 node *ch;
3407
3408 REQ(n, flow_stmt);
3409 ch = CHILD(n, 0);
3410 switch (TYPE(ch)) {
3411 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003412 return Break(LINENO(n), n->n_col_offset,
3413 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003415 return Continue(LINENO(n), n->n_col_offset,
3416 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003418 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3419 if (!exp)
3420 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003421 return Expr(exp, LINENO(n), n->n_col_offset,
3422 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423 }
3424 case return_stmt:
3425 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003426 return Return(NULL, 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 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003429 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 if (!expression)
3431 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003432 return Return(expression, LINENO(n), n->n_col_offset,
3433 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434 }
3435 case raise_stmt:
3436 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003437 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3438 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003439 else if (NCH(ch) >= 2) {
3440 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3442 if (!expression)
3443 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003444 if (NCH(ch) == 4) {
3445 cause = ast_for_expr(c, CHILD(ch, 3));
3446 if (!cause)
3447 return NULL;
3448 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003449 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3450 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451 }
Stefan Krahf432a322017-08-21 13:09:59 +02003452 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003454 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 "unexpected flow_stmt: %d", TYPE(ch));
3456 return NULL;
3457 }
3458}
3459
3460static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003461alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462{
3463 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003464 import_as_name: NAME ['as' NAME]
3465 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 dotted_name: NAME ('.' NAME)*
3467 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003468 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003469
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 loop:
3471 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003472 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003473 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003474 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003475 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003476 if (!name)
3477 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003478 if (NCH(n) == 3) {
3479 node *str_node = CHILD(n, 2);
3480 str = NEW_IDENTIFIER(str_node);
3481 if (!str)
3482 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003483 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003484 return NULL;
3485 }
3486 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003487 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003488 return NULL;
3489 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003490 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492 case dotted_as_name:
3493 if (NCH(n) == 1) {
3494 n = CHILD(n, 0);
3495 goto loop;
3496 }
3497 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003498 node *asname_node = CHILD(n, 2);
3499 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003500 if (!a)
3501 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003503 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003504 if (!a->asname)
3505 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003506 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 return a;
3509 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003511 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003512 node *name_node = CHILD(n, 0);
3513 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003514 if (!name)
3515 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003516 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003517 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003518 return alias(name, NULL, c->c_arena);
3519 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003520 else {
3521 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003522 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003523 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526
3527 len = 0;
3528 for (i = 0; i < NCH(n); i += 2)
3529 /* length of string plus one for the dot */
3530 len += strlen(STR(CHILD(n, i))) + 1;
3531 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003532 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533 if (!str)
3534 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003535 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003536 if (!s)
3537 return NULL;
3538 for (i = 0; i < NCH(n); i += 2) {
3539 char *sch = STR(CHILD(n, i));
3540 strcpy(s, STR(CHILD(n, i)));
3541 s += strlen(sch);
3542 *s++ = '.';
3543 }
3544 --s;
3545 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3547 PyBytes_GET_SIZE(str),
3548 NULL);
3549 Py_DECREF(str);
3550 if (!uni)
3551 return NULL;
3552 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003553 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003554 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3555 Py_DECREF(str);
3556 return NULL;
3557 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003558 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003561 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003562 if (!str)
3563 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003564 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3565 Py_DECREF(str);
3566 return NULL;
3567 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003568 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003570 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571 "unexpected import name: %d", TYPE(n));
3572 return NULL;
3573 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574}
3575
3576static stmt_ty
3577ast_for_import_stmt(struct compiling *c, const node *n)
3578{
3579 /*
3580 import_stmt: import_name | import_from
3581 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003582 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3583 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003585 int lineno;
3586 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587 int i;
3588 asdl_seq *aliases;
3589
3590 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003591 lineno = LINENO(n);
3592 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003594 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003596 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003597 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003598 if (!aliases)
3599 return NULL;
3600 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003601 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003602 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003604 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003606 // Even though n is modified above, the end position is not changed
3607 return Import(aliases, lineno, col_offset,
3608 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003610 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003612 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003613 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003614 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003615 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003617 /* Count the number of dots (for relative imports) and check for the
3618 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003619 for (idx = 1; idx < NCH(n); idx++) {
3620 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003621 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3622 if (!mod)
3623 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003624 idx++;
3625 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003626 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003628 ndots += 3;
3629 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003630 } else if (TYPE(CHILD(n, idx)) != DOT) {
3631 break;
3632 }
3633 ndots++;
3634 }
3635 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003636 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003637 case STAR:
3638 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003639 n = CHILD(n, idx);
3640 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003641 break;
3642 case LPAR:
3643 /* from ... import (x, y, z) */
3644 n = CHILD(n, idx + 1);
3645 n_children = NCH(n);
3646 break;
3647 case import_as_names:
3648 /* from ... import x, y, z */
3649 n = CHILD(n, idx);
3650 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003651 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003652 ast_error(c, n,
3653 "trailing comma not allowed without"
3654 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 return NULL;
3656 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003657 break;
3658 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003659 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003660 return NULL;
3661 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003663 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003664 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666
3667 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003668 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003669 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003670 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003672 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003674 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003675 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003676 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003677 if (!import_alias)
3678 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003679 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003680 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003682 if (mod != NULL)
3683 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003684 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003685 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003686 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 }
Neal Norwitz79792652005-11-14 04:25:03 +00003688 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689 "unknown import statement: starts with command '%s'",
3690 STR(CHILD(n, 0)));
3691 return NULL;
3692}
3693
3694static stmt_ty
3695ast_for_global_stmt(struct compiling *c, const node *n)
3696{
3697 /* global_stmt: 'global' NAME (',' NAME)* */
3698 identifier name;
3699 asdl_seq *s;
3700 int i;
3701
3702 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003703 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003705 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003707 name = NEW_IDENTIFIER(CHILD(n, i));
3708 if (!name)
3709 return NULL;
3710 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003712 return Global(s, LINENO(n), n->n_col_offset,
3713 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714}
3715
3716static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003717ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3718{
3719 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3720 identifier name;
3721 asdl_seq *s;
3722 int i;
3723
3724 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003725 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003726 if (!s)
3727 return NULL;
3728 for (i = 1; i < NCH(n); i += 2) {
3729 name = NEW_IDENTIFIER(CHILD(n, i));
3730 if (!name)
3731 return NULL;
3732 asdl_seq_SET(s, i / 2, name);
3733 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003734 return Nonlocal(s, LINENO(n), n->n_col_offset,
3735 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003736}
3737
3738static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739ast_for_assert_stmt(struct compiling *c, const node *n)
3740{
3741 /* assert_stmt: 'assert' test [',' test] */
3742 REQ(n, assert_stmt);
3743 if (NCH(n) == 2) {
3744 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3745 if (!expression)
3746 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003747 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3748 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 }
3750 else if (NCH(n) == 4) {
3751 expr_ty expr1, expr2;
3752
3753 expr1 = ast_for_expr(c, CHILD(n, 1));
3754 if (!expr1)
3755 return NULL;
3756 expr2 = ast_for_expr(c, CHILD(n, 3));
3757 if (!expr2)
3758 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003760 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3761 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 }
Neal Norwitz79792652005-11-14 04:25:03 +00003763 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764 "improper number of parts to 'assert' statement: %d",
3765 NCH(n));
3766 return NULL;
3767}
3768
3769static asdl_seq *
3770ast_for_suite(struct compiling *c, const node *n)
3771{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003772 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003773 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 stmt_ty s;
3775 int i, total, num, end, pos = 0;
3776 node *ch;
3777
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003778 if (TYPE(n) != func_body_suite) {
3779 REQ(n, suite);
3780 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781
3782 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003783 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003785 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003787 n = CHILD(n, 0);
3788 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003790 */
3791 end = NCH(n) - 1;
3792 if (TYPE(CHILD(n, end - 1)) == SEMI)
3793 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003795 for (i = 0; i < end; i += 2) {
3796 ch = CHILD(n, i);
3797 s = ast_for_stmt(c, ch);
3798 if (!s)
3799 return NULL;
3800 asdl_seq_SET(seq, pos++, s);
3801 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 }
3803 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003804 i = 2;
3805 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3806 i += 2;
3807 REQ(CHILD(n, 2), NEWLINE);
3808 }
3809
3810 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003811 ch = CHILD(n, i);
3812 REQ(ch, stmt);
3813 num = num_stmts(ch);
3814 if (num == 1) {
3815 /* small_stmt or compound_stmt with only one child */
3816 s = ast_for_stmt(c, ch);
3817 if (!s)
3818 return NULL;
3819 asdl_seq_SET(seq, pos++, s);
3820 }
3821 else {
3822 int j;
3823 ch = CHILD(ch, 0);
3824 REQ(ch, simple_stmt);
3825 for (j = 0; j < NCH(ch); j += 2) {
3826 /* statement terminates with a semi-colon ';' */
3827 if (NCH(CHILD(ch, j)) == 0) {
3828 assert((j + 1) == NCH(ch));
3829 break;
3830 }
3831 s = ast_for_stmt(c, CHILD(ch, j));
3832 if (!s)
3833 return NULL;
3834 asdl_seq_SET(seq, pos++, s);
3835 }
3836 }
3837 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838 }
3839 assert(pos == seq->size);
3840 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841}
3842
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003843static void
3844get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3845{
Pablo Galindo46a97922019-02-19 22:51:53 +00003846 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003847 // There must be no empty suites.
3848 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003849 stmt_ty last = asdl_seq_GET(s, tot - 1);
3850 *end_lineno = last->end_lineno;
3851 *end_col_offset = last->end_col_offset;
3852}
3853
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854static stmt_ty
3855ast_for_if_stmt(struct compiling *c, const node *n)
3856{
3857 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3858 ['else' ':' suite]
3859 */
3860 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003861 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862
3863 REQ(n, if_stmt);
3864
3865 if (NCH(n) == 4) {
3866 expr_ty expression;
3867 asdl_seq *suite_seq;
3868
3869 expression = ast_for_expr(c, CHILD(n, 1));
3870 if (!expression)
3871 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003873 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003875 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876
Guido van Rossumd8faa362007-04-27 19:54:29 +00003877 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003878 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003880
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 s = STR(CHILD(n, 4));
3882 /* s[2], the third character in the string, will be
3883 's' for el_s_e, or
3884 'i' for el_i_f
3885 */
3886 if (s[2] == 's') {
3887 expr_ty expression;
3888 asdl_seq *seq1, *seq2;
3889
3890 expression = ast_for_expr(c, CHILD(n, 1));
3891 if (!expression)
3892 return NULL;
3893 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003894 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895 return NULL;
3896 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003897 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003899 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900
Guido van Rossumd8faa362007-04-27 19:54:29 +00003901 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003902 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003903 }
3904 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003905 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003906 expr_ty expression;
3907 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003908 asdl_seq *orelse = NULL;
3909 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 /* must reference the child n_elif+1 since 'else' token is third,
3911 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003912 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3913 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3914 has_else = 1;
3915 n_elif -= 3;
3916 }
3917 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918
Thomas Wouters89f507f2006-12-13 04:49:30 +00003919 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003920 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003922 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003923 if (!orelse)
3924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003926 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003928 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3929 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003931 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3932 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003934 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 asdl_seq_SET(orelse, 0,
3937 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003938 LINENO(CHILD(n, NCH(n) - 7)),
3939 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003940 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003941 /* the just-created orelse handled the last elif */
3942 n_elif--;
3943 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944
Thomas Wouters89f507f2006-12-13 04:49:30 +00003945 for (i = 0; i < n_elif; i++) {
3946 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003947 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003948 if (!newobj)
3949 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003951 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003954 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003957 if (orelse != NULL) {
3958 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3959 } else {
3960 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3961 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003962 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003964 LINENO(CHILD(n, off - 1)),
3965 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003966 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003967 orelse = newobj;
3968 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003969 expression = ast_for_expr(c, CHILD(n, 1));
3970 if (!expression)
3971 return NULL;
3972 suite_seq = ast_for_suite(c, CHILD(n, 3));
3973 if (!suite_seq)
3974 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003975 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003976 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003977 LINENO(n), n->n_col_offset,
3978 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003980
3981 PyErr_Format(PyExc_SystemError,
3982 "unexpected token in 'if' statement: %s", s);
3983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984}
3985
3986static stmt_ty
3987ast_for_while_stmt(struct compiling *c, const node *n)
3988{
3989 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3990 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003991 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992
3993 if (NCH(n) == 4) {
3994 expr_ty expression;
3995 asdl_seq *suite_seq;
3996
3997 expression = ast_for_expr(c, CHILD(n, 1));
3998 if (!expression)
3999 return NULL;
4000 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004001 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004003 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4004 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4005 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 }
4007 else if (NCH(n) == 7) {
4008 expr_ty expression;
4009 asdl_seq *seq1, *seq2;
4010
4011 expression = ast_for_expr(c, CHILD(n, 1));
4012 if (!expression)
4013 return NULL;
4014 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004015 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 return NULL;
4017 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004018 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004020 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004021
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004022 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4023 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004024 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004025
4026 PyErr_Format(PyExc_SystemError,
4027 "wrong number of tokens for 'while' statement: %d",
4028 NCH(n));
4029 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030}
4031
4032static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004033ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034{
guoci90fc8982018-09-11 17:45:45 -04004035 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004036 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004038 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004039 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004040 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004041 int has_type_comment;
4042 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004043
4044 if (is_async && c->c_feature_version < 5) {
4045 ast_error(c, n,
4046 "Async for loops are only supported in Python 3.5 and greater");
4047 return NULL;
4048 }
4049
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004050 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051 REQ(n, for_stmt);
4052
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004053 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4054
4055 if (NCH(n) == 9 + has_type_comment) {
4056 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057 if (!seq)
4058 return NULL;
4059 }
4060
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004061 node_target = CHILD(n, 1);
4062 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004063 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004065 /* Check the # of children rather than the length of _target, since
4066 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004067 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004068 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004069 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004071 target = Tuple(_target, Store, first->lineno, first->col_offset,
4072 node_target->n_end_lineno, node_target->n_end_col_offset,
4073 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004075 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004076 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004077 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004078 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004079 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080 return NULL;
4081
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004082 if (seq != NULL) {
4083 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4084 } else {
4085 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4086 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004087
4088 if (has_type_comment) {
4089 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4090 if (!type_comment)
4091 return NULL;
4092 }
4093 else
4094 type_comment = NULL;
4095
Yury Selivanov75445082015-05-11 22:57:16 -04004096 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004097 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004098 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004099 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004100 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004101 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004102 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004103 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104}
4105
4106static excepthandler_ty
4107ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4108{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004109 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004110 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004111 REQ(exc, except_clause);
4112 REQ(body, suite);
4113
4114 if (NCH(exc) == 1) {
4115 asdl_seq *suite_seq = ast_for_suite(c, body);
4116 if (!suite_seq)
4117 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004118 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004119
Neal Norwitzad74aa82008-03-31 05:14:30 +00004120 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004121 exc->n_col_offset,
4122 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004123 }
4124 else if (NCH(exc) == 2) {
4125 expr_ty expression;
4126 asdl_seq *suite_seq;
4127
4128 expression = ast_for_expr(c, CHILD(exc, 1));
4129 if (!expression)
4130 return NULL;
4131 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004132 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004134 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004135
Neal Norwitzad74aa82008-03-31 05:14:30 +00004136 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004137 exc->n_col_offset,
4138 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 }
4140 else if (NCH(exc) == 4) {
4141 asdl_seq *suite_seq;
4142 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004143 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004144 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004146 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004147 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004149 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004150 return NULL;
4151 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004152 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004154 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155
Neal Norwitzad74aa82008-03-31 05:14:30 +00004156 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004157 exc->n_col_offset,
4158 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004160
4161 PyErr_Format(PyExc_SystemError,
4162 "wrong number of children for 'except' clause: %d",
4163 NCH(exc));
4164 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004165}
4166
4167static stmt_ty
4168ast_for_try_stmt(struct compiling *c, const node *n)
4169{
Neal Norwitzf599f422005-12-17 21:33:47 +00004170 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004171 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004172 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004173 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004174
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175 REQ(n, try_stmt);
4176
Neal Norwitzf599f422005-12-17 21:33:47 +00004177 body = ast_for_suite(c, CHILD(n, 2));
4178 if (body == NULL)
4179 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004180
Neal Norwitzf599f422005-12-17 21:33:47 +00004181 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4182 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4183 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4184 /* we can assume it's an "else",
4185 because nch >= 9 for try-else-finally and
4186 it would otherwise have a type of except_clause */
4187 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4188 if (orelse == NULL)
4189 return NULL;
4190 n_except--;
4191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192
Neal Norwitzf599f422005-12-17 21:33:47 +00004193 finally = ast_for_suite(c, CHILD(n, nch - 1));
4194 if (finally == NULL)
4195 return NULL;
4196 n_except--;
4197 }
4198 else {
4199 /* we can assume it's an "else",
4200 otherwise it would have a type of except_clause */
4201 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4202 if (orelse == NULL)
4203 return NULL;
4204 n_except--;
4205 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004206 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004207 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004208 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004209 return NULL;
4210 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211
Neal Norwitzf599f422005-12-17 21:33:47 +00004212 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004213 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004214 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004215 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004216 if (handlers == NULL)
4217 return NULL;
4218
4219 for (i = 0; i < n_except; i++) {
4220 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4221 CHILD(n, 5 + i * 3));
4222 if (!e)
4223 return NULL;
4224 asdl_seq_SET(handlers, i, e);
4225 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004226 }
4227
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004228 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004229 if (finally != NULL) {
4230 // finally is always last
4231 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4232 } else if (orelse != NULL) {
4233 // otherwise else is last
4234 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4235 } else {
4236 // inline the get_last_end_pos logic due to layout mismatch
4237 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4238 end_lineno = last_handler->end_lineno;
4239 end_col_offset = last_handler->end_col_offset;
4240 }
4241 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4242 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004243}
4244
Georg Brandl0c315622009-05-25 21:10:36 +00004245/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004246static withitem_ty
4247ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004248{
4249 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004250
Georg Brandl0c315622009-05-25 21:10:36 +00004251 REQ(n, with_item);
4252 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004253 if (!context_expr)
4254 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004255 if (NCH(n) == 3) {
4256 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004257
4258 if (!optional_vars) {
4259 return NULL;
4260 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004261 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004262 return NULL;
4263 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004264 }
4265
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004266 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004267}
4268
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004269/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004270static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004271ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004272{
guoci90fc8982018-09-11 17:45:45 -04004273 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004274 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004275 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004276 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004277
Guido van Rossum495da292019-03-07 12:38:08 -08004278 if (is_async && c->c_feature_version < 5) {
4279 ast_error(c, n,
4280 "Async with statements are only supported in Python 3.5 and greater");
4281 return NULL;
4282 }
4283
Georg Brandl0c315622009-05-25 21:10:36 +00004284 REQ(n, with_stmt);
4285
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004286 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4287 nch_minus_type = NCH(n) - has_type_comment;
4288
4289 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004290 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004291 if (!items)
4292 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004293 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004294 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4295 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004296 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004297 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004298 }
4299
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004300 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4301 if (!body)
4302 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004303 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004304
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004305 if (has_type_comment) {
4306 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4307 if (!type_comment)
4308 return NULL;
4309 }
4310 else
4311 type_comment = NULL;
4312
Yury Selivanov75445082015-05-11 22:57:16 -04004313 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004314 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004315 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004316 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004317 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004318 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004319}
4320
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004321static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004322ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004323{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004324 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004325 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004326 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004327 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004328 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004329
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004330 REQ(n, classdef);
4331
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004332 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004333 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004334 if (!s)
4335 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004336 get_last_end_pos(s, &end_lineno, &end_col_offset);
4337
Benjamin Peterson30760062008-11-25 04:02:28 +00004338 classname = NEW_IDENTIFIER(CHILD(n, 1));
4339 if (!classname)
4340 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004341 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004342 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004343 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004344 LINENO(n), n->n_col_offset,
4345 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004346 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004347
4348 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004349 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004350 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004351 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004352 get_last_end_pos(s, &end_lineno, &end_col_offset);
4353
Benjamin Peterson30760062008-11-25 04:02:28 +00004354 classname = NEW_IDENTIFIER(CHILD(n, 1));
4355 if (!classname)
4356 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004357 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004358 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004359 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004360 LINENO(n), n->n_col_offset,
4361 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004362 }
4363
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004364 /* class NAME '(' arglist ')' ':' suite */
4365 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004366 {
4367 PyObject *dummy_name;
4368 expr_ty dummy;
4369 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4370 if (!dummy_name)
4371 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004372 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4373 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4374 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004375 call = ast_for_call(c, CHILD(n, 3), dummy,
4376 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004377 if (!call)
4378 return NULL;
4379 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004380 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004381 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004382 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004383 get_last_end_pos(s, &end_lineno, &end_col_offset);
4384
Benjamin Peterson30760062008-11-25 04:02:28 +00004385 classname = NEW_IDENTIFIER(CHILD(n, 1));
4386 if (!classname)
4387 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004388 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004389 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004390
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004391 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004392 decorator_seq, LINENO(n), n->n_col_offset,
4393 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004394}
4395
4396static stmt_ty
4397ast_for_stmt(struct compiling *c, const node *n)
4398{
4399 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004400 assert(NCH(n) == 1);
4401 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004402 }
4403 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004404 assert(num_stmts(n) == 1);
4405 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004406 }
4407 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004408 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004409 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4410 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004411 */
4412 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004413 case expr_stmt:
4414 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004415 case del_stmt:
4416 return ast_for_del_stmt(c, n);
4417 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004418 return Pass(LINENO(n), n->n_col_offset,
4419 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004420 case flow_stmt:
4421 return ast_for_flow_stmt(c, n);
4422 case import_stmt:
4423 return ast_for_import_stmt(c, n);
4424 case global_stmt:
4425 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004426 case nonlocal_stmt:
4427 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004428 case assert_stmt:
4429 return ast_for_assert_stmt(c, n);
4430 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004431 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004432 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4433 TYPE(n), NCH(n));
4434 return NULL;
4435 }
4436 }
4437 else {
4438 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004439 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004440 */
4441 node *ch = CHILD(n, 0);
4442 REQ(n, compound_stmt);
4443 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004444 case if_stmt:
4445 return ast_for_if_stmt(c, ch);
4446 case while_stmt:
4447 return ast_for_while_stmt(c, ch);
4448 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004449 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004450 case try_stmt:
4451 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004452 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004453 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004455 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004456 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004457 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 case decorated:
4459 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004460 case async_stmt:
4461 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004463 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004464 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004465 TYPE(n), NCH(n));
4466 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004467 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004468 }
4469}
4470
4471static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004472parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004473{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004474 const char *end;
4475 long x;
4476 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004477 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004478 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004479
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004480 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004481 errno = 0;
4482 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004483 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004484 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004485 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004486 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004487 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004488 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004489 }
4490 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004491 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004492 if (*end == '\0') {
4493 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004494 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004495 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004496 }
4497 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004498 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004499 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004500 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4501 if (compl.imag == -1.0 && PyErr_Occurred())
4502 return NULL;
4503 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004504 }
4505 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004506 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004507 dx = PyOS_string_to_double(s, NULL, NULL);
4508 if (dx == -1.0 && PyErr_Occurred())
4509 return NULL;
4510 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004512}
4513
4514static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004515parsenumber(struct compiling *c, const char *s)
4516{
4517 char *dup, *end;
4518 PyObject *res = NULL;
4519
4520 assert(s != NULL);
4521
4522 if (strchr(s, '_') == NULL) {
4523 return parsenumber_raw(c, s);
4524 }
4525 /* Create a duplicate without underscores. */
4526 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004527 if (dup == NULL) {
4528 return PyErr_NoMemory();
4529 }
Brett Cannona721aba2016-09-09 14:57:09 -07004530 end = dup;
4531 for (; *s; s++) {
4532 if (*s != '_') {
4533 *end++ = *s;
4534 }
4535 }
4536 *end = '\0';
4537 res = parsenumber_raw(c, dup);
4538 PyMem_Free(dup);
4539 return res;
4540}
4541
4542static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004543decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004544{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004545 const char *s, *t;
4546 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004547 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4548 while (s < end && (*s & 0x80)) s++;
4549 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004550 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004551}
4552
Eric V. Smith56466482016-10-31 14:46:26 -04004553static int
4554warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004555 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004556{
4557 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4558 first_invalid_escape_char);
4559 if (msg == NULL) {
4560 return -1;
4561 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004562 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004563 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004564 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004565 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004566 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4567 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004568 to get a more accurate error report */
4569 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004570 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004571 }
4572 Py_DECREF(msg);
4573 return -1;
4574 }
4575 Py_DECREF(msg);
4576 return 0;
4577}
4578
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004579static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004580decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4581 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004582{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004583 PyObject *v, *u;
4584 char *buf;
4585 char *p;
4586 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004587
Benjamin Peterson202803a2016-02-25 22:34:45 -08004588 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004589 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004590 return NULL;
4591 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4592 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4593 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4594 if (u == NULL)
4595 return NULL;
4596 p = buf = PyBytes_AsString(u);
4597 end = s + len;
4598 while (s < end) {
4599 if (*s == '\\') {
4600 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004601 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004602 strcpy(p, "u005c");
4603 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004604 if (s >= end)
4605 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004606 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004607 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004608 if (*s & 0x80) { /* XXX inefficient */
4609 PyObject *w;
4610 int kind;
4611 void *data;
4612 Py_ssize_t len, i;
4613 w = decode_utf8(c, &s, end);
4614 if (w == NULL) {
4615 Py_DECREF(u);
4616 return NULL;
4617 }
4618 kind = PyUnicode_KIND(w);
4619 data = PyUnicode_DATA(w);
4620 len = PyUnicode_GET_LENGTH(w);
4621 for (i = 0; i < len; i++) {
4622 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4623 sprintf(p, "\\U%08x", chr);
4624 p += 10;
4625 }
4626 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004627 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004628 Py_DECREF(w);
4629 } else {
4630 *p++ = *s++;
4631 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004632 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004633 len = p - buf;
4634 s = buf;
4635
Eric V. Smith56466482016-10-31 14:46:26 -04004636 const char *first_invalid_escape;
4637 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4638
4639 if (v != NULL && first_invalid_escape != NULL) {
4640 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4641 /* We have not decref u before because first_invalid_escape points
4642 inside u. */
4643 Py_XDECREF(u);
4644 Py_DECREF(v);
4645 return NULL;
4646 }
4647 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004648 Py_XDECREF(u);
4649 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004650}
4651
Eric V. Smith56466482016-10-31 14:46:26 -04004652static PyObject *
4653decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4654 size_t len)
4655{
4656 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004657 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004658 &first_invalid_escape);
4659 if (result == NULL)
4660 return NULL;
4661
4662 if (first_invalid_escape != NULL) {
4663 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4664 Py_DECREF(result);
4665 return NULL;
4666 }
4667 }
4668 return result;
4669}
4670
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004671/* Shift locations for the given node and all its children by adding `lineno`
4672 and `col_offset` to existing locations. */
4673static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4674{
4675 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004676 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004677 for (int i = 0; i < NCH(n); ++i) {
4678 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4679 /* Shifting column offsets unnecessary if there's been newlines. */
4680 col_offset = 0;
4681 }
4682 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4683 }
4684 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004685 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004686}
4687
4688/* Fix locations for the given node and its children.
4689
4690 `parent` is the enclosing node.
4691 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004692 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004693*/
4694static void
4695fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4696{
4697 char *substr = NULL;
4698 char *start;
4699 int lines = LINENO(parent) - 1;
4700 int cols = parent->n_col_offset;
4701 /* Find the full fstring to fix location information in `n`. */
4702 while (parent && parent->n_type != STRING)
4703 parent = parent->n_child;
4704 if (parent && parent->n_str) {
4705 substr = strstr(parent->n_str, expr_str);
4706 if (substr) {
4707 start = substr;
4708 while (start > parent->n_str) {
4709 if (start[0] == '\n')
4710 break;
4711 start--;
4712 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004713 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004714 /* adjust the start based on the number of newlines encountered
4715 before the f-string expression */
4716 for (char* p = parent->n_str; p < substr; p++) {
4717 if (*p == '\n') {
4718 lines++;
4719 }
4720 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004721 }
4722 }
4723 fstring_shift_node_locations(n, lines, cols);
4724}
4725
Eric V. Smith451d0e32016-09-09 21:56:20 -04004726/* Compile this expression in to an expr_ty. Add parens around the
4727 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004728static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004729fstring_compile_expr(const char *expr_start, const char *expr_end,
4730 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004731
Eric V. Smith235a6f02015-09-19 14:51:32 -04004732{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004733 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004734 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004735 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004736 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004737 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004738
Eric V. Smith1d44c412015-09-23 07:49:00 -04004739 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004740 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004741 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4742 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004743
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004744 /* If the substring is all whitespace, it's an error. We need to catch this
4745 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4746 because turning the expression '' in to '()' would go from being invalid
4747 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004748 for (s = expr_start; s != expr_end; s++) {
4749 char c = *s;
4750 /* The Python parser ignores only the following whitespace
4751 characters (\r already is converted to \n). */
4752 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004753 break;
4754 }
4755 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004756 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004757 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004758 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004759 }
4760
Eric V. Smith451d0e32016-09-09 21:56:20 -04004761 len = expr_end - expr_start;
4762 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4763 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004764 if (str == NULL) {
4765 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004766 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004767 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004768
Eric V. Smith451d0e32016-09-09 21:56:20 -04004769 str[0] = '(';
4770 memcpy(str+1, expr_start, len);
4771 str[len+1] = ')';
4772 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004773
Victor Stinner37d66d72019-06-13 02:16:41 +02004774 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004775 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004776 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4777 Py_eval_input, 0);
4778 if (!mod_n) {
4779 PyMem_RawFree(str);
4780 return NULL;
4781 }
4782 /* Reuse str to find the correct column offset. */
4783 str[0] = '{';
4784 str[len+1] = '}';
4785 fstring_fix_node_location(n, mod_n, str);
4786 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004787 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004788 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004789 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004790 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004791 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004792}
4793
4794/* Return -1 on error.
4795
4796 Return 0 if we reached the end of the literal.
4797
4798 Return 1 if we haven't reached the end of the literal, but we want
4799 the caller to process the literal up to this point. Used for
4800 doubled braces.
4801*/
4802static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004803fstring_find_literal(const char **str, const char *end, int raw,
4804 PyObject **literal, int recurse_lvl,
4805 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004806{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004807 /* Get any literal string. It ends when we hit an un-doubled left
4808 brace (which isn't part of a unicode name escape such as
4809 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004810
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004811 const char *s = *str;
4812 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004813 int result = 0;
4814
Eric V. Smith235a6f02015-09-19 14:51:32 -04004815 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004816 while (s < end) {
4817 char ch = *s++;
4818 if (!raw && ch == '\\' && s < end) {
4819 ch = *s++;
4820 if (ch == 'N') {
4821 if (s < end && *s++ == '{') {
4822 while (s < end && *s++ != '}') {
4823 }
4824 continue;
4825 }
4826 break;
4827 }
4828 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4829 return -1;
4830 }
4831 }
4832 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004833 /* Check for doubled braces, but only at the top level. If
4834 we checked at every level, then f'{0:{3}}' would fail
4835 with the two closing braces. */
4836 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004837 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004838 /* We're going to tell the caller that the literal ends
4839 here, but that they should continue scanning. But also
4840 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004841 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004842 result = 1;
4843 goto done;
4844 }
4845
4846 /* Where a single '{' is the start of a new expression, a
4847 single '}' is not allowed. */
4848 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004849 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004850 ast_error(c, n, "f-string: single '}' is not allowed");
4851 return -1;
4852 }
4853 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004854 /* We're either at a '{', which means we're starting another
4855 expression; or a '}', which means we're at the end of this
4856 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004857 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004858 break;
4859 }
4860 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004861 *str = s;
4862 assert(s <= end);
4863 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004864done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004865 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004866 if (raw)
4867 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004868 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004869 NULL, NULL);
4870 else
Eric V. Smith56466482016-10-31 14:46:26 -04004871 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004872 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004873 if (!*literal)
4874 return -1;
4875 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004876 return result;
4877}
4878
4879/* Forward declaration because parsing is recursive. */
4880static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004881fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004882 struct compiling *c, const node *n);
4883
Eric V. Smith451d0e32016-09-09 21:56:20 -04004884/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004885 expression (so it must be a '{'). Returns the FormattedValue node, which
4886 includes the expression, conversion character, format_spec expression, and
4887 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888
4889 Note that I don't do a perfect job here: I don't make sure that a
4890 closing brace doesn't match an opening paren, for example. It
4891 doesn't need to error on all invalid expressions, just correctly
4892 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004893 will be caught when we parse it later.
4894
4895 *expression is set to the expression. For an '=' "debug" expression,
4896 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004897 including the '=' and any whitespace around it, as a string object). If
4898 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004899static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004900fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004901 PyObject **expr_text, expr_ty *expression,
4902 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004903{
4904 /* Return -1 on error, else 0. */
4905
Eric V. Smith451d0e32016-09-09 21:56:20 -04004906 const char *expr_start;
4907 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004908 expr_ty simple_expression;
4909 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004910 int conversion = -1; /* The conversion char. Use default if not
4911 specified, or !r if using = and no format
4912 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004913
4914 /* 0 if we're not in a string, else the quote char we're trying to
4915 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004916 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917
4918 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4919 int string_type = 0;
4920
4921 /* Keep track of nesting level for braces/parens/brackets in
4922 expressions. */
4923 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004924 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004925
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004926 *expr_text = NULL;
4927
Eric V. Smith235a6f02015-09-19 14:51:32 -04004928 /* Can only nest one level deep. */
4929 if (recurse_lvl >= 2) {
4930 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004931 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004932 }
4933
4934 /* The first char must be a left brace, or we wouldn't have gotten
4935 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004936 assert(**str == '{');
4937 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004938
Eric V. Smith451d0e32016-09-09 21:56:20 -04004939 expr_start = *str;
4940 for (; *str < end; (*str)++) {
4941 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004942
4943 /* Loop invariants. */
4944 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004945 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004946 if (quote_char)
4947 assert(string_type == 1 || string_type == 3);
4948 else
4949 assert(string_type == 0);
4950
Eric V. Smith451d0e32016-09-09 21:56:20 -04004951 ch = **str;
4952 /* Nowhere inside an expression is a backslash allowed. */
4953 if (ch == '\\') {
4954 /* Error: can't include a backslash character, inside
4955 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004956 ast_error(c, n,
4957 "f-string expression part "
4958 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004959 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004960 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004961 if (quote_char) {
4962 /* We're inside a string. See if we're at the end. */
4963 /* This code needs to implement the same non-error logic
4964 as tok_get from tokenizer.c, at the letter_quote
4965 label. To actually share that code would be a
4966 nightmare. But, it's unlikely to change and is small,
4967 so duplicate it here. Note we don't need to catch all
4968 of the errors, since they'll be caught when parsing the
4969 expression. We just need to match the non-error
4970 cases. Thus we can ignore \n in single-quoted strings,
4971 for example. Or non-terminated strings. */
4972 if (ch == quote_char) {
4973 /* Does this match the string_type (single or triple
4974 quoted)? */
4975 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004976 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004977 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004978 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979 string_type = 0;
4980 quote_char = 0;
4981 continue;
4982 }
4983 } else {
4984 /* We're at the end of a normal string. */
4985 quote_char = 0;
4986 string_type = 0;
4987 continue;
4988 }
4989 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004990 } else if (ch == '\'' || ch == '"') {
4991 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004992 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004994 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 } else {
4996 /* Start of a normal string. */
4997 string_type = 1;
4998 }
4999 /* Start looking for the end of the string. */
5000 quote_char = ch;
5001 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005002 if (nested_depth >= MAXLEVEL) {
5003 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005004 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005005 }
5006 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005007 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005008 } else if (ch == '#') {
5009 /* Error: can't include a comment character, inside parens
5010 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005011 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005012 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005014 (ch == '!' || ch == ':' || ch == '}' ||
5015 ch == '=' || ch == '>' || ch == '<')) {
5016 /* See if there's a next character. */
5017 if (*str+1 < end) {
5018 char next = *(*str+1);
5019
5020 /* For "!=". since '=' is not an allowed conversion character,
5021 nothing is lost in this test. */
5022 if ((ch == '!' && next == '=') || /* != */
5023 (ch == '=' && next == '=') || /* == */
5024 (ch == '<' && next == '=') || /* <= */
5025 (ch == '>' && next == '=') /* >= */
5026 ) {
5027 *str += 1;
5028 continue;
5029 }
5030 /* Don't get out of the loop for these, if they're single
5031 chars (not part of 2-char tokens). If by themselves, they
5032 don't end an expression (unlike say '!'). */
5033 if (ch == '>' || ch == '<') {
5034 continue;
5035 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005036 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005037
Eric V. Smith235a6f02015-09-19 14:51:32 -04005038 /* Normal way out of this loop. */
5039 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005040 } else if (ch == ']' || ch == '}' || ch == ')') {
5041 if (!nested_depth) {
5042 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005043 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005044 }
5045 nested_depth--;
5046 int opening = parenstack[nested_depth];
5047 if (!((opening == '(' && ch == ')') ||
5048 (opening == '[' && ch == ']') ||
5049 (opening == '{' && ch == '}')))
5050 {
5051 ast_error(c, n,
5052 "f-string: closing parenthesis '%c' "
5053 "does not match opening parenthesis '%c'",
5054 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005055 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005056 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005057 } else {
5058 /* Just consume this char and loop around. */
5059 }
5060 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005061 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005062 /* If we leave this loop in a string or with mismatched parens, we
5063 don't care. We'll get a syntax error when compiling the
5064 expression. But, we can produce a better error message, so
5065 let's just do that.*/
5066 if (quote_char) {
5067 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005068 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005069 }
5070 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005071 int opening = parenstack[nested_depth - 1];
5072 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005073 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005074 }
5075
Eric V. Smith451d0e32016-09-09 21:56:20 -04005076 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005077 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005078
5079 /* Compile the expression as soon as possible, so we show errors
5080 related to the expression before errors related to the
5081 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005083 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005084 goto error;
5085
5086 /* Check for =, which puts the text value of the expression in
5087 expr_text. */
5088 if (**str == '=') {
5089 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005090
5091 /* Skip over ASCII whitespace. No need to test for end of string
5092 here, since we know there's at least a trailing quote somewhere
5093 ahead. */
5094 while (Py_ISSPACE(**str)) {
5095 *str += 1;
5096 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005097
5098 /* Set *expr_text to the text of the expression. */
5099 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5100 if (!*expr_text) {
5101 goto error;
5102 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005103 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005104
5105 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005106 if (**str == '!') {
5107 *str += 1;
5108 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109 goto unexpected_end_of_string;
5110
Eric V. Smith451d0e32016-09-09 21:56:20 -04005111 conversion = **str;
5112 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005113
5114 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005115 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005116 ast_error(c, n,
5117 "f-string: invalid conversion character: "
5118 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005119 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005120 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005121
5122 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005123
5124 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005125 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005126 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005127 if (**str == ':') {
5128 *str += 1;
5129 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005130 goto unexpected_end_of_string;
5131
5132 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005133 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005135 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005136 }
5137
Eric V. Smith451d0e32016-09-09 21:56:20 -04005138 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005139 goto unexpected_end_of_string;
5140
5141 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005142 assert(*str < end);
5143 assert(**str == '}');
5144 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005145
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005146 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005147 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005148 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005149 conversion = 'r';
5150 }
5151
Eric V. Smith451d0e32016-09-09 21:56:20 -04005152 /* And now create the FormattedValue node that represents this
5153 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005154 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005155 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005156 n->n_col_offset, n->n_end_lineno,
5157 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005158 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005159 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005160
5161 return 0;
5162
5163unexpected_end_of_string:
5164 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005165 /* Falls through to error. */
5166
5167error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005168 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005169 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005170
Eric V. Smith235a6f02015-09-19 14:51:32 -04005171}
5172
5173/* Return -1 on error.
5174
5175 Return 0 if we have a literal (possible zero length) and an
5176 expression (zero length if at the end of the string.
5177
5178 Return 1 if we have a literal, but no expression, and we want the
5179 caller to call us again. This is used to deal with doubled
5180 braces.
5181
5182 When called multiple times on the string 'a{{b{0}c', this function
5183 will return:
5184
5185 1. the literal 'a{' with no expression, and a return value
5186 of 1. Despite the fact that there's no expression, the return
5187 value of 1 means we're not finished yet.
5188
5189 2. the literal 'b' and the expression '0', with a return value of
5190 0. The fact that there's an expression means we're not finished.
5191
5192 3. literal 'c' with no expression and a return value of 0. The
5193 combination of the return value of 0 with no expression means
5194 we're finished.
5195*/
5196static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005197fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5198 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005199 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005200 struct compiling *c, const node *n)
5201{
5202 int result;
5203
5204 assert(*literal == NULL && *expression == NULL);
5205
5206 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005207 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005208 if (result < 0)
5209 goto error;
5210
5211 assert(result == 0 || result == 1);
5212
5213 if (result == 1)
5214 /* We have a literal, but don't look at the expression. */
5215 return 1;
5216
Eric V. Smith451d0e32016-09-09 21:56:20 -04005217 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005218 /* We're at the end of the string or the end of a nested
5219 f-string: no expression. The top-level error case where we
5220 expect to be at the end of the string but we're at a '}' is
5221 handled later. */
5222 return 0;
5223
5224 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005225 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005226
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005227 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5228 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005229 goto error;
5230
5231 return 0;
5232
5233error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005234 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005235 return -1;
5236}
5237
5238#define EXPRLIST_N_CACHED 64
5239
5240typedef struct {
5241 /* Incrementally build an array of expr_ty, so be used in an
5242 asdl_seq. Cache some small but reasonably sized number of
5243 expr_ty's, and then after that start dynamically allocating,
5244 doubling the number allocated each time. Note that the f-string
5245 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005246 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005247 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005248
5249 Py_ssize_t allocated; /* Number we've allocated. */
5250 Py_ssize_t size; /* Number we've used. */
5251 expr_ty *p; /* Pointer to the memory we're actually
5252 using. Will point to 'data' until we
5253 start dynamically allocating. */
5254 expr_ty data[EXPRLIST_N_CACHED];
5255} ExprList;
5256
5257#ifdef NDEBUG
5258#define ExprList_check_invariants(l)
5259#else
5260static void
5261ExprList_check_invariants(ExprList *l)
5262{
5263 /* Check our invariants. Make sure this object is "live", and
5264 hasn't been deallocated. */
5265 assert(l->size >= 0);
5266 assert(l->p != NULL);
5267 if (l->size <= EXPRLIST_N_CACHED)
5268 assert(l->data == l->p);
5269}
5270#endif
5271
5272static void
5273ExprList_Init(ExprList *l)
5274{
5275 l->allocated = EXPRLIST_N_CACHED;
5276 l->size = 0;
5277
5278 /* Until we start allocating dynamically, p points to data. */
5279 l->p = l->data;
5280
5281 ExprList_check_invariants(l);
5282}
5283
5284static int
5285ExprList_Append(ExprList *l, expr_ty exp)
5286{
5287 ExprList_check_invariants(l);
5288 if (l->size >= l->allocated) {
5289 /* We need to alloc (or realloc) the memory. */
5290 Py_ssize_t new_size = l->allocated * 2;
5291
5292 /* See if we've ever allocated anything dynamically. */
5293 if (l->p == l->data) {
5294 Py_ssize_t i;
5295 /* We're still using the cached data. Switch to
5296 alloc-ing. */
5297 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5298 if (!l->p)
5299 return -1;
5300 /* Copy the cached data into the new buffer. */
5301 for (i = 0; i < l->size; i++)
5302 l->p[i] = l->data[i];
5303 } else {
5304 /* Just realloc. */
5305 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5306 if (!tmp) {
5307 PyMem_RawFree(l->p);
5308 l->p = NULL;
5309 return -1;
5310 }
5311 l->p = tmp;
5312 }
5313
5314 l->allocated = new_size;
5315 assert(l->allocated == 2 * l->size);
5316 }
5317
5318 l->p[l->size++] = exp;
5319
5320 ExprList_check_invariants(l);
5321 return 0;
5322}
5323
5324static void
5325ExprList_Dealloc(ExprList *l)
5326{
5327 ExprList_check_invariants(l);
5328
5329 /* If there's been an error, or we've never dynamically allocated,
5330 do nothing. */
5331 if (!l->p || l->p == l->data) {
5332 /* Do nothing. */
5333 } else {
5334 /* We have dynamically allocated. Free the memory. */
5335 PyMem_RawFree(l->p);
5336 }
5337 l->p = NULL;
5338 l->size = -1;
5339}
5340
5341static asdl_seq *
5342ExprList_Finish(ExprList *l, PyArena *arena)
5343{
5344 asdl_seq *seq;
5345
5346 ExprList_check_invariants(l);
5347
5348 /* Allocate the asdl_seq and copy the expressions in to it. */
5349 seq = _Py_asdl_seq_new(l->size, arena);
5350 if (seq) {
5351 Py_ssize_t i;
5352 for (i = 0; i < l->size; i++)
5353 asdl_seq_SET(seq, i, l->p[i]);
5354 }
5355 ExprList_Dealloc(l);
5356 return seq;
5357}
5358
5359/* The FstringParser is designed to add a mix of strings and
5360 f-strings, and concat them together as needed. Ultimately, it
5361 generates an expr_ty. */
5362typedef struct {
5363 PyObject *last_str;
5364 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005365 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005366} FstringParser;
5367
5368#ifdef NDEBUG
5369#define FstringParser_check_invariants(state)
5370#else
5371static void
5372FstringParser_check_invariants(FstringParser *state)
5373{
5374 if (state->last_str)
5375 assert(PyUnicode_CheckExact(state->last_str));
5376 ExprList_check_invariants(&state->expr_list);
5377}
5378#endif
5379
5380static void
5381FstringParser_Init(FstringParser *state)
5382{
5383 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005384 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005385 ExprList_Init(&state->expr_list);
5386 FstringParser_check_invariants(state);
5387}
5388
5389static void
5390FstringParser_Dealloc(FstringParser *state)
5391{
5392 FstringParser_check_invariants(state);
5393
5394 Py_XDECREF(state->last_str);
5395 ExprList_Dealloc(&state->expr_list);
5396}
5397
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005398/* Constants for the following */
5399static PyObject *u_kind;
5400
5401/* Compute 'kind' field for string Constant (either 'u' or None) */
5402static PyObject *
5403make_kind(struct compiling *c, const node *n)
5404{
5405 char *s = NULL;
5406 PyObject *kind = NULL;
5407
5408 /* Find the first string literal, if any */
5409 while (TYPE(n) != STRING) {
5410 if (NCH(n) == 0)
5411 return NULL;
5412 n = CHILD(n, 0);
5413 }
5414 REQ(n, STRING);
5415
5416 /* If it starts with 'u', return a PyUnicode "u" string */
5417 s = STR(n);
5418 if (s && *s == 'u') {
5419 if (!u_kind) {
5420 u_kind = PyUnicode_InternFromString("u");
5421 if (!u_kind)
5422 return NULL;
5423 }
5424 kind = u_kind;
5425 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5426 return NULL;
5427 }
5428 Py_INCREF(kind);
5429 }
5430 return kind;
5431}
5432
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005433/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005434static expr_ty
5435make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5436{
5437 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005438 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005439 *str = NULL;
5440 assert(PyUnicode_CheckExact(s));
5441 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5442 Py_DECREF(s);
5443 return NULL;
5444 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005445 kind = make_kind(c, n);
5446 if (kind == NULL && PyErr_Occurred())
5447 return NULL;
5448 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005449 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005450}
5451
5452/* Add a non-f-string (that is, a regular literal string). str is
5453 decref'd. */
5454static int
5455FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5456{
5457 FstringParser_check_invariants(state);
5458
5459 assert(PyUnicode_CheckExact(str));
5460
5461 if (PyUnicode_GET_LENGTH(str) == 0) {
5462 Py_DECREF(str);
5463 return 0;
5464 }
5465
5466 if (!state->last_str) {
5467 /* We didn't have a string before, so just remember this one. */
5468 state->last_str = str;
5469 } else {
5470 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005471 PyUnicode_AppendAndDel(&state->last_str, str);
5472 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005473 return -1;
5474 }
5475 FstringParser_check_invariants(state);
5476 return 0;
5477}
5478
Eric V. Smith451d0e32016-09-09 21:56:20 -04005479/* Parse an f-string. The f-string is in *str to end, with no
5480 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005481static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005482FstringParser_ConcatFstring(FstringParser *state, const char **str,
5483 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005484 struct compiling *c, const node *n)
5485{
5486 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005487 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005488
5489 /* Parse the f-string. */
5490 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005491 PyObject *literal = NULL;
5492 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005493 expr_ty expression = NULL;
5494
5495 /* If there's a zero length literal in front of the
5496 expression, literal will be NULL. If we're at the end of
5497 the f-string, expression will be NULL (unless result == 1,
5498 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005499 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005500 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005501 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005502 if (result < 0)
5503 return -1;
5504
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005505 /* Add the literal, if any. */
5506 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5507 Py_XDECREF(expr_text);
5508 return -1;
5509 }
5510 /* Add the expr_text, if any. */
5511 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5512 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005513 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005514
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005515 /* We've dealt with the literal and expr_text, their ownership has
5516 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005517
5518 /* See if we should just loop around to get the next literal
5519 and expression, while ignoring the expression this
5520 time. This is used for un-doubling braces, as an
5521 optimization. */
5522 if (result == 1)
5523 continue;
5524
5525 if (!expression)
5526 /* We're done with this f-string. */
5527 break;
5528
5529 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005530 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005531 if (!state->last_str) {
5532 /* Do nothing. No previous literal. */
5533 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005534 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005535 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5536 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5537 return -1;
5538 }
5539
5540 if (ExprList_Append(&state->expr_list, expression) < 0)
5541 return -1;
5542 }
5543
Eric V. Smith235a6f02015-09-19 14:51:32 -04005544 /* If recurse_lvl is zero, then we must be at the end of the
5545 string. Otherwise, we must be at a right brace. */
5546
Eric V. Smith451d0e32016-09-09 21:56:20 -04005547 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005548 ast_error(c, n, "f-string: unexpected end of string");
5549 return -1;
5550 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005551 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005552 ast_error(c, n, "f-string: expecting '}'");
5553 return -1;
5554 }
5555
5556 FstringParser_check_invariants(state);
5557 return 0;
5558}
5559
5560/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005561 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005562static expr_ty
5563FstringParser_Finish(FstringParser *state, struct compiling *c,
5564 const node *n)
5565{
5566 asdl_seq *seq;
5567
5568 FstringParser_check_invariants(state);
5569
5570 /* If we're just a constant string with no expressions, return
5571 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005572 if (!state->fmode) {
5573 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005574 if (!state->last_str) {
5575 /* Create a zero length string. */
5576 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5577 if (!state->last_str)
5578 goto error;
5579 }
5580 return make_str_node_and_del(&state->last_str, c, n);
5581 }
5582
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005583 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005584 last node in our expression list. */
5585 if (state->last_str) {
5586 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5587 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5588 goto error;
5589 }
5590 /* This has already been freed. */
5591 assert(state->last_str == NULL);
5592
5593 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5594 if (!seq)
5595 goto error;
5596
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005597 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5598 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005599
5600error:
5601 FstringParser_Dealloc(state);
5602 return NULL;
5603}
5604
Eric V. Smith451d0e32016-09-09 21:56:20 -04005605/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5606 at end, parse it into an expr_ty. Return NULL on error. Adjust
5607 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005608static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005609fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005610 struct compiling *c, const node *n)
5611{
5612 FstringParser state;
5613
5614 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005615 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005616 c, n) < 0) {
5617 FstringParser_Dealloc(&state);
5618 return NULL;
5619 }
5620
5621 return FstringParser_Finish(&state, c, n);
5622}
5623
5624/* n is a Python string literal, including the bracketing quote
5625 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005626 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005627 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005628 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5629 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005630*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005631static int
5632parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5633 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005634{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005635 size_t len;
5636 const char *s = STR(n);
5637 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005638 int fmode = 0;
5639 *bytesmode = 0;
5640 *rawmode = 0;
5641 *result = NULL;
5642 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005643 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005644 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005645 if (quote == 'b' || quote == 'B') {
5646 quote = *++s;
5647 *bytesmode = 1;
5648 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005649 else if (quote == 'u' || quote == 'U') {
5650 quote = *++s;
5651 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005652 else if (quote == 'r' || quote == 'R') {
5653 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005654 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005655 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005656 else if (quote == 'f' || quote == 'F') {
5657 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005658 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005659 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005660 else {
5661 break;
5662 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005663 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005664 }
Guido van Rossum495da292019-03-07 12:38:08 -08005665
5666 /* fstrings are only allowed in Python 3.6 and greater */
5667 if (fmode && c->c_feature_version < 6) {
5668 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5669 return -1;
5670 }
5671
Eric V. Smith451d0e32016-09-09 21:56:20 -04005672 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005673 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005674 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005675 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005676 if (quote != '\'' && quote != '\"') {
5677 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005678 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005679 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005680 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005681 s++;
5682 len = strlen(s);
5683 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005684 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005685 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005686 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005687 }
5688 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005689 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005690 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005691 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005692 }
5693 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005694 /* A triple quoted string. We've already skipped one quote at
5695 the start and one at the end of the string. Now skip the
5696 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005697 s += 2;
5698 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005699 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005700 if (s[--len] != quote || s[--len] != quote) {
5701 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005702 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005703 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005704 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005705
Eric V. Smith451d0e32016-09-09 21:56:20 -04005706 if (fmode) {
5707 /* Just return the bytes. The caller will parse the resulting
5708 string. */
5709 *fstr = s;
5710 *fstrlen = len;
5711 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005712 }
5713
Eric V. Smith451d0e32016-09-09 21:56:20 -04005714 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005715 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005716 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005717 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005718 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005719 const char *ch;
5720 for (ch = s; *ch; ch++) {
5721 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005722 ast_error(c, n,
5723 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005724 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005725 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005726 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005727 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005728 if (*rawmode)
5729 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005730 else
Eric V. Smith56466482016-10-31 14:46:26 -04005731 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005732 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005733 if (*rawmode)
5734 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005735 else
Eric V. Smith56466482016-10-31 14:46:26 -04005736 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005737 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005738 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005739}
5740
Eric V. Smith235a6f02015-09-19 14:51:32 -04005741/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5742 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005743 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005744 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005745 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005746 node if there's just an f-string (with no leading or trailing
5747 literals), or a JoinedStr node if there are multiple f-strings or
5748 any literals involved. */
5749static expr_ty
5750parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005751{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005752 int bytesmode = 0;
5753 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005754 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005755
5756 FstringParser state;
5757 FstringParser_Init(&state);
5758
5759 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005760 int this_bytesmode;
5761 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005762 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005763 const char *fstr;
5764 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005765
5766 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005767 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5768 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005769 goto error;
5770
5771 /* Check that we're not mixing bytes with unicode. */
5772 if (i != 0 && bytesmode != this_bytesmode) {
5773 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005774 /* s is NULL if the current string part is an f-string. */
5775 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005776 goto error;
5777 }
5778 bytesmode = this_bytesmode;
5779
Eric V. Smith451d0e32016-09-09 21:56:20 -04005780 if (fstr != NULL) {
5781 int result;
5782 assert(s == NULL && !bytesmode);
5783 /* This is an f-string. Parse and concatenate it. */
5784 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5785 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005786 if (result < 0)
5787 goto error;
5788 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005789 /* A string or byte string. */
5790 assert(s != NULL && fstr == NULL);
5791
Eric V. Smith451d0e32016-09-09 21:56:20 -04005792 assert(bytesmode ? PyBytes_CheckExact(s) :
5793 PyUnicode_CheckExact(s));
5794
Eric V. Smith451d0e32016-09-09 21:56:20 -04005795 if (bytesmode) {
5796 /* For bytes, concat as we go. */
5797 if (i == 0) {
5798 /* First time, just remember this value. */
5799 bytes_str = s;
5800 } else {
5801 PyBytes_ConcatAndDel(&bytes_str, s);
5802 if (!bytes_str)
5803 goto error;
5804 }
5805 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005806 /* This is a regular string. Concatenate it. */
5807 if (FstringParser_ConcatAndDel(&state, s) < 0)
5808 goto error;
5809 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005810 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005811 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005812 if (bytesmode) {
5813 /* Just return the bytes object and we're done. */
5814 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5815 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005816 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005817 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005819
Eric V. Smith235a6f02015-09-19 14:51:32 -04005820 /* We're not a bytes string, bytes_str should never have been set. */
5821 assert(bytes_str == NULL);
5822
5823 return FstringParser_Finish(&state, c, n);
5824
5825error:
5826 Py_XDECREF(bytes_str);
5827 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005829}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005830
5831PyObject *
5832_PyAST_GetDocString(asdl_seq *body)
5833{
5834 if (!asdl_seq_LEN(body)) {
5835 return NULL;
5836 }
5837 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5838 if (st->kind != Expr_kind) {
5839 return NULL;
5840 }
5841 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005842 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5843 return e->v.Constant.value;
5844 }
5845 return NULL;
5846}