blob: 2e9a8d05f7ea260dfdfb4938f561419c164ab6ae [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";
Benjamin Peterson832bfe22011-08-09 16:15:04 -050074 default:
Barry Warsawb2e57942017-09-14 18:13:16 -070075 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -050076 }
77}
78
79static int
80validate_arguments(arguments_ty args)
81{
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010082 if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050083 return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010084 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -070085 if (args->vararg && args->vararg->annotation
86 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050087 return 0;
88 }
89 if (!validate_args(args->kwonlyargs))
90 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +010091 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -070092 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050093 return 0;
94 }
Pablo Galindo2f58a842019-05-31 14:09:49 +010095 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050096 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
97 return 0;
98 }
99 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
100 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
101 "kw_defaults on arguments");
102 return 0;
103 }
104 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
105}
106
107static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100108validate_constant(PyObject *value)
109{
110 if (value == Py_None || value == Py_Ellipsis)
111 return 1;
112
113 if (PyLong_CheckExact(value)
114 || PyFloat_CheckExact(value)
115 || PyComplex_CheckExact(value)
116 || PyBool_Check(value)
117 || PyUnicode_CheckExact(value)
118 || PyBytes_CheckExact(value))
119 return 1;
120
121 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
122 PyObject *it;
123
124 it = PyObject_GetIter(value);
125 if (it == NULL)
126 return 0;
127
128 while (1) {
129 PyObject *item = PyIter_Next(it);
130 if (item == NULL) {
131 if (PyErr_Occurred()) {
132 Py_DECREF(it);
133 return 0;
134 }
135 break;
136 }
137
138 if (!validate_constant(item)) {
139 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100140 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100141 return 0;
142 }
Victor Stinner726f6902016-01-27 00:11:47 +0100143 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100144 }
145
146 Py_DECREF(it);
147 return 1;
148 }
149
150 return 0;
151}
152
153static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500154validate_expr(expr_ty exp, expr_context_ty ctx)
155{
156 int check_ctx = 1;
157 expr_context_ty actual_ctx;
158
159 /* First check expression context. */
160 switch (exp->kind) {
161 case Attribute_kind:
162 actual_ctx = exp->v.Attribute.ctx;
163 break;
164 case Subscript_kind:
165 actual_ctx = exp->v.Subscript.ctx;
166 break;
167 case Starred_kind:
168 actual_ctx = exp->v.Starred.ctx;
169 break;
170 case Name_kind:
171 actual_ctx = exp->v.Name.ctx;
172 break;
173 case List_kind:
174 actual_ctx = exp->v.List.ctx;
175 break;
176 case Tuple_kind:
177 actual_ctx = exp->v.Tuple.ctx;
178 break;
179 default:
180 if (ctx != Load) {
181 PyErr_Format(PyExc_ValueError, "expression which can't be "
182 "assigned to in %s context", expr_context_name(ctx));
183 return 0;
184 }
185 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100186 /* set actual_ctx to prevent gcc warning */
187 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500188 }
189 if (check_ctx && actual_ctx != ctx) {
190 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
191 expr_context_name(ctx), expr_context_name(actual_ctx));
192 return 0;
193 }
194
195 /* Now validate expression. */
196 switch (exp->kind) {
197 case BoolOp_kind:
198 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
199 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
200 return 0;
201 }
202 return validate_exprs(exp->v.BoolOp.values, Load, 0);
203 case BinOp_kind:
204 return validate_expr(exp->v.BinOp.left, Load) &&
205 validate_expr(exp->v.BinOp.right, Load);
206 case UnaryOp_kind:
207 return validate_expr(exp->v.UnaryOp.operand, Load);
208 case Lambda_kind:
209 return validate_arguments(exp->v.Lambda.args) &&
210 validate_expr(exp->v.Lambda.body, Load);
211 case IfExp_kind:
212 return validate_expr(exp->v.IfExp.test, Load) &&
213 validate_expr(exp->v.IfExp.body, Load) &&
214 validate_expr(exp->v.IfExp.orelse, Load);
215 case Dict_kind:
216 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
217 PyErr_SetString(PyExc_ValueError,
218 "Dict doesn't have the same number of keys as values");
219 return 0;
220 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400221 /* null_ok=1 for keys expressions to allow dict unpacking to work in
222 dict literals, i.e. ``{**{a:b}}`` */
223 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
224 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500225 case Set_kind:
226 return validate_exprs(exp->v.Set.elts, Load, 0);
227#define COMP(NAME) \
228 case NAME ## _kind: \
229 return validate_comprehension(exp->v.NAME.generators) && \
230 validate_expr(exp->v.NAME.elt, Load);
231 COMP(ListComp)
232 COMP(SetComp)
233 COMP(GeneratorExp)
234#undef COMP
235 case DictComp_kind:
236 return validate_comprehension(exp->v.DictComp.generators) &&
237 validate_expr(exp->v.DictComp.key, Load) &&
238 validate_expr(exp->v.DictComp.value, Load);
239 case Yield_kind:
240 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500241 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000242 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400243 case Await_kind:
244 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500245 case Compare_kind:
246 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
247 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
248 return 0;
249 }
250 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
251 asdl_seq_LEN(exp->v.Compare.ops)) {
252 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
253 "of comparators and operands");
254 return 0;
255 }
256 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
257 validate_expr(exp->v.Compare.left, Load);
258 case Call_kind:
259 return validate_expr(exp->v.Call.func, Load) &&
260 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400261 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100262 case Constant_kind:
263 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100264 PyErr_Format(PyExc_TypeError,
265 "got an invalid type in Constant: %s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700266 _PyType_Name(Py_TYPE(exp->v.Constant.value)));
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100267 return 0;
268 }
269 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400270 case JoinedStr_kind:
271 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
272 case FormattedValue_kind:
273 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
274 return 0;
275 if (exp->v.FormattedValue.format_spec)
276 return validate_expr(exp->v.FormattedValue.format_spec, Load);
277 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500278 case Attribute_kind:
279 return validate_expr(exp->v.Attribute.value, Load);
280 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200281 return validate_expr(exp->v.Subscript.slice, Load) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500282 validate_expr(exp->v.Subscript.value, Load);
283 case Starred_kind:
284 return validate_expr(exp->v.Starred.value, ctx);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200285 case Slice_kind:
286 return (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) &&
287 (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) &&
288 (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500289 case List_kind:
290 return validate_exprs(exp->v.List.elts, ctx, 0);
291 case Tuple_kind:
292 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000293 case NamedExpr_kind:
294 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300295 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500296 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500298 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000299 PyErr_SetString(PyExc_SystemError, "unexpected expression");
300 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500301}
302
303static int
304validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
305{
306 if (asdl_seq_LEN(seq))
307 return 1;
308 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
309 return 0;
310}
311
312static int
313validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
314{
315 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
316 validate_exprs(targets, ctx, 0);
317}
318
319static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300320validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500321{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300322 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323}
324
325static int
326validate_stmt(stmt_ty stmt)
327{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100328 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500329 switch (stmt->kind) {
330 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300331 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500332 validate_arguments(stmt->v.FunctionDef.args) &&
333 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
334 (!stmt->v.FunctionDef.returns ||
335 validate_expr(stmt->v.FunctionDef.returns, Load));
336 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300337 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500338 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
339 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400340 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500341 case Return_kind:
342 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
343 case Delete_kind:
344 return validate_assignlist(stmt->v.Delete.targets, Del);
345 case Assign_kind:
346 return validate_assignlist(stmt->v.Assign.targets, Store) &&
347 validate_expr(stmt->v.Assign.value, Load);
348 case AugAssign_kind:
349 return validate_expr(stmt->v.AugAssign.target, Store) &&
350 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700351 case AnnAssign_kind:
352 if (stmt->v.AnnAssign.target->kind != Name_kind &&
353 stmt->v.AnnAssign.simple) {
354 PyErr_SetString(PyExc_TypeError,
355 "AnnAssign with simple non-Name target");
356 return 0;
357 }
358 return validate_expr(stmt->v.AnnAssign.target, Store) &&
359 (!stmt->v.AnnAssign.value ||
360 validate_expr(stmt->v.AnnAssign.value, Load)) &&
361 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500362 case For_kind:
363 return validate_expr(stmt->v.For.target, Store) &&
364 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300365 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500366 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400367 case AsyncFor_kind:
368 return validate_expr(stmt->v.AsyncFor.target, Store) &&
369 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300370 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400371 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500372 case While_kind:
373 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300374 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500375 validate_stmts(stmt->v.While.orelse);
376 case If_kind:
377 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300378 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500379 validate_stmts(stmt->v.If.orelse);
380 case With_kind:
381 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
382 return 0;
383 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
384 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
385 if (!validate_expr(item->context_expr, Load) ||
386 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
387 return 0;
388 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300389 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400390 case AsyncWith_kind:
391 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
392 return 0;
393 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
394 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
395 if (!validate_expr(item->context_expr, Load) ||
396 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
397 return 0;
398 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300399 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500400 case Raise_kind:
401 if (stmt->v.Raise.exc) {
402 return validate_expr(stmt->v.Raise.exc, Load) &&
403 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
404 }
405 if (stmt->v.Raise.cause) {
406 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
407 return 0;
408 }
409 return 1;
410 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300411 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500412 return 0;
413 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
414 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
415 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
416 return 0;
417 }
418 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
419 asdl_seq_LEN(stmt->v.Try.orelse)) {
420 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
421 return 0;
422 }
423 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
424 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
425 if ((handler->v.ExceptHandler.type &&
426 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300427 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500428 return 0;
429 }
430 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
431 validate_stmts(stmt->v.Try.finalbody)) &&
432 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
433 validate_stmts(stmt->v.Try.orelse));
434 case Assert_kind:
435 return validate_expr(stmt->v.Assert.test, Load) &&
436 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
437 case Import_kind:
438 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
439 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300440 if (stmt->v.ImportFrom.level < 0) {
441 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500442 return 0;
443 }
444 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
445 case Global_kind:
446 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
447 case Nonlocal_kind:
448 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
449 case Expr_kind:
450 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400451 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300452 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400453 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
454 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
455 (!stmt->v.AsyncFunctionDef.returns ||
456 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500457 case Pass_kind:
458 case Break_kind:
459 case Continue_kind:
460 return 1;
461 default:
462 PyErr_SetString(PyExc_SystemError, "unexpected statement");
463 return 0;
464 }
465}
466
467static int
468validate_stmts(asdl_seq *seq)
469{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100470 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500471 for (i = 0; i < asdl_seq_LEN(seq); i++) {
472 stmt_ty stmt = asdl_seq_GET(seq, i);
473 if (stmt) {
474 if (!validate_stmt(stmt))
475 return 0;
476 }
477 else {
478 PyErr_SetString(PyExc_ValueError,
479 "None disallowed in statement list");
480 return 0;
481 }
482 }
483 return 1;
484}
485
486static int
487validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
488{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100489 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500490 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
491 expr_ty expr = asdl_seq_GET(exprs, i);
492 if (expr) {
493 if (!validate_expr(expr, ctx))
494 return 0;
495 }
496 else if (!null_ok) {
497 PyErr_SetString(PyExc_ValueError,
498 "None disallowed in expression list");
499 return 0;
500 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100501
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500502 }
503 return 1;
504}
505
506int
507PyAST_Validate(mod_ty mod)
508{
509 int res = 0;
510
511 switch (mod->kind) {
512 case Module_kind:
513 res = validate_stmts(mod->v.Module.body);
514 break;
515 case Interactive_kind:
516 res = validate_stmts(mod->v.Interactive.body);
517 break;
518 case Expression_kind:
519 res = validate_expr(mod->v.Expression.body, Load);
520 break;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500521 default:
522 PyErr_SetString(PyExc_SystemError, "impossible module node");
523 res = 0;
524 break;
525 }
526 return res;
527}
528
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500529/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500530#include "grammar.h"
531#include "parsetok.h"
532#include "graminit.h"
533
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534/* Data structure used internally */
535struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400536 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200537 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500538 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800539 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540};
541
542static asdl_seq *seq_for_testlist(struct compiling *, const node *);
543static expr_ty ast_for_expr(struct compiling *, const node *);
544static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300545static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000546static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
547 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000548static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000549static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
guoci90fc8982018-09-11 17:45:45 -0400551static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
552static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200555static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +0200556 const node *, const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000558static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400559static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000560static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561
Nick Coghlan650f0d02007-04-15 12:05:43 +0000562#define COMP_GENEXP 0
563#define COMP_LISTCOMP 1
564#define COMP_SETCOMP 2
565
Benjamin Peterson55e00432012-01-16 17:22:31 -0500566static int
567init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000568{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500569 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
570 if (!m)
571 return 0;
572 c->c_normalize = PyObject_GetAttrString(m, "normalize");
573 Py_DECREF(m);
574 if (!c->c_normalize)
575 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500576 return 1;
577}
578
579static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400580new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500581{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400582 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500583 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000584 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500585 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500586 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000587 /* Check whether there are non-ASCII characters in the
588 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500589 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200590 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500591 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500592 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200593 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500594 }
Dino Viehland5b172c22019-09-11 08:47:17 -0700595 PyObject *form = PyUnicode_InternFromString("NFKC");
Oren Milman7dc46d82017-09-30 20:16:24 +0300596 if (form == NULL) {
597 Py_DECREF(id);
598 return NULL;
599 }
600 PyObject *args[2] = {form, id};
601 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500602 Py_DECREF(id);
Dino Viehland8d88e8c2019-09-12 15:38:13 +0100603 Py_DECREF(form);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200604 if (!id2)
605 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300606 if (!PyUnicode_Check(id2)) {
607 PyErr_Format(PyExc_TypeError,
608 "unicodedata.normalize() must return a string, not "
609 "%.200s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700610 _PyType_Name(Py_TYPE(id2)));
Oren Milman7dc46d82017-09-30 20:16:24 +0300611 Py_DECREF(id2);
612 return NULL;
613 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200614 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000615 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000616 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200617 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
618 Py_DECREF(id);
619 return NULL;
620 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000621 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622}
623
Benjamin Peterson55e00432012-01-16 17:22:31 -0500624#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200627ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400629 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200630 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200632 va_start(va, errmsg);
633 errstr = PyUnicode_FromFormatV(errmsg, va);
634 va_end(va);
635 if (!errstr) {
636 return 0;
637 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200638 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000640 Py_INCREF(Py_None);
641 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 }
Ammar Askar025eb982018-09-24 17:12:49 -0400643 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200644 if (!tmp) {
645 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400646 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000647 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 Py_DECREF(errstr);
650 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400651 if (value) {
652 PyErr_SetObject(PyExc_SyntaxError, value);
653 Py_DECREF(value);
654 }
655 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656}
657
658/* num_stmts() returns number of contained statements.
659
660 Use this routine to determine how big a sequence is needed for
661 the statements in a parse tree. Its raison d'etre is this bit of
662 grammar:
663
664 stmt: simple_stmt | compound_stmt
665 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
666
667 A simple_stmt can contain multiple small_stmt elements joined
668 by semicolons. If the arg is a simple_stmt, the number of
669 small_stmt elements is returned.
670*/
671
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800672static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800673new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800674{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800675 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800676 if (res == NULL)
677 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800678 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
679 Py_DECREF(res);
680 return NULL;
681 }
682 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800683}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800684#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800685
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686static int
687num_stmts(const node *n)
688{
689 int i, l;
690 node *ch;
691
692 switch (TYPE(n)) {
693 case single_input:
694 if (TYPE(CHILD(n, 0)) == NEWLINE)
695 return 0;
696 else
697 return num_stmts(CHILD(n, 0));
698 case file_input:
699 l = 0;
700 for (i = 0; i < NCH(n); i++) {
701 ch = CHILD(n, i);
702 if (TYPE(ch) == stmt)
703 l += num_stmts(ch);
704 }
705 return l;
706 case stmt:
707 return num_stmts(CHILD(n, 0));
708 case compound_stmt:
709 return 1;
710 case simple_stmt:
711 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
712 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800713 case func_body_suite:
714 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
715 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 if (NCH(n) == 1)
717 return num_stmts(CHILD(n, 0));
718 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800719 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800721 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
722 i += 2;
723 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 l += num_stmts(CHILD(n, i));
725 return l;
726 }
727 default: {
728 char buf[128];
729
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000730 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731 TYPE(n), NCH(n));
732 Py_FatalError(buf);
733 }
734 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700735 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736}
737
738/* Transform the CST rooted at node * to the appropriate AST
739*/
740
741mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200742PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
743 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000745 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800747 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 stmt_ty s;
749 node *ch;
750 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500751 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800752 asdl_seq *argtypes = NULL;
753 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400755 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200756 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400757 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800758 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700759 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800760
761 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
Jeremy Hyltona8293132006-02-28 17:58:27 +0000764 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 switch (TYPE(n)) {
766 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200767 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500769 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 for (i = 0; i < NCH(n) - 1; i++) {
771 ch = CHILD(n, i);
772 if (TYPE(ch) == NEWLINE)
773 continue;
774 REQ(ch, stmt);
775 num = num_stmts(ch);
776 if (num == 1) {
777 s = ast_for_stmt(&c, ch);
778 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500779 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000780 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 }
782 else {
783 ch = CHILD(ch, 0);
784 REQ(ch, simple_stmt);
785 for (j = 0; j < num; j++) {
786 s = ast_for_stmt(&c, CHILD(ch, j * 2));
787 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500788 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000789 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 }
791 }
792 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800793
794 /* Type ignores are stored under the ENDMARKER in file_input. */
795 ch = CHILD(n, NCH(n) - 1);
796 REQ(ch, ENDMARKER);
797 num = NCH(ch);
798 type_ignores = _Py_asdl_seq_new(num, arena);
799 if (!type_ignores)
800 goto out;
801
802 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700803 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
804 if (!type_comment)
805 goto out;
806 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800807 if (!ti)
808 goto out;
809 asdl_seq_SET(type_ignores, i, ti);
810 }
811
812 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500813 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 case eval_input: {
815 expr_ty testlist_ast;
816
Nick Coghlan650f0d02007-04-15 12:05:43 +0000817 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000818 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500820 goto out;
821 res = Expression(testlist_ast, arena);
822 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 }
824 case single_input:
825 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200826 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500828 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000829 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000830 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000832 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500833 goto out;
834 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 }
836 else {
837 n = CHILD(n, 0);
838 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200839 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500841 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000843 s = ast_for_stmt(&c, n);
844 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500845 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 asdl_seq_SET(stmts, 0, s);
847 }
848 else {
849 /* Only a simple_stmt can contain multiple statements. */
850 REQ(n, simple_stmt);
851 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 if (TYPE(CHILD(n, i)) == NEWLINE)
853 break;
854 s = ast_for_stmt(&c, CHILD(n, i));
855 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500856 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 asdl_seq_SET(stmts, i / 2, s);
858 }
859 }
860
Benjamin Peterson55e00432012-01-16 17:22:31 -0500861 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500863 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800864 case func_type_input:
865 n = CHILD(n, 0);
866 REQ(n, func_type);
867
868 if (TYPE(CHILD(n, 1)) == typelist) {
869 ch = CHILD(n, 1);
870 /* this is overly permissive -- we don't pay any attention to
871 * stars on the args -- just parse them into an ordered list */
872 num = 0;
873 for (i = 0; i < NCH(ch); i++) {
874 if (TYPE(CHILD(ch, i)) == test) {
875 num++;
876 }
877 }
878
879 argtypes = _Py_asdl_seq_new(num, arena);
880 if (!argtypes)
881 goto out;
882
883 j = 0;
884 for (i = 0; i < NCH(ch); i++) {
885 if (TYPE(CHILD(ch, i)) == test) {
886 arg = ast_for_expr(&c, CHILD(ch, i));
887 if (!arg)
888 goto out;
889 asdl_seq_SET(argtypes, j++, arg);
890 }
891 }
892 }
893 else {
894 argtypes = _Py_asdl_seq_new(0, arena);
895 if (!argtypes)
896 goto out;
897 }
898
899 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
900 if (!ret)
901 goto out;
902 res = FunctionType(argtypes, ret, arena);
903 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000905 PyErr_Format(PyExc_SystemError,
906 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500907 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500909 out:
910 if (c.c_normalize) {
911 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500912 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500913 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914}
915
Victor Stinner14e461d2013-08-26 22:28:21 +0200916mod_ty
917PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
918 PyArena *arena)
919{
920 mod_ty mod;
921 PyObject *filename;
922 filename = PyUnicode_DecodeFSDefault(filename_str);
923 if (filename == NULL)
924 return NULL;
925 mod = PyAST_FromNodeObject(n, flags, filename, arena);
926 Py_DECREF(filename);
927 return mod;
928
929}
930
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
932*/
933
934static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800935get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936{
937 switch (TYPE(n)) {
938 case VBAR:
939 return BitOr;
940 case CIRCUMFLEX:
941 return BitXor;
942 case AMPER:
943 return BitAnd;
944 case LEFTSHIFT:
945 return LShift;
946 case RIGHTSHIFT:
947 return RShift;
948 case PLUS:
949 return Add;
950 case MINUS:
951 return Sub;
952 case STAR:
953 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400954 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800955 if (c->c_feature_version < 5) {
956 ast_error(c, n,
957 "The '@' operator is only supported in Python 3.5 and greater");
958 return (operator_ty)0;
959 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400960 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 case SLASH:
962 return Div;
963 case DOUBLESLASH:
964 return FloorDiv;
965 case PERCENT:
966 return Mod;
967 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000968 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 }
970}
971
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200972static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000973 "None",
974 "True",
975 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200976 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000977 NULL,
978};
979
980static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400981forbidden_name(struct compiling *c, identifier name, const node *n,
982 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000983{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000984 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200985 const char * const *p = FORBIDDEN;
986 if (!full_checks) {
987 /* In most cases, the parser will protect True, False, and None
988 from being assign to. */
989 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000990 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200991 for (; *p; p++) {
992 if (_PyUnicode_EqualToASCIIString(name, *p)) {
993 ast_error(c, n, "cannot assign to %U", name);
994 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000995 }
996 }
997 return 0;
998}
999
Serhiy Storchakab619b092018-11-27 09:40:29 +02001000static expr_ty
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001001copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001002{
1003 if (e) {
1004 e->lineno = LINENO(n);
1005 e->col_offset = n->n_col_offset;
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001006 e->end_lineno = end->n_end_lineno;
1007 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001008 }
1009 return e;
1010}
1011
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001012static const char *
1013get_expr_name(expr_ty e)
1014{
1015 switch (e->kind) {
1016 case Attribute_kind:
1017 return "attribute";
1018 case Subscript_kind:
1019 return "subscript";
1020 case Starred_kind:
1021 return "starred";
1022 case Name_kind:
1023 return "name";
1024 case List_kind:
1025 return "list";
1026 case Tuple_kind:
1027 return "tuple";
1028 case Lambda_kind:
1029 return "lambda";
1030 case Call_kind:
1031 return "function call";
1032 case BoolOp_kind:
1033 case BinOp_kind:
1034 case UnaryOp_kind:
1035 return "operator";
1036 case GeneratorExp_kind:
1037 return "generator expression";
1038 case Yield_kind:
1039 case YieldFrom_kind:
1040 return "yield expression";
1041 case Await_kind:
1042 return "await expression";
1043 case ListComp_kind:
1044 return "list comprehension";
1045 case SetComp_kind:
1046 return "set comprehension";
1047 case DictComp_kind:
1048 return "dict comprehension";
1049 case Dict_kind:
1050 return "dict display";
1051 case Set_kind:
1052 return "set display";
1053 case JoinedStr_kind:
1054 case FormattedValue_kind:
1055 return "f-string expression";
1056 case Constant_kind: {
1057 PyObject *value = e->v.Constant.value;
1058 if (value == Py_None) {
1059 return "None";
1060 }
1061 if (value == Py_False) {
1062 return "False";
1063 }
1064 if (value == Py_True) {
1065 return "True";
1066 }
1067 if (value == Py_Ellipsis) {
1068 return "Ellipsis";
1069 }
1070 return "literal";
1071 }
1072 case Compare_kind:
1073 return "comparison";
1074 case IfExp_kind:
1075 return "conditional expression";
1076 case NamedExpr_kind:
1077 return "named expression";
1078 default:
1079 PyErr_Format(PyExc_SystemError,
1080 "unexpected expression in assignment %d (line %d)",
1081 e->kind, e->lineno);
1082 return NULL;
1083 }
1084}
1085
Jeremy Hyltona8293132006-02-28 17:58:27 +00001086/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087
1088 Only sets context for expr kinds that "can appear in assignment context"
1089 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1090 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091*/
1092
1093static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001094set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095{
1096 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001097
Serhiy Storchaka6b975982020-03-17 23:41:08 +02001098 /* Expressions in an augmented assignment have a Store context. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099
1100 switch (e->kind) {
1101 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001102 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001103 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001104 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001105 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001107 e->v.Subscript.ctx = ctx;
1108 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001109 case Starred_kind:
1110 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001111 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001112 return 0;
1113 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001115 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001116 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001117 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001118 }
1119 e->v.Name.ctx = ctx;
1120 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001122 e->v.List.ctx = ctx;
1123 s = e->v.List.elts;
1124 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001126 e->v.Tuple.ctx = ctx;
1127 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001128 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001129 default: {
1130 const char *expr_name = get_expr_name(e);
1131 if (expr_name != NULL) {
1132 ast_error(c, n, "cannot %s %s",
1133 ctx == Store ? "assign to" : "delete",
1134 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001135 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001136 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001137 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001138 }
1139
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 */
1143 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001144 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145
Thomas Wouters89f507f2006-12-13 04:49:30 +00001146 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001147 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001148 return 0;
1149 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 }
1151 return 1;
1152}
1153
1154static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001155ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156{
1157 REQ(n, augassign);
1158 n = CHILD(n, 0);
1159 switch (STR(n)[0]) {
1160 case '+':
1161 return Add;
1162 case '-':
1163 return Sub;
1164 case '/':
1165 if (STR(n)[1] == '/')
1166 return FloorDiv;
1167 else
1168 return Div;
1169 case '%':
1170 return Mod;
1171 case '<':
1172 return LShift;
1173 case '>':
1174 return RShift;
1175 case '&':
1176 return BitAnd;
1177 case '^':
1178 return BitXor;
1179 case '|':
1180 return BitOr;
1181 case '*':
1182 if (STR(n)[1] == '*')
1183 return Pow;
1184 else
1185 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001186 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001187 if (c->c_feature_version < 5) {
1188 ast_error(c, n,
1189 "The '@' operator is only supported in Python 3.5 and greater");
1190 return (operator_ty)0;
1191 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001192 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001194 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001195 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 }
1197}
1198
1199static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001200ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001202 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 |'is' 'not'
1204 */
1205 REQ(n, comp_op);
1206 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001207 n = CHILD(n, 0);
1208 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 case LESS:
1210 return Lt;
1211 case GREATER:
1212 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001213 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 return Eq;
1215 case LESSEQUAL:
1216 return LtE;
1217 case GREATEREQUAL:
1218 return GtE;
1219 case NOTEQUAL:
1220 return NotEq;
1221 case NAME:
1222 if (strcmp(STR(n), "in") == 0)
1223 return In;
1224 if (strcmp(STR(n), "is") == 0)
1225 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001226 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001228 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001230 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001231 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 }
1233 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001234 /* handle "not in" and "is not" */
1235 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 case NAME:
1237 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1238 return NotIn;
1239 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1240 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001241 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001243 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001245 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 }
Neal Norwitz79792652005-11-14 04:25:03 +00001248 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251}
1252
1253static asdl_seq *
1254seq_for_testlist(struct compiling *c, const node *n)
1255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001257 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1258 */
Armin Rigo31441302005-10-21 12:57:31 +00001259 asdl_seq *seq;
1260 expr_ty expression;
1261 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001262 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001264 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 if (!seq)
1266 return NULL;
1267
1268 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001270 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271
Benjamin Peterson4905e802009-09-27 02:43:28 +00001272 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001273 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275
1276 assert(i / 2 < seq->size);
1277 asdl_seq_SET(seq, i / 2, expression);
1278 }
1279 return seq;
1280}
1281
Neal Norwitzc1505362006-12-28 06:47:50 +00001282static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001283ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001284{
1285 identifier name;
1286 expr_ty annotation = NULL;
1287 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001288 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001289
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001290 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001292 name = NEW_IDENTIFIER(ch);
1293 if (!name)
1294 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001295 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001296 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001297
1298 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1299 annotation = ast_for_expr(c, CHILD(n, 2));
1300 if (!annotation)
1301 return NULL;
1302 }
1303
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001304 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001305 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001306 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001307 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001308 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309}
1310
Guido van Rossum4f72a782006-10-27 23:31:49 +00001311/* returns -1 if failed to handle keyword only arguments
1312 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001313 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001314 ^^^
1315 start pointing here
1316 */
1317static int
1318handle_keywordonly_args(struct compiling *c, const node *n, int start,
1319 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1320{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001321 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001322 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001323 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001324 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001325 int i = start;
1326 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001327
1328 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001329 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001330 return -1;
1331 }
1332 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001333 while (i < NCH(n)) {
1334 ch = CHILD(n, i);
1335 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001336 case vfpdef:
1337 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001339 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001340 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001341 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001342 asdl_seq_SET(kwdefaults, j, expression);
1343 i += 2; /* '=' and test */
1344 }
1345 else { /* setting NULL if no default value exists */
1346 asdl_seq_SET(kwdefaults, j, NULL);
1347 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001348 if (NCH(ch) == 3) {
1349 /* ch is NAME ':' test */
1350 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001351 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001352 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001353 }
1354 else {
1355 annotation = NULL;
1356 }
1357 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001358 argname = NEW_IDENTIFIER(ch);
1359 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001360 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001361 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001362 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001363 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001364 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001365 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001366 if (!arg)
1367 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001368 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001369 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001370 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001371 i += 1; /* the comma, if present */
1372 break;
1373 case TYPE_COMMENT:
1374 /* arg will be equal to the last argument processed */
1375 arg->type_comment = NEW_TYPE_COMMENT(ch);
1376 if (!arg->type_comment)
1377 goto error;
1378 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001379 break;
1380 case DOUBLESTAR:
1381 return i;
1382 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001383 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 goto error;
1385 }
1386 }
1387 return i;
1388 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001390}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391
Jeremy Hyltona8293132006-02-28 17:58:27 +00001392/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393
1394static arguments_ty
1395ast_for_arguments(struct compiling *c, const node *n)
1396{
Neal Norwitzc1505362006-12-28 06:47:50 +00001397 /* This function handles both typedargslist (function definition)
1398 and varargslist (lambda definition).
1399
1400 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001401
1402 The following definition for typedarglist is equivalent to this set of rules:
1403
1404 arguments = argument (',' [TYPE_COMMENT] argument)*
1405 argument = tfpdef ['=' test]
1406 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1407 args = '*' [tfpdef]
1408 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1409 [TYPE_COMMENT] [kwargs]])
1410 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1411 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1412 [TYPE_COMMENT] [args_kwonly_kwargs]])
1413 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1414 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1415 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1416
1417 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1418 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1419 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1420 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1421 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1422 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1423 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1424 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1425 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1426 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1427 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1428 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1429 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1430 '**' tfpdef [','] [TYPE_COMMENT]))
1431
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001432 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001433
1434 The following definition for varargslist is equivalent to this set of rules:
1435
1436 arguments = argument (',' argument )*
1437 argument = vfpdef ['=' test]
1438 kwargs = '**' vfpdef [',']
1439 args = '*' [vfpdef]
1440 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1441 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1442 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1443 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1444 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1445 (vararglist_no_posonly)
1446
1447 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1448 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1449 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1450 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1451 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1452 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1453 [',']]] | '**' vfpdef [','])
1454
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001455 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001456
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001458 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001459 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001460 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001461 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001462 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 node *ch;
1464
1465 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001466 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001467 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001468 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001470 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471
Jeremy Hyltone921e022008-07-17 16:37:17 +00001472 /* First count the number of positional args & defaults. The
1473 variable i is the loop index for this for loop and the next.
1474 The next loop picks up where the first leaves off.
1475 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001477 ch = CHILD(n, i);
1478 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001479 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001480 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001481 if (i < NCH(n) && /* skip argument following star */
1482 (TYPE(CHILD(n, i)) == tfpdef ||
1483 TYPE(CHILD(n, i)) == vfpdef)) {
1484 i++;
1485 }
1486 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001487 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001488 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001489 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001490 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001491 if (TYPE(ch) == SLASH ) {
1492 nposonlyargs = nposargs;
1493 nposargs = 0;
1494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001497 defaults for keyword only args */
1498 for ( ; i < NCH(n); ++i) {
1499 ch = CHILD(n, i);
1500 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001501 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001502 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001503 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1504 if (!posonlyargs && nposonlyargs) {
1505 return NULL;
1506 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001507 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001508 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001509 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001510 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001511 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001512 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001513 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001515 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001516 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001517 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001519 since we set NULL as default for keyword only argument w/o default
1520 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001521 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001522 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001523 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001524 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001525
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001526 /* tfpdef: NAME [':' test]
1527 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 */
1529 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001530 j = 0; /* index for defaults */
1531 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001532 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001534 ch = CHILD(n, i);
1535 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001536 case tfpdef:
1537 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1539 anything other than EQUAL or a comma? */
1540 /* XXX Should NCH(n) check be made a separate check? */
1541 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001542 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1543 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001544 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001545 assert(posdefaults != NULL);
1546 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001548 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001550 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001551 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001552 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001553 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001554 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001555 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001556 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001557 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001558 if (l < nposonlyargs) {
1559 asdl_seq_SET(posonlyargs, l++, arg);
1560 } else {
1561 asdl_seq_SET(posargs, k++, arg);
1562 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001563 i += 1; /* the name */
1564 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1565 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001567 case SLASH:
1568 /* Advance the slash and the comma. If there are more names
1569 * after the slash there will be a comma so we are advancing
1570 * the correct number of nodes. If the slash is the last item,
1571 * we will be advancing an extra token but then * i > NCH(n)
1572 * and the enclosing while will finish correctly. */
1573 i += 2;
1574 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001576 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001577 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1578 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001579 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001580 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001581 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001582 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001583 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001584 if (TYPE(ch) == COMMA) {
1585 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001586 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001587
1588 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1589 ast_error(c, CHILD(n, i),
1590 "bare * has associated type comment");
1591 return NULL;
1592 }
1593
Guido van Rossum4f72a782006-10-27 23:31:49 +00001594 res = handle_keywordonly_args(c, n, i,
1595 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001596 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001597 i = res; /* res has new position to process */
1598 }
1599 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001600 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001601 if (!vararg)
1602 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001603
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001604 i += 2; /* the star and the name */
1605 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1606 i += 1; /* the comma, if present */
1607
1608 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1609 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1610 if (!vararg->type_comment)
1611 return NULL;
1612 i += 1;
1613 }
1614
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001615 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1616 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001617 int res = 0;
1618 res = handle_keywordonly_args(c, n, i,
1619 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001620 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001621 i = res; /* res has new position to process */
1622 }
1623 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624 break;
1625 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001626 ch = CHILD(n, i+1); /* tfpdef */
1627 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001628 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001629 if (!kwarg)
1630 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001631 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001632 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001633 i += 1; /* the comma, if present */
1634 break;
1635 case TYPE_COMMENT:
1636 assert(i);
1637
1638 if (kwarg)
1639 arg = kwarg;
1640
1641 /* arg will be equal to the last argument processed */
1642 arg->type_comment = NEW_TYPE_COMMENT(ch);
1643 if (!arg->type_comment)
1644 return NULL;
1645 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 break;
1647 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001648 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649 "unexpected node in varargslist: %d @ %d",
1650 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001651 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001652 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001654 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655}
1656
1657static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658ast_for_decorator(struct compiling *c, const node *n)
1659{
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001660 /* decorator: '@' namedexpr_test NEWLINE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001663 REQ(CHILD(n, 0), AT);
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001664 REQ(CHILD(n, 2), NEWLINE);
1665
1666 return ast_for_expr(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667}
1668
1669static asdl_seq*
1670ast_for_decorators(struct compiling *c, const node *n)
1671{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001672 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001673 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001677 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 if (!decorator_seq)
1679 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001682 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001683 if (!d)
1684 return NULL;
1685 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 }
1687 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688}
1689
1690static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001691ast_for_funcdef_impl(struct compiling *c, const node *n0,
1692 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001694 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001695 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001696 identifier name;
1697 arguments_ty args;
1698 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001699 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001700 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001701 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001702 node *tc;
1703 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704
Guido van Rossum495da292019-03-07 12:38:08 -08001705 if (is_async && c->c_feature_version < 5) {
1706 ast_error(c, n,
1707 "Async functions are only supported in Python 3.5 and greater");
1708 return NULL;
1709 }
1710
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 REQ(n, funcdef);
1712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 name = NEW_IDENTIFIER(CHILD(n, name_i));
1714 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001715 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001716 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001717 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1719 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001720 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001721 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1722 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1723 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001724 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001725 name_i += 2;
1726 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001727 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1728 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1729 if (!type_comment)
1730 return NULL;
1731 name_i += 1;
1732 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001733 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001735 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001736 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001738 if (NCH(CHILD(n, name_i + 3)) > 1) {
1739 /* Check if the suite has a type comment in it. */
1740 tc = CHILD(CHILD(n, name_i + 3), 1);
1741
1742 if (TYPE(tc) == TYPE_COMMENT) {
1743 if (type_comment != NULL) {
1744 ast_error(c, n, "Cannot have two type comments on def");
1745 return NULL;
1746 }
1747 type_comment = NEW_TYPE_COMMENT(tc);
1748 if (!type_comment)
1749 return NULL;
1750 }
1751 }
1752
Yury Selivanov75445082015-05-11 22:57:16 -04001753 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001754 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001755 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001756 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001757 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001758 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001759}
1760
1761static stmt_ty
1762ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1763{
Guido van Rossum495da292019-03-07 12:38:08 -08001764 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001765 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001766 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001767 REQ(CHILD(n, 1), funcdef);
1768
guoci90fc8982018-09-11 17:45:45 -04001769 return ast_for_funcdef_impl(c, n, decorator_seq,
1770 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001771}
1772
1773static stmt_ty
1774ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1775{
1776 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1777 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001778 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001779}
1780
1781
1782static stmt_ty
1783ast_for_async_stmt(struct compiling *c, const node *n)
1784{
Guido van Rossum495da292019-03-07 12:38:08 -08001785 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001786 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001787 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001788
1789 switch (TYPE(CHILD(n, 1))) {
1790 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001791 return ast_for_funcdef_impl(c, n, NULL,
1792 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001793 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001794 return ast_for_with_stmt(c, n,
1795 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001796
1797 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001798 return ast_for_for_stmt(c, n,
1799 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001800
1801 default:
1802 PyErr_Format(PyExc_SystemError,
1803 "invalid async stament: %s",
1804 STR(CHILD(n, 1)));
1805 return NULL;
1806 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807}
1808
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001809static stmt_ty
1810ast_for_decorated(struct compiling *c, const node *n)
1811{
Yury Selivanov75445082015-05-11 22:57:16 -04001812 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001813 stmt_ty thing = NULL;
1814 asdl_seq *decorator_seq = NULL;
1815
1816 REQ(n, decorated);
1817
1818 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1819 if (!decorator_seq)
1820 return NULL;
1821
1822 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001823 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001825
1826 if (TYPE(CHILD(n, 1)) == funcdef) {
1827 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1828 } else if (TYPE(CHILD(n, 1)) == classdef) {
1829 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001830 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1831 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001832 }
1833 return thing;
1834}
1835
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001837ast_for_namedexpr(struct compiling *c, const node *n)
1838{
Guido van Rossumb08d3f72019-12-15 10:00:33 -08001839 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001840 argument: ( test [comp_for] |
1841 test ':=' test |
1842 test '=' test |
1843 '**' test |
1844 '*' test )
1845 */
1846 expr_ty target, value;
1847
1848 target = ast_for_expr(c, CHILD(n, 0));
1849 if (!target)
1850 return NULL;
1851
1852 value = ast_for_expr(c, CHILD(n, 2));
1853 if (!value)
1854 return NULL;
1855
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001856 if (target->kind != Name_kind) {
1857 const char *expr_name = get_expr_name(target);
1858 if (expr_name != NULL) {
Ned Batchelder37143a82019-12-31 21:40:58 -05001859 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001860 }
1861 return NULL;
1862 }
1863
1864 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001865 return NULL;
1866
1867 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1868 n->n_end_col_offset, c->c_arena);
1869}
1870
1871static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872ast_for_lambdef(struct compiling *c, const node *n)
1873{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001874 /* lambdef: 'lambda' [varargslist] ':' test
1875 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 arguments_ty args;
1877 expr_ty expression;
1878
1879 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001880 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 if (!args)
1882 return NULL;
1883 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 }
1887 else {
1888 args = ast_for_arguments(c, CHILD(n, 1));
1889 if (!args)
1890 return NULL;
1891 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 }
1895
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001896 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1897 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898}
1899
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001900static expr_ty
1901ast_for_ifexpr(struct compiling *c, const node *n)
1902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001904 expr_ty expression, body, orelse;
1905
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001906 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001907 body = ast_for_expr(c, CHILD(n, 0));
1908 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001909 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001910 expression = ast_for_expr(c, CHILD(n, 2));
1911 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001912 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001913 orelse = ast_for_expr(c, CHILD(n, 4));
1914 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001915 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001916 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001917 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001918 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001919}
1920
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001922 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923
Nick Coghlan650f0d02007-04-15 12:05:43 +00001924 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925*/
1926
1927static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001928count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001930 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931
Guido van Rossumd8faa362007-04-27 19:54:29 +00001932 count_comp_for:
1933 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001934 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001935 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001936 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001937 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001938 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001939 else if (NCH(n) == 1) {
1940 n = CHILD(n, 0);
1941 }
1942 else {
1943 goto error;
1944 }
1945 if (NCH(n) == (5)) {
1946 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001947 }
1948 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001949 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001950 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001951 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001952 REQ(n, comp_iter);
1953 n = CHILD(n, 0);
1954 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001955 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001956 else if (TYPE(n) == comp_if) {
1957 if (NCH(n) == 3) {
1958 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001959 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001960 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001961 else
1962 return n_fors;
1963 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001964
Jelle Zijlstraac317702017-10-05 20:24:46 -07001965 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001966 /* Should never be reached */
1967 PyErr_SetString(PyExc_SystemError,
1968 "logic error in count_comp_fors");
1969 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970}
1971
Nick Coghlan650f0d02007-04-15 12:05:43 +00001972/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973
Nick Coghlan650f0d02007-04-15 12:05:43 +00001974 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975*/
1976
1977static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001978count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001980 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981
Guido van Rossumd8faa362007-04-27 19:54:29 +00001982 while (1) {
1983 REQ(n, comp_iter);
1984 if (TYPE(CHILD(n, 0)) == comp_for)
1985 return n_ifs;
1986 n = CHILD(n, 0);
1987 REQ(n, comp_if);
1988 n_ifs++;
1989 if (NCH(n) == 2)
1990 return n_ifs;
1991 n = CHILD(n, 2);
1992 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993}
1994
Guido van Rossum992d4a32007-07-11 13:09:30 +00001995static asdl_seq *
1996ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001999 asdl_seq *comps;
2000
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002001 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 if (n_fors == -1)
2003 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002004
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002005 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002006 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002008
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002010 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002012 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002013 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002014 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002015 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016
Guido van Rossum992d4a32007-07-11 13:09:30 +00002017 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018
Jelle Zijlstraac317702017-10-05 20:24:46 -07002019 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002020 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002021 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002022 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002023 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002024 else {
2025 sync_n = CHILD(n, 0);
2026 }
2027 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002028
Guido van Rossum495da292019-03-07 12:38:08 -08002029 /* Async comprehensions only allowed in Python 3.6 and greater */
2030 if (is_async && c->c_feature_version < 6) {
2031 ast_error(c, n,
2032 "Async comprehensions are only supported in Python 3.6 and greater");
2033 return NULL;
2034 }
2035
Jelle Zijlstraac317702017-10-05 20:24:46 -07002036 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002037 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002038 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002040 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002041 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002043
Thomas Wouters89f507f2006-12-13 04:49:30 +00002044 /* Check the # of children rather than the length of t, since
2045 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002046 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002047 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002048 comp = comprehension(first, expression, NULL,
2049 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002051 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2052 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2053 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002054 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002055 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002057
Jelle Zijlstraac317702017-10-05 20:24:46 -07002058 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 int j, n_ifs;
2060 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061
Jelle Zijlstraac317702017-10-05 20:24:46 -07002062 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002063 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002064 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002066
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002067 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002068 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002072 REQ(n, comp_iter);
2073 n = CHILD(n, 0);
2074 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075
Guido van Rossum992d4a32007-07-11 13:09:30 +00002076 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002077 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002078 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002079 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002080 if (NCH(n) == 3)
2081 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002083 /* on exit, must guarantee that n is a comp_for */
2084 if (TYPE(n) == comp_iter)
2085 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002086 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002088 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002090 return comps;
2091}
2092
2093static expr_ty
2094ast_for_itercomp(struct compiling *c, const node *n, int type)
2095{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002096 /* testlist_comp: (test|star_expr)
2097 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002098 expr_ty elt;
2099 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002100 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101
Guido van Rossum992d4a32007-07-11 13:09:30 +00002102 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002104 ch = CHILD(n, 0);
2105 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002106 if (!elt)
2107 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002108 if (elt->kind == Starred_kind) {
2109 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2110 return NULL;
2111 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112
Guido van Rossum992d4a32007-07-11 13:09:30 +00002113 comps = ast_for_comprehension(c, CHILD(n, 1));
2114 if (!comps)
2115 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002116
2117 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002118 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2119 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002120 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002121 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2122 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002123 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002124 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2125 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002126 else
2127 /* Should never happen */
2128 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129}
2130
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002131/* Fills in the key, value pair corresponding to the dict element. In case
2132 * of an unpacking, key is NULL. *i is advanced by the number of ast
2133 * elements. Iff successful, nonzero is returned.
2134 */
2135static int
2136ast_for_dictelement(struct compiling *c, const node *n, int *i,
2137 expr_ty *key, expr_ty *value)
2138{
2139 expr_ty expression;
2140 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2141 assert(NCH(n) - *i >= 2);
2142
2143 expression = ast_for_expr(c, CHILD(n, *i + 1));
2144 if (!expression)
2145 return 0;
2146 *key = NULL;
2147 *value = expression;
2148
2149 *i += 2;
2150 }
2151 else {
2152 assert(NCH(n) - *i >= 3);
2153
2154 expression = ast_for_expr(c, CHILD(n, *i));
2155 if (!expression)
2156 return 0;
2157 *key = expression;
2158
2159 REQ(CHILD(n, *i + 1), COLON);
2160
2161 expression = ast_for_expr(c, CHILD(n, *i + 2));
2162 if (!expression)
2163 return 0;
2164 *value = expression;
2165
2166 *i += 3;
2167 }
2168 return 1;
2169}
2170
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002172ast_for_dictcomp(struct compiling *c, const node *n)
2173{
2174 expr_ty key, value;
2175 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002176 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002178 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002179 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002180 assert(key);
2181 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002183 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002184 if (!comps)
2185 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002187 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2188 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002189}
2190
2191static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002192ast_for_dictdisplay(struct compiling *c, const node *n)
2193{
2194 int i;
2195 int j;
2196 int size;
2197 asdl_seq *keys, *values;
2198
2199 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2200 keys = _Py_asdl_seq_new(size, c->c_arena);
2201 if (!keys)
2202 return NULL;
2203
2204 values = _Py_asdl_seq_new(size, c->c_arena);
2205 if (!values)
2206 return NULL;
2207
2208 j = 0;
2209 for (i = 0; i < NCH(n); i++) {
2210 expr_ty key, value;
2211
2212 if (!ast_for_dictelement(c, n, &i, &key, &value))
2213 return NULL;
2214 asdl_seq_SET(keys, j, key);
2215 asdl_seq_SET(values, j, value);
2216
2217 j++;
2218 }
2219 keys->size = j;
2220 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002221 return Dict(keys, values, LINENO(n), n->n_col_offset,
2222 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002223}
2224
2225static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002226ast_for_genexp(struct compiling *c, const node *n)
2227{
2228 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002229 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002230}
2231
2232static expr_ty
2233ast_for_listcomp(struct compiling *c, const node *n)
2234{
2235 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002236 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002237}
2238
2239static expr_ty
2240ast_for_setcomp(struct compiling *c, const node *n)
2241{
2242 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002243 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002244}
2245
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002246static expr_ty
2247ast_for_setdisplay(struct compiling *c, const node *n)
2248{
2249 int i;
2250 int size;
2251 asdl_seq *elts;
2252
2253 assert(TYPE(n) == (dictorsetmaker));
2254 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2255 elts = _Py_asdl_seq_new(size, c->c_arena);
2256 if (!elts)
2257 return NULL;
2258 for (i = 0; i < NCH(n); i += 2) {
2259 expr_ty expression;
2260 expression = ast_for_expr(c, CHILD(n, i));
2261 if (!expression)
2262 return NULL;
2263 asdl_seq_SET(elts, i / 2, expression);
2264 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002265 return Set(elts, LINENO(n), n->n_col_offset,
2266 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002267}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002268
2269static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270ast_for_atom(struct compiling *c, const node *n)
2271{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002272 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2273 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002274 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 */
2276 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002279 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002280 PyObject *name;
2281 const char *s = STR(ch);
2282 size_t len = strlen(s);
2283 if (len >= 4 && len <= 5) {
2284 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002285 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002286 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002287 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002288 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002289 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002290 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002291 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002292 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002293 }
2294 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002295 if (!name)
2296 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002297 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002298 return Name(name, Load, LINENO(n), n->n_col_offset,
2299 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002302 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002303 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002304 const char *errtype = NULL;
2305 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2306 errtype = "unicode error";
2307 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2308 errtype = "value error";
2309 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002310 PyObject *type, *value, *tback, *errstr;
2311 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002312 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002313 if (errstr) {
2314 ast_error(c, n, "(%s) %U", errtype, errstr);
2315 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002316 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002317 else {
2318 PyErr_Clear();
2319 ast_error(c, n, "(%s) unknown error", errtype);
2320 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002321 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002322 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002323 Py_XDECREF(tback);
2324 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002325 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002326 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002327 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 }
2329 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002330 PyObject *pynum;
2331 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2332 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2333 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2334 ast_error(c, ch,
2335 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2336 return NULL;
2337 }
2338 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002339 if (!pynum)
2340 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002341
Victor Stinner43d81952013-07-17 00:57:58 +02002342 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2343 Py_DECREF(pynum);
2344 return NULL;
2345 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002346 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002347 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 }
Georg Brandldde00282007-03-18 19:01:53 +00002349 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002350 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002351 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002353 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354
Thomas Wouters89f507f2006-12-13 04:49:30 +00002355 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002356 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2357 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358
Thomas Wouters89f507f2006-12-13 04:49:30 +00002359 if (TYPE(ch) == yield_expr)
2360 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002363 if (NCH(ch) == 1) {
2364 return ast_for_testlist(c, ch);
2365 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002366
Serhiy Storchakab619b092018-11-27 09:40:29 +02002367 if (TYPE(CHILD(ch, 1)) == comp_for) {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002368 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002369 }
2370 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002371 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002372 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002374 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375
Thomas Wouters89f507f2006-12-13 04:49:30 +00002376 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002377 return List(NULL, Load, LINENO(n), n->n_col_offset,
2378 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379
Nick Coghlan650f0d02007-04-15 12:05:43 +00002380 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002381 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2382 asdl_seq *elts = seq_for_testlist(c, ch);
2383 if (!elts)
2384 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002385
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002386 return List(elts, Load, LINENO(n), n->n_col_offset,
2387 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002388 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002389 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002390 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002391 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002393 /* dictorsetmaker: ( ((test ':' test | '**' test)
2394 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2395 * ((test | '*' test)
2396 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002397 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002398 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002399 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002400 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002401 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2402 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002403 }
2404 else {
2405 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2406 if (NCH(ch) == 1 ||
2407 (NCH(ch) > 1 &&
2408 TYPE(CHILD(ch, 1)) == COMMA)) {
2409 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002410 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002411 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002412 else if (NCH(ch) > 1 &&
2413 TYPE(CHILD(ch, 1)) == comp_for) {
2414 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002415 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002416 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002417 else if (NCH(ch) > 3 - is_dict &&
2418 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2419 /* It's a dictionary comprehension. */
2420 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002421 ast_error(c, n,
2422 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002423 return NULL;
2424 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002425 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002426 }
2427 else {
2428 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002429 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002430 }
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002431 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002435 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2436 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 }
2438}
2439
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002440static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441ast_for_slice(struct compiling *c, const node *n)
2442{
2443 node *ch;
2444 expr_ty lower = NULL, upper = NULL, step = NULL;
2445
2446 REQ(n, subscript);
2447
2448 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002449 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 sliceop: ':' [test]
2451 */
2452 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 if (NCH(n) == 1 && TYPE(ch) == test) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002454 return ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 }
2456
2457 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002458 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 if (!lower)
2460 return NULL;
2461 }
2462
2463 /* If there's an upper bound it's in the second or third position. */
2464 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002465 if (NCH(n) > 1) {
2466 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467
Thomas Wouters89f507f2006-12-13 04:49:30 +00002468 if (TYPE(n2) == test) {
2469 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 if (!upper)
2471 return NULL;
2472 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002473 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002475 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476
Thomas Wouters89f507f2006-12-13 04:49:30 +00002477 if (TYPE(n2) == test) {
2478 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 if (!upper)
2480 return NULL;
2481 }
2482 }
2483
2484 ch = CHILD(n, NCH(n) - 1);
2485 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002486 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002487 ch = CHILD(ch, 1);
2488 if (TYPE(ch) == test) {
2489 step = ast_for_expr(c, ch);
2490 if (!step)
2491 return NULL;
2492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 }
2494 }
2495
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002496 return Slice(lower, upper, step, LINENO(n), n->n_col_offset,
2497 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498}
2499
2500static expr_ty
2501ast_for_binop(struct compiling *c, const node *n)
2502{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002503 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002505 BinOp(BinOp(A, op, B), op, C).
2506 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507
Guido van Rossumd8faa362007-04-27 19:54:29 +00002508 int i, nops;
2509 expr_ty expr1, expr2, result;
2510 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511
Guido van Rossumd8faa362007-04-27 19:54:29 +00002512 expr1 = ast_for_expr(c, CHILD(n, 0));
2513 if (!expr1)
2514 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515
Guido van Rossumd8faa362007-04-27 19:54:29 +00002516 expr2 = ast_for_expr(c, CHILD(n, 2));
2517 if (!expr2)
2518 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519
Guido van Rossum495da292019-03-07 12:38:08 -08002520 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002521 if (!newoperator)
2522 return NULL;
2523
2524 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002525 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002526 c->c_arena);
2527 if (!result)
2528 return NULL;
2529
2530 nops = (NCH(n) - 1) / 2;
2531 for (i = 1; i < nops; i++) {
2532 expr_ty tmp_result, tmp;
2533 const node* next_oper = CHILD(n, i * 2 + 1);
2534
Guido van Rossum495da292019-03-07 12:38:08 -08002535 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002536 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 return NULL;
2538
Guido van Rossumd8faa362007-04-27 19:54:29 +00002539 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2540 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 return NULL;
2542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002544 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002545 CHILD(n, i * 2 + 2)->n_end_lineno,
2546 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002547 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002549 return NULL;
2550 result = tmp_result;
2551 }
2552 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553}
2554
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002555static expr_ty
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002556ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002559 subscriptlist: subscript (',' subscript)* [',']
2560 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2561 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002562 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002563 REQ(n, trailer);
2564 if (TYPE(CHILD(n, 0)) == LPAR) {
2565 if (NCH(n) == 2)
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002566 return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002567 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002568 else
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002569 return ast_for_call(c, CHILD(n, 1), left_expr,
2570 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002571 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002572 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002573 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2574 if (!attr_id)
2575 return NULL;
2576 return Attribute(left_expr, attr_id, Load,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002577 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002578 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002579 }
2580 else {
2581 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002582 REQ(CHILD(n, 2), RSQB);
2583 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002584 if (NCH(n) == 1) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002585 expr_ty slc = ast_for_slice(c, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002586 if (!slc)
2587 return NULL;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002588 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002589 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002590 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002591 }
2592 else {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002593 int j;
2594 expr_ty slc, e;
2595 asdl_seq *elts;
2596 elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2597 if (!elts)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002598 return NULL;
2599 for (j = 0; j < NCH(n); j += 2) {
2600 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002601 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002602 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002603 asdl_seq_SET(elts, j / 2, slc);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002604 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002605 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002606 n->n_end_lineno, n->n_end_col_offset,
2607 c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002608 if (!e)
2609 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002610 return Subscript(left_expr, e,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002611 Load, LINENO(start), start->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002612 n_copy->n_end_lineno, n_copy->n_end_col_offset,
2613 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002614 }
2615 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002616}
2617
2618static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002619ast_for_factor(struct compiling *c, const node *n)
2620{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002621 expr_ty expression;
2622
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002623 expression = ast_for_expr(c, CHILD(n, 1));
2624 if (!expression)
2625 return NULL;
2626
2627 switch (TYPE(CHILD(n, 0))) {
2628 case PLUS:
2629 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002630 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002631 c->c_arena);
2632 case MINUS:
2633 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002634 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002635 c->c_arena);
2636 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002637 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2638 n->n_end_lineno, n->n_end_col_offset,
2639 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002640 }
2641 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2642 TYPE(CHILD(n, 0)));
2643 return NULL;
2644}
2645
2646static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002647ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002648{
Yury Selivanov75445082015-05-11 22:57:16 -04002649 int i, nch, start = 0;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002650 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002651
2652 REQ(n, atom_expr);
2653 nch = NCH(n);
2654
Guido van Rossum495da292019-03-07 12:38:08 -08002655 if (TYPE(CHILD(n, 0)) == AWAIT) {
2656 if (c->c_feature_version < 5) {
2657 ast_error(c, n,
2658 "Await expressions are only supported in Python 3.5 and greater");
2659 return NULL;
2660 }
Yury Selivanov75445082015-05-11 22:57:16 -04002661 start = 1;
2662 assert(nch > 1);
2663 }
2664
2665 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002666 if (!e)
2667 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002668 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002669 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002670 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002671 return Await(e, LINENO(n), n->n_col_offset,
2672 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002673 }
2674
2675 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002676 node *ch = CHILD(n, i);
2677 if (TYPE(ch) != trailer)
2678 break;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002679 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2680 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002681 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002682 }
Yury Selivanov75445082015-05-11 22:57:16 -04002683
2684 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002685 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002686 return Await(e, LINENO(n), n->n_col_offset,
2687 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002688 }
2689 else {
2690 return e;
2691 }
2692}
2693
2694static expr_ty
2695ast_for_power(struct compiling *c, const node *n)
2696{
2697 /* power: atom trailer* ('**' factor)*
2698 */
2699 expr_ty e;
2700 REQ(n, power);
2701 e = ast_for_atom_expr(c, CHILD(n, 0));
2702 if (!e)
2703 return NULL;
2704 if (NCH(n) == 1)
2705 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002706 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2707 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002708 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002709 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002710 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2711 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002712 }
2713 return e;
2714}
2715
Guido van Rossum0368b722007-05-11 16:50:42 +00002716static expr_ty
2717ast_for_starred(struct compiling *c, const node *n)
2718{
2719 expr_ty tmp;
2720 REQ(n, star_expr);
2721
2722 tmp = ast_for_expr(c, CHILD(n, 1));
2723 if (!tmp)
2724 return NULL;
2725
2726 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002727 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2728 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002729}
2730
2731
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732/* Do not name a variable 'expr'! Will cause a compile error.
2733*/
2734
2735static expr_ty
2736ast_for_expr(struct compiling *c, const node *n)
2737{
2738 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002739 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002740 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002741 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 and_test: not_test ('and' not_test)*
2744 not_test: 'not' not_test | comparison
2745 comparison: expr (comp_op expr)*
2746 expr: xor_expr ('|' xor_expr)*
2747 xor_expr: and_expr ('^' and_expr)*
2748 and_expr: shift_expr ('&' shift_expr)*
2749 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2750 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002751 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002753 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002754 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002755 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 */
2757
2758 asdl_seq *seq;
2759 int i;
2760
2761 loop:
2762 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002763 case namedexpr_test:
2764 if (NCH(n) == 3)
2765 return ast_for_namedexpr(c, n);
2766 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002768 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002769 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002770 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002772 else if (NCH(n) > 1)
2773 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002774 /* Fallthrough */
2775 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 case and_test:
2777 if (NCH(n) == 1) {
2778 n = CHILD(n, 0);
2779 goto loop;
2780 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002781 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 if (!seq)
2783 return NULL;
2784 for (i = 0; i < NCH(n); i += 2) {
2785 expr_ty e = ast_for_expr(c, CHILD(n, i));
2786 if (!e)
2787 return NULL;
2788 asdl_seq_SET(seq, i / 2, e);
2789 }
2790 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002791 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002792 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002793 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002794 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002795 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2796 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 case not_test:
2798 if (NCH(n) == 1) {
2799 n = CHILD(n, 0);
2800 goto loop;
2801 }
2802 else {
2803 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2804 if (!expression)
2805 return NULL;
2806
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002807 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002808 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002809 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 }
2811 case comparison:
2812 if (NCH(n) == 1) {
2813 n = CHILD(n, 0);
2814 goto loop;
2815 }
2816 else {
2817 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002818 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002819 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002820 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 if (!ops)
2822 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002823 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 return NULL;
2826 }
2827 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002828 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002830 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002831 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002833 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
2835 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002836 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002838 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002840 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 asdl_seq_SET(cmps, i / 2, expression);
2842 }
2843 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002844 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002846 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002848 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2849 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851
Guido van Rossum0368b722007-05-11 16:50:42 +00002852 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 /* The next five cases all handle BinOps. The main body of code
2855 is the same in each case, but the switch turned inside out to
2856 reuse the code for each type of operator.
2857 */
2858 case expr:
2859 case xor_expr:
2860 case and_expr:
2861 case shift_expr:
2862 case arith_expr:
2863 case term:
2864 if (NCH(n) == 1) {
2865 n = CHILD(n, 0);
2866 goto loop;
2867 }
2868 return ast_for_binop(c, n);
2869 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002870 node *an = NULL;
2871 node *en = NULL;
2872 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002873 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002874 if (NCH(n) > 1)
2875 an = CHILD(n, 1); /* yield_arg */
2876 if (an) {
2877 en = CHILD(an, NCH(an) - 1);
2878 if (NCH(an) == 2) {
2879 is_from = 1;
2880 exp = ast_for_expr(c, en);
2881 }
2882 else
2883 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 if (!exp)
2885 return NULL;
2886 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002887 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002888 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2889 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2890 return Yield(exp, LINENO(n), n->n_col_offset,
2891 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002892 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002893 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 if (NCH(n) == 1) {
2895 n = CHILD(n, 0);
2896 goto loop;
2897 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002898 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002899 case power:
2900 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002902 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 return NULL;
2904 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002905 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 return NULL;
2907}
2908
2909static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002910ast_for_call(struct compiling *c, const node *n, expr_ty func,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002911 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912{
2913 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002914 arglist: argument (',' argument)* [',']
2915 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 */
2917
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002918 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002919 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002920 asdl_seq *args;
2921 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922
2923 REQ(n, arglist);
2924
2925 nargs = 0;
2926 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002928 node *ch = CHILD(n, i);
2929 if (TYPE(ch) == argument) {
2930 if (NCH(ch) == 1)
2931 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002932 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2933 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002934 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002935 ast_error(c, ch, "invalid syntax");
2936 return NULL;
2937 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002938 if (NCH(n) > 1) {
2939 ast_error(c, ch, "Generator expression must be parenthesized");
2940 return NULL;
2941 }
2942 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002943 else if (TYPE(CHILD(ch, 0)) == STAR)
2944 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002945 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2946 nargs++;
2947 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002949 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002950 nkeywords++;
2951 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002954 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002956 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002957 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002959 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002960
2961 nargs = 0; /* positional arguments + iterable argument unpackings */
2962 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2963 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002965 node *ch = CHILD(n, i);
2966 if (TYPE(ch) == argument) {
2967 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002968 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002969 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002970 /* a positional argument */
2971 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002972 if (ndoublestars) {
2973 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002974 "positional argument follows "
2975 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002976 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002977 else {
2978 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002979 "positional argument follows "
2980 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002981 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002982 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002983 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002984 e = ast_for_expr(c, chch);
2985 if (!e)
2986 return NULL;
2987 asdl_seq_SET(args, nargs++, e);
2988 }
2989 else if (TYPE(chch) == STAR) {
2990 /* an iterable argument unpacking */
2991 expr_ty starred;
2992 if (ndoublestars) {
2993 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002994 "iterable argument unpacking follows "
2995 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002996 return NULL;
2997 }
2998 e = ast_for_expr(c, CHILD(ch, 1));
2999 if (!e)
3000 return NULL;
3001 starred = Starred(e, Load, LINENO(chch),
3002 chch->n_col_offset,
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +01003003 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003004 c->c_arena);
3005 if (!starred)
3006 return NULL;
3007 asdl_seq_SET(args, nargs++, starred);
3008
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003009 }
3010 else if (TYPE(chch) == DOUBLESTAR) {
3011 /* a keyword argument unpacking */
3012 keyword_ty kw;
3013 i++;
3014 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003016 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003017 kw = keyword(NULL, e, c->c_arena);
3018 asdl_seq_SET(keywords, nkeywords++, kw);
3019 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003021 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003022 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003023 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003025 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003026 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003028 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3029 /* treat colon equal as positional argument */
3030 if (nkeywords) {
3031 if (ndoublestars) {
3032 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003033 "positional argument follows "
3034 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003035 }
3036 else {
3037 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003038 "positional argument follows "
3039 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003040 }
3041 return NULL;
3042 }
3043 e = ast_for_namedexpr(c, ch);
3044 if (!e)
3045 return NULL;
3046 asdl_seq_SET(args, nargs++, e);
3047 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003048 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003049 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003050 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003051 identifier key, tmp;
3052 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003054 // To remain LL(1), the grammar accepts any test (basically, any
3055 // expression) in the keyword slot of a call site. So, we need
3056 // to manually enforce that the keyword is a NAME here.
3057 static const int name_tree[] = {
3058 test,
3059 or_test,
3060 and_test,
3061 not_test,
3062 comparison,
3063 expr,
3064 xor_expr,
3065 and_expr,
3066 shift_expr,
3067 arith_expr,
3068 term,
3069 factor,
3070 power,
3071 atom_expr,
3072 atom,
3073 0,
3074 };
3075 node *expr_node = chch;
3076 for (int i = 0; name_tree[i]; i++) {
3077 if (TYPE(expr_node) != name_tree[i])
3078 break;
3079 if (NCH(expr_node) != 1)
3080 break;
3081 expr_node = CHILD(expr_node, 0);
3082 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003083 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003084 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003085 "expression cannot contain assignment, "
3086 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003087 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003088 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003089 key = new_identifier(STR(expr_node), c);
3090 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003091 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003093 if (forbidden_name(c, key, chch, 1)) {
3094 return NULL;
3095 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003096 for (k = 0; k < nkeywords; k++) {
3097 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003098 if (tmp && !PyUnicode_Compare(tmp, key)) {
3099 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003100 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003101 return NULL;
3102 }
3103 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003104 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003105 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003106 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003107 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003109 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003110 asdl_seq_SET(keywords, nkeywords++, kw);
3111 }
3112 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 }
3114
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003115 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003116 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117}
3118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003120ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003122 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003123 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003125 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003126 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003127 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003128 }
3129 else {
3130 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003131 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003132 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003134 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 else {
3136 asdl_seq *tmp = seq_for_testlist(c, n);
3137 if (!tmp)
3138 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003139 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3140 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003142}
3143
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144static stmt_ty
3145ast_for_expr_stmt(struct compiling *c, const node *n)
3146{
3147 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003148 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003149 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3150 annassign: ':' test ['=' (yield_expr|testlist)]
3151 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3152 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3153 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003154 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003156 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003158 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003159 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 if (!e)
3161 return NULL;
3162
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003163 return Expr(e, LINENO(n), n->n_col_offset,
3164 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 }
3166 else if (TYPE(CHILD(n, 1)) == augassign) {
3167 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003168 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003169 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170
Thomas Wouters89f507f2006-12-13 04:49:30 +00003171 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 if (!expr1)
3173 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003174 if(!set_context(c, expr1, Store, ch))
3175 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003176 /* set_context checks that most expressions are not the left side.
3177 Augmented assignments can only have a name, a subscript, or an
3178 attribute on the left, though, so we have to explicitly check for
3179 those. */
3180 switch (expr1->kind) {
3181 case Name_kind:
3182 case Attribute_kind:
3183 case Subscript_kind:
3184 break;
3185 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003186 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003187 return NULL;
3188 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189
Thomas Wouters89f507f2006-12-13 04:49:30 +00003190 ch = CHILD(n, 2);
3191 if (TYPE(ch) == testlist)
3192 expr2 = ast_for_testlist(c, ch);
3193 else
3194 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003195 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 return NULL;
3197
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003198 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003199 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 return NULL;
3201
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003202 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3203 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003205 else if (TYPE(CHILD(n, 1)) == annassign) {
3206 expr_ty expr1, expr2, expr3;
3207 node *ch = CHILD(n, 0);
3208 node *deep, *ann = CHILD(n, 1);
3209 int simple = 1;
3210
Guido van Rossum495da292019-03-07 12:38:08 -08003211 /* AnnAssigns are only allowed in Python 3.6 or greater */
3212 if (c->c_feature_version < 6) {
3213 ast_error(c, ch,
3214 "Variable annotation syntax is only supported in Python 3.6 and greater");
3215 return NULL;
3216 }
3217
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003218 /* we keep track of parens to qualify (x) as expression not name */
3219 deep = ch;
3220 while (NCH(deep) == 1) {
3221 deep = CHILD(deep, 0);
3222 }
3223 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3224 simple = 0;
3225 }
3226 expr1 = ast_for_testlist(c, ch);
3227 if (!expr1) {
3228 return NULL;
3229 }
3230 switch (expr1->kind) {
3231 case Name_kind:
3232 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3233 return NULL;
3234 }
3235 expr1->v.Name.ctx = Store;
3236 break;
3237 case Attribute_kind:
3238 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3239 return NULL;
3240 }
3241 expr1->v.Attribute.ctx = Store;
3242 break;
3243 case Subscript_kind:
3244 expr1->v.Subscript.ctx = Store;
3245 break;
3246 case List_kind:
3247 ast_error(c, ch,
3248 "only single target (not list) can be annotated");
3249 return NULL;
3250 case Tuple_kind:
3251 ast_error(c, ch,
3252 "only single target (not tuple) can be annotated");
3253 return NULL;
3254 default:
3255 ast_error(c, ch,
3256 "illegal target for annotation");
3257 return NULL;
3258 }
3259
3260 if (expr1->kind != Name_kind) {
3261 simple = 0;
3262 }
3263 ch = CHILD(ann, 1);
3264 expr2 = ast_for_expr(c, ch);
3265 if (!expr2) {
3266 return NULL;
3267 }
3268 if (NCH(ann) == 2) {
3269 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003270 LINENO(n), n->n_col_offset,
3271 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003272 }
3273 else {
3274 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003275 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003276 expr3 = ast_for_testlist(c, ch);
3277 }
3278 else {
3279 expr3 = ast_for_expr(c, ch);
3280 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003281 if (!expr3) {
3282 return NULL;
3283 }
3284 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003285 LINENO(n), n->n_col_offset,
3286 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003287 }
3288 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003290 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003291 asdl_seq *targets;
3292 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003294 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 /* a normal assignment */
3297 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003298
3299 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3300 nch_minus_type = num - has_type_comment;
3301
3302 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 if (!targets)
3304 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003305 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003306 expr_ty e;
3307 node *ch = CHILD(n, i);
3308 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003309 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 return NULL;
3311 }
3312 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003316 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003317 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 asdl_seq_SET(targets, i / 2, e);
3321 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003322 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003323 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003324 expression = ast_for_testlist(c, value);
3325 else
3326 expression = ast_for_expr(c, value);
3327 if (!expression)
3328 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003329 if (has_type_comment) {
3330 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3331 if (!type_comment)
3332 return NULL;
3333 }
3334 else
3335 type_comment = NULL;
3336 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003337 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339}
3340
Benjamin Peterson78565b22009-06-28 19:19:51 +00003341
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003342static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003343ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344{
3345 asdl_seq *seq;
3346 int i;
3347 expr_ty e;
3348
3349 REQ(n, exprlist);
3350
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003351 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 e = ast_for_expr(c, CHILD(n, i));
3356 if (!e)
3357 return NULL;
3358 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003359 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003360 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361 }
3362 return seq;
3363}
3364
3365static stmt_ty
3366ast_for_del_stmt(struct compiling *c, const node *n)
3367{
3368 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370 /* del_stmt: 'del' exprlist */
3371 REQ(n, del_stmt);
3372
3373 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3374 if (!expr_list)
3375 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003376 return Delete(expr_list, LINENO(n), n->n_col_offset,
3377 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378}
3379
3380static stmt_ty
3381ast_for_flow_stmt(struct compiling *c, const node *n)
3382{
3383 /*
3384 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3385 | yield_stmt
3386 break_stmt: 'break'
3387 continue_stmt: 'continue'
3388 return_stmt: 'return' [testlist]
3389 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003390 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391 raise_stmt: 'raise' [test [',' test [',' test]]]
3392 */
3393 node *ch;
3394
3395 REQ(n, flow_stmt);
3396 ch = CHILD(n, 0);
3397 switch (TYPE(ch)) {
3398 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003399 return Break(LINENO(n), n->n_col_offset,
3400 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003402 return Continue(LINENO(n), n->n_col_offset,
3403 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003405 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3406 if (!exp)
3407 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003408 return Expr(exp, LINENO(n), n->n_col_offset,
3409 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 }
3411 case return_stmt:
3412 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003413 return Return(NULL, LINENO(n), n->n_col_offset,
3414 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003416 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 if (!expression)
3418 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003419 return Return(expression, LINENO(n), n->n_col_offset,
3420 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 }
3422 case raise_stmt:
3423 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003424 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3425 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003426 else if (NCH(ch) >= 2) {
3427 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3429 if (!expression)
3430 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003431 if (NCH(ch) == 4) {
3432 cause = ast_for_expr(c, CHILD(ch, 3));
3433 if (!cause)
3434 return NULL;
3435 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003436 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3437 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438 }
Stefan Krahf432a322017-08-21 13:09:59 +02003439 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003441 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 "unexpected flow_stmt: %d", TYPE(ch));
3443 return NULL;
3444 }
3445}
3446
3447static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003448alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449{
3450 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003451 import_as_name: NAME ['as' NAME]
3452 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 dotted_name: NAME ('.' NAME)*
3454 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003455 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003456
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 loop:
3458 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003459 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003460 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003461 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003462 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003463 if (!name)
3464 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003465 if (NCH(n) == 3) {
3466 node *str_node = CHILD(n, 2);
3467 str = NEW_IDENTIFIER(str_node);
3468 if (!str)
3469 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003470 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003471 return NULL;
3472 }
3473 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003474 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003475 return NULL;
3476 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003477 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003478 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 case dotted_as_name:
3480 if (NCH(n) == 1) {
3481 n = CHILD(n, 0);
3482 goto loop;
3483 }
3484 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003485 node *asname_node = CHILD(n, 2);
3486 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003487 if (!a)
3488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003490 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003491 if (!a->asname)
3492 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003493 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003494 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 return a;
3496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003498 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003499 node *name_node = CHILD(n, 0);
3500 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003501 if (!name)
3502 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003503 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003504 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003505 return alias(name, NULL, c->c_arena);
3506 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507 else {
3508 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003509 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003510 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513
3514 len = 0;
3515 for (i = 0; i < NCH(n); i += 2)
3516 /* length of string plus one for the dot */
3517 len += strlen(STR(CHILD(n, i))) + 1;
3518 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003519 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003520 if (!str)
3521 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003522 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523 if (!s)
3524 return NULL;
3525 for (i = 0; i < NCH(n); i += 2) {
3526 char *sch = STR(CHILD(n, i));
3527 strcpy(s, STR(CHILD(n, i)));
3528 s += strlen(sch);
3529 *s++ = '.';
3530 }
3531 --s;
3532 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3534 PyBytes_GET_SIZE(str),
3535 NULL);
3536 Py_DECREF(str);
3537 if (!uni)
3538 return NULL;
3539 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003540 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003541 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3542 Py_DECREF(str);
3543 return NULL;
3544 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003545 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003548 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003549 if (!str)
3550 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003551 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3552 Py_DECREF(str);
3553 return NULL;
3554 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003555 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003557 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 "unexpected import name: %d", TYPE(n));
3559 return NULL;
3560 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561}
3562
3563static stmt_ty
3564ast_for_import_stmt(struct compiling *c, const node *n)
3565{
3566 /*
3567 import_stmt: import_name | import_from
3568 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003569 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3570 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003572 int lineno;
3573 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 int i;
3575 asdl_seq *aliases;
3576
3577 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003578 lineno = LINENO(n);
3579 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003581 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003583 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003584 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003585 if (!aliases)
3586 return NULL;
3587 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003588 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003589 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003591 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003593 // Even though n is modified above, the end position is not changed
3594 return Import(aliases, lineno, col_offset,
3595 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003597 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003599 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003600 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003601 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003602 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003604 /* Count the number of dots (for relative imports) and check for the
3605 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003606 for (idx = 1; idx < NCH(n); idx++) {
3607 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003608 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3609 if (!mod)
3610 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003611 idx++;
3612 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003613 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003615 ndots += 3;
3616 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003617 } else if (TYPE(CHILD(n, idx)) != DOT) {
3618 break;
3619 }
3620 ndots++;
3621 }
3622 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003623 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003624 case STAR:
3625 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003626 n = CHILD(n, idx);
3627 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003628 break;
3629 case LPAR:
3630 /* from ... import (x, y, z) */
3631 n = CHILD(n, idx + 1);
3632 n_children = NCH(n);
3633 break;
3634 case import_as_names:
3635 /* from ... import x, y, z */
3636 n = CHILD(n, idx);
3637 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003638 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003639 ast_error(c, n,
3640 "trailing comma not allowed without"
3641 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003642 return NULL;
3643 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003644 break;
3645 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003646 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003647 return NULL;
3648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003650 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003651 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653
3654 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003655 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003656 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003657 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003658 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003659 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003661 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003662 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003663 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003664 if (!import_alias)
3665 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003666 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003669 if (mod != NULL)
3670 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003671 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003672 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003673 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674 }
Neal Norwitz79792652005-11-14 04:25:03 +00003675 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 "unknown import statement: starts with command '%s'",
3677 STR(CHILD(n, 0)));
3678 return NULL;
3679}
3680
3681static stmt_ty
3682ast_for_global_stmt(struct compiling *c, const node *n)
3683{
3684 /* global_stmt: 'global' NAME (',' NAME)* */
3685 identifier name;
3686 asdl_seq *s;
3687 int i;
3688
3689 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003690 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003692 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003694 name = NEW_IDENTIFIER(CHILD(n, i));
3695 if (!name)
3696 return NULL;
3697 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003698 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003699 return Global(s, LINENO(n), n->n_col_offset,
3700 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701}
3702
3703static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003704ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3705{
3706 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3707 identifier name;
3708 asdl_seq *s;
3709 int i;
3710
3711 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003712 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003713 if (!s)
3714 return NULL;
3715 for (i = 1; i < NCH(n); i += 2) {
3716 name = NEW_IDENTIFIER(CHILD(n, i));
3717 if (!name)
3718 return NULL;
3719 asdl_seq_SET(s, i / 2, name);
3720 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003721 return Nonlocal(s, LINENO(n), n->n_col_offset,
3722 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003723}
3724
3725static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726ast_for_assert_stmt(struct compiling *c, const node *n)
3727{
3728 /* assert_stmt: 'assert' test [',' test] */
3729 REQ(n, assert_stmt);
3730 if (NCH(n) == 2) {
3731 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3732 if (!expression)
3733 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003734 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3735 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736 }
3737 else if (NCH(n) == 4) {
3738 expr_ty expr1, expr2;
3739
3740 expr1 = ast_for_expr(c, CHILD(n, 1));
3741 if (!expr1)
3742 return NULL;
3743 expr2 = ast_for_expr(c, CHILD(n, 3));
3744 if (!expr2)
3745 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003747 return Assert(expr1, expr2, 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 }
Neal Norwitz79792652005-11-14 04:25:03 +00003750 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 "improper number of parts to 'assert' statement: %d",
3752 NCH(n));
3753 return NULL;
3754}
3755
3756static asdl_seq *
3757ast_for_suite(struct compiling *c, const node *n)
3758{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003759 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003760 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761 stmt_ty s;
3762 int i, total, num, end, pos = 0;
3763 node *ch;
3764
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003765 if (TYPE(n) != func_body_suite) {
3766 REQ(n, suite);
3767 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768
3769 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003770 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003772 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003774 n = CHILD(n, 0);
3775 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003777 */
3778 end = NCH(n) - 1;
3779 if (TYPE(CHILD(n, end - 1)) == SEMI)
3780 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003782 for (i = 0; i < end; i += 2) {
3783 ch = CHILD(n, i);
3784 s = ast_for_stmt(c, ch);
3785 if (!s)
3786 return NULL;
3787 asdl_seq_SET(seq, pos++, s);
3788 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789 }
3790 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003791 i = 2;
3792 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3793 i += 2;
3794 REQ(CHILD(n, 2), NEWLINE);
3795 }
3796
3797 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003798 ch = CHILD(n, i);
3799 REQ(ch, stmt);
3800 num = num_stmts(ch);
3801 if (num == 1) {
3802 /* small_stmt or compound_stmt with only one child */
3803 s = ast_for_stmt(c, ch);
3804 if (!s)
3805 return NULL;
3806 asdl_seq_SET(seq, pos++, s);
3807 }
3808 else {
3809 int j;
3810 ch = CHILD(ch, 0);
3811 REQ(ch, simple_stmt);
3812 for (j = 0; j < NCH(ch); j += 2) {
3813 /* statement terminates with a semi-colon ';' */
3814 if (NCH(CHILD(ch, j)) == 0) {
3815 assert((j + 1) == NCH(ch));
3816 break;
3817 }
3818 s = ast_for_stmt(c, CHILD(ch, j));
3819 if (!s)
3820 return NULL;
3821 asdl_seq_SET(seq, pos++, s);
3822 }
3823 }
3824 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 }
3826 assert(pos == seq->size);
3827 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828}
3829
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003830static void
3831get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3832{
Pablo Galindo46a97922019-02-19 22:51:53 +00003833 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003834 // There must be no empty suites.
3835 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003836 stmt_ty last = asdl_seq_GET(s, tot - 1);
3837 *end_lineno = last->end_lineno;
3838 *end_col_offset = last->end_col_offset;
3839}
3840
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841static stmt_ty
3842ast_for_if_stmt(struct compiling *c, const node *n)
3843{
3844 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3845 ['else' ':' suite]
3846 */
3847 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003848 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003849
3850 REQ(n, if_stmt);
3851
3852 if (NCH(n) == 4) {
3853 expr_ty expression;
3854 asdl_seq *suite_seq;
3855
3856 expression = ast_for_expr(c, CHILD(n, 1));
3857 if (!expression)
3858 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003860 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003862 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863
Guido van Rossumd8faa362007-04-27 19:54:29 +00003864 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003865 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 s = STR(CHILD(n, 4));
3869 /* s[2], the third character in the string, will be
3870 's' for el_s_e, or
3871 'i' for el_i_f
3872 */
3873 if (s[2] == 's') {
3874 expr_ty expression;
3875 asdl_seq *seq1, *seq2;
3876
3877 expression = ast_for_expr(c, CHILD(n, 1));
3878 if (!expression)
3879 return NULL;
3880 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003881 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882 return NULL;
3883 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003884 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003885 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003886 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887
Guido van Rossumd8faa362007-04-27 19:54:29 +00003888 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003889 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003890 }
3891 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003892 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003893 expr_ty expression;
3894 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003895 asdl_seq *orelse = NULL;
3896 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003897 /* must reference the child n_elif+1 since 'else' token is third,
3898 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003899 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3900 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3901 has_else = 1;
3902 n_elif -= 3;
3903 }
3904 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905
Thomas Wouters89f507f2006-12-13 04:49:30 +00003906 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003907 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003909 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003910 if (!orelse)
3911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003912 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003913 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003915 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3916 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003918 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3919 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003921 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 asdl_seq_SET(orelse, 0,
3924 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003925 LINENO(CHILD(n, NCH(n) - 7)),
3926 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003927 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003928 /* the just-created orelse handled the last elif */
3929 n_elif--;
3930 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931
Thomas Wouters89f507f2006-12-13 04:49:30 +00003932 for (i = 0; i < n_elif; i++) {
3933 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003934 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003935 if (!newobj)
3936 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003938 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003941 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003944 if (orelse != NULL) {
3945 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3946 } else {
3947 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3948 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003949 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003951 LINENO(CHILD(n, off - 1)),
3952 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003953 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003954 orelse = newobj;
3955 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003956 expression = ast_for_expr(c, CHILD(n, 1));
3957 if (!expression)
3958 return NULL;
3959 suite_seq = ast_for_suite(c, CHILD(n, 3));
3960 if (!suite_seq)
3961 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003962 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003963 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003964 LINENO(n), n->n_col_offset,
3965 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003967
3968 PyErr_Format(PyExc_SystemError,
3969 "unexpected token in 'if' statement: %s", s);
3970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971}
3972
3973static stmt_ty
3974ast_for_while_stmt(struct compiling *c, const node *n)
3975{
3976 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3977 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003978 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979
3980 if (NCH(n) == 4) {
3981 expr_ty expression;
3982 asdl_seq *suite_seq;
3983
3984 expression = ast_for_expr(c, CHILD(n, 1));
3985 if (!expression)
3986 return NULL;
3987 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003988 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003990 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3991 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3992 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 }
3994 else if (NCH(n) == 7) {
3995 expr_ty expression;
3996 asdl_seq *seq1, *seq2;
3997
3998 expression = ast_for_expr(c, CHILD(n, 1));
3999 if (!expression)
4000 return NULL;
4001 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004002 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003 return NULL;
4004 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004005 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004007 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004009 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4010 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004012
4013 PyErr_Format(PyExc_SystemError,
4014 "wrong number of tokens for 'while' statement: %d",
4015 NCH(n));
4016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017}
4018
4019static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004020ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004021{
guoci90fc8982018-09-11 17:45:45 -04004022 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004023 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004024 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004025 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004026 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004027 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004028 int has_type_comment;
4029 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004030
4031 if (is_async && c->c_feature_version < 5) {
4032 ast_error(c, n,
4033 "Async for loops are only supported in Python 3.5 and greater");
4034 return NULL;
4035 }
4036
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004037 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038 REQ(n, for_stmt);
4039
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004040 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4041
4042 if (NCH(n) == 9 + has_type_comment) {
4043 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004044 if (!seq)
4045 return NULL;
4046 }
4047
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004048 node_target = CHILD(n, 1);
4049 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004050 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004052 /* Check the # of children rather than the length of _target, since
4053 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004054 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004055 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004056 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004058 target = Tuple(_target, Store, first->lineno, first->col_offset,
4059 node_target->n_end_lineno, node_target->n_end_col_offset,
4060 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004062 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004063 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004065 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004066 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 return NULL;
4068
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004069 if (seq != NULL) {
4070 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4071 } else {
4072 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4073 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004074
4075 if (has_type_comment) {
4076 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4077 if (!type_comment)
4078 return NULL;
4079 }
4080 else
4081 type_comment = NULL;
4082
Yury Selivanov75445082015-05-11 22:57:16 -04004083 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004084 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004085 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004086 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004087 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004088 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004089 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004090 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091}
4092
4093static excepthandler_ty
4094ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4095{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004096 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004097 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004098 REQ(exc, except_clause);
4099 REQ(body, suite);
4100
4101 if (NCH(exc) == 1) {
4102 asdl_seq *suite_seq = ast_for_suite(c, body);
4103 if (!suite_seq)
4104 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004105 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004106
Neal Norwitzad74aa82008-03-31 05:14:30 +00004107 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004108 exc->n_col_offset,
4109 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004110 }
4111 else if (NCH(exc) == 2) {
4112 expr_ty expression;
4113 asdl_seq *suite_seq;
4114
4115 expression = ast_for_expr(c, CHILD(exc, 1));
4116 if (!expression)
4117 return NULL;
4118 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004119 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004120 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004121 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122
Neal Norwitzad74aa82008-03-31 05:14:30 +00004123 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004124 exc->n_col_offset,
4125 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126 }
4127 else if (NCH(exc) == 4) {
4128 asdl_seq *suite_seq;
4129 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004130 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004131 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004132 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004133 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004134 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004135 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004136 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137 return NULL;
4138 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004139 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004141 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142
Neal Norwitzad74aa82008-03-31 05:14:30 +00004143 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004144 exc->n_col_offset,
4145 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004147
4148 PyErr_Format(PyExc_SystemError,
4149 "wrong number of children for 'except' clause: %d",
4150 NCH(exc));
4151 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004152}
4153
4154static stmt_ty
4155ast_for_try_stmt(struct compiling *c, const node *n)
4156{
Neal Norwitzf599f422005-12-17 21:33:47 +00004157 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004158 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004159 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004160 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004161
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004162 REQ(n, try_stmt);
4163
Neal Norwitzf599f422005-12-17 21:33:47 +00004164 body = ast_for_suite(c, CHILD(n, 2));
4165 if (body == NULL)
4166 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004167
Neal Norwitzf599f422005-12-17 21:33:47 +00004168 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4169 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4170 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4171 /* we can assume it's an "else",
4172 because nch >= 9 for try-else-finally and
4173 it would otherwise have a type of except_clause */
4174 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4175 if (orelse == NULL)
4176 return NULL;
4177 n_except--;
4178 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004179
Neal Norwitzf599f422005-12-17 21:33:47 +00004180 finally = ast_for_suite(c, CHILD(n, nch - 1));
4181 if (finally == NULL)
4182 return NULL;
4183 n_except--;
4184 }
4185 else {
4186 /* we can assume it's an "else",
4187 otherwise it would have a type of except_clause */
4188 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4189 if (orelse == NULL)
4190 return NULL;
4191 n_except--;
4192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004193 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004194 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004195 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004196 return NULL;
4197 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198
Neal Norwitzf599f422005-12-17 21:33:47 +00004199 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004200 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004201 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004202 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004203 if (handlers == NULL)
4204 return NULL;
4205
4206 for (i = 0; i < n_except; i++) {
4207 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4208 CHILD(n, 5 + i * 3));
4209 if (!e)
4210 return NULL;
4211 asdl_seq_SET(handlers, i, e);
4212 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004213 }
4214
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004215 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004216 if (finally != NULL) {
4217 // finally is always last
4218 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4219 } else if (orelse != NULL) {
4220 // otherwise else is last
4221 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4222 } else {
4223 // inline the get_last_end_pos logic due to layout mismatch
4224 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4225 end_lineno = last_handler->end_lineno;
4226 end_col_offset = last_handler->end_col_offset;
4227 }
4228 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4229 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004230}
4231
Georg Brandl0c315622009-05-25 21:10:36 +00004232/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004233static withitem_ty
4234ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004235{
4236 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004237
Georg Brandl0c315622009-05-25 21:10:36 +00004238 REQ(n, with_item);
4239 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004240 if (!context_expr)
4241 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004242 if (NCH(n) == 3) {
4243 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004244
4245 if (!optional_vars) {
4246 return NULL;
4247 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004248 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004249 return NULL;
4250 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004251 }
4252
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004253 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004254}
4255
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004256/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004257static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004258ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004259{
guoci90fc8982018-09-11 17:45:45 -04004260 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004261 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004262 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004263 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004264
Guido van Rossum495da292019-03-07 12:38:08 -08004265 if (is_async && c->c_feature_version < 5) {
4266 ast_error(c, n,
4267 "Async with statements are only supported in Python 3.5 and greater");
4268 return NULL;
4269 }
4270
Georg Brandl0c315622009-05-25 21:10:36 +00004271 REQ(n, with_stmt);
4272
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004273 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4274 nch_minus_type = NCH(n) - has_type_comment;
4275
4276 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004277 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004278 if (!items)
4279 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004280 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004281 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4282 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004283 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004284 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004285 }
4286
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004287 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4288 if (!body)
4289 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004290 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004291
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004292 if (has_type_comment) {
4293 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4294 if (!type_comment)
4295 return NULL;
4296 }
4297 else
4298 type_comment = NULL;
4299
Yury Selivanov75445082015-05-11 22:57:16 -04004300 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004301 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004302 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004303 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004304 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004305 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004306}
4307
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004308static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004309ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004310{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004311 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004312 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004313 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004314 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004315 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004316
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004317 REQ(n, classdef);
4318
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004319 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004320 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004321 if (!s)
4322 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004323 get_last_end_pos(s, &end_lineno, &end_col_offset);
4324
Benjamin Peterson30760062008-11-25 04:02:28 +00004325 classname = NEW_IDENTIFIER(CHILD(n, 1));
4326 if (!classname)
4327 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004328 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004329 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004330 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004331 LINENO(n), n->n_col_offset,
4332 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004333 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004334
4335 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004336 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004337 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004338 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004339 get_last_end_pos(s, &end_lineno, &end_col_offset);
4340
Benjamin Peterson30760062008-11-25 04:02:28 +00004341 classname = NEW_IDENTIFIER(CHILD(n, 1));
4342 if (!classname)
4343 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004344 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004345 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004346 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004347 LINENO(n), n->n_col_offset,
4348 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004349 }
4350
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004351 /* class NAME '(' arglist ')' ':' suite */
4352 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004353 {
4354 PyObject *dummy_name;
4355 expr_ty dummy;
4356 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4357 if (!dummy_name)
4358 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004359 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4360 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4361 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004362 call = ast_for_call(c, CHILD(n, 3), dummy,
4363 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004364 if (!call)
4365 return NULL;
4366 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004367 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004368 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004369 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004370 get_last_end_pos(s, &end_lineno, &end_col_offset);
4371
Benjamin Peterson30760062008-11-25 04:02:28 +00004372 classname = NEW_IDENTIFIER(CHILD(n, 1));
4373 if (!classname)
4374 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004375 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004376 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004377
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004378 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004379 decorator_seq, LINENO(n), n->n_col_offset,
4380 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004381}
4382
4383static stmt_ty
4384ast_for_stmt(struct compiling *c, const node *n)
4385{
4386 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004387 assert(NCH(n) == 1);
4388 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004389 }
4390 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004391 assert(num_stmts(n) == 1);
4392 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004393 }
4394 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004395 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004396 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4397 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004398 */
4399 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004400 case expr_stmt:
4401 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004402 case del_stmt:
4403 return ast_for_del_stmt(c, n);
4404 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004405 return Pass(LINENO(n), n->n_col_offset,
4406 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004407 case flow_stmt:
4408 return ast_for_flow_stmt(c, n);
4409 case import_stmt:
4410 return ast_for_import_stmt(c, n);
4411 case global_stmt:
4412 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004413 case nonlocal_stmt:
4414 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004415 case assert_stmt:
4416 return ast_for_assert_stmt(c, n);
4417 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004418 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004419 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4420 TYPE(n), NCH(n));
4421 return NULL;
4422 }
4423 }
4424 else {
4425 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004426 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004427 */
4428 node *ch = CHILD(n, 0);
4429 REQ(n, compound_stmt);
4430 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004431 case if_stmt:
4432 return ast_for_if_stmt(c, ch);
4433 case while_stmt:
4434 return ast_for_while_stmt(c, ch);
4435 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004436 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004437 case try_stmt:
4438 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004439 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004440 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004441 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004442 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004443 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004444 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 case decorated:
4446 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004447 case async_stmt:
4448 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004449 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004450 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004451 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004452 TYPE(n), NCH(n));
4453 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004455 }
4456}
4457
4458static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004459parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004460{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004461 const char *end;
4462 long x;
4463 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004464 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004465 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004466
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004467 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004468 errno = 0;
4469 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004470 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004471 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004472 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004473 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004474 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004475 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004476 }
4477 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004478 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004479 if (*end == '\0') {
4480 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004481 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004482 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004483 }
4484 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004485 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004486 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004487 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4488 if (compl.imag == -1.0 && PyErr_Occurred())
4489 return NULL;
4490 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004491 }
4492 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004493 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004494 dx = PyOS_string_to_double(s, NULL, NULL);
4495 if (dx == -1.0 && PyErr_Occurred())
4496 return NULL;
4497 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004498 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004499}
4500
4501static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004502parsenumber(struct compiling *c, const char *s)
4503{
4504 char *dup, *end;
4505 PyObject *res = NULL;
4506
4507 assert(s != NULL);
4508
4509 if (strchr(s, '_') == NULL) {
4510 return parsenumber_raw(c, s);
4511 }
4512 /* Create a duplicate without underscores. */
4513 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004514 if (dup == NULL) {
4515 return PyErr_NoMemory();
4516 }
Brett Cannona721aba2016-09-09 14:57:09 -07004517 end = dup;
4518 for (; *s; s++) {
4519 if (*s != '_') {
4520 *end++ = *s;
4521 }
4522 }
4523 *end = '\0';
4524 res = parsenumber_raw(c, dup);
4525 PyMem_Free(dup);
4526 return res;
4527}
4528
4529static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004530decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004531{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004532 const char *s, *t;
4533 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004534 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4535 while (s < end && (*s & 0x80)) s++;
4536 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004537 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004538}
4539
Eric V. Smith56466482016-10-31 14:46:26 -04004540static int
4541warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004542 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004543{
4544 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4545 first_invalid_escape_char);
4546 if (msg == NULL) {
4547 return -1;
4548 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004549 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004550 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004551 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004552 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004553 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4554 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004555 to get a more accurate error report */
4556 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004557 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004558 }
4559 Py_DECREF(msg);
4560 return -1;
4561 }
4562 Py_DECREF(msg);
4563 return 0;
4564}
4565
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004566static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004567decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4568 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004569{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004570 PyObject *v, *u;
4571 char *buf;
4572 char *p;
4573 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004574
Benjamin Peterson202803a2016-02-25 22:34:45 -08004575 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004576 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004577 return NULL;
4578 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4579 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4580 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4581 if (u == NULL)
4582 return NULL;
4583 p = buf = PyBytes_AsString(u);
4584 end = s + len;
4585 while (s < end) {
4586 if (*s == '\\') {
4587 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004588 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004589 strcpy(p, "u005c");
4590 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004591 if (s >= end)
4592 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004593 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004594 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004595 if (*s & 0x80) { /* XXX inefficient */
4596 PyObject *w;
4597 int kind;
4598 void *data;
4599 Py_ssize_t len, i;
4600 w = decode_utf8(c, &s, end);
4601 if (w == NULL) {
4602 Py_DECREF(u);
4603 return NULL;
4604 }
4605 kind = PyUnicode_KIND(w);
4606 data = PyUnicode_DATA(w);
4607 len = PyUnicode_GET_LENGTH(w);
4608 for (i = 0; i < len; i++) {
4609 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4610 sprintf(p, "\\U%08x", chr);
4611 p += 10;
4612 }
4613 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004614 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004615 Py_DECREF(w);
4616 } else {
4617 *p++ = *s++;
4618 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004619 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004620 len = p - buf;
4621 s = buf;
4622
Eric V. Smith56466482016-10-31 14:46:26 -04004623 const char *first_invalid_escape;
4624 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4625
4626 if (v != NULL && first_invalid_escape != NULL) {
4627 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4628 /* We have not decref u before because first_invalid_escape points
4629 inside u. */
4630 Py_XDECREF(u);
4631 Py_DECREF(v);
4632 return NULL;
4633 }
4634 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004635 Py_XDECREF(u);
4636 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004637}
4638
Eric V. Smith56466482016-10-31 14:46:26 -04004639static PyObject *
4640decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4641 size_t len)
4642{
4643 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004644 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004645 &first_invalid_escape);
4646 if (result == NULL)
4647 return NULL;
4648
4649 if (first_invalid_escape != NULL) {
4650 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4651 Py_DECREF(result);
4652 return NULL;
4653 }
4654 }
4655 return result;
4656}
4657
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004658/* Shift locations for the given node and all its children by adding `lineno`
4659 and `col_offset` to existing locations. */
4660static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4661{
4662 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004663 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004664 for (int i = 0; i < NCH(n); ++i) {
4665 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4666 /* Shifting column offsets unnecessary if there's been newlines. */
4667 col_offset = 0;
4668 }
4669 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4670 }
4671 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004672 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004673}
4674
4675/* Fix locations for the given node and its children.
4676
4677 `parent` is the enclosing node.
4678 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004679 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004680*/
4681static void
4682fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4683{
4684 char *substr = NULL;
4685 char *start;
4686 int lines = LINENO(parent) - 1;
4687 int cols = parent->n_col_offset;
4688 /* Find the full fstring to fix location information in `n`. */
4689 while (parent && parent->n_type != STRING)
4690 parent = parent->n_child;
4691 if (parent && parent->n_str) {
4692 substr = strstr(parent->n_str, expr_str);
4693 if (substr) {
4694 start = substr;
4695 while (start > parent->n_str) {
4696 if (start[0] == '\n')
4697 break;
4698 start--;
4699 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004700 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004701 /* adjust the start based on the number of newlines encountered
4702 before the f-string expression */
4703 for (char* p = parent->n_str; p < substr; p++) {
4704 if (*p == '\n') {
4705 lines++;
4706 }
4707 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004708 }
4709 }
4710 fstring_shift_node_locations(n, lines, cols);
4711}
4712
Eric V. Smith451d0e32016-09-09 21:56:20 -04004713/* Compile this expression in to an expr_ty. Add parens around the
4714 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004715static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004716fstring_compile_expr(const char *expr_start, const char *expr_end,
4717 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004718
Eric V. Smith235a6f02015-09-19 14:51:32 -04004719{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004720 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004721 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004722 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004723 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004724 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004725
Eric V. Smith1d44c412015-09-23 07:49:00 -04004726 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004727 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004728 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4729 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004730
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004731 /* If the substring is all whitespace, it's an error. We need to catch this
4732 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4733 because turning the expression '' in to '()' would go from being invalid
4734 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004735 for (s = expr_start; s != expr_end; s++) {
4736 char c = *s;
4737 /* The Python parser ignores only the following whitespace
4738 characters (\r already is converted to \n). */
4739 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004740 break;
4741 }
4742 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004743 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004744 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004745 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004746 }
4747
Eric V. Smith451d0e32016-09-09 21:56:20 -04004748 len = expr_end - expr_start;
4749 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4750 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004751 if (str == NULL) {
4752 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004753 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004754 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004755
Eric V. Smith451d0e32016-09-09 21:56:20 -04004756 str[0] = '(';
4757 memcpy(str+1, expr_start, len);
4758 str[len+1] = ')';
4759 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004760
Victor Stinner37d66d72019-06-13 02:16:41 +02004761 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004762 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004763 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4764 Py_eval_input, 0);
4765 if (!mod_n) {
4766 PyMem_RawFree(str);
4767 return NULL;
4768 }
4769 /* Reuse str to find the correct column offset. */
4770 str[0] = '{';
4771 str[len+1] = '}';
4772 fstring_fix_node_location(n, mod_n, str);
4773 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004774 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004775 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004776 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004777 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004778 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004779}
4780
4781/* Return -1 on error.
4782
4783 Return 0 if we reached the end of the literal.
4784
4785 Return 1 if we haven't reached the end of the literal, but we want
4786 the caller to process the literal up to this point. Used for
4787 doubled braces.
4788*/
4789static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004790fstring_find_literal(const char **str, const char *end, int raw,
4791 PyObject **literal, int recurse_lvl,
4792 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004793{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004794 /* Get any literal string. It ends when we hit an un-doubled left
4795 brace (which isn't part of a unicode name escape such as
4796 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004797
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004798 const char *s = *str;
4799 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004800 int result = 0;
4801
Eric V. Smith235a6f02015-09-19 14:51:32 -04004802 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004803 while (s < end) {
4804 char ch = *s++;
4805 if (!raw && ch == '\\' && s < end) {
4806 ch = *s++;
4807 if (ch == 'N') {
4808 if (s < end && *s++ == '{') {
4809 while (s < end && *s++ != '}') {
4810 }
4811 continue;
4812 }
4813 break;
4814 }
4815 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4816 return -1;
4817 }
4818 }
4819 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004820 /* Check for doubled braces, but only at the top level. If
4821 we checked at every level, then f'{0:{3}}' would fail
4822 with the two closing braces. */
4823 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004824 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004825 /* We're going to tell the caller that the literal ends
4826 here, but that they should continue scanning. But also
4827 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004828 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004829 result = 1;
4830 goto done;
4831 }
4832
4833 /* Where a single '{' is the start of a new expression, a
4834 single '}' is not allowed. */
4835 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004836 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004837 ast_error(c, n, "f-string: single '}' is not allowed");
4838 return -1;
4839 }
4840 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004841 /* We're either at a '{', which means we're starting another
4842 expression; or a '}', which means we're at the end of this
4843 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004844 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004845 break;
4846 }
4847 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004848 *str = s;
4849 assert(s <= end);
4850 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004851done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004852 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004853 if (raw)
4854 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004855 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004856 NULL, NULL);
4857 else
Eric V. Smith56466482016-10-31 14:46:26 -04004858 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004859 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004860 if (!*literal)
4861 return -1;
4862 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004863 return result;
4864}
4865
4866/* Forward declaration because parsing is recursive. */
4867static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004868fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004869 struct compiling *c, const node *n);
4870
Eric V. Smith451d0e32016-09-09 21:56:20 -04004871/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004872 expression (so it must be a '{'). Returns the FormattedValue node, which
4873 includes the expression, conversion character, format_spec expression, and
4874 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004875
4876 Note that I don't do a perfect job here: I don't make sure that a
4877 closing brace doesn't match an opening paren, for example. It
4878 doesn't need to error on all invalid expressions, just correctly
4879 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004880 will be caught when we parse it later.
4881
4882 *expression is set to the expression. For an '=' "debug" expression,
4883 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004884 including the '=' and any whitespace around it, as a string object). If
4885 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004886static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004887fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004888 PyObject **expr_text, expr_ty *expression,
4889 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004890{
4891 /* Return -1 on error, else 0. */
4892
Eric V. Smith451d0e32016-09-09 21:56:20 -04004893 const char *expr_start;
4894 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004895 expr_ty simple_expression;
4896 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004897 int conversion = -1; /* The conversion char. Use default if not
4898 specified, or !r if using = and no format
4899 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900
4901 /* 0 if we're not in a string, else the quote char we're trying to
4902 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004903 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004904
4905 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4906 int string_type = 0;
4907
4908 /* Keep track of nesting level for braces/parens/brackets in
4909 expressions. */
4910 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004911 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004912
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004913 *expr_text = NULL;
4914
Eric V. Smith235a6f02015-09-19 14:51:32 -04004915 /* Can only nest one level deep. */
4916 if (recurse_lvl >= 2) {
4917 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004918 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919 }
4920
4921 /* The first char must be a left brace, or we wouldn't have gotten
4922 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004923 assert(**str == '{');
4924 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004925
Eric V. Smith451d0e32016-09-09 21:56:20 -04004926 expr_start = *str;
4927 for (; *str < end; (*str)++) {
4928 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929
4930 /* Loop invariants. */
4931 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004932 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004933 if (quote_char)
4934 assert(string_type == 1 || string_type == 3);
4935 else
4936 assert(string_type == 0);
4937
Eric V. Smith451d0e32016-09-09 21:56:20 -04004938 ch = **str;
4939 /* Nowhere inside an expression is a backslash allowed. */
4940 if (ch == '\\') {
4941 /* Error: can't include a backslash character, inside
4942 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004943 ast_error(c, n,
4944 "f-string expression part "
4945 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004946 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004947 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004948 if (quote_char) {
4949 /* We're inside a string. See if we're at the end. */
4950 /* This code needs to implement the same non-error logic
4951 as tok_get from tokenizer.c, at the letter_quote
4952 label. To actually share that code would be a
4953 nightmare. But, it's unlikely to change and is small,
4954 so duplicate it here. Note we don't need to catch all
4955 of the errors, since they'll be caught when parsing the
4956 expression. We just need to match the non-error
4957 cases. Thus we can ignore \n in single-quoted strings,
4958 for example. Or non-terminated strings. */
4959 if (ch == quote_char) {
4960 /* Does this match the string_type (single or triple
4961 quoted)? */
4962 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004963 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004964 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004965 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966 string_type = 0;
4967 quote_char = 0;
4968 continue;
4969 }
4970 } else {
4971 /* We're at the end of a normal string. */
4972 quote_char = 0;
4973 string_type = 0;
4974 continue;
4975 }
4976 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004977 } else if (ch == '\'' || ch == '"') {
4978 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004979 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004980 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004981 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004982 } else {
4983 /* Start of a normal string. */
4984 string_type = 1;
4985 }
4986 /* Start looking for the end of the string. */
4987 quote_char = ch;
4988 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004989 if (nested_depth >= MAXLEVEL) {
4990 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004991 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004992 }
4993 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004994 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 } else if (ch == '#') {
4996 /* Error: can't include a comment character, inside parens
4997 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004998 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004999 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005000 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005001 (ch == '!' || ch == ':' || ch == '}' ||
5002 ch == '=' || ch == '>' || ch == '<')) {
5003 /* See if there's a next character. */
5004 if (*str+1 < end) {
5005 char next = *(*str+1);
5006
5007 /* For "!=". since '=' is not an allowed conversion character,
5008 nothing is lost in this test. */
5009 if ((ch == '!' && next == '=') || /* != */
5010 (ch == '=' && next == '=') || /* == */
5011 (ch == '<' && next == '=') || /* <= */
5012 (ch == '>' && next == '=') /* >= */
5013 ) {
5014 *str += 1;
5015 continue;
5016 }
5017 /* Don't get out of the loop for these, if they're single
5018 chars (not part of 2-char tokens). If by themselves, they
5019 don't end an expression (unlike say '!'). */
5020 if (ch == '>' || ch == '<') {
5021 continue;
5022 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005023 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005024
Eric V. Smith235a6f02015-09-19 14:51:32 -04005025 /* Normal way out of this loop. */
5026 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005027 } else if (ch == ']' || ch == '}' || ch == ')') {
5028 if (!nested_depth) {
5029 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005030 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005031 }
5032 nested_depth--;
5033 int opening = parenstack[nested_depth];
5034 if (!((opening == '(' && ch == ')') ||
5035 (opening == '[' && ch == ']') ||
5036 (opening == '{' && ch == '}')))
5037 {
5038 ast_error(c, n,
5039 "f-string: closing parenthesis '%c' "
5040 "does not match opening parenthesis '%c'",
5041 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005042 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005043 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005044 } else {
5045 /* Just consume this char and loop around. */
5046 }
5047 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005048 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005049 /* If we leave this loop in a string or with mismatched parens, we
5050 don't care. We'll get a syntax error when compiling the
5051 expression. But, we can produce a better error message, so
5052 let's just do that.*/
5053 if (quote_char) {
5054 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005055 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005056 }
5057 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005058 int opening = parenstack[nested_depth - 1];
5059 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005060 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005061 }
5062
Eric V. Smith451d0e32016-09-09 21:56:20 -04005063 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005064 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005065
5066 /* Compile the expression as soon as possible, so we show errors
5067 related to the expression before errors related to the
5068 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005069 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005070 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005071 goto error;
5072
5073 /* Check for =, which puts the text value of the expression in
5074 expr_text. */
5075 if (**str == '=') {
5076 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005077
5078 /* Skip over ASCII whitespace. No need to test for end of string
5079 here, since we know there's at least a trailing quote somewhere
5080 ahead. */
5081 while (Py_ISSPACE(**str)) {
5082 *str += 1;
5083 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005084
5085 /* Set *expr_text to the text of the expression. */
5086 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5087 if (!*expr_text) {
5088 goto error;
5089 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005090 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005091
5092 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005093 if (**str == '!') {
5094 *str += 1;
5095 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005096 goto unexpected_end_of_string;
5097
Eric V. Smith451d0e32016-09-09 21:56:20 -04005098 conversion = **str;
5099 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005100
5101 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005102 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005103 ast_error(c, n,
5104 "f-string: invalid conversion character: "
5105 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005106 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005107 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005108
5109 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005110
5111 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005112 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005113 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005114 if (**str == ':') {
5115 *str += 1;
5116 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005117 goto unexpected_end_of_string;
5118
5119 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005120 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005121 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005122 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005123 }
5124
Eric V. Smith451d0e32016-09-09 21:56:20 -04005125 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005126 goto unexpected_end_of_string;
5127
5128 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005129 assert(*str < end);
5130 assert(**str == '}');
5131 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005132
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005133 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005134 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005135 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005136 conversion = 'r';
5137 }
5138
Eric V. Smith451d0e32016-09-09 21:56:20 -04005139 /* And now create the FormattedValue node that represents this
5140 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005141 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005142 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005143 n->n_col_offset, n->n_end_lineno,
5144 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005145 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005146 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147
5148 return 0;
5149
5150unexpected_end_of_string:
5151 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005152 /* Falls through to error. */
5153
5154error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005155 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005156 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005157
Eric V. Smith235a6f02015-09-19 14:51:32 -04005158}
5159
5160/* Return -1 on error.
5161
5162 Return 0 if we have a literal (possible zero length) and an
5163 expression (zero length if at the end of the string.
5164
5165 Return 1 if we have a literal, but no expression, and we want the
5166 caller to call us again. This is used to deal with doubled
5167 braces.
5168
5169 When called multiple times on the string 'a{{b{0}c', this function
5170 will return:
5171
5172 1. the literal 'a{' with no expression, and a return value
5173 of 1. Despite the fact that there's no expression, the return
5174 value of 1 means we're not finished yet.
5175
5176 2. the literal 'b' and the expression '0', with a return value of
5177 0. The fact that there's an expression means we're not finished.
5178
5179 3. literal 'c' with no expression and a return value of 0. The
5180 combination of the return value of 0 with no expression means
5181 we're finished.
5182*/
5183static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005184fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5185 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005186 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005187 struct compiling *c, const node *n)
5188{
5189 int result;
5190
5191 assert(*literal == NULL && *expression == NULL);
5192
5193 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005194 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005195 if (result < 0)
5196 goto error;
5197
5198 assert(result == 0 || result == 1);
5199
5200 if (result == 1)
5201 /* We have a literal, but don't look at the expression. */
5202 return 1;
5203
Eric V. Smith451d0e32016-09-09 21:56:20 -04005204 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005205 /* We're at the end of the string or the end of a nested
5206 f-string: no expression. The top-level error case where we
5207 expect to be at the end of the string but we're at a '}' is
5208 handled later. */
5209 return 0;
5210
5211 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005212 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005213
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005214 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5215 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005216 goto error;
5217
5218 return 0;
5219
5220error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005221 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005222 return -1;
5223}
5224
5225#define EXPRLIST_N_CACHED 64
5226
5227typedef struct {
5228 /* Incrementally build an array of expr_ty, so be used in an
5229 asdl_seq. Cache some small but reasonably sized number of
5230 expr_ty's, and then after that start dynamically allocating,
5231 doubling the number allocated each time. Note that the f-string
5232 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005233 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005234 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005235
5236 Py_ssize_t allocated; /* Number we've allocated. */
5237 Py_ssize_t size; /* Number we've used. */
5238 expr_ty *p; /* Pointer to the memory we're actually
5239 using. Will point to 'data' until we
5240 start dynamically allocating. */
5241 expr_ty data[EXPRLIST_N_CACHED];
5242} ExprList;
5243
5244#ifdef NDEBUG
5245#define ExprList_check_invariants(l)
5246#else
5247static void
5248ExprList_check_invariants(ExprList *l)
5249{
5250 /* Check our invariants. Make sure this object is "live", and
5251 hasn't been deallocated. */
5252 assert(l->size >= 0);
5253 assert(l->p != NULL);
5254 if (l->size <= EXPRLIST_N_CACHED)
5255 assert(l->data == l->p);
5256}
5257#endif
5258
5259static void
5260ExprList_Init(ExprList *l)
5261{
5262 l->allocated = EXPRLIST_N_CACHED;
5263 l->size = 0;
5264
5265 /* Until we start allocating dynamically, p points to data. */
5266 l->p = l->data;
5267
5268 ExprList_check_invariants(l);
5269}
5270
5271static int
5272ExprList_Append(ExprList *l, expr_ty exp)
5273{
5274 ExprList_check_invariants(l);
5275 if (l->size >= l->allocated) {
5276 /* We need to alloc (or realloc) the memory. */
5277 Py_ssize_t new_size = l->allocated * 2;
5278
5279 /* See if we've ever allocated anything dynamically. */
5280 if (l->p == l->data) {
5281 Py_ssize_t i;
5282 /* We're still using the cached data. Switch to
5283 alloc-ing. */
5284 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5285 if (!l->p)
5286 return -1;
5287 /* Copy the cached data into the new buffer. */
5288 for (i = 0; i < l->size; i++)
5289 l->p[i] = l->data[i];
5290 } else {
5291 /* Just realloc. */
5292 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5293 if (!tmp) {
5294 PyMem_RawFree(l->p);
5295 l->p = NULL;
5296 return -1;
5297 }
5298 l->p = tmp;
5299 }
5300
5301 l->allocated = new_size;
5302 assert(l->allocated == 2 * l->size);
5303 }
5304
5305 l->p[l->size++] = exp;
5306
5307 ExprList_check_invariants(l);
5308 return 0;
5309}
5310
5311static void
5312ExprList_Dealloc(ExprList *l)
5313{
5314 ExprList_check_invariants(l);
5315
5316 /* If there's been an error, or we've never dynamically allocated,
5317 do nothing. */
5318 if (!l->p || l->p == l->data) {
5319 /* Do nothing. */
5320 } else {
5321 /* We have dynamically allocated. Free the memory. */
5322 PyMem_RawFree(l->p);
5323 }
5324 l->p = NULL;
5325 l->size = -1;
5326}
5327
5328static asdl_seq *
5329ExprList_Finish(ExprList *l, PyArena *arena)
5330{
5331 asdl_seq *seq;
5332
5333 ExprList_check_invariants(l);
5334
5335 /* Allocate the asdl_seq and copy the expressions in to it. */
5336 seq = _Py_asdl_seq_new(l->size, arena);
5337 if (seq) {
5338 Py_ssize_t i;
5339 for (i = 0; i < l->size; i++)
5340 asdl_seq_SET(seq, i, l->p[i]);
5341 }
5342 ExprList_Dealloc(l);
5343 return seq;
5344}
5345
5346/* The FstringParser is designed to add a mix of strings and
5347 f-strings, and concat them together as needed. Ultimately, it
5348 generates an expr_ty. */
5349typedef struct {
5350 PyObject *last_str;
5351 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005352 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005353} FstringParser;
5354
5355#ifdef NDEBUG
5356#define FstringParser_check_invariants(state)
5357#else
5358static void
5359FstringParser_check_invariants(FstringParser *state)
5360{
5361 if (state->last_str)
5362 assert(PyUnicode_CheckExact(state->last_str));
5363 ExprList_check_invariants(&state->expr_list);
5364}
5365#endif
5366
5367static void
5368FstringParser_Init(FstringParser *state)
5369{
5370 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005371 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005372 ExprList_Init(&state->expr_list);
5373 FstringParser_check_invariants(state);
5374}
5375
5376static void
5377FstringParser_Dealloc(FstringParser *state)
5378{
5379 FstringParser_check_invariants(state);
5380
5381 Py_XDECREF(state->last_str);
5382 ExprList_Dealloc(&state->expr_list);
5383}
5384
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005385/* Constants for the following */
5386static PyObject *u_kind;
5387
5388/* Compute 'kind' field for string Constant (either 'u' or None) */
5389static PyObject *
5390make_kind(struct compiling *c, const node *n)
5391{
5392 char *s = NULL;
5393 PyObject *kind = NULL;
5394
5395 /* Find the first string literal, if any */
5396 while (TYPE(n) != STRING) {
5397 if (NCH(n) == 0)
5398 return NULL;
5399 n = CHILD(n, 0);
5400 }
5401 REQ(n, STRING);
5402
5403 /* If it starts with 'u', return a PyUnicode "u" string */
5404 s = STR(n);
5405 if (s && *s == 'u') {
5406 if (!u_kind) {
5407 u_kind = PyUnicode_InternFromString("u");
5408 if (!u_kind)
5409 return NULL;
5410 }
5411 kind = u_kind;
5412 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5413 return NULL;
5414 }
5415 Py_INCREF(kind);
5416 }
5417 return kind;
5418}
5419
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005420/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005421static expr_ty
5422make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5423{
5424 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005425 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005426 *str = NULL;
5427 assert(PyUnicode_CheckExact(s));
5428 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5429 Py_DECREF(s);
5430 return NULL;
5431 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005432 kind = make_kind(c, n);
5433 if (kind == NULL && PyErr_Occurred())
5434 return NULL;
5435 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005436 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005437}
5438
5439/* Add a non-f-string (that is, a regular literal string). str is
5440 decref'd. */
5441static int
5442FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5443{
5444 FstringParser_check_invariants(state);
5445
5446 assert(PyUnicode_CheckExact(str));
5447
5448 if (PyUnicode_GET_LENGTH(str) == 0) {
5449 Py_DECREF(str);
5450 return 0;
5451 }
5452
5453 if (!state->last_str) {
5454 /* We didn't have a string before, so just remember this one. */
5455 state->last_str = str;
5456 } else {
5457 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005458 PyUnicode_AppendAndDel(&state->last_str, str);
5459 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005460 return -1;
5461 }
5462 FstringParser_check_invariants(state);
5463 return 0;
5464}
5465
Eric V. Smith451d0e32016-09-09 21:56:20 -04005466/* Parse an f-string. The f-string is in *str to end, with no
5467 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005468static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005469FstringParser_ConcatFstring(FstringParser *state, const char **str,
5470 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005471 struct compiling *c, const node *n)
5472{
5473 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005474 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005475
5476 /* Parse the f-string. */
5477 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005478 PyObject *literal = NULL;
5479 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005480 expr_ty expression = NULL;
5481
5482 /* If there's a zero length literal in front of the
5483 expression, literal will be NULL. If we're at the end of
5484 the f-string, expression will be NULL (unless result == 1,
5485 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005486 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005487 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005488 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005489 if (result < 0)
5490 return -1;
5491
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005492 /* Add the literal, if any. */
5493 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5494 Py_XDECREF(expr_text);
5495 return -1;
5496 }
5497 /* Add the expr_text, if any. */
5498 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5499 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005500 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005501
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005502 /* We've dealt with the literal and expr_text, their ownership has
5503 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005504
5505 /* See if we should just loop around to get the next literal
5506 and expression, while ignoring the expression this
5507 time. This is used for un-doubling braces, as an
5508 optimization. */
5509 if (result == 1)
5510 continue;
5511
5512 if (!expression)
5513 /* We're done with this f-string. */
5514 break;
5515
5516 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005517 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005518 if (!state->last_str) {
5519 /* Do nothing. No previous literal. */
5520 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005521 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005522 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5523 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5524 return -1;
5525 }
5526
5527 if (ExprList_Append(&state->expr_list, expression) < 0)
5528 return -1;
5529 }
5530
Eric V. Smith235a6f02015-09-19 14:51:32 -04005531 /* If recurse_lvl is zero, then we must be at the end of the
5532 string. Otherwise, we must be at a right brace. */
5533
Eric V. Smith451d0e32016-09-09 21:56:20 -04005534 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005535 ast_error(c, n, "f-string: unexpected end of string");
5536 return -1;
5537 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005538 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005539 ast_error(c, n, "f-string: expecting '}'");
5540 return -1;
5541 }
5542
5543 FstringParser_check_invariants(state);
5544 return 0;
5545}
5546
5547/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005548 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005549static expr_ty
5550FstringParser_Finish(FstringParser *state, struct compiling *c,
5551 const node *n)
5552{
5553 asdl_seq *seq;
5554
5555 FstringParser_check_invariants(state);
5556
5557 /* If we're just a constant string with no expressions, return
5558 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005559 if (!state->fmode) {
5560 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005561 if (!state->last_str) {
5562 /* Create a zero length string. */
5563 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5564 if (!state->last_str)
5565 goto error;
5566 }
5567 return make_str_node_and_del(&state->last_str, c, n);
5568 }
5569
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005570 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005571 last node in our expression list. */
5572 if (state->last_str) {
5573 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5574 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5575 goto error;
5576 }
5577 /* This has already been freed. */
5578 assert(state->last_str == NULL);
5579
5580 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5581 if (!seq)
5582 goto error;
5583
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005584 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5585 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005586
5587error:
5588 FstringParser_Dealloc(state);
5589 return NULL;
5590}
5591
Eric V. Smith451d0e32016-09-09 21:56:20 -04005592/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5593 at end, parse it into an expr_ty. Return NULL on error. Adjust
5594 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005595static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005596fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005597 struct compiling *c, const node *n)
5598{
5599 FstringParser state;
5600
5601 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005602 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005603 c, n) < 0) {
5604 FstringParser_Dealloc(&state);
5605 return NULL;
5606 }
5607
5608 return FstringParser_Finish(&state, c, n);
5609}
5610
5611/* n is a Python string literal, including the bracketing quote
5612 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005613 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005614 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005615 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5616 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005617*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005618static int
5619parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5620 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005621{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005622 size_t len;
5623 const char *s = STR(n);
5624 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005625 int fmode = 0;
5626 *bytesmode = 0;
5627 *rawmode = 0;
5628 *result = NULL;
5629 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005630 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005631 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005632 if (quote == 'b' || quote == 'B') {
5633 quote = *++s;
5634 *bytesmode = 1;
5635 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005636 else if (quote == 'u' || quote == 'U') {
5637 quote = *++s;
5638 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005639 else if (quote == 'r' || quote == 'R') {
5640 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005641 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005642 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005643 else if (quote == 'f' || quote == 'F') {
5644 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005645 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005646 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005647 else {
5648 break;
5649 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005650 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005651 }
Guido van Rossum495da292019-03-07 12:38:08 -08005652
5653 /* fstrings are only allowed in Python 3.6 and greater */
5654 if (fmode && c->c_feature_version < 6) {
5655 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5656 return -1;
5657 }
5658
Eric V. Smith451d0e32016-09-09 21:56:20 -04005659 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005660 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005661 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005662 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005663 if (quote != '\'' && quote != '\"') {
5664 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005665 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005666 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005667 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005668 s++;
5669 len = strlen(s);
5670 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005671 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005672 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005673 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005674 }
5675 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005676 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005677 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005678 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005679 }
5680 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005681 /* A triple quoted string. We've already skipped one quote at
5682 the start and one at the end of the string. Now skip the
5683 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005684 s += 2;
5685 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005686 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005687 if (s[--len] != quote || s[--len] != quote) {
5688 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005689 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005690 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005691 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005692
Eric V. Smith451d0e32016-09-09 21:56:20 -04005693 if (fmode) {
5694 /* Just return the bytes. The caller will parse the resulting
5695 string. */
5696 *fstr = s;
5697 *fstrlen = len;
5698 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005699 }
5700
Eric V. Smith451d0e32016-09-09 21:56:20 -04005701 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005702 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005703 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005704 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005705 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005706 const char *ch;
5707 for (ch = s; *ch; ch++) {
5708 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005709 ast_error(c, n,
5710 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005711 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005712 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005713 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005714 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005715 if (*rawmode)
5716 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005717 else
Eric V. Smith56466482016-10-31 14:46:26 -04005718 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005719 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005720 if (*rawmode)
5721 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005722 else
Eric V. Smith56466482016-10-31 14:46:26 -04005723 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005724 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005725 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005726}
5727
Eric V. Smith235a6f02015-09-19 14:51:32 -04005728/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5729 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005730 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005731 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005732 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005733 node if there's just an f-string (with no leading or trailing
5734 literals), or a JoinedStr node if there are multiple f-strings or
5735 any literals involved. */
5736static expr_ty
5737parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005738{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005739 int bytesmode = 0;
5740 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005741 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005742
5743 FstringParser state;
5744 FstringParser_Init(&state);
5745
5746 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005747 int this_bytesmode;
5748 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005749 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005750 const char *fstr;
5751 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005752
5753 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005754 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5755 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005756 goto error;
5757
5758 /* Check that we're not mixing bytes with unicode. */
5759 if (i != 0 && bytesmode != this_bytesmode) {
5760 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005761 /* s is NULL if the current string part is an f-string. */
5762 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005763 goto error;
5764 }
5765 bytesmode = this_bytesmode;
5766
Eric V. Smith451d0e32016-09-09 21:56:20 -04005767 if (fstr != NULL) {
5768 int result;
5769 assert(s == NULL && !bytesmode);
5770 /* This is an f-string. Parse and concatenate it. */
5771 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5772 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005773 if (result < 0)
5774 goto error;
5775 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005776 /* A string or byte string. */
5777 assert(s != NULL && fstr == NULL);
5778
Eric V. Smith451d0e32016-09-09 21:56:20 -04005779 assert(bytesmode ? PyBytes_CheckExact(s) :
5780 PyUnicode_CheckExact(s));
5781
Eric V. Smith451d0e32016-09-09 21:56:20 -04005782 if (bytesmode) {
5783 /* For bytes, concat as we go. */
5784 if (i == 0) {
5785 /* First time, just remember this value. */
5786 bytes_str = s;
5787 } else {
5788 PyBytes_ConcatAndDel(&bytes_str, s);
5789 if (!bytes_str)
5790 goto error;
5791 }
5792 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005793 /* This is a regular string. Concatenate it. */
5794 if (FstringParser_ConcatAndDel(&state, s) < 0)
5795 goto error;
5796 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005797 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005798 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005799 if (bytesmode) {
5800 /* Just return the bytes object and we're done. */
5801 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5802 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005803 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005804 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005806
Eric V. Smith235a6f02015-09-19 14:51:32 -04005807 /* We're not a bytes string, bytes_str should never have been set. */
5808 assert(bytes_str == NULL);
5809
5810 return FstringParser_Finish(&state, c, n);
5811
5812error:
5813 Py_XDECREF(bytes_str);
5814 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005816}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005817
5818PyObject *
5819_PyAST_GetDocString(asdl_seq *body)
5820{
5821 if (!asdl_seq_LEN(body)) {
5822 return NULL;
5823 }
5824 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5825 if (st->kind != Expr_kind) {
5826 return NULL;
5827 }
5828 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005829 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5830 return e->v.Constant.value;
5831 }
5832 return NULL;
5833}