blob: 1c1395f6f036d199a00a64f80dcaccfc0c8c432f [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
Serhiy Storchakaddbce132017-11-15 17:39:37 +020014#include <stdbool.h>
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Serhiy Storchaka58159ef2019-01-12 09:46:50 +020016#define MAXLEVEL 200 /* Max parentheses level */
17
Benjamin Peterson832bfe22011-08-09 16:15:04 -050018static int validate_stmts(asdl_seq *);
19static int validate_exprs(asdl_seq *, expr_context_ty, int);
20static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
21static int validate_stmt(stmt_ty);
22static int validate_expr(expr_ty, expr_context_ty);
23
24static int
25validate_comprehension(asdl_seq *gens)
26{
Victor Stinner4d73ae72018-11-22 14:45:16 +010027 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050028 if (!asdl_seq_LEN(gens)) {
29 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
30 return 0;
31 }
32 for (i = 0; i < asdl_seq_LEN(gens); i++) {
33 comprehension_ty comp = asdl_seq_GET(gens, i);
34 if (!validate_expr(comp->target, Store) ||
35 !validate_expr(comp->iter, Load) ||
36 !validate_exprs(comp->ifs, Load, 0))
37 return 0;
38 }
39 return 1;
40}
41
42static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -050043validate_keywords(asdl_seq *keywords)
44{
Victor Stinner4d73ae72018-11-22 14:45:16 +010045 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050046 for (i = 0; i < asdl_seq_LEN(keywords); i++)
47 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
48 return 0;
49 return 1;
50}
51
52static int
53validate_args(asdl_seq *args)
54{
Victor Stinner4d73ae72018-11-22 14:45:16 +010055 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050056 for (i = 0; i < asdl_seq_LEN(args); i++) {
57 arg_ty arg = asdl_seq_GET(args, i);
58 if (arg->annotation && !validate_expr(arg->annotation, Load))
59 return 0;
60 }
61 return 1;
62}
63
64static const char *
65expr_context_name(expr_context_ty ctx)
66{
67 switch (ctx) {
68 case Load:
69 return "Load";
70 case Store:
71 return "Store";
72 case Del:
73 return "Del";
74 case AugLoad:
75 return "AugLoad";
76 case AugStore:
77 return "AugStore";
Benjamin Peterson832bfe22011-08-09 16:15:04 -050078 default:
Barry Warsawb2e57942017-09-14 18:13:16 -070079 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -050080 }
81}
82
83static int
84validate_arguments(arguments_ty args)
85{
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010086 if (!validate_args(args->posonlyargs) || !validate_args(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050087 return 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +010088 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -070089 if (args->vararg && args->vararg->annotation
90 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050091 return 0;
92 }
93 if (!validate_args(args->kwonlyargs))
94 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +010095 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -070096 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -050097 return 0;
98 }
Pablo Galindo2f58a842019-05-31 14:09:49 +010099 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500100 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
101 return 0;
102 }
103 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
104 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
105 "kw_defaults on arguments");
106 return 0;
107 }
108 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
109}
110
111static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100112validate_constant(PyObject *value)
113{
114 if (value == Py_None || value == Py_Ellipsis)
115 return 1;
116
117 if (PyLong_CheckExact(value)
118 || PyFloat_CheckExact(value)
119 || PyComplex_CheckExact(value)
120 || PyBool_Check(value)
121 || PyUnicode_CheckExact(value)
122 || PyBytes_CheckExact(value))
123 return 1;
124
125 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
126 PyObject *it;
127
128 it = PyObject_GetIter(value);
129 if (it == NULL)
130 return 0;
131
132 while (1) {
133 PyObject *item = PyIter_Next(it);
134 if (item == NULL) {
135 if (PyErr_Occurred()) {
136 Py_DECREF(it);
137 return 0;
138 }
139 break;
140 }
141
142 if (!validate_constant(item)) {
143 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100144 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100145 return 0;
146 }
Victor Stinner726f6902016-01-27 00:11:47 +0100147 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100148 }
149
150 Py_DECREF(it);
151 return 1;
152 }
153
154 return 0;
155}
156
157static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500158validate_expr(expr_ty exp, expr_context_ty ctx)
159{
160 int check_ctx = 1;
161 expr_context_ty actual_ctx;
162
163 /* First check expression context. */
164 switch (exp->kind) {
165 case Attribute_kind:
166 actual_ctx = exp->v.Attribute.ctx;
167 break;
168 case Subscript_kind:
169 actual_ctx = exp->v.Subscript.ctx;
170 break;
171 case Starred_kind:
172 actual_ctx = exp->v.Starred.ctx;
173 break;
174 case Name_kind:
175 actual_ctx = exp->v.Name.ctx;
176 break;
177 case List_kind:
178 actual_ctx = exp->v.List.ctx;
179 break;
180 case Tuple_kind:
181 actual_ctx = exp->v.Tuple.ctx;
182 break;
183 default:
184 if (ctx != Load) {
185 PyErr_Format(PyExc_ValueError, "expression which can't be "
186 "assigned to in %s context", expr_context_name(ctx));
187 return 0;
188 }
189 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100190 /* set actual_ctx to prevent gcc warning */
191 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500192 }
193 if (check_ctx && actual_ctx != ctx) {
194 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
195 expr_context_name(ctx), expr_context_name(actual_ctx));
196 return 0;
197 }
198
199 /* Now validate expression. */
200 switch (exp->kind) {
201 case BoolOp_kind:
202 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
203 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
204 return 0;
205 }
206 return validate_exprs(exp->v.BoolOp.values, Load, 0);
207 case BinOp_kind:
208 return validate_expr(exp->v.BinOp.left, Load) &&
209 validate_expr(exp->v.BinOp.right, Load);
210 case UnaryOp_kind:
211 return validate_expr(exp->v.UnaryOp.operand, Load);
212 case Lambda_kind:
213 return validate_arguments(exp->v.Lambda.args) &&
214 validate_expr(exp->v.Lambda.body, Load);
215 case IfExp_kind:
216 return validate_expr(exp->v.IfExp.test, Load) &&
217 validate_expr(exp->v.IfExp.body, Load) &&
218 validate_expr(exp->v.IfExp.orelse, Load);
219 case Dict_kind:
220 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
221 PyErr_SetString(PyExc_ValueError,
222 "Dict doesn't have the same number of keys as values");
223 return 0;
224 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400225 /* null_ok=1 for keys expressions to allow dict unpacking to work in
226 dict literals, i.e. ``{**{a:b}}`` */
227 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
228 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500229 case Set_kind:
230 return validate_exprs(exp->v.Set.elts, Load, 0);
231#define COMP(NAME) \
232 case NAME ## _kind: \
233 return validate_comprehension(exp->v.NAME.generators) && \
234 validate_expr(exp->v.NAME.elt, Load);
235 COMP(ListComp)
236 COMP(SetComp)
237 COMP(GeneratorExp)
238#undef COMP
239 case DictComp_kind:
240 return validate_comprehension(exp->v.DictComp.generators) &&
241 validate_expr(exp->v.DictComp.key, Load) &&
242 validate_expr(exp->v.DictComp.value, Load);
243 case Yield_kind:
244 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500245 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000246 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400247 case Await_kind:
248 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500249 case Compare_kind:
250 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
251 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
252 return 0;
253 }
254 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
255 asdl_seq_LEN(exp->v.Compare.ops)) {
256 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
257 "of comparators and operands");
258 return 0;
259 }
260 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
261 validate_expr(exp->v.Compare.left, Load);
262 case Call_kind:
263 return validate_expr(exp->v.Call.func, Load) &&
264 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400265 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100266 case Constant_kind:
267 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100268 PyErr_Format(PyExc_TypeError,
269 "got an invalid type in Constant: %s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700270 _PyType_Name(Py_TYPE(exp->v.Constant.value)));
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100271 return 0;
272 }
273 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400274 case JoinedStr_kind:
275 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
276 case FormattedValue_kind:
277 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
278 return 0;
279 if (exp->v.FormattedValue.format_spec)
280 return validate_expr(exp->v.FormattedValue.format_spec, Load);
281 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500282 case Attribute_kind:
283 return validate_expr(exp->v.Attribute.value, Load);
284 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200285 return validate_expr(exp->v.Subscript.slice, Load) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500286 validate_expr(exp->v.Subscript.value, Load);
287 case Starred_kind:
288 return validate_expr(exp->v.Starred.value, ctx);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200289 case Slice_kind:
290 return (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) &&
291 (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) &&
292 (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500293 case List_kind:
294 return validate_exprs(exp->v.List.elts, ctx, 0);
295 case Tuple_kind:
296 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000297 case NamedExpr_kind:
298 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300299 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500300 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500301 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500302 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000303 PyErr_SetString(PyExc_SystemError, "unexpected expression");
304 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500305}
306
307static int
308validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
309{
310 if (asdl_seq_LEN(seq))
311 return 1;
312 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
313 return 0;
314}
315
316static int
317validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
318{
319 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
320 validate_exprs(targets, ctx, 0);
321}
322
323static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300324validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500325{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300326 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500327}
328
329static int
330validate_stmt(stmt_ty stmt)
331{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100332 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500333 switch (stmt->kind) {
334 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300335 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500336 validate_arguments(stmt->v.FunctionDef.args) &&
337 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
338 (!stmt->v.FunctionDef.returns ||
339 validate_expr(stmt->v.FunctionDef.returns, Load));
340 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300341 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500342 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
343 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400344 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500345 case Return_kind:
346 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
347 case Delete_kind:
348 return validate_assignlist(stmt->v.Delete.targets, Del);
349 case Assign_kind:
350 return validate_assignlist(stmt->v.Assign.targets, Store) &&
351 validate_expr(stmt->v.Assign.value, Load);
352 case AugAssign_kind:
353 return validate_expr(stmt->v.AugAssign.target, Store) &&
354 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700355 case AnnAssign_kind:
356 if (stmt->v.AnnAssign.target->kind != Name_kind &&
357 stmt->v.AnnAssign.simple) {
358 PyErr_SetString(PyExc_TypeError,
359 "AnnAssign with simple non-Name target");
360 return 0;
361 }
362 return validate_expr(stmt->v.AnnAssign.target, Store) &&
363 (!stmt->v.AnnAssign.value ||
364 validate_expr(stmt->v.AnnAssign.value, Load)) &&
365 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500366 case For_kind:
367 return validate_expr(stmt->v.For.target, Store) &&
368 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300369 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500370 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400371 case AsyncFor_kind:
372 return validate_expr(stmt->v.AsyncFor.target, Store) &&
373 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300374 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400375 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500376 case While_kind:
377 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300378 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500379 validate_stmts(stmt->v.While.orelse);
380 case If_kind:
381 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300382 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500383 validate_stmts(stmt->v.If.orelse);
384 case With_kind:
385 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
386 return 0;
387 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
388 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
389 if (!validate_expr(item->context_expr, Load) ||
390 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
391 return 0;
392 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300393 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400394 case AsyncWith_kind:
395 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
396 return 0;
397 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
398 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
399 if (!validate_expr(item->context_expr, Load) ||
400 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
401 return 0;
402 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300403 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500404 case Raise_kind:
405 if (stmt->v.Raise.exc) {
406 return validate_expr(stmt->v.Raise.exc, Load) &&
407 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
408 }
409 if (stmt->v.Raise.cause) {
410 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
411 return 0;
412 }
413 return 1;
414 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300415 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500416 return 0;
417 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
418 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
419 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
420 return 0;
421 }
422 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
423 asdl_seq_LEN(stmt->v.Try.orelse)) {
424 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
425 return 0;
426 }
427 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
428 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
429 if ((handler->v.ExceptHandler.type &&
430 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300431 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500432 return 0;
433 }
434 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
435 validate_stmts(stmt->v.Try.finalbody)) &&
436 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
437 validate_stmts(stmt->v.Try.orelse));
438 case Assert_kind:
439 return validate_expr(stmt->v.Assert.test, Load) &&
440 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
441 case Import_kind:
442 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
443 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300444 if (stmt->v.ImportFrom.level < 0) {
445 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500446 return 0;
447 }
448 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
449 case Global_kind:
450 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
451 case Nonlocal_kind:
452 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
453 case Expr_kind:
454 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400455 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300456 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400457 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
458 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
459 (!stmt->v.AsyncFunctionDef.returns ||
460 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500461 case Pass_kind:
462 case Break_kind:
463 case Continue_kind:
464 return 1;
465 default:
466 PyErr_SetString(PyExc_SystemError, "unexpected statement");
467 return 0;
468 }
469}
470
471static int
472validate_stmts(asdl_seq *seq)
473{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100474 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500475 for (i = 0; i < asdl_seq_LEN(seq); i++) {
476 stmt_ty stmt = asdl_seq_GET(seq, i);
477 if (stmt) {
478 if (!validate_stmt(stmt))
479 return 0;
480 }
481 else {
482 PyErr_SetString(PyExc_ValueError,
483 "None disallowed in statement list");
484 return 0;
485 }
486 }
487 return 1;
488}
489
490static int
491validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
492{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100493 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500494 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
495 expr_ty expr = asdl_seq_GET(exprs, i);
496 if (expr) {
497 if (!validate_expr(expr, ctx))
498 return 0;
499 }
500 else if (!null_ok) {
501 PyErr_SetString(PyExc_ValueError,
502 "None disallowed in expression list");
503 return 0;
504 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100505
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500506 }
507 return 1;
508}
509
510int
511PyAST_Validate(mod_ty mod)
512{
513 int res = 0;
514
515 switch (mod->kind) {
516 case Module_kind:
517 res = validate_stmts(mod->v.Module.body);
518 break;
519 case Interactive_kind:
520 res = validate_stmts(mod->v.Interactive.body);
521 break;
522 case Expression_kind:
523 res = validate_expr(mod->v.Expression.body, Load);
524 break;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500525 default:
526 PyErr_SetString(PyExc_SystemError, "impossible module node");
527 res = 0;
528 break;
529 }
530 return res;
531}
532
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500533/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500534#include "grammar.h"
535#include "parsetok.h"
536#include "graminit.h"
537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538/* Data structure used internally */
539struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400540 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200541 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500542 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800543 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544};
545
546static asdl_seq *seq_for_testlist(struct compiling *, const node *);
547static expr_ty ast_for_expr(struct compiling *, const node *);
548static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300549static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000550static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
551 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000552static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000553static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
guoci90fc8982018-09-11 17:45:45 -0400555static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
556static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400557
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200559static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +0200560 const node *, const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000562static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400563static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000564static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565
Nick Coghlan650f0d02007-04-15 12:05:43 +0000566#define COMP_GENEXP 0
567#define COMP_LISTCOMP 1
568#define COMP_SETCOMP 2
569
Benjamin Peterson55e00432012-01-16 17:22:31 -0500570static int
571init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000572{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500573 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
574 if (!m)
575 return 0;
576 c->c_normalize = PyObject_GetAttrString(m, "normalize");
577 Py_DECREF(m);
578 if (!c->c_normalize)
579 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500580 return 1;
581}
582
583static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400584new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500585{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400586 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500587 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000588 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500589 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500590 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000591 /* Check whether there are non-ASCII characters in the
592 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500593 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200594 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500595 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500596 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200597 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500598 }
Dino Viehland5b172c22019-09-11 08:47:17 -0700599 PyObject *form = PyUnicode_InternFromString("NFKC");
Oren Milman7dc46d82017-09-30 20:16:24 +0300600 if (form == NULL) {
601 Py_DECREF(id);
602 return NULL;
603 }
604 PyObject *args[2] = {form, id};
605 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500606 Py_DECREF(id);
Dino Viehland8d88e8c2019-09-12 15:38:13 +0100607 Py_DECREF(form);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200608 if (!id2)
609 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300610 if (!PyUnicode_Check(id2)) {
611 PyErr_Format(PyExc_TypeError,
612 "unicodedata.normalize() must return a string, not "
613 "%.200s",
Dino Viehland5b172c22019-09-11 08:47:17 -0700614 _PyType_Name(Py_TYPE(id2)));
Oren Milman7dc46d82017-09-30 20:16:24 +0300615 Py_DECREF(id2);
616 return NULL;
617 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200618 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000619 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000620 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200621 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
622 Py_DECREF(id);
623 return NULL;
624 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000625 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626}
627
Benjamin Peterson55e00432012-01-16 17:22:31 -0500628#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200631ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400633 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200634 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200636 va_start(va, errmsg);
637 errstr = PyUnicode_FromFormatV(errmsg, va);
638 va_end(va);
639 if (!errstr) {
640 return 0;
641 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200642 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000644 Py_INCREF(Py_None);
645 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 }
Ammar Askar025eb982018-09-24 17:12:49 -0400647 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200648 if (!tmp) {
649 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400650 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000651 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 Py_DECREF(errstr);
654 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400655 if (value) {
656 PyErr_SetObject(PyExc_SyntaxError, value);
657 Py_DECREF(value);
658 }
659 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660}
661
662/* num_stmts() returns number of contained statements.
663
664 Use this routine to determine how big a sequence is needed for
665 the statements in a parse tree. Its raison d'etre is this bit of
666 grammar:
667
668 stmt: simple_stmt | compound_stmt
669 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
670
671 A simple_stmt can contain multiple small_stmt elements joined
672 by semicolons. If the arg is a simple_stmt, the number of
673 small_stmt elements is returned.
674*/
675
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800676static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800677new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800678{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800679 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800680 if (res == NULL)
681 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800682 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
683 Py_DECREF(res);
684 return NULL;
685 }
686 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800687}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800688#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690static int
691num_stmts(const node *n)
692{
693 int i, l;
694 node *ch;
695
696 switch (TYPE(n)) {
697 case single_input:
698 if (TYPE(CHILD(n, 0)) == NEWLINE)
699 return 0;
700 else
701 return num_stmts(CHILD(n, 0));
702 case file_input:
703 l = 0;
704 for (i = 0; i < NCH(n); i++) {
705 ch = CHILD(n, i);
706 if (TYPE(ch) == stmt)
707 l += num_stmts(ch);
708 }
709 return l;
710 case stmt:
711 return num_stmts(CHILD(n, 0));
712 case compound_stmt:
713 return 1;
714 case simple_stmt:
715 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
716 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800717 case func_body_suite:
718 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
719 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 if (NCH(n) == 1)
721 return num_stmts(CHILD(n, 0));
722 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800723 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800725 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
726 i += 2;
727 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 l += num_stmts(CHILD(n, i));
729 return l;
730 }
731 default: {
732 char buf[128];
733
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000734 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 TYPE(n), NCH(n));
736 Py_FatalError(buf);
737 }
738 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700739 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740}
741
742/* Transform the CST rooted at node * to the appropriate AST
743*/
744
745mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200746PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
747 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000749 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800751 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 stmt_ty s;
753 node *ch;
754 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500755 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800756 asdl_seq *argtypes = NULL;
757 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400759 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200760 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400761 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800762 c.c_normalize = NULL;
Guido van Rossum77f0ed72019-05-28 16:44:58 -0700763 c.c_feature_version = flags ? flags->cf_feature_version : PY_MINOR_VERSION;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800764
765 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767
Jeremy Hyltona8293132006-02-28 17:58:27 +0000768 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 switch (TYPE(n)) {
770 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200771 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500773 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 for (i = 0; i < NCH(n) - 1; i++) {
775 ch = CHILD(n, i);
776 if (TYPE(ch) == NEWLINE)
777 continue;
778 REQ(ch, stmt);
779 num = num_stmts(ch);
780 if (num == 1) {
781 s = ast_for_stmt(&c, ch);
782 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500783 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000784 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 }
786 else {
787 ch = CHILD(ch, 0);
788 REQ(ch, simple_stmt);
789 for (j = 0; j < num; j++) {
790 s = ast_for_stmt(&c, CHILD(ch, j * 2));
791 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500792 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000793 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 }
795 }
796 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800797
798 /* Type ignores are stored under the ENDMARKER in file_input. */
799 ch = CHILD(n, NCH(n) - 1);
800 REQ(ch, ENDMARKER);
801 num = NCH(ch);
802 type_ignores = _Py_asdl_seq_new(num, arena);
803 if (!type_ignores)
804 goto out;
805
806 for (i = 0; i < num; i++) {
Michael J. Sullivan933e1502019-05-22 07:54:20 -0700807 string type_comment = new_type_comment(STR(CHILD(ch, i)), &c);
808 if (!type_comment)
809 goto out;
810 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena);
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800811 if (!ti)
812 goto out;
813 asdl_seq_SET(type_ignores, i, ti);
814 }
815
816 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 case eval_input: {
819 expr_ty testlist_ast;
820
Nick Coghlan650f0d02007-04-15 12:05:43 +0000821 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000822 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500824 goto out;
825 res = Expression(testlist_ast, arena);
826 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 }
828 case single_input:
829 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200830 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500832 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000834 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000836 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500837 goto out;
838 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 }
840 else {
841 n = CHILD(n, 0);
842 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200843 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500845 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000847 s = ast_for_stmt(&c, n);
848 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500849 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 asdl_seq_SET(stmts, 0, s);
851 }
852 else {
853 /* Only a simple_stmt can contain multiple statements. */
854 REQ(n, simple_stmt);
855 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 if (TYPE(CHILD(n, i)) == NEWLINE)
857 break;
858 s = ast_for_stmt(&c, CHILD(n, i));
859 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500860 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 asdl_seq_SET(stmts, i / 2, s);
862 }
863 }
864
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500867 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800868 case func_type_input:
869 n = CHILD(n, 0);
870 REQ(n, func_type);
871
872 if (TYPE(CHILD(n, 1)) == typelist) {
873 ch = CHILD(n, 1);
874 /* this is overly permissive -- we don't pay any attention to
875 * stars on the args -- just parse them into an ordered list */
876 num = 0;
877 for (i = 0; i < NCH(ch); i++) {
878 if (TYPE(CHILD(ch, i)) == test) {
879 num++;
880 }
881 }
882
883 argtypes = _Py_asdl_seq_new(num, arena);
884 if (!argtypes)
885 goto out;
886
887 j = 0;
888 for (i = 0; i < NCH(ch); i++) {
889 if (TYPE(CHILD(ch, i)) == test) {
890 arg = ast_for_expr(&c, CHILD(ch, i));
891 if (!arg)
892 goto out;
893 asdl_seq_SET(argtypes, j++, arg);
894 }
895 }
896 }
897 else {
898 argtypes = _Py_asdl_seq_new(0, arena);
899 if (!argtypes)
900 goto out;
901 }
902
903 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
904 if (!ret)
905 goto out;
906 res = FunctionType(argtypes, ret, arena);
907 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000909 PyErr_Format(PyExc_SystemError,
910 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500911 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500913 out:
914 if (c.c_normalize) {
915 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500916 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500917 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918}
919
Victor Stinner14e461d2013-08-26 22:28:21 +0200920mod_ty
921PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
922 PyArena *arena)
923{
924 mod_ty mod;
925 PyObject *filename;
926 filename = PyUnicode_DecodeFSDefault(filename_str);
927 if (filename == NULL)
928 return NULL;
929 mod = PyAST_FromNodeObject(n, flags, filename, arena);
930 Py_DECREF(filename);
931 return mod;
932
933}
934
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
936*/
937
938static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800939get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940{
941 switch (TYPE(n)) {
942 case VBAR:
943 return BitOr;
944 case CIRCUMFLEX:
945 return BitXor;
946 case AMPER:
947 return BitAnd;
948 case LEFTSHIFT:
949 return LShift;
950 case RIGHTSHIFT:
951 return RShift;
952 case PLUS:
953 return Add;
954 case MINUS:
955 return Sub;
956 case STAR:
957 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400958 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800959 if (c->c_feature_version < 5) {
960 ast_error(c, n,
961 "The '@' operator is only supported in Python 3.5 and greater");
962 return (operator_ty)0;
963 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400964 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 case SLASH:
966 return Div;
967 case DOUBLESLASH:
968 return FloorDiv;
969 case PERCENT:
970 return Mod;
971 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000972 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 }
974}
975
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200976static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000977 "None",
978 "True",
979 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200980 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000981 NULL,
982};
983
984static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400985forbidden_name(struct compiling *c, identifier name, const node *n,
986 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000987{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000988 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200989 const char * const *p = FORBIDDEN;
990 if (!full_checks) {
991 /* In most cases, the parser will protect True, False, and None
992 from being assign to. */
993 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000994 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200995 for (; *p; p++) {
996 if (_PyUnicode_EqualToASCIIString(name, *p)) {
997 ast_error(c, n, "cannot assign to %U", name);
998 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000999 }
1000 }
1001 return 0;
1002}
1003
Serhiy Storchakab619b092018-11-27 09:40:29 +02001004static expr_ty
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001005copy_location(expr_ty e, const node *n, const node *end)
Serhiy Storchakab619b092018-11-27 09:40:29 +02001006{
1007 if (e) {
1008 e->lineno = LINENO(n);
1009 e->col_offset = n->n_col_offset;
Guido van Rossuma796d8e2020-01-09 11:18:47 -08001010 e->end_lineno = end->n_end_lineno;
1011 e->end_col_offset = end->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001012 }
1013 return e;
1014}
1015
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001016static const char *
1017get_expr_name(expr_ty e)
1018{
1019 switch (e->kind) {
1020 case Attribute_kind:
1021 return "attribute";
1022 case Subscript_kind:
1023 return "subscript";
1024 case Starred_kind:
1025 return "starred";
1026 case Name_kind:
1027 return "name";
1028 case List_kind:
1029 return "list";
1030 case Tuple_kind:
1031 return "tuple";
1032 case Lambda_kind:
1033 return "lambda";
1034 case Call_kind:
1035 return "function call";
1036 case BoolOp_kind:
1037 case BinOp_kind:
1038 case UnaryOp_kind:
1039 return "operator";
1040 case GeneratorExp_kind:
1041 return "generator expression";
1042 case Yield_kind:
1043 case YieldFrom_kind:
1044 return "yield expression";
1045 case Await_kind:
1046 return "await expression";
1047 case ListComp_kind:
1048 return "list comprehension";
1049 case SetComp_kind:
1050 return "set comprehension";
1051 case DictComp_kind:
1052 return "dict comprehension";
1053 case Dict_kind:
1054 return "dict display";
1055 case Set_kind:
1056 return "set display";
1057 case JoinedStr_kind:
1058 case FormattedValue_kind:
1059 return "f-string expression";
1060 case Constant_kind: {
1061 PyObject *value = e->v.Constant.value;
1062 if (value == Py_None) {
1063 return "None";
1064 }
1065 if (value == Py_False) {
1066 return "False";
1067 }
1068 if (value == Py_True) {
1069 return "True";
1070 }
1071 if (value == Py_Ellipsis) {
1072 return "Ellipsis";
1073 }
1074 return "literal";
1075 }
1076 case Compare_kind:
1077 return "comparison";
1078 case IfExp_kind:
1079 return "conditional expression";
1080 case NamedExpr_kind:
1081 return "named expression";
1082 default:
1083 PyErr_Format(PyExc_SystemError,
1084 "unexpected expression in assignment %d (line %d)",
1085 e->kind, e->lineno);
1086 return NULL;
1087 }
1088}
1089
Jeremy Hyltona8293132006-02-28 17:58:27 +00001090/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091
1092 Only sets context for expr kinds that "can appear in assignment context"
1093 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1094 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095*/
1096
1097static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001098set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099{
1100 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001101
1102 /* The ast defines augmented store and load contexts, but the
1103 implementation here doesn't actually use them. The code may be
1104 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001105 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001106 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001107 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001108 */
1109 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110
1111 switch (e->kind) {
1112 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001113 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001114 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001115 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001116 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001118 e->v.Subscript.ctx = ctx;
1119 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001120 case Starred_kind:
1121 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001122 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001123 return 0;
1124 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001126 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001127 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001128 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001129 }
1130 e->v.Name.ctx = ctx;
1131 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001133 e->v.List.ctx = ctx;
1134 s = e->v.List.elts;
1135 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001137 e->v.Tuple.ctx = ctx;
1138 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001140 default: {
1141 const char *expr_name = get_expr_name(e);
1142 if (expr_name != NULL) {
1143 ast_error(c, n, "cannot %s %s",
1144 ctx == Store ? "assign to" : "delete",
1145 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001146 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001147 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001148 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001149 }
1150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 */
1154 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001155 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156
Thomas Wouters89f507f2006-12-13 04:49:30 +00001157 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001158 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 return 0;
1160 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 }
1162 return 1;
1163}
1164
1165static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001166ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167{
1168 REQ(n, augassign);
1169 n = CHILD(n, 0);
1170 switch (STR(n)[0]) {
1171 case '+':
1172 return Add;
1173 case '-':
1174 return Sub;
1175 case '/':
1176 if (STR(n)[1] == '/')
1177 return FloorDiv;
1178 else
1179 return Div;
1180 case '%':
1181 return Mod;
1182 case '<':
1183 return LShift;
1184 case '>':
1185 return RShift;
1186 case '&':
1187 return BitAnd;
1188 case '^':
1189 return BitXor;
1190 case '|':
1191 return BitOr;
1192 case '*':
1193 if (STR(n)[1] == '*')
1194 return Pow;
1195 else
1196 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001197 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001198 if (c->c_feature_version < 5) {
1199 ast_error(c, n,
1200 "The '@' operator is only supported in Python 3.5 and greater");
1201 return (operator_ty)0;
1202 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001203 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001205 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001206 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 }
1208}
1209
1210static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001211ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001213 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 |'is' 'not'
1215 */
1216 REQ(n, comp_op);
1217 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001218 n = CHILD(n, 0);
1219 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 case LESS:
1221 return Lt;
1222 case GREATER:
1223 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001224 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 return Eq;
1226 case LESSEQUAL:
1227 return LtE;
1228 case GREATEREQUAL:
1229 return GtE;
1230 case NOTEQUAL:
1231 return NotEq;
1232 case NAME:
1233 if (strcmp(STR(n), "in") == 0)
1234 return In;
1235 if (strcmp(STR(n), "is") == 0)
1236 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001237 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001239 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001241 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001242 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 }
1244 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001245 /* handle "not in" and "is not" */
1246 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 case NAME:
1248 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1249 return NotIn;
1250 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1251 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001252 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001254 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001256 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 }
Neal Norwitz79792652005-11-14 04:25:03 +00001259 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262}
1263
1264static asdl_seq *
1265seq_for_testlist(struct compiling *c, const node *n)
1266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001268 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1269 */
Armin Rigo31441302005-10-21 12:57:31 +00001270 asdl_seq *seq;
1271 expr_ty expression;
1272 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001273 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001275 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 if (!seq)
1277 return NULL;
1278
1279 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001281 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282
Benjamin Peterson4905e802009-09-27 02:43:28 +00001283 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001284 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286
1287 assert(i / 2 < seq->size);
1288 asdl_seq_SET(seq, i / 2, expression);
1289 }
1290 return seq;
1291}
1292
Neal Norwitzc1505362006-12-28 06:47:50 +00001293static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001294ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001295{
1296 identifier name;
1297 expr_ty annotation = NULL;
1298 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001299 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001300
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001301 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001302 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001303 name = NEW_IDENTIFIER(ch);
1304 if (!name)
1305 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001306 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001307 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001308
1309 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1310 annotation = ast_for_expr(c, CHILD(n, 2));
1311 if (!annotation)
1312 return NULL;
1313 }
1314
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001315 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001316 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001317 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001318 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001319 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320}
1321
Guido van Rossum4f72a782006-10-27 23:31:49 +00001322/* returns -1 if failed to handle keyword only arguments
1323 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001324 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001325 ^^^
1326 start pointing here
1327 */
1328static int
1329handle_keywordonly_args(struct compiling *c, const node *n, int start,
1330 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1331{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001332 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001333 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001334 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001335 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001336 int i = start;
1337 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001338
1339 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001340 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001341 return -1;
1342 }
1343 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001344 while (i < NCH(n)) {
1345 ch = CHILD(n, i);
1346 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001347 case vfpdef:
1348 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001349 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001351 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001352 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001353 asdl_seq_SET(kwdefaults, j, expression);
1354 i += 2; /* '=' and test */
1355 }
1356 else { /* setting NULL if no default value exists */
1357 asdl_seq_SET(kwdefaults, j, NULL);
1358 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001359 if (NCH(ch) == 3) {
1360 /* ch is NAME ':' test */
1361 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001362 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001363 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001364 }
1365 else {
1366 annotation = NULL;
1367 }
1368 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001369 argname = NEW_IDENTIFIER(ch);
1370 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001372 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001373 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001374 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001375 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001376 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001377 if (!arg)
1378 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001379 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001380 i += 1; /* the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001381 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001382 i += 1; /* the comma, if present */
1383 break;
1384 case TYPE_COMMENT:
1385 /* arg will be equal to the last argument processed */
1386 arg->type_comment = NEW_TYPE_COMMENT(ch);
1387 if (!arg->type_comment)
1388 goto error;
1389 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001390 break;
1391 case DOUBLESTAR:
1392 return i;
1393 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001394 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 goto error;
1396 }
1397 }
1398 return i;
1399 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001401}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402
Jeremy Hyltona8293132006-02-28 17:58:27 +00001403/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404
1405static arguments_ty
1406ast_for_arguments(struct compiling *c, const node *n)
1407{
Neal Norwitzc1505362006-12-28 06:47:50 +00001408 /* This function handles both typedargslist (function definition)
1409 and varargslist (lambda definition).
1410
1411 parameters: '(' [typedargslist] ')'
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001412
1413 The following definition for typedarglist is equivalent to this set of rules:
1414
1415 arguments = argument (',' [TYPE_COMMENT] argument)*
1416 argument = tfpdef ['=' test]
1417 kwargs = '**' tfpdef [','] [TYPE_COMMENT]
1418 args = '*' [tfpdef]
1419 kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [','
1420 [TYPE_COMMENT] [kwargs]])
1421 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1422 poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [','
1423 [TYPE_COMMENT] [args_kwonly_kwargs]])
1424 typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1425 typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT]
1426 typedargslist_no_posonly]])|(typedargslist_no_posonly)"
1427
1428 typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1429 ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ','
1430 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1431 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1432 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1433 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1434 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1435 '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (','
1436 [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*'
1437 [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [','
1438 [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [',']
1439 [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])*
1440 (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) |
1441 '**' tfpdef [','] [TYPE_COMMENT]))
1442
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001443 tfpdef: NAME [':' test]
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001444
1445 The following definition for varargslist is equivalent to this set of rules:
1446
1447 arguments = argument (',' argument )*
1448 argument = vfpdef ['=' test]
1449 kwargs = '**' vfpdef [',']
1450 args = '*' [vfpdef]
1451 kwonly_kwargs = (',' argument )* [',' [kwargs]]
1452 args_kwonly_kwargs = args kwonly_kwargs | kwargs
1453 poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]]
1454 vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs
1455 varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] |
1456 (vararglist_no_posonly)
1457
1458 varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['='
1459 test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [','
1460 ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])*
1461 [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef
1462 ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1463 | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef
1464 [',']]] | '**' vfpdef [','])
1465
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001466 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001467
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001469 int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001470 int nposdefaults = 0, found_default = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001471 asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001472 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001473 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 node *ch;
1475
1476 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001477 if (NCH(n) == 2) /* () as argument list */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001478 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001479 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001481 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482
Jeremy Hyltone921e022008-07-17 16:37:17 +00001483 /* First count the number of positional args & defaults. The
1484 variable i is the loop index for this for loop and the next.
1485 The next loop picks up where the first leaves off.
1486 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001488 ch = CHILD(n, i);
1489 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001490 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001491 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001492 if (i < NCH(n) && /* skip argument following star */
1493 (TYPE(CHILD(n, i)) == tfpdef ||
1494 TYPE(CHILD(n, i)) == vfpdef)) {
1495 i++;
1496 }
1497 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001498 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001499 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001500 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001501 if (TYPE(ch) == EQUAL) nposdefaults++;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001502 if (TYPE(ch) == SLASH ) {
1503 nposonlyargs = nposargs;
1504 nposargs = 0;
1505 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001508 defaults for keyword only args */
1509 for ( ; i < NCH(n); ++i) {
1510 ch = CHILD(n, i);
1511 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001512 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001513 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001514 posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL);
1515 if (!posonlyargs && nposonlyargs) {
1516 return NULL;
1517 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001518 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001519 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001520 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001521 kwonlyargs = (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 (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001524 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001526 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001527 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001528 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001530 since we set NULL as default for keyword only argument w/o default
1531 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001532 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001533 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001534 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001535 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001536
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001537 /* tfpdef: NAME [':' test]
1538 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 */
1540 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001541 j = 0; /* index for defaults */
1542 k = 0; /* index for args */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001543 l = 0; /* index for posonlyargs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001545 ch = CHILD(n, i);
1546 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001547 case tfpdef:
1548 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1550 anything other than EQUAL or a comma? */
1551 /* XXX Should NCH(n) check be made a separate check? */
1552 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001553 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1554 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001555 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001556 assert(posdefaults != NULL);
1557 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001559 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001561 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001562 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001563 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001564 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001565 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001566 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001567 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001568 return NULL;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001569 if (l < nposonlyargs) {
1570 asdl_seq_SET(posonlyargs, l++, arg);
1571 } else {
1572 asdl_seq_SET(posargs, k++, arg);
1573 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001574 i += 1; /* the name */
1575 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1576 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 break;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001578 case SLASH:
1579 /* Advance the slash and the comma. If there are more names
1580 * after the slash there will be a comma so we are advancing
1581 * the correct number of nodes. If the slash is the last item,
1582 * we will be advancing an extra token but then * i > NCH(n)
1583 * and the enclosing while will finish correctly. */
1584 i += 2;
1585 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001587 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001588 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1589 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001590 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001591 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001592 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001593 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001594 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001595 if (TYPE(ch) == COMMA) {
1596 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001597 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001598
1599 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1600 ast_error(c, CHILD(n, i),
1601 "bare * has associated type comment");
1602 return NULL;
1603 }
1604
Guido van Rossum4f72a782006-10-27 23:31:49 +00001605 res = handle_keywordonly_args(c, n, i,
1606 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001607 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001608 i = res; /* res has new position to process */
1609 }
1610 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001611 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001612 if (!vararg)
1613 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001614
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001615 i += 2; /* the star and the name */
1616 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1617 i += 1; /* the comma, if present */
1618
1619 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1620 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1621 if (!vararg->type_comment)
1622 return NULL;
1623 i += 1;
1624 }
1625
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001626 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1627 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001628 int res = 0;
1629 res = handle_keywordonly_args(c, n, i,
1630 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001631 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001632 i = res; /* res has new position to process */
1633 }
1634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635 break;
1636 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001637 ch = CHILD(n, i+1); /* tfpdef */
1638 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001639 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001640 if (!kwarg)
1641 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001642 i += 2; /* the double star and the name */
Brad Larsena4d78362019-04-01 10:36:05 -04001643 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001644 i += 1; /* the comma, if present */
1645 break;
1646 case TYPE_COMMENT:
1647 assert(i);
1648
1649 if (kwarg)
1650 arg = kwarg;
1651
1652 /* arg will be equal to the last argument processed */
1653 arg->type_comment = NEW_TYPE_COMMENT(ch);
1654 if (!arg->type_comment)
1655 return NULL;
1656 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 break;
1658 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001659 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 "unexpected node in varargslist: %d @ %d",
1661 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001662 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664 }
Pablo Galindocd6e83b2019-07-15 01:32:18 +02001665 return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666}
1667
1668static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669ast_for_decorator(struct compiling *c, const node *n)
1670{
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001671 /* decorator: '@' namedexpr_test NEWLINE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001674 REQ(CHILD(n, 0), AT);
Brandt Bucherbe501ca2020-03-03 14:25:44 -08001675 REQ(CHILD(n, 2), NEWLINE);
1676
1677 return ast_for_expr(c, CHILD(n, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678}
1679
1680static asdl_seq*
1681ast_for_decorators(struct compiling *c, const node *n)
1682{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001683 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001684 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001688 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 if (!decorator_seq)
1690 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001693 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001694 if (!d)
1695 return NULL;
1696 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 }
1698 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699}
1700
1701static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001702ast_for_funcdef_impl(struct compiling *c, const node *n0,
1703 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001705 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001706 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001707 identifier name;
1708 arguments_ty args;
1709 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001710 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001711 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001712 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001713 node *tc;
1714 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715
Guido van Rossum495da292019-03-07 12:38:08 -08001716 if (is_async && c->c_feature_version < 5) {
1717 ast_error(c, n,
1718 "Async functions are only supported in Python 3.5 and greater");
1719 return NULL;
1720 }
1721
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 REQ(n, funcdef);
1723
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 name = NEW_IDENTIFIER(CHILD(n, name_i));
1725 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001726 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001727 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001728 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1730 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001731 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001732 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1733 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1734 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001735 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001736 name_i += 2;
1737 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001738 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1739 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1740 if (!type_comment)
1741 return NULL;
1742 name_i += 1;
1743 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001744 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001746 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001747 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001749 if (NCH(CHILD(n, name_i + 3)) > 1) {
1750 /* Check if the suite has a type comment in it. */
1751 tc = CHILD(CHILD(n, name_i + 3), 1);
1752
1753 if (TYPE(tc) == TYPE_COMMENT) {
1754 if (type_comment != NULL) {
1755 ast_error(c, n, "Cannot have two type comments on def");
1756 return NULL;
1757 }
1758 type_comment = NEW_TYPE_COMMENT(tc);
1759 if (!type_comment)
1760 return NULL;
1761 }
1762 }
1763
Yury Selivanov75445082015-05-11 22:57:16 -04001764 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001765 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001766 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001767 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001768 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001769 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001770}
1771
1772static stmt_ty
1773ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1774{
Guido van Rossum495da292019-03-07 12:38:08 -08001775 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001776 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001777 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001778 REQ(CHILD(n, 1), funcdef);
1779
guoci90fc8982018-09-11 17:45:45 -04001780 return ast_for_funcdef_impl(c, n, decorator_seq,
1781 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001782}
1783
1784static stmt_ty
1785ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1786{
1787 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1788 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001789 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001790}
1791
1792
1793static stmt_ty
1794ast_for_async_stmt(struct compiling *c, const node *n)
1795{
Guido van Rossum495da292019-03-07 12:38:08 -08001796 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001797 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001798 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001799
1800 switch (TYPE(CHILD(n, 1))) {
1801 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001802 return ast_for_funcdef_impl(c, n, NULL,
1803 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001804 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001805 return ast_for_with_stmt(c, n,
1806 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001807
1808 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001809 return ast_for_for_stmt(c, n,
1810 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001811
1812 default:
1813 PyErr_Format(PyExc_SystemError,
1814 "invalid async stament: %s",
1815 STR(CHILD(n, 1)));
1816 return NULL;
1817 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818}
1819
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001820static stmt_ty
1821ast_for_decorated(struct compiling *c, const node *n)
1822{
Yury Selivanov75445082015-05-11 22:57:16 -04001823 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001824 stmt_ty thing = NULL;
1825 asdl_seq *decorator_seq = NULL;
1826
1827 REQ(n, decorated);
1828
1829 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1830 if (!decorator_seq)
1831 return NULL;
1832
1833 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001834 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001836
1837 if (TYPE(CHILD(n, 1)) == funcdef) {
1838 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1839 } else if (TYPE(CHILD(n, 1)) == classdef) {
1840 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001841 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1842 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001843 }
1844 return thing;
1845}
1846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001848ast_for_namedexpr(struct compiling *c, const node *n)
1849{
Guido van Rossumb08d3f72019-12-15 10:00:33 -08001850 /* namedexpr_test: test [':=' test]
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001851 argument: ( test [comp_for] |
1852 test ':=' test |
1853 test '=' test |
1854 '**' test |
1855 '*' test )
1856 */
1857 expr_ty target, value;
1858
1859 target = ast_for_expr(c, CHILD(n, 0));
1860 if (!target)
1861 return NULL;
1862
1863 value = ast_for_expr(c, CHILD(n, 2));
1864 if (!value)
1865 return NULL;
1866
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001867 if (target->kind != Name_kind) {
1868 const char *expr_name = get_expr_name(target);
1869 if (expr_name != NULL) {
Ned Batchelder37143a82019-12-31 21:40:58 -05001870 ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001871 }
1872 return NULL;
1873 }
1874
1875 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001876 return NULL;
1877
1878 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1879 n->n_end_col_offset, c->c_arena);
1880}
1881
1882static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883ast_for_lambdef(struct compiling *c, const node *n)
1884{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001885 /* lambdef: 'lambda' [varargslist] ':' test
1886 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 arguments_ty args;
1888 expr_ty expression;
1889
1890 if (NCH(n) == 3) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001891 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 if (!args)
1893 return NULL;
1894 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 }
1898 else {
1899 args = ast_for_arguments(c, CHILD(n, 1));
1900 if (!args)
1901 return NULL;
1902 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001903 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 }
1906
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001907 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1908 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909}
1910
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001911static expr_ty
1912ast_for_ifexpr(struct compiling *c, const node *n)
1913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001915 expr_ty expression, body, orelse;
1916
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001917 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001918 body = ast_for_expr(c, CHILD(n, 0));
1919 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001920 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001921 expression = ast_for_expr(c, CHILD(n, 2));
1922 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001923 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001924 orelse = ast_for_expr(c, CHILD(n, 4));
1925 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001926 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001927 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001928 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001929 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001930}
1931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001933 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934
Nick Coghlan650f0d02007-04-15 12:05:43 +00001935 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936*/
1937
1938static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001939count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001941 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942
Guido van Rossumd8faa362007-04-27 19:54:29 +00001943 count_comp_for:
1944 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001945 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001946 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001947 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001948 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001949 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001950 else if (NCH(n) == 1) {
1951 n = CHILD(n, 0);
1952 }
1953 else {
1954 goto error;
1955 }
1956 if (NCH(n) == (5)) {
1957 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001958 }
1959 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001960 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001961 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001962 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001963 REQ(n, comp_iter);
1964 n = CHILD(n, 0);
1965 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001966 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001967 else if (TYPE(n) == comp_if) {
1968 if (NCH(n) == 3) {
1969 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001970 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001971 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001972 else
1973 return n_fors;
1974 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001975
Jelle Zijlstraac317702017-10-05 20:24:46 -07001976 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001977 /* Should never be reached */
1978 PyErr_SetString(PyExc_SystemError,
1979 "logic error in count_comp_fors");
1980 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981}
1982
Nick Coghlan650f0d02007-04-15 12:05:43 +00001983/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984
Nick Coghlan650f0d02007-04-15 12:05:43 +00001985 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986*/
1987
1988static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001989count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001991 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992
Guido van Rossumd8faa362007-04-27 19:54:29 +00001993 while (1) {
1994 REQ(n, comp_iter);
1995 if (TYPE(CHILD(n, 0)) == comp_for)
1996 return n_ifs;
1997 n = CHILD(n, 0);
1998 REQ(n, comp_if);
1999 n_ifs++;
2000 if (NCH(n) == 2)
2001 return n_ifs;
2002 n = CHILD(n, 2);
2003 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004}
2005
Guido van Rossum992d4a32007-07-11 13:09:30 +00002006static asdl_seq *
2007ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002010 asdl_seq *comps;
2011
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002012 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 if (n_fors == -1)
2014 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002015
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002016 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002017 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002021 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002023 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002024 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002025 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002026 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027
Guido van Rossum992d4a32007-07-11 13:09:30 +00002028 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029
Jelle Zijlstraac317702017-10-05 20:24:46 -07002030 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002031 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002032 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002033 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002034 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002035 else {
2036 sync_n = CHILD(n, 0);
2037 }
2038 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002039
Guido van Rossum495da292019-03-07 12:38:08 -08002040 /* Async comprehensions only allowed in Python 3.6 and greater */
2041 if (is_async && c->c_feature_version < 6) {
2042 ast_error(c, n,
2043 "Async comprehensions are only supported in Python 3.6 and greater");
2044 return NULL;
2045 }
2046
Jelle Zijlstraac317702017-10-05 20:24:46 -07002047 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002048 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002049 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002051 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002052 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002054
Thomas Wouters89f507f2006-12-13 04:49:30 +00002055 /* Check the # of children rather than the length of t, since
2056 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002057 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002058 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002059 comp = comprehension(first, expression, NULL,
2060 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002062 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2063 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2064 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002065 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002066 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002068
Jelle Zijlstraac317702017-10-05 20:24:46 -07002069 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 int j, n_ifs;
2071 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072
Jelle Zijlstraac317702017-10-05 20:24:46 -07002073 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002074 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002075 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002077
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002078 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002079 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002083 REQ(n, comp_iter);
2084 n = CHILD(n, 0);
2085 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086
Guido van Rossum992d4a32007-07-11 13:09:30 +00002087 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002088 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002089 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002090 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002091 if (NCH(n) == 3)
2092 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002094 /* on exit, must guarantee that n is a comp_for */
2095 if (TYPE(n) == comp_iter)
2096 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002097 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002099 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002101 return comps;
2102}
2103
2104static expr_ty
2105ast_for_itercomp(struct compiling *c, const node *n, int type)
2106{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002107 /* testlist_comp: (test|star_expr)
2108 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002109 expr_ty elt;
2110 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002111 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112
Guido van Rossum992d4a32007-07-11 13:09:30 +00002113 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002115 ch = CHILD(n, 0);
2116 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002117 if (!elt)
2118 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002119 if (elt->kind == Starred_kind) {
2120 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2121 return NULL;
2122 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123
Guido van Rossum992d4a32007-07-11 13:09:30 +00002124 comps = ast_for_comprehension(c, CHILD(n, 1));
2125 if (!comps)
2126 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002127
2128 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002129 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2130 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002131 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002132 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2133 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002134 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002135 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2136 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002137 else
2138 /* Should never happen */
2139 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140}
2141
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002142/* Fills in the key, value pair corresponding to the dict element. In case
2143 * of an unpacking, key is NULL. *i is advanced by the number of ast
2144 * elements. Iff successful, nonzero is returned.
2145 */
2146static int
2147ast_for_dictelement(struct compiling *c, const node *n, int *i,
2148 expr_ty *key, expr_ty *value)
2149{
2150 expr_ty expression;
2151 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2152 assert(NCH(n) - *i >= 2);
2153
2154 expression = ast_for_expr(c, CHILD(n, *i + 1));
2155 if (!expression)
2156 return 0;
2157 *key = NULL;
2158 *value = expression;
2159
2160 *i += 2;
2161 }
2162 else {
2163 assert(NCH(n) - *i >= 3);
2164
2165 expression = ast_for_expr(c, CHILD(n, *i));
2166 if (!expression)
2167 return 0;
2168 *key = expression;
2169
2170 REQ(CHILD(n, *i + 1), COLON);
2171
2172 expression = ast_for_expr(c, CHILD(n, *i + 2));
2173 if (!expression)
2174 return 0;
2175 *value = expression;
2176
2177 *i += 3;
2178 }
2179 return 1;
2180}
2181
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002183ast_for_dictcomp(struct compiling *c, const node *n)
2184{
2185 expr_ty key, value;
2186 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002187 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002189 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002190 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191 assert(key);
2192 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002194 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002195 if (!comps)
2196 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002198 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2199 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002200}
2201
2202static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002203ast_for_dictdisplay(struct compiling *c, const node *n)
2204{
2205 int i;
2206 int j;
2207 int size;
2208 asdl_seq *keys, *values;
2209
2210 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2211 keys = _Py_asdl_seq_new(size, c->c_arena);
2212 if (!keys)
2213 return NULL;
2214
2215 values = _Py_asdl_seq_new(size, c->c_arena);
2216 if (!values)
2217 return NULL;
2218
2219 j = 0;
2220 for (i = 0; i < NCH(n); i++) {
2221 expr_ty key, value;
2222
2223 if (!ast_for_dictelement(c, n, &i, &key, &value))
2224 return NULL;
2225 asdl_seq_SET(keys, j, key);
2226 asdl_seq_SET(values, j, value);
2227
2228 j++;
2229 }
2230 keys->size = j;
2231 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002232 return Dict(keys, values, LINENO(n), n->n_col_offset,
2233 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002234}
2235
2236static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002237ast_for_genexp(struct compiling *c, const node *n)
2238{
2239 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002240 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002241}
2242
2243static expr_ty
2244ast_for_listcomp(struct compiling *c, const node *n)
2245{
2246 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002247 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002248}
2249
2250static expr_ty
2251ast_for_setcomp(struct compiling *c, const node *n)
2252{
2253 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002254 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002255}
2256
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002257static expr_ty
2258ast_for_setdisplay(struct compiling *c, const node *n)
2259{
2260 int i;
2261 int size;
2262 asdl_seq *elts;
2263
2264 assert(TYPE(n) == (dictorsetmaker));
2265 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2266 elts = _Py_asdl_seq_new(size, c->c_arena);
2267 if (!elts)
2268 return NULL;
2269 for (i = 0; i < NCH(n); i += 2) {
2270 expr_ty expression;
2271 expression = ast_for_expr(c, CHILD(n, i));
2272 if (!expression)
2273 return NULL;
2274 asdl_seq_SET(elts, i / 2, expression);
2275 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002276 return Set(elts, LINENO(n), n->n_col_offset,
2277 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002278}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002279
2280static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281ast_for_atom(struct compiling *c, const node *n)
2282{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002283 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2284 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002285 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 */
2287 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002290 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002291 PyObject *name;
2292 const char *s = STR(ch);
2293 size_t len = strlen(s);
2294 if (len >= 4 && len <= 5) {
2295 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002296 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002297 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002298 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002299 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002300 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002301 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002302 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002303 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002304 }
2305 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002306 if (!name)
2307 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002308 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002309 return Name(name, Load, LINENO(n), n->n_col_offset,
2310 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002311 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002313 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002314 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002315 const char *errtype = NULL;
2316 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2317 errtype = "unicode error";
2318 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2319 errtype = "value error";
2320 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002321 PyObject *type, *value, *tback, *errstr;
2322 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002323 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002324 if (errstr) {
2325 ast_error(c, n, "(%s) %U", errtype, errstr);
2326 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002327 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002328 else {
2329 PyErr_Clear();
2330 ast_error(c, n, "(%s) unknown error", errtype);
2331 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002332 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002333 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002334 Py_XDECREF(tback);
2335 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002336 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002337 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002338 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 }
2340 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002341 PyObject *pynum;
2342 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2343 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2344 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2345 ast_error(c, ch,
2346 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2347 return NULL;
2348 }
2349 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002350 if (!pynum)
2351 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002352
Victor Stinner43d81952013-07-17 00:57:58 +02002353 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2354 Py_DECREF(pynum);
2355 return NULL;
2356 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002357 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002358 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 }
Georg Brandldde00282007-03-18 19:01:53 +00002360 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002361 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002362 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002363 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002364 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365
Thomas Wouters89f507f2006-12-13 04:49:30 +00002366 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002367 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2368 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369
Thomas Wouters89f507f2006-12-13 04:49:30 +00002370 if (TYPE(ch) == yield_expr)
2371 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002374 if (NCH(ch) == 1) {
2375 return ast_for_testlist(c, ch);
2376 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002377
Serhiy Storchakab619b092018-11-27 09:40:29 +02002378 if (TYPE(CHILD(ch, 1)) == comp_for) {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002379 return copy_location(ast_for_genexp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002380 }
2381 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002382 return copy_location(ast_for_testlist(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002383 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002385 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386
Thomas Wouters89f507f2006-12-13 04:49:30 +00002387 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002388 return List(NULL, Load, LINENO(n), n->n_col_offset,
2389 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390
Nick Coghlan650f0d02007-04-15 12:05:43 +00002391 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002392 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2393 asdl_seq *elts = seq_for_testlist(c, ch);
2394 if (!elts)
2395 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002396
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002397 return List(elts, Load, LINENO(n), n->n_col_offset,
2398 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002399 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002400 else {
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002401 return copy_location(ast_for_listcomp(c, ch), n, n);
Serhiy Storchakab619b092018-11-27 09:40:29 +02002402 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002404 /* dictorsetmaker: ( ((test ':' test | '**' test)
2405 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2406 * ((test | '*' test)
2407 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002408 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002409 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002410 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002411 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002412 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2413 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002414 }
2415 else {
2416 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2417 if (NCH(ch) == 1 ||
2418 (NCH(ch) > 1 &&
2419 TYPE(CHILD(ch, 1)) == COMMA)) {
2420 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002421 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002422 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002423 else if (NCH(ch) > 1 &&
2424 TYPE(CHILD(ch, 1)) == comp_for) {
2425 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002426 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002427 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002428 else if (NCH(ch) > 3 - is_dict &&
2429 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2430 /* It's a dictionary comprehension. */
2431 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002432 ast_error(c, n,
2433 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002434 return NULL;
2435 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002436 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002437 }
2438 else {
2439 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002440 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002441 }
Guido van Rossuma796d8e2020-01-09 11:18:47 -08002442 return copy_location(res, n, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002443 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002446 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2447 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 }
2449}
2450
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002451static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452ast_for_slice(struct compiling *c, const node *n)
2453{
2454 node *ch;
2455 expr_ty lower = NULL, upper = NULL, step = NULL;
2456
2457 REQ(n, subscript);
2458
2459 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002460 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 sliceop: ':' [test]
2462 */
2463 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 if (NCH(n) == 1 && TYPE(ch) == test) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002465 return ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 }
2467
2468 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002469 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 if (!lower)
2471 return NULL;
2472 }
2473
2474 /* If there's an upper bound it's in the second or third position. */
2475 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002476 if (NCH(n) > 1) {
2477 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478
Thomas Wouters89f507f2006-12-13 04:49:30 +00002479 if (TYPE(n2) == test) {
2480 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 if (!upper)
2482 return NULL;
2483 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002486 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487
Thomas Wouters89f507f2006-12-13 04:49:30 +00002488 if (TYPE(n2) == test) {
2489 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 if (!upper)
2491 return NULL;
2492 }
2493 }
2494
2495 ch = CHILD(n, NCH(n) - 1);
2496 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002497 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002498 ch = CHILD(ch, 1);
2499 if (TYPE(ch) == test) {
2500 step = ast_for_expr(c, ch);
2501 if (!step)
2502 return NULL;
2503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 }
2505 }
2506
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002507 return Slice(lower, upper, step, LINENO(n), n->n_col_offset,
2508 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509}
2510
2511static expr_ty
2512ast_for_binop(struct compiling *c, const node *n)
2513{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002514 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002516 BinOp(BinOp(A, op, B), op, C).
2517 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518
Guido van Rossumd8faa362007-04-27 19:54:29 +00002519 int i, nops;
2520 expr_ty expr1, expr2, result;
2521 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522
Guido van Rossumd8faa362007-04-27 19:54:29 +00002523 expr1 = ast_for_expr(c, CHILD(n, 0));
2524 if (!expr1)
2525 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526
Guido van Rossumd8faa362007-04-27 19:54:29 +00002527 expr2 = ast_for_expr(c, CHILD(n, 2));
2528 if (!expr2)
2529 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530
Guido van Rossum495da292019-03-07 12:38:08 -08002531 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002532 if (!newoperator)
2533 return NULL;
2534
2535 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002536 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002537 c->c_arena);
2538 if (!result)
2539 return NULL;
2540
2541 nops = (NCH(n) - 1) / 2;
2542 for (i = 1; i < nops; i++) {
2543 expr_ty tmp_result, tmp;
2544 const node* next_oper = CHILD(n, i * 2 + 1);
2545
Guido van Rossum495da292019-03-07 12:38:08 -08002546 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002547 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 return NULL;
2549
Guido van Rossumd8faa362007-04-27 19:54:29 +00002550 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2551 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 return NULL;
2553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 tmp_result = BinOp(result, newoperator, tmp,
Carl Friedrich Bolz-Tereick110a47c2019-07-08 23:17:56 +02002555 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002556 CHILD(n, i * 2 + 2)->n_end_lineno,
2557 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002558 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002560 return NULL;
2561 result = tmp_result;
2562 }
2563 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564}
2565
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002566static expr_ty
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002567ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002570 subscriptlist: subscript (',' subscript)* [',']
2571 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2572 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002573 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002574 REQ(n, trailer);
2575 if (TYPE(CHILD(n, 0)) == LPAR) {
2576 if (NCH(n) == 2)
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002577 return Call(left_expr, NULL, NULL, 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);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002579 else
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002580 return ast_for_call(c, CHILD(n, 1), left_expr,
2581 start, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002582 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002583 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002584 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2585 if (!attr_id)
2586 return NULL;
2587 return Attribute(left_expr, attr_id, Load,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002588 LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002589 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002590 }
2591 else {
2592 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002593 REQ(CHILD(n, 2), RSQB);
2594 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002595 if (NCH(n) == 1) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002596 expr_ty slc = ast_for_slice(c, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002597 if (!slc)
2598 return NULL;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002599 return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002600 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002601 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002602 }
2603 else {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002604 int j;
2605 expr_ty slc, e;
2606 asdl_seq *elts;
2607 elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
2608 if (!elts)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002609 return NULL;
2610 for (j = 0; j < NCH(n); j += 2) {
2611 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002612 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002613 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002614 asdl_seq_SET(elts, j / 2, slc);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002615 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002616 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002617 n->n_end_lineno, n->n_end_col_offset,
2618 c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002619 if (!e)
2620 return NULL;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002621 return Subscript(left_expr, e,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002622 Load, LINENO(start), start->n_col_offset,
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02002623 n_copy->n_end_lineno, n_copy->n_end_col_offset,
2624 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002625 }
2626 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002627}
2628
2629static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002630ast_for_factor(struct compiling *c, const node *n)
2631{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002632 expr_ty expression;
2633
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002634 expression = ast_for_expr(c, CHILD(n, 1));
2635 if (!expression)
2636 return NULL;
2637
2638 switch (TYPE(CHILD(n, 0))) {
2639 case PLUS:
2640 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002641 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002642 c->c_arena);
2643 case MINUS:
2644 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002645 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002646 c->c_arena);
2647 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002648 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2649 n->n_end_lineno, n->n_end_col_offset,
2650 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002651 }
2652 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2653 TYPE(CHILD(n, 0)));
2654 return NULL;
2655}
2656
2657static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002658ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002659{
Yury Selivanov75445082015-05-11 22:57:16 -04002660 int i, nch, start = 0;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002661 expr_ty e;
Yury Selivanov75445082015-05-11 22:57:16 -04002662
2663 REQ(n, atom_expr);
2664 nch = NCH(n);
2665
Guido van Rossum495da292019-03-07 12:38:08 -08002666 if (TYPE(CHILD(n, 0)) == AWAIT) {
2667 if (c->c_feature_version < 5) {
2668 ast_error(c, n,
2669 "Await expressions are only supported in Python 3.5 and greater");
2670 return NULL;
2671 }
Yury Selivanov75445082015-05-11 22:57:16 -04002672 start = 1;
2673 assert(nch > 1);
2674 }
2675
2676 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002677 if (!e)
2678 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002679 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002680 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002681 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002682 return Await(e, LINENO(n), n->n_col_offset,
2683 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002684 }
2685
2686 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002687 node *ch = CHILD(n, i);
2688 if (TYPE(ch) != trailer)
2689 break;
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002690 e = ast_for_trailer(c, ch, e, CHILD(n, start));
2691 if (!e)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002692 return NULL;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002693 }
Yury Selivanov75445082015-05-11 22:57:16 -04002694
2695 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002696 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002697 return Await(e, LINENO(n), n->n_col_offset,
2698 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002699 }
2700 else {
2701 return e;
2702 }
2703}
2704
2705static expr_ty
2706ast_for_power(struct compiling *c, const node *n)
2707{
2708 /* power: atom trailer* ('**' factor)*
2709 */
2710 expr_ty e;
2711 REQ(n, power);
2712 e = ast_for_atom_expr(c, CHILD(n, 0));
2713 if (!e)
2714 return NULL;
2715 if (NCH(n) == 1)
2716 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002717 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2718 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002719 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002720 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002721 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2722 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002723 }
2724 return e;
2725}
2726
Guido van Rossum0368b722007-05-11 16:50:42 +00002727static expr_ty
2728ast_for_starred(struct compiling *c, const node *n)
2729{
2730 expr_ty tmp;
2731 REQ(n, star_expr);
2732
2733 tmp = ast_for_expr(c, CHILD(n, 1));
2734 if (!tmp)
2735 return NULL;
2736
2737 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002738 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2739 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002740}
2741
2742
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743/* Do not name a variable 'expr'! Will cause a compile error.
2744*/
2745
2746static expr_ty
2747ast_for_expr(struct compiling *c, const node *n)
2748{
2749 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002750 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002751 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002752 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 and_test: not_test ('and' not_test)*
2755 not_test: 'not' not_test | comparison
2756 comparison: expr (comp_op expr)*
2757 expr: xor_expr ('|' xor_expr)*
2758 xor_expr: and_expr ('^' and_expr)*
2759 and_expr: shift_expr ('&' shift_expr)*
2760 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2761 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002762 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002764 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002765 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002766 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 */
2768
2769 asdl_seq *seq;
2770 int i;
2771
2772 loop:
2773 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002774 case namedexpr_test:
2775 if (NCH(n) == 3)
2776 return ast_for_namedexpr(c, n);
2777 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002779 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002780 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002781 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002783 else if (NCH(n) > 1)
2784 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002785 /* Fallthrough */
2786 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787 case and_test:
2788 if (NCH(n) == 1) {
2789 n = CHILD(n, 0);
2790 goto loop;
2791 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002792 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 if (!seq)
2794 return NULL;
2795 for (i = 0; i < NCH(n); i += 2) {
2796 expr_ty e = ast_for_expr(c, CHILD(n, i));
2797 if (!e)
2798 return NULL;
2799 asdl_seq_SET(seq, i / 2, e);
2800 }
2801 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002802 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002803 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002804 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002805 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002806 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2807 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 case not_test:
2809 if (NCH(n) == 1) {
2810 n = CHILD(n, 0);
2811 goto loop;
2812 }
2813 else {
2814 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2815 if (!expression)
2816 return NULL;
2817
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002818 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002819 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002820 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 }
2822 case comparison:
2823 if (NCH(n) == 1) {
2824 n = CHILD(n, 0);
2825 goto loop;
2826 }
2827 else {
2828 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002829 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002830 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002831 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 if (!ops)
2833 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002834 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 return NULL;
2837 }
2838 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002839 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002841 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002842 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002844 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845
2846 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002847 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002849 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002851 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 asdl_seq_SET(cmps, i / 2, expression);
2853 }
2854 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002855 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002857 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002859 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2860 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862
Guido van Rossum0368b722007-05-11 16:50:42 +00002863 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 /* The next five cases all handle BinOps. The main body of code
2866 is the same in each case, but the switch turned inside out to
2867 reuse the code for each type of operator.
2868 */
2869 case expr:
2870 case xor_expr:
2871 case and_expr:
2872 case shift_expr:
2873 case arith_expr:
2874 case term:
2875 if (NCH(n) == 1) {
2876 n = CHILD(n, 0);
2877 goto loop;
2878 }
2879 return ast_for_binop(c, n);
2880 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002881 node *an = NULL;
2882 node *en = NULL;
2883 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002884 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002885 if (NCH(n) > 1)
2886 an = CHILD(n, 1); /* yield_arg */
2887 if (an) {
2888 en = CHILD(an, NCH(an) - 1);
2889 if (NCH(an) == 2) {
2890 is_from = 1;
2891 exp = ast_for_expr(c, en);
2892 }
2893 else
2894 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002895 if (!exp)
2896 return NULL;
2897 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002898 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002899 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2900 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2901 return Yield(exp, LINENO(n), n->n_col_offset,
2902 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002903 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002904 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 if (NCH(n) == 1) {
2906 n = CHILD(n, 0);
2907 goto loop;
2908 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002909 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002910 case power:
2911 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002913 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 return NULL;
2915 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002916 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 return NULL;
2918}
2919
2920static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002921ast_for_call(struct compiling *c, const node *n, expr_ty func,
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02002922 const node *start, const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923{
2924 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002925 arglist: argument (',' argument)* [',']
2926 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 */
2928
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002929 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002930 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002931 asdl_seq *args;
2932 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933
2934 REQ(n, arglist);
2935
2936 nargs = 0;
2937 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002939 node *ch = CHILD(n, i);
2940 if (TYPE(ch) == argument) {
2941 if (NCH(ch) == 1)
2942 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002943 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2944 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002945 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002946 ast_error(c, ch, "invalid syntax");
2947 return NULL;
2948 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002949 if (NCH(n) > 1) {
2950 ast_error(c, ch, "Generator expression must be parenthesized");
2951 return NULL;
2952 }
2953 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002954 else if (TYPE(CHILD(ch, 0)) == STAR)
2955 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002956 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2957 nargs++;
2958 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002960 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002961 nkeywords++;
2962 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002965 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002967 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002968 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002970 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002971
2972 nargs = 0; /* positional arguments + iterable argument unpackings */
2973 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2974 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002976 node *ch = CHILD(n, i);
2977 if (TYPE(ch) == argument) {
2978 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002979 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002980 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002981 /* a positional argument */
2982 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002983 if (ndoublestars) {
2984 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002985 "positional argument follows "
2986 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002987 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002988 else {
2989 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002990 "positional argument follows "
2991 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002992 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002993 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002994 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002995 e = ast_for_expr(c, chch);
2996 if (!e)
2997 return NULL;
2998 asdl_seq_SET(args, nargs++, e);
2999 }
3000 else if (TYPE(chch) == STAR) {
3001 /* an iterable argument unpacking */
3002 expr_ty starred;
3003 if (ndoublestars) {
3004 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003005 "iterable argument unpacking follows "
3006 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003007 return NULL;
3008 }
3009 e = ast_for_expr(c, CHILD(ch, 1));
3010 if (!e)
3011 return NULL;
3012 starred = Starred(e, Load, LINENO(chch),
3013 chch->n_col_offset,
Lysandros Nikolaou50d4f122019-12-18 01:20:55 +01003014 e->end_lineno, e->end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003015 c->c_arena);
3016 if (!starred)
3017 return NULL;
3018 asdl_seq_SET(args, nargs++, starred);
3019
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003020 }
3021 else if (TYPE(chch) == DOUBLESTAR) {
3022 /* a keyword argument unpacking */
3023 keyword_ty kw;
3024 i++;
3025 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003027 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003028 kw = keyword(NULL, e, c->c_arena);
3029 asdl_seq_SET(keywords, nkeywords++, kw);
3030 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003032 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003033 /* the lone generator expression */
Guido van Rossuma796d8e2020-01-09 11:18:47 -08003034 e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003036 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003037 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003039 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3040 /* treat colon equal as positional argument */
3041 if (nkeywords) {
3042 if (ndoublestars) {
3043 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003044 "positional argument follows "
3045 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003046 }
3047 else {
3048 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003049 "positional argument follows "
3050 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003051 }
3052 return NULL;
3053 }
3054 e = ast_for_namedexpr(c, ch);
3055 if (!e)
3056 return NULL;
3057 asdl_seq_SET(args, nargs++, e);
3058 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003059 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003060 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003061 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003062 identifier key, tmp;
3063 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003065 // To remain LL(1), the grammar accepts any test (basically, any
3066 // expression) in the keyword slot of a call site. So, we need
3067 // to manually enforce that the keyword is a NAME here.
3068 static const int name_tree[] = {
3069 test,
3070 or_test,
3071 and_test,
3072 not_test,
3073 comparison,
3074 expr,
3075 xor_expr,
3076 and_expr,
3077 shift_expr,
3078 arith_expr,
3079 term,
3080 factor,
3081 power,
3082 atom_expr,
3083 atom,
3084 0,
3085 };
3086 node *expr_node = chch;
3087 for (int i = 0; name_tree[i]; i++) {
3088 if (TYPE(expr_node) != name_tree[i])
3089 break;
3090 if (NCH(expr_node) != 1)
3091 break;
3092 expr_node = CHILD(expr_node, 0);
3093 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003094 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003095 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003096 "expression cannot contain assignment, "
3097 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003098 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003099 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003100 key = new_identifier(STR(expr_node), c);
3101 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003102 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003104 if (forbidden_name(c, key, chch, 1)) {
3105 return NULL;
3106 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003107 for (k = 0; k < nkeywords; k++) {
3108 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003109 if (tmp && !PyUnicode_Compare(tmp, key)) {
3110 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003111 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003112 return NULL;
3113 }
3114 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003115 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003117 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003118 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003120 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003121 asdl_seq_SET(keywords, nkeywords++, kw);
3122 }
3123 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 }
3125
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02003126 return Call(func, args, keywords, LINENO(start), start->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003127 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128}
3129
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003131ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003133 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003134 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003136 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003137 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003138 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003139 }
3140 else {
3141 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003142 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003143 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003145 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 else {
3147 asdl_seq *tmp = seq_for_testlist(c, n);
3148 if (!tmp)
3149 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003150 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3151 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003153}
3154
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155static stmt_ty
3156ast_for_expr_stmt(struct compiling *c, const node *n)
3157{
3158 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003159 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003160 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3161 annassign: ':' test ['=' (yield_expr|testlist)]
3162 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3163 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3164 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003165 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003167 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003169 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003170 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 if (!e)
3172 return NULL;
3173
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003174 return Expr(e, LINENO(n), n->n_col_offset,
3175 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176 }
3177 else if (TYPE(CHILD(n, 1)) == augassign) {
3178 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003179 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003180 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181
Thomas Wouters89f507f2006-12-13 04:49:30 +00003182 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 if (!expr1)
3184 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003185 if(!set_context(c, expr1, Store, ch))
3186 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003187 /* set_context checks that most expressions are not the left side.
3188 Augmented assignments can only have a name, a subscript, or an
3189 attribute on the left, though, so we have to explicitly check for
3190 those. */
3191 switch (expr1->kind) {
3192 case Name_kind:
3193 case Attribute_kind:
3194 case Subscript_kind:
3195 break;
3196 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003197 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003198 return NULL;
3199 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200
Thomas Wouters89f507f2006-12-13 04:49:30 +00003201 ch = CHILD(n, 2);
3202 if (TYPE(ch) == testlist)
3203 expr2 = ast_for_testlist(c, ch);
3204 else
3205 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003206 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 return NULL;
3208
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003209 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003210 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 return NULL;
3212
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003213 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3214 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003216 else if (TYPE(CHILD(n, 1)) == annassign) {
3217 expr_ty expr1, expr2, expr3;
3218 node *ch = CHILD(n, 0);
3219 node *deep, *ann = CHILD(n, 1);
3220 int simple = 1;
3221
Guido van Rossum495da292019-03-07 12:38:08 -08003222 /* AnnAssigns are only allowed in Python 3.6 or greater */
3223 if (c->c_feature_version < 6) {
3224 ast_error(c, ch,
3225 "Variable annotation syntax is only supported in Python 3.6 and greater");
3226 return NULL;
3227 }
3228
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003229 /* we keep track of parens to qualify (x) as expression not name */
3230 deep = ch;
3231 while (NCH(deep) == 1) {
3232 deep = CHILD(deep, 0);
3233 }
3234 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3235 simple = 0;
3236 }
3237 expr1 = ast_for_testlist(c, ch);
3238 if (!expr1) {
3239 return NULL;
3240 }
3241 switch (expr1->kind) {
3242 case Name_kind:
3243 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3244 return NULL;
3245 }
3246 expr1->v.Name.ctx = Store;
3247 break;
3248 case Attribute_kind:
3249 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3250 return NULL;
3251 }
3252 expr1->v.Attribute.ctx = Store;
3253 break;
3254 case Subscript_kind:
3255 expr1->v.Subscript.ctx = Store;
3256 break;
3257 case List_kind:
3258 ast_error(c, ch,
3259 "only single target (not list) can be annotated");
3260 return NULL;
3261 case Tuple_kind:
3262 ast_error(c, ch,
3263 "only single target (not tuple) can be annotated");
3264 return NULL;
3265 default:
3266 ast_error(c, ch,
3267 "illegal target for annotation");
3268 return NULL;
3269 }
3270
3271 if (expr1->kind != Name_kind) {
3272 simple = 0;
3273 }
3274 ch = CHILD(ann, 1);
3275 expr2 = ast_for_expr(c, ch);
3276 if (!expr2) {
3277 return NULL;
3278 }
3279 if (NCH(ann) == 2) {
3280 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003281 LINENO(n), n->n_col_offset,
3282 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003283 }
3284 else {
3285 ch = CHILD(ann, 3);
Pablo Galindo8565f6b2019-06-03 08:34:20 +01003286 if (TYPE(ch) == testlist_star_expr) {
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003287 expr3 = ast_for_testlist(c, ch);
3288 }
3289 else {
3290 expr3 = ast_for_expr(c, ch);
3291 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003292 if (!expr3) {
3293 return NULL;
3294 }
3295 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003296 LINENO(n), n->n_col_offset,
3297 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003298 }
3299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003301 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003302 asdl_seq *targets;
3303 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003305 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 /* a normal assignment */
3308 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003309
3310 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3311 nch_minus_type = num - has_type_comment;
3312
3313 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 if (!targets)
3315 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003316 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 expr_ty e;
3318 node *ch = CHILD(n, i);
3319 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003320 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 return NULL;
3322 }
3323 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003327 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003328 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003329 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330
Thomas Wouters89f507f2006-12-13 04:49:30 +00003331 asdl_seq_SET(targets, i / 2, e);
3332 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003333 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003334 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003335 expression = ast_for_testlist(c, value);
3336 else
3337 expression = ast_for_expr(c, value);
3338 if (!expression)
3339 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003340 if (has_type_comment) {
3341 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3342 if (!type_comment)
3343 return NULL;
3344 }
3345 else
3346 type_comment = NULL;
3347 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003348 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350}
3351
Benjamin Peterson78565b22009-06-28 19:19:51 +00003352
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003354ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355{
3356 asdl_seq *seq;
3357 int i;
3358 expr_ty e;
3359
3360 REQ(n, exprlist);
3361
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003362 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003364 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003366 e = ast_for_expr(c, CHILD(n, i));
3367 if (!e)
3368 return NULL;
3369 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003370 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003371 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372 }
3373 return seq;
3374}
3375
3376static stmt_ty
3377ast_for_del_stmt(struct compiling *c, const node *n)
3378{
3379 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 /* del_stmt: 'del' exprlist */
3382 REQ(n, del_stmt);
3383
3384 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3385 if (!expr_list)
3386 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003387 return Delete(expr_list, LINENO(n), n->n_col_offset,
3388 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389}
3390
3391static stmt_ty
3392ast_for_flow_stmt(struct compiling *c, const node *n)
3393{
3394 /*
3395 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3396 | yield_stmt
3397 break_stmt: 'break'
3398 continue_stmt: 'continue'
3399 return_stmt: 'return' [testlist]
3400 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003401 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402 raise_stmt: 'raise' [test [',' test [',' test]]]
3403 */
3404 node *ch;
3405
3406 REQ(n, flow_stmt);
3407 ch = CHILD(n, 0);
3408 switch (TYPE(ch)) {
3409 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003410 return Break(LINENO(n), n->n_col_offset,
3411 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003413 return Continue(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 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003416 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3417 if (!exp)
3418 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003419 return Expr(exp, 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 return_stmt:
3423 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003424 return Return(NULL, LINENO(n), n->n_col_offset,
3425 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003427 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428 if (!expression)
3429 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003430 return Return(expression, LINENO(n), n->n_col_offset,
3431 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432 }
3433 case raise_stmt:
3434 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003435 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3436 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003437 else if (NCH(ch) >= 2) {
3438 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3440 if (!expression)
3441 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003442 if (NCH(ch) == 4) {
3443 cause = ast_for_expr(c, CHILD(ch, 3));
3444 if (!cause)
3445 return NULL;
3446 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003447 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3448 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449 }
Stefan Krahf432a322017-08-21 13:09:59 +02003450 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003452 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 "unexpected flow_stmt: %d", TYPE(ch));
3454 return NULL;
3455 }
3456}
3457
3458static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003459alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460{
3461 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003462 import_as_name: NAME ['as' NAME]
3463 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 dotted_name: NAME ('.' NAME)*
3465 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003466 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003467
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468 loop:
3469 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003470 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003471 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003472 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003473 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003474 if (!name)
3475 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003476 if (NCH(n) == 3) {
3477 node *str_node = CHILD(n, 2);
3478 str = NEW_IDENTIFIER(str_node);
3479 if (!str)
3480 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003481 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003482 return NULL;
3483 }
3484 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003485 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003486 return NULL;
3487 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003488 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003489 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490 case dotted_as_name:
3491 if (NCH(n) == 1) {
3492 n = CHILD(n, 0);
3493 goto loop;
3494 }
3495 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003496 node *asname_node = CHILD(n, 2);
3497 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003498 if (!a)
3499 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003501 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003502 if (!a->asname)
3503 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003504 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003505 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 return a;
3507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003509 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003510 node *name_node = CHILD(n, 0);
3511 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003512 if (!name)
3513 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003514 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003515 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003516 return alias(name, NULL, c->c_arena);
3517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003518 else {
3519 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003520 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003521 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524
3525 len = 0;
3526 for (i = 0; i < NCH(n); i += 2)
3527 /* length of string plus one for the dot */
3528 len += strlen(STR(CHILD(n, i))) + 1;
3529 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003530 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531 if (!str)
3532 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003533 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534 if (!s)
3535 return NULL;
3536 for (i = 0; i < NCH(n); i += 2) {
3537 char *sch = STR(CHILD(n, i));
3538 strcpy(s, STR(CHILD(n, i)));
3539 s += strlen(sch);
3540 *s++ = '.';
3541 }
3542 --s;
3543 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3545 PyBytes_GET_SIZE(str),
3546 NULL);
3547 Py_DECREF(str);
3548 if (!uni)
3549 return NULL;
3550 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003551 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003552 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3553 Py_DECREF(str);
3554 return NULL;
3555 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003556 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003559 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003560 if (!str)
3561 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003562 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3563 Py_DECREF(str);
3564 return NULL;
3565 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003566 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003568 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 "unexpected import name: %d", TYPE(n));
3570 return NULL;
3571 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572}
3573
3574static stmt_ty
3575ast_for_import_stmt(struct compiling *c, const node *n)
3576{
3577 /*
3578 import_stmt: import_name | import_from
3579 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003580 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3581 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003583 int lineno;
3584 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585 int i;
3586 asdl_seq *aliases;
3587
3588 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003589 lineno = LINENO(n);
3590 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003592 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003594 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003595 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003596 if (!aliases)
3597 return NULL;
3598 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003599 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003600 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003602 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003604 // Even though n is modified above, the end position is not changed
3605 return Import(aliases, lineno, col_offset,
3606 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003608 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003610 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003611 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003612 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003613 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003615 /* Count the number of dots (for relative imports) and check for the
3616 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003617 for (idx = 1; idx < NCH(n); idx++) {
3618 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003619 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3620 if (!mod)
3621 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003622 idx++;
3623 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003624 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003626 ndots += 3;
3627 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003628 } else if (TYPE(CHILD(n, idx)) != DOT) {
3629 break;
3630 }
3631 ndots++;
3632 }
3633 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003634 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003635 case STAR:
3636 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003637 n = CHILD(n, idx);
3638 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003639 break;
3640 case LPAR:
3641 /* from ... import (x, y, z) */
3642 n = CHILD(n, idx + 1);
3643 n_children = NCH(n);
3644 break;
3645 case import_as_names:
3646 /* from ... import x, y, z */
3647 n = CHILD(n, idx);
3648 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003649 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003650 ast_error(c, n,
3651 "trailing comma not allowed without"
3652 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 return NULL;
3654 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003655 break;
3656 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003657 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003658 return NULL;
3659 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003661 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003662 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664
3665 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003666 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003667 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003668 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003670 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003672 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003673 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003674 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003675 if (!import_alias)
3676 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003677 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003678 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003680 if (mod != NULL)
3681 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003682 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003683 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003684 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685 }
Neal Norwitz79792652005-11-14 04:25:03 +00003686 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 "unknown import statement: starts with command '%s'",
3688 STR(CHILD(n, 0)));
3689 return NULL;
3690}
3691
3692static stmt_ty
3693ast_for_global_stmt(struct compiling *c, const node *n)
3694{
3695 /* global_stmt: 'global' NAME (',' NAME)* */
3696 identifier name;
3697 asdl_seq *s;
3698 int i;
3699
3700 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003701 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003703 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003705 name = NEW_IDENTIFIER(CHILD(n, i));
3706 if (!name)
3707 return NULL;
3708 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003710 return Global(s, LINENO(n), n->n_col_offset,
3711 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712}
3713
3714static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003715ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3716{
3717 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3718 identifier name;
3719 asdl_seq *s;
3720 int i;
3721
3722 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003723 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003724 if (!s)
3725 return NULL;
3726 for (i = 1; i < NCH(n); i += 2) {
3727 name = NEW_IDENTIFIER(CHILD(n, i));
3728 if (!name)
3729 return NULL;
3730 asdl_seq_SET(s, i / 2, name);
3731 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003732 return Nonlocal(s, LINENO(n), n->n_col_offset,
3733 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003734}
3735
3736static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003737ast_for_assert_stmt(struct compiling *c, const node *n)
3738{
3739 /* assert_stmt: 'assert' test [',' test] */
3740 REQ(n, assert_stmt);
3741 if (NCH(n) == 2) {
3742 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3743 if (!expression)
3744 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003745 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3746 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 }
3748 else if (NCH(n) == 4) {
3749 expr_ty expr1, expr2;
3750
3751 expr1 = ast_for_expr(c, CHILD(n, 1));
3752 if (!expr1)
3753 return NULL;
3754 expr2 = ast_for_expr(c, CHILD(n, 3));
3755 if (!expr2)
3756 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003758 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3759 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 }
Neal Norwitz79792652005-11-14 04:25:03 +00003761 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 "improper number of parts to 'assert' statement: %d",
3763 NCH(n));
3764 return NULL;
3765}
3766
3767static asdl_seq *
3768ast_for_suite(struct compiling *c, const node *n)
3769{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003770 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003771 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 stmt_ty s;
3773 int i, total, num, end, pos = 0;
3774 node *ch;
3775
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003776 if (TYPE(n) != func_body_suite) {
3777 REQ(n, suite);
3778 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779
3780 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003781 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003783 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003785 n = CHILD(n, 0);
3786 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003788 */
3789 end = NCH(n) - 1;
3790 if (TYPE(CHILD(n, end - 1)) == SEMI)
3791 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003793 for (i = 0; i < end; i += 2) {
3794 ch = CHILD(n, i);
3795 s = ast_for_stmt(c, ch);
3796 if (!s)
3797 return NULL;
3798 asdl_seq_SET(seq, pos++, s);
3799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 }
3801 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003802 i = 2;
3803 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3804 i += 2;
3805 REQ(CHILD(n, 2), NEWLINE);
3806 }
3807
3808 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003809 ch = CHILD(n, i);
3810 REQ(ch, stmt);
3811 num = num_stmts(ch);
3812 if (num == 1) {
3813 /* small_stmt or compound_stmt with only one child */
3814 s = ast_for_stmt(c, ch);
3815 if (!s)
3816 return NULL;
3817 asdl_seq_SET(seq, pos++, s);
3818 }
3819 else {
3820 int j;
3821 ch = CHILD(ch, 0);
3822 REQ(ch, simple_stmt);
3823 for (j = 0; j < NCH(ch); j += 2) {
3824 /* statement terminates with a semi-colon ';' */
3825 if (NCH(CHILD(ch, j)) == 0) {
3826 assert((j + 1) == NCH(ch));
3827 break;
3828 }
3829 s = ast_for_stmt(c, CHILD(ch, j));
3830 if (!s)
3831 return NULL;
3832 asdl_seq_SET(seq, pos++, s);
3833 }
3834 }
3835 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836 }
3837 assert(pos == seq->size);
3838 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003839}
3840
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003841static void
3842get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3843{
Pablo Galindo46a97922019-02-19 22:51:53 +00003844 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003845 // There must be no empty suites.
3846 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003847 stmt_ty last = asdl_seq_GET(s, tot - 1);
3848 *end_lineno = last->end_lineno;
3849 *end_col_offset = last->end_col_offset;
3850}
3851
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852static stmt_ty
3853ast_for_if_stmt(struct compiling *c, const node *n)
3854{
3855 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3856 ['else' ':' suite]
3857 */
3858 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003859 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860
3861 REQ(n, if_stmt);
3862
3863 if (NCH(n) == 4) {
3864 expr_ty expression;
3865 asdl_seq *suite_seq;
3866
3867 expression = ast_for_expr(c, CHILD(n, 1));
3868 if (!expression)
3869 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003871 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003872 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003873 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874
Guido van Rossumd8faa362007-04-27 19:54:29 +00003875 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003876 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003878
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879 s = STR(CHILD(n, 4));
3880 /* s[2], the third character in the string, will be
3881 's' for el_s_e, or
3882 'i' for el_i_f
3883 */
3884 if (s[2] == 's') {
3885 expr_ty expression;
3886 asdl_seq *seq1, *seq2;
3887
3888 expression = ast_for_expr(c, CHILD(n, 1));
3889 if (!expression)
3890 return NULL;
3891 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003892 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893 return NULL;
3894 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003895 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003896 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003897 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003898
Guido van Rossumd8faa362007-04-27 19:54:29 +00003899 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003900 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 }
3902 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003903 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003904 expr_ty expression;
3905 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003906 asdl_seq *orelse = NULL;
3907 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 /* must reference the child n_elif+1 since 'else' token is third,
3909 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003910 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3911 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3912 has_else = 1;
3913 n_elif -= 3;
3914 }
3915 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916
Thomas Wouters89f507f2006-12-13 04:49:30 +00003917 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003918 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003920 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003921 if (!orelse)
3922 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003924 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003926 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3927 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003929 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3930 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003932 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 asdl_seq_SET(orelse, 0,
3935 If(expression, suite_seq, suite_seq2,
Lysandros Nikolaou5936a4c2019-12-14 11:24:57 +01003936 LINENO(CHILD(n, NCH(n) - 7)),
3937 CHILD(n, NCH(n) - 7)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003938 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003939 /* the just-created orelse handled the last elif */
3940 n_elif--;
3941 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942
Thomas Wouters89f507f2006-12-13 04:49:30 +00003943 for (i = 0; i < n_elif; i++) {
3944 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003945 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003946 if (!newobj)
3947 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003948 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003949 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003952 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003955 if (orelse != NULL) {
3956 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3957 } else {
3958 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3959 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003960 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 If(expression, suite_seq, orelse,
Lysandros Nikolaou025a6022019-12-12 22:40:21 +01003962 LINENO(CHILD(n, off - 1)),
3963 CHILD(n, off - 1)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003964 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003965 orelse = newobj;
3966 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003967 expression = ast_for_expr(c, CHILD(n, 1));
3968 if (!expression)
3969 return NULL;
3970 suite_seq = ast_for_suite(c, CHILD(n, 3));
3971 if (!suite_seq)
3972 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003973 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003974 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003975 LINENO(n), n->n_col_offset,
3976 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003978
3979 PyErr_Format(PyExc_SystemError,
3980 "unexpected token in 'if' statement: %s", s);
3981 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982}
3983
3984static stmt_ty
3985ast_for_while_stmt(struct compiling *c, const node *n)
3986{
3987 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3988 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003989 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990
3991 if (NCH(n) == 4) {
3992 expr_ty expression;
3993 asdl_seq *suite_seq;
3994
3995 expression = ast_for_expr(c, CHILD(n, 1));
3996 if (!expression)
3997 return NULL;
3998 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003999 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004001 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4002 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4003 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 }
4005 else if (NCH(n) == 7) {
4006 expr_ty expression;
4007 asdl_seq *seq1, *seq2;
4008
4009 expression = ast_for_expr(c, CHILD(n, 1));
4010 if (!expression)
4011 return NULL;
4012 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004013 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014 return NULL;
4015 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004016 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004018 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004020 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4021 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004023
4024 PyErr_Format(PyExc_SystemError,
4025 "wrong number of tokens for 'while' statement: %d",
4026 NCH(n));
4027 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028}
4029
4030static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004031ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004032{
guoci90fc8982018-09-11 17:45:45 -04004033 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004034 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004036 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004037 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004038 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004039 int has_type_comment;
4040 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004041
4042 if (is_async && c->c_feature_version < 5) {
4043 ast_error(c, n,
4044 "Async for loops are only supported in Python 3.5 and greater");
4045 return NULL;
4046 }
4047
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004048 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 REQ(n, for_stmt);
4050
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004051 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4052
4053 if (NCH(n) == 9 + has_type_comment) {
4054 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004055 if (!seq)
4056 return NULL;
4057 }
4058
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004059 node_target = CHILD(n, 1);
4060 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004061 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004063 /* Check the # of children rather than the length of _target, since
4064 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004065 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004066 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004067 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004069 target = Tuple(_target, Store, first->lineno, first->col_offset,
4070 node_target->n_end_lineno, node_target->n_end_col_offset,
4071 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004073 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004074 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004076 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004077 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004078 return NULL;
4079
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004080 if (seq != NULL) {
4081 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4082 } else {
4083 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4084 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004085
4086 if (has_type_comment) {
4087 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4088 if (!type_comment)
4089 return NULL;
4090 }
4091 else
4092 type_comment = NULL;
4093
Yury Selivanov75445082015-05-11 22:57:16 -04004094 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004095 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004096 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004097 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004098 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004099 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004100 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004101 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004102}
4103
4104static excepthandler_ty
4105ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4106{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004107 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004108 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109 REQ(exc, except_clause);
4110 REQ(body, suite);
4111
4112 if (NCH(exc) == 1) {
4113 asdl_seq *suite_seq = ast_for_suite(c, body);
4114 if (!suite_seq)
4115 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004116 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117
Neal Norwitzad74aa82008-03-31 05:14:30 +00004118 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004119 exc->n_col_offset,
4120 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004121 }
4122 else if (NCH(exc) == 2) {
4123 expr_ty expression;
4124 asdl_seq *suite_seq;
4125
4126 expression = ast_for_expr(c, CHILD(exc, 1));
4127 if (!expression)
4128 return NULL;
4129 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004130 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004132 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133
Neal Norwitzad74aa82008-03-31 05:14:30 +00004134 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004135 exc->n_col_offset,
4136 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137 }
4138 else if (NCH(exc) == 4) {
4139 asdl_seq *suite_seq;
4140 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004141 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004142 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004143 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004144 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004145 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004147 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148 return NULL;
4149 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004150 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004152 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153
Neal Norwitzad74aa82008-03-31 05:14:30 +00004154 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004155 exc->n_col_offset,
4156 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004158
4159 PyErr_Format(PyExc_SystemError,
4160 "wrong number of children for 'except' clause: %d",
4161 NCH(exc));
4162 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004163}
4164
4165static stmt_ty
4166ast_for_try_stmt(struct compiling *c, const node *n)
4167{
Neal Norwitzf599f422005-12-17 21:33:47 +00004168 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004169 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004170 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004171 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173 REQ(n, try_stmt);
4174
Neal Norwitzf599f422005-12-17 21:33:47 +00004175 body = ast_for_suite(c, CHILD(n, 2));
4176 if (body == NULL)
4177 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178
Neal Norwitzf599f422005-12-17 21:33:47 +00004179 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4180 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4181 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4182 /* we can assume it's an "else",
4183 because nch >= 9 for try-else-finally and
4184 it would otherwise have a type of except_clause */
4185 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4186 if (orelse == NULL)
4187 return NULL;
4188 n_except--;
4189 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004190
Neal Norwitzf599f422005-12-17 21:33:47 +00004191 finally = ast_for_suite(c, CHILD(n, nch - 1));
4192 if (finally == NULL)
4193 return NULL;
4194 n_except--;
4195 }
4196 else {
4197 /* we can assume it's an "else",
4198 otherwise it would have a type of except_clause */
4199 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4200 if (orelse == NULL)
4201 return NULL;
4202 n_except--;
4203 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004204 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004205 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004206 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004207 return NULL;
4208 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209
Neal Norwitzf599f422005-12-17 21:33:47 +00004210 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004211 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004212 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004213 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004214 if (handlers == NULL)
4215 return NULL;
4216
4217 for (i = 0; i < n_except; i++) {
4218 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4219 CHILD(n, 5 + i * 3));
4220 if (!e)
4221 return NULL;
4222 asdl_seq_SET(handlers, i, e);
4223 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004224 }
4225
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004226 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004227 if (finally != NULL) {
4228 // finally is always last
4229 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4230 } else if (orelse != NULL) {
4231 // otherwise else is last
4232 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4233 } else {
4234 // inline the get_last_end_pos logic due to layout mismatch
4235 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4236 end_lineno = last_handler->end_lineno;
4237 end_col_offset = last_handler->end_col_offset;
4238 }
4239 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4240 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004241}
4242
Georg Brandl0c315622009-05-25 21:10:36 +00004243/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004244static withitem_ty
4245ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004246{
4247 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004248
Georg Brandl0c315622009-05-25 21:10:36 +00004249 REQ(n, with_item);
4250 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004251 if (!context_expr)
4252 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004253 if (NCH(n) == 3) {
4254 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004255
4256 if (!optional_vars) {
4257 return NULL;
4258 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004259 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004260 return NULL;
4261 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004262 }
4263
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004264 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004265}
4266
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004267/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004268static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004269ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004270{
guoci90fc8982018-09-11 17:45:45 -04004271 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004272 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004273 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004274 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004275
Guido van Rossum495da292019-03-07 12:38:08 -08004276 if (is_async && c->c_feature_version < 5) {
4277 ast_error(c, n,
4278 "Async with statements are only supported in Python 3.5 and greater");
4279 return NULL;
4280 }
4281
Georg Brandl0c315622009-05-25 21:10:36 +00004282 REQ(n, with_stmt);
4283
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004284 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4285 nch_minus_type = NCH(n) - has_type_comment;
4286
4287 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004288 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004289 if (!items)
4290 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004291 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004292 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4293 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004294 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004295 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004296 }
4297
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004298 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4299 if (!body)
4300 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004301 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004302
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004303 if (has_type_comment) {
4304 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4305 if (!type_comment)
4306 return NULL;
4307 }
4308 else
4309 type_comment = NULL;
4310
Yury Selivanov75445082015-05-11 22:57:16 -04004311 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004312 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004313 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004314 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004315 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004316 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004317}
4318
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004319static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004320ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004321{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004322 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004323 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004324 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004325 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004326 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004327
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004328 REQ(n, classdef);
4329
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004330 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004331 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004332 if (!s)
4333 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004334 get_last_end_pos(s, &end_lineno, &end_col_offset);
4335
Benjamin Peterson30760062008-11-25 04:02:28 +00004336 classname = NEW_IDENTIFIER(CHILD(n, 1));
4337 if (!classname)
4338 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004339 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004340 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004341 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004342 LINENO(n), n->n_col_offset,
4343 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004344 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004345
4346 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004347 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004348 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004349 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004350 get_last_end_pos(s, &end_lineno, &end_col_offset);
4351
Benjamin Peterson30760062008-11-25 04:02:28 +00004352 classname = NEW_IDENTIFIER(CHILD(n, 1));
4353 if (!classname)
4354 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004355 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004356 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004357 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004358 LINENO(n), n->n_col_offset,
4359 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004360 }
4361
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004362 /* class NAME '(' arglist ')' ':' suite */
4363 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004364 {
4365 PyObject *dummy_name;
4366 expr_ty dummy;
4367 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4368 if (!dummy_name)
4369 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004370 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4371 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4372 c->c_arena);
Serhiy Storchaka6e619c42020-02-12 22:37:49 +02004373 call = ast_for_call(c, CHILD(n, 3), dummy,
4374 CHILD(n, 1), NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004375 if (!call)
4376 return NULL;
4377 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004378 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004379 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004380 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004381 get_last_end_pos(s, &end_lineno, &end_col_offset);
4382
Benjamin Peterson30760062008-11-25 04:02:28 +00004383 classname = NEW_IDENTIFIER(CHILD(n, 1));
4384 if (!classname)
4385 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004386 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004387 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004388
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004389 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004390 decorator_seq, LINENO(n), n->n_col_offset,
4391 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004392}
4393
4394static stmt_ty
4395ast_for_stmt(struct compiling *c, const node *n)
4396{
4397 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004398 assert(NCH(n) == 1);
4399 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004400 }
4401 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004402 assert(num_stmts(n) == 1);
4403 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004404 }
4405 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004406 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004407 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4408 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004409 */
4410 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004411 case expr_stmt:
4412 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004413 case del_stmt:
4414 return ast_for_del_stmt(c, n);
4415 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004416 return Pass(LINENO(n), n->n_col_offset,
4417 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004418 case flow_stmt:
4419 return ast_for_flow_stmt(c, n);
4420 case import_stmt:
4421 return ast_for_import_stmt(c, n);
4422 case global_stmt:
4423 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004424 case nonlocal_stmt:
4425 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004426 case assert_stmt:
4427 return ast_for_assert_stmt(c, n);
4428 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004429 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004430 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4431 TYPE(n), NCH(n));
4432 return NULL;
4433 }
4434 }
4435 else {
4436 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004437 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004438 */
4439 node *ch = CHILD(n, 0);
4440 REQ(n, compound_stmt);
4441 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004442 case if_stmt:
4443 return ast_for_if_stmt(c, ch);
4444 case while_stmt:
4445 return ast_for_while_stmt(c, ch);
4446 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004447 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004448 case try_stmt:
4449 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004450 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004451 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004452 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004453 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004455 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 case decorated:
4457 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004458 case async_stmt:
4459 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004460 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004461 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004462 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004463 TYPE(n), NCH(n));
4464 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004466 }
4467}
4468
4469static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004470parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004471{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004472 const char *end;
4473 long x;
4474 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004475 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004476 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004477
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004478 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004479 errno = 0;
4480 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004481 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004482 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004483 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004484 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004485 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004486 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004487 }
4488 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004489 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004490 if (*end == '\0') {
4491 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004492 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004493 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004494 }
4495 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004496 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004497 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004498 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4499 if (compl.imag == -1.0 && PyErr_Occurred())
4500 return NULL;
4501 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004502 }
4503 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004504 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004505 dx = PyOS_string_to_double(s, NULL, NULL);
4506 if (dx == -1.0 && PyErr_Occurred())
4507 return NULL;
4508 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004509 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004510}
4511
4512static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004513parsenumber(struct compiling *c, const char *s)
4514{
4515 char *dup, *end;
4516 PyObject *res = NULL;
4517
4518 assert(s != NULL);
4519
4520 if (strchr(s, '_') == NULL) {
4521 return parsenumber_raw(c, s);
4522 }
4523 /* Create a duplicate without underscores. */
4524 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004525 if (dup == NULL) {
4526 return PyErr_NoMemory();
4527 }
Brett Cannona721aba2016-09-09 14:57:09 -07004528 end = dup;
4529 for (; *s; s++) {
4530 if (*s != '_') {
4531 *end++ = *s;
4532 }
4533 }
4534 *end = '\0';
4535 res = parsenumber_raw(c, dup);
4536 PyMem_Free(dup);
4537 return res;
4538}
4539
4540static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004541decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004542{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004543 const char *s, *t;
4544 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004545 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4546 while (s < end && (*s & 0x80)) s++;
4547 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004548 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004549}
4550
Eric V. Smith56466482016-10-31 14:46:26 -04004551static int
4552warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004553 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004554{
4555 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4556 first_invalid_escape_char);
4557 if (msg == NULL) {
4558 return -1;
4559 }
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004560 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004561 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004562 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004563 {
Gregory P. Smithb4be87a2019-08-10 00:19:07 -07004564 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
4565 /* Replace the DeprecationWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004566 to get a more accurate error report */
4567 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004568 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004569 }
4570 Py_DECREF(msg);
4571 return -1;
4572 }
4573 Py_DECREF(msg);
4574 return 0;
4575}
4576
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004577static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004578decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4579 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004580{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004581 PyObject *v, *u;
4582 char *buf;
4583 char *p;
4584 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004585
Benjamin Peterson202803a2016-02-25 22:34:45 -08004586 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004587 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004588 return NULL;
4589 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4590 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4591 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4592 if (u == NULL)
4593 return NULL;
4594 p = buf = PyBytes_AsString(u);
4595 end = s + len;
4596 while (s < end) {
4597 if (*s == '\\') {
4598 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004599 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004600 strcpy(p, "u005c");
4601 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004602 if (s >= end)
4603 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004604 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004605 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004606 if (*s & 0x80) { /* XXX inefficient */
4607 PyObject *w;
4608 int kind;
4609 void *data;
4610 Py_ssize_t len, i;
4611 w = decode_utf8(c, &s, end);
4612 if (w == NULL) {
4613 Py_DECREF(u);
4614 return NULL;
4615 }
4616 kind = PyUnicode_KIND(w);
4617 data = PyUnicode_DATA(w);
4618 len = PyUnicode_GET_LENGTH(w);
4619 for (i = 0; i < len; i++) {
4620 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4621 sprintf(p, "\\U%08x", chr);
4622 p += 10;
4623 }
4624 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004625 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004626 Py_DECREF(w);
4627 } else {
4628 *p++ = *s++;
4629 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004630 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004631 len = p - buf;
4632 s = buf;
4633
Eric V. Smith56466482016-10-31 14:46:26 -04004634 const char *first_invalid_escape;
4635 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4636
4637 if (v != NULL && first_invalid_escape != NULL) {
4638 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4639 /* We have not decref u before because first_invalid_escape points
4640 inside u. */
4641 Py_XDECREF(u);
4642 Py_DECREF(v);
4643 return NULL;
4644 }
4645 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004646 Py_XDECREF(u);
4647 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004648}
4649
Eric V. Smith56466482016-10-31 14:46:26 -04004650static PyObject *
4651decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4652 size_t len)
4653{
4654 const char *first_invalid_escape;
Greg Price3a4f6672019-09-12 11:12:22 -07004655 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL,
Eric V. Smith56466482016-10-31 14:46:26 -04004656 &first_invalid_escape);
4657 if (result == NULL)
4658 return NULL;
4659
4660 if (first_invalid_escape != NULL) {
4661 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4662 Py_DECREF(result);
4663 return NULL;
4664 }
4665 }
4666 return result;
4667}
4668
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004669/* Shift locations for the given node and all its children by adding `lineno`
4670 and `col_offset` to existing locations. */
4671static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4672{
4673 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004674 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004675 for (int i = 0; i < NCH(n); ++i) {
4676 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4677 /* Shifting column offsets unnecessary if there's been newlines. */
4678 col_offset = 0;
4679 }
4680 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4681 }
4682 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004683 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004684}
4685
4686/* Fix locations for the given node and its children.
4687
4688 `parent` is the enclosing node.
4689 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004690 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004691*/
4692static void
4693fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4694{
4695 char *substr = NULL;
4696 char *start;
4697 int lines = LINENO(parent) - 1;
4698 int cols = parent->n_col_offset;
4699 /* Find the full fstring to fix location information in `n`. */
4700 while (parent && parent->n_type != STRING)
4701 parent = parent->n_child;
4702 if (parent && parent->n_str) {
4703 substr = strstr(parent->n_str, expr_str);
4704 if (substr) {
4705 start = substr;
4706 while (start > parent->n_str) {
4707 if (start[0] == '\n')
4708 break;
4709 start--;
4710 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004711 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004712 /* adjust the start based on the number of newlines encountered
4713 before the f-string expression */
4714 for (char* p = parent->n_str; p < substr; p++) {
4715 if (*p == '\n') {
4716 lines++;
4717 }
4718 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004719 }
4720 }
4721 fstring_shift_node_locations(n, lines, cols);
4722}
4723
Eric V. Smith451d0e32016-09-09 21:56:20 -04004724/* Compile this expression in to an expr_ty. Add parens around the
4725 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004726static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004727fstring_compile_expr(const char *expr_start, const char *expr_end,
4728 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004729
Eric V. Smith235a6f02015-09-19 14:51:32 -04004730{
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004731 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004732 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004733 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004734 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004735 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004736
Eric V. Smith1d44c412015-09-23 07:49:00 -04004737 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004738 assert(*(expr_start-1) == '{');
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004739 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' ||
4740 *expr_end == '=');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004741
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004742 /* If the substring is all whitespace, it's an error. We need to catch this
4743 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4744 because turning the expression '' in to '()' would go from being invalid
4745 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004746 for (s = expr_start; s != expr_end; s++) {
4747 char c = *s;
4748 /* The Python parser ignores only the following whitespace
4749 characters (\r already is converted to \n). */
4750 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004751 break;
4752 }
4753 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004754 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004755 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004756 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004757 }
4758
Eric V. Smith451d0e32016-09-09 21:56:20 -04004759 len = expr_end - expr_start;
4760 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4761 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004762 if (str == NULL) {
4763 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004764 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004765 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004766
Eric V. Smith451d0e32016-09-09 21:56:20 -04004767 str[0] = '(';
4768 memcpy(str+1, expr_start, len);
4769 str[len+1] = ')';
4770 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004771
Victor Stinner37d66d72019-06-13 02:16:41 +02004772 PyCompilerFlags cf = _PyCompilerFlags_INIT;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004773 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004774 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4775 Py_eval_input, 0);
4776 if (!mod_n) {
4777 PyMem_RawFree(str);
4778 return NULL;
4779 }
4780 /* Reuse str to find the correct column offset. */
4781 str[0] = '{';
4782 str[len+1] = '}';
4783 fstring_fix_node_location(n, mod_n, str);
4784 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004785 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004786 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004787 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004788 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004789 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004790}
4791
4792/* Return -1 on error.
4793
4794 Return 0 if we reached the end of the literal.
4795
4796 Return 1 if we haven't reached the end of the literal, but we want
4797 the caller to process the literal up to this point. Used for
4798 doubled braces.
4799*/
4800static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004801fstring_find_literal(const char **str, const char *end, int raw,
4802 PyObject **literal, int recurse_lvl,
4803 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004804{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004805 /* Get any literal string. It ends when we hit an un-doubled left
4806 brace (which isn't part of a unicode name escape such as
4807 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004808
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004809 const char *s = *str;
4810 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004811 int result = 0;
4812
Eric V. Smith235a6f02015-09-19 14:51:32 -04004813 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004814 while (s < end) {
4815 char ch = *s++;
4816 if (!raw && ch == '\\' && s < end) {
4817 ch = *s++;
4818 if (ch == 'N') {
4819 if (s < end && *s++ == '{') {
4820 while (s < end && *s++ != '}') {
4821 }
4822 continue;
4823 }
4824 break;
4825 }
4826 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4827 return -1;
4828 }
4829 }
4830 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004831 /* Check for doubled braces, but only at the top level. If
4832 we checked at every level, then f'{0:{3}}' would fail
4833 with the two closing braces. */
4834 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004835 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004836 /* We're going to tell the caller that the literal ends
4837 here, but that they should continue scanning. But also
4838 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004839 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004840 result = 1;
4841 goto done;
4842 }
4843
4844 /* Where a single '{' is the start of a new expression, a
4845 single '}' is not allowed. */
4846 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004847 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004848 ast_error(c, n, "f-string: single '}' is not allowed");
4849 return -1;
4850 }
4851 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004852 /* We're either at a '{', which means we're starting another
4853 expression; or a '}', which means we're at the end of this
4854 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004855 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004856 break;
4857 }
4858 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004859 *str = s;
4860 assert(s <= end);
4861 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004862done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004863 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004864 if (raw)
4865 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004866 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004867 NULL, NULL);
4868 else
Eric V. Smith56466482016-10-31 14:46:26 -04004869 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004870 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004871 if (!*literal)
4872 return -1;
4873 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004874 return result;
4875}
4876
4877/* Forward declaration because parsing is recursive. */
4878static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004879fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004880 struct compiling *c, const node *n);
4881
Eric V. Smith451d0e32016-09-09 21:56:20 -04004882/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004883 expression (so it must be a '{'). Returns the FormattedValue node, which
4884 includes the expression, conversion character, format_spec expression, and
4885 optionally the text of the expression (if = is used).
Eric V. Smith235a6f02015-09-19 14:51:32 -04004886
4887 Note that I don't do a perfect job here: I don't make sure that a
4888 closing brace doesn't match an opening paren, for example. It
4889 doesn't need to error on all invalid expressions, just correctly
4890 find the end of all valid ones. Any errors inside the expression
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004891 will be caught when we parse it later.
4892
4893 *expression is set to the expression. For an '=' "debug" expression,
4894 *expr_text is set to the debug text (the original text of the expression,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004895 including the '=' and any whitespace around it, as a string object). If
4896 not a debug expression, *expr_text set to NULL. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004898fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04004899 PyObject **expr_text, expr_ty *expression,
4900 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004901{
4902 /* Return -1 on error, else 0. */
4903
Eric V. Smith451d0e32016-09-09 21:56:20 -04004904 const char *expr_start;
4905 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906 expr_ty simple_expression;
4907 expr_ty format_spec = NULL; /* Optional format specifier. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004908 int conversion = -1; /* The conversion char. Use default if not
4909 specified, or !r if using = and no format
4910 spec. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004911
4912 /* 0 if we're not in a string, else the quote char we're trying to
4913 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004914 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004915
4916 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4917 int string_type = 0;
4918
4919 /* Keep track of nesting level for braces/parens/brackets in
4920 expressions. */
4921 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004922 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004923
Eric V. Smithf83d1db2019-05-29 03:55:44 -04004924 *expr_text = NULL;
4925
Eric V. Smith235a6f02015-09-19 14:51:32 -04004926 /* Can only nest one level deep. */
4927 if (recurse_lvl >= 2) {
4928 ast_error(c, n, "f-string: expressions nested too deeply");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004929 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004930 }
4931
4932 /* The first char must be a left brace, or we wouldn't have gotten
4933 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004934 assert(**str == '{');
4935 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004936
Eric V. Smith451d0e32016-09-09 21:56:20 -04004937 expr_start = *str;
4938 for (; *str < end; (*str)++) {
4939 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004940
4941 /* Loop invariants. */
4942 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004943 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944 if (quote_char)
4945 assert(string_type == 1 || string_type == 3);
4946 else
4947 assert(string_type == 0);
4948
Eric V. Smith451d0e32016-09-09 21:56:20 -04004949 ch = **str;
4950 /* Nowhere inside an expression is a backslash allowed. */
4951 if (ch == '\\') {
4952 /* Error: can't include a backslash character, inside
4953 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004954 ast_error(c, n,
4955 "f-string expression part "
4956 "cannot include a backslash");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004957 goto error;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004958 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004959 if (quote_char) {
4960 /* We're inside a string. See if we're at the end. */
4961 /* This code needs to implement the same non-error logic
4962 as tok_get from tokenizer.c, at the letter_quote
4963 label. To actually share that code would be a
4964 nightmare. But, it's unlikely to change and is small,
4965 so duplicate it here. Note we don't need to catch all
4966 of the errors, since they'll be caught when parsing the
4967 expression. We just need to match the non-error
4968 cases. Thus we can ignore \n in single-quoted strings,
4969 for example. Or non-terminated strings. */
4970 if (ch == quote_char) {
4971 /* Does this match the string_type (single or triple
4972 quoted)? */
4973 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004974 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004975 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004976 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004977 string_type = 0;
4978 quote_char = 0;
4979 continue;
4980 }
4981 } else {
4982 /* We're at the end of a normal string. */
4983 quote_char = 0;
4984 string_type = 0;
4985 continue;
4986 }
4987 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004988 } else if (ch == '\'' || ch == '"') {
4989 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004990 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004991 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004992 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 } else {
4994 /* Start of a normal string. */
4995 string_type = 1;
4996 }
4997 /* Start looking for the end of the string. */
4998 quote_char = ch;
4999 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005000 if (nested_depth >= MAXLEVEL) {
5001 ast_error(c, n, "f-string: too many nested parenthesis");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005002 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005003 }
5004 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005005 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005006 } else if (ch == '#') {
5007 /* Error: can't include a comment character, inside parens
5008 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005009 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005010 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005011 } else if (nested_depth == 0 &&
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005012 (ch == '!' || ch == ':' || ch == '}' ||
5013 ch == '=' || ch == '>' || ch == '<')) {
5014 /* See if there's a next character. */
5015 if (*str+1 < end) {
5016 char next = *(*str+1);
5017
5018 /* For "!=". since '=' is not an allowed conversion character,
5019 nothing is lost in this test. */
5020 if ((ch == '!' && next == '=') || /* != */
5021 (ch == '=' && next == '=') || /* == */
5022 (ch == '<' && next == '=') || /* <= */
5023 (ch == '>' && next == '=') /* >= */
5024 ) {
5025 *str += 1;
5026 continue;
5027 }
5028 /* Don't get out of the loop for these, if they're single
5029 chars (not part of 2-char tokens). If by themselves, they
5030 don't end an expression (unlike say '!'). */
5031 if (ch == '>' || ch == '<') {
5032 continue;
5033 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005034 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005035
Eric V. Smith235a6f02015-09-19 14:51:32 -04005036 /* Normal way out of this loop. */
5037 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005038 } else if (ch == ']' || ch == '}' || ch == ')') {
5039 if (!nested_depth) {
5040 ast_error(c, n, "f-string: unmatched '%c'", ch);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005041 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005042 }
5043 nested_depth--;
5044 int opening = parenstack[nested_depth];
5045 if (!((opening == '(' && ch == ')') ||
5046 (opening == '[' && ch == ']') ||
5047 (opening == '{' && ch == '}')))
5048 {
5049 ast_error(c, n,
5050 "f-string: closing parenthesis '%c' "
5051 "does not match opening parenthesis '%c'",
5052 ch, opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005053 goto error;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005054 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005055 } else {
5056 /* Just consume this char and loop around. */
5057 }
5058 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005059 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005060 /* If we leave this loop in a string or with mismatched parens, we
5061 don't care. We'll get a syntax error when compiling the
5062 expression. But, we can produce a better error message, so
5063 let's just do that.*/
5064 if (quote_char) {
5065 ast_error(c, n, "f-string: unterminated string");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005066 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005067 }
5068 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005069 int opening = parenstack[nested_depth - 1];
5070 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005071 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005072 }
5073
Eric V. Smith451d0e32016-09-09 21:56:20 -04005074 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005075 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005076
5077 /* Compile the expression as soon as possible, so we show errors
5078 related to the expression before errors related to the
5079 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005080 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005081 if (!simple_expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005082 goto error;
5083
5084 /* Check for =, which puts the text value of the expression in
5085 expr_text. */
5086 if (**str == '=') {
5087 *str += 1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005088
5089 /* Skip over ASCII whitespace. No need to test for end of string
5090 here, since we know there's at least a trailing quote somewhere
5091 ahead. */
5092 while (Py_ISSPACE(**str)) {
5093 *str += 1;
5094 }
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005095
5096 /* Set *expr_text to the text of the expression. */
5097 *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start);
5098 if (!*expr_text) {
5099 goto error;
5100 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005101 }
Eric V. Smith1d44c412015-09-23 07:49:00 -04005102
5103 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005104 if (**str == '!') {
5105 *str += 1;
5106 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005107 goto unexpected_end_of_string;
5108
Eric V. Smith451d0e32016-09-09 21:56:20 -04005109 conversion = **str;
5110 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005111
5112 /* Validate the conversion. */
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005113 if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005114 ast_error(c, n,
5115 "f-string: invalid conversion character: "
5116 "expected 's', 'r', or 'a'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005117 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118 }
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005119
5120 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005121
5122 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005123 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005124 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005125 if (**str == ':') {
5126 *str += 1;
5127 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005128 goto unexpected_end_of_string;
5129
5130 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005131 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005132 if (!format_spec)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005133 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134 }
5135
Eric V. Smith451d0e32016-09-09 21:56:20 -04005136 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005137 goto unexpected_end_of_string;
5138
5139 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005140 assert(*str < end);
5141 assert(**str == '}');
5142 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005143
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005144 /* If we're in = mode (detected by non-NULL expr_text), and have no format
Min ho Kimc4cacc82019-07-31 08:16:13 +10005145 spec and no explicit conversion, set the conversion to 'r'. */
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005146 if (*expr_text && format_spec == NULL && conversion == -1) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005147 conversion = 'r';
5148 }
5149
Eric V. Smith451d0e32016-09-09 21:56:20 -04005150 /* And now create the FormattedValue node that represents this
5151 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005152 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005153 format_spec, LINENO(n),
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005154 n->n_col_offset, n->n_end_lineno,
5155 n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005156 if (!*expression)
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005157 goto error;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005158
5159 return 0;
5160
5161unexpected_end_of_string:
5162 ast_error(c, n, "f-string: expecting '}'");
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005163 /* Falls through to error. */
5164
5165error:
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005166 Py_XDECREF(*expr_text);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005167 return -1;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04005168
Eric V. Smith235a6f02015-09-19 14:51:32 -04005169}
5170
5171/* Return -1 on error.
5172
5173 Return 0 if we have a literal (possible zero length) and an
5174 expression (zero length if at the end of the string.
5175
5176 Return 1 if we have a literal, but no expression, and we want the
5177 caller to call us again. This is used to deal with doubled
5178 braces.
5179
5180 When called multiple times on the string 'a{{b{0}c', this function
5181 will return:
5182
5183 1. the literal 'a{' with no expression, and a return value
5184 of 1. Despite the fact that there's no expression, the return
5185 value of 1 means we're not finished yet.
5186
5187 2. the literal 'b' and the expression '0', with a return value of
5188 0. The fact that there's an expression means we're not finished.
5189
5190 3. literal 'c' with no expression and a return value of 0. The
5191 combination of the return value of 0 with no expression means
5192 we're finished.
5193*/
5194static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005195fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5196 int recurse_lvl, PyObject **literal,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005197 PyObject **expr_text, expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005198 struct compiling *c, const node *n)
5199{
5200 int result;
5201
5202 assert(*literal == NULL && *expression == NULL);
5203
5204 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005205 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005206 if (result < 0)
5207 goto error;
5208
5209 assert(result == 0 || result == 1);
5210
5211 if (result == 1)
5212 /* We have a literal, but don't look at the expression. */
5213 return 1;
5214
Eric V. Smith451d0e32016-09-09 21:56:20 -04005215 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005216 /* We're at the end of the string or the end of a nested
5217 f-string: no expression. The top-level error case where we
5218 expect to be at the end of the string but we're at a '}' is
5219 handled later. */
5220 return 0;
5221
5222 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005223 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005224
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005225 if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text,
5226 expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005227 goto error;
5228
5229 return 0;
5230
5231error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005232 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005233 return -1;
5234}
5235
5236#define EXPRLIST_N_CACHED 64
5237
5238typedef struct {
5239 /* Incrementally build an array of expr_ty, so be used in an
5240 asdl_seq. Cache some small but reasonably sized number of
5241 expr_ty's, and then after that start dynamically allocating,
5242 doubling the number allocated each time. Note that the f-string
5243 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005244 Constant for the literal 'a'. So you add expr_ty's about twice as
Min ho Kim39d87b52019-08-31 06:21:19 +10005245 fast as you add expressions in an f-string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005246
5247 Py_ssize_t allocated; /* Number we've allocated. */
5248 Py_ssize_t size; /* Number we've used. */
5249 expr_ty *p; /* Pointer to the memory we're actually
5250 using. Will point to 'data' until we
5251 start dynamically allocating. */
5252 expr_ty data[EXPRLIST_N_CACHED];
5253} ExprList;
5254
5255#ifdef NDEBUG
5256#define ExprList_check_invariants(l)
5257#else
5258static void
5259ExprList_check_invariants(ExprList *l)
5260{
5261 /* Check our invariants. Make sure this object is "live", and
5262 hasn't been deallocated. */
5263 assert(l->size >= 0);
5264 assert(l->p != NULL);
5265 if (l->size <= EXPRLIST_N_CACHED)
5266 assert(l->data == l->p);
5267}
5268#endif
5269
5270static void
5271ExprList_Init(ExprList *l)
5272{
5273 l->allocated = EXPRLIST_N_CACHED;
5274 l->size = 0;
5275
5276 /* Until we start allocating dynamically, p points to data. */
5277 l->p = l->data;
5278
5279 ExprList_check_invariants(l);
5280}
5281
5282static int
5283ExprList_Append(ExprList *l, expr_ty exp)
5284{
5285 ExprList_check_invariants(l);
5286 if (l->size >= l->allocated) {
5287 /* We need to alloc (or realloc) the memory. */
5288 Py_ssize_t new_size = l->allocated * 2;
5289
5290 /* See if we've ever allocated anything dynamically. */
5291 if (l->p == l->data) {
5292 Py_ssize_t i;
5293 /* We're still using the cached data. Switch to
5294 alloc-ing. */
5295 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5296 if (!l->p)
5297 return -1;
5298 /* Copy the cached data into the new buffer. */
5299 for (i = 0; i < l->size; i++)
5300 l->p[i] = l->data[i];
5301 } else {
5302 /* Just realloc. */
5303 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5304 if (!tmp) {
5305 PyMem_RawFree(l->p);
5306 l->p = NULL;
5307 return -1;
5308 }
5309 l->p = tmp;
5310 }
5311
5312 l->allocated = new_size;
5313 assert(l->allocated == 2 * l->size);
5314 }
5315
5316 l->p[l->size++] = exp;
5317
5318 ExprList_check_invariants(l);
5319 return 0;
5320}
5321
5322static void
5323ExprList_Dealloc(ExprList *l)
5324{
5325 ExprList_check_invariants(l);
5326
5327 /* If there's been an error, or we've never dynamically allocated,
5328 do nothing. */
5329 if (!l->p || l->p == l->data) {
5330 /* Do nothing. */
5331 } else {
5332 /* We have dynamically allocated. Free the memory. */
5333 PyMem_RawFree(l->p);
5334 }
5335 l->p = NULL;
5336 l->size = -1;
5337}
5338
5339static asdl_seq *
5340ExprList_Finish(ExprList *l, PyArena *arena)
5341{
5342 asdl_seq *seq;
5343
5344 ExprList_check_invariants(l);
5345
5346 /* Allocate the asdl_seq and copy the expressions in to it. */
5347 seq = _Py_asdl_seq_new(l->size, arena);
5348 if (seq) {
5349 Py_ssize_t i;
5350 for (i = 0; i < l->size; i++)
5351 asdl_seq_SET(seq, i, l->p[i]);
5352 }
5353 ExprList_Dealloc(l);
5354 return seq;
5355}
5356
5357/* The FstringParser is designed to add a mix of strings and
5358 f-strings, and concat them together as needed. Ultimately, it
5359 generates an expr_ty. */
5360typedef struct {
5361 PyObject *last_str;
5362 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005363 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005364} FstringParser;
5365
5366#ifdef NDEBUG
5367#define FstringParser_check_invariants(state)
5368#else
5369static void
5370FstringParser_check_invariants(FstringParser *state)
5371{
5372 if (state->last_str)
5373 assert(PyUnicode_CheckExact(state->last_str));
5374 ExprList_check_invariants(&state->expr_list);
5375}
5376#endif
5377
5378static void
5379FstringParser_Init(FstringParser *state)
5380{
5381 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005382 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005383 ExprList_Init(&state->expr_list);
5384 FstringParser_check_invariants(state);
5385}
5386
5387static void
5388FstringParser_Dealloc(FstringParser *state)
5389{
5390 FstringParser_check_invariants(state);
5391
5392 Py_XDECREF(state->last_str);
5393 ExprList_Dealloc(&state->expr_list);
5394}
5395
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005396/* Constants for the following */
5397static PyObject *u_kind;
5398
5399/* Compute 'kind' field for string Constant (either 'u' or None) */
5400static PyObject *
5401make_kind(struct compiling *c, const node *n)
5402{
5403 char *s = NULL;
5404 PyObject *kind = NULL;
5405
5406 /* Find the first string literal, if any */
5407 while (TYPE(n) != STRING) {
5408 if (NCH(n) == 0)
5409 return NULL;
5410 n = CHILD(n, 0);
5411 }
5412 REQ(n, STRING);
5413
5414 /* If it starts with 'u', return a PyUnicode "u" string */
5415 s = STR(n);
5416 if (s && *s == 'u') {
5417 if (!u_kind) {
5418 u_kind = PyUnicode_InternFromString("u");
5419 if (!u_kind)
5420 return NULL;
5421 }
5422 kind = u_kind;
5423 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5424 return NULL;
5425 }
5426 Py_INCREF(kind);
5427 }
5428 return kind;
5429}
5430
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005431/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005432static expr_ty
5433make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5434{
5435 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005436 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005437 *str = NULL;
5438 assert(PyUnicode_CheckExact(s));
5439 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5440 Py_DECREF(s);
5441 return NULL;
5442 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005443 kind = make_kind(c, n);
5444 if (kind == NULL && PyErr_Occurred())
5445 return NULL;
5446 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005447 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005448}
5449
5450/* Add a non-f-string (that is, a regular literal string). str is
5451 decref'd. */
5452static int
5453FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5454{
5455 FstringParser_check_invariants(state);
5456
5457 assert(PyUnicode_CheckExact(str));
5458
5459 if (PyUnicode_GET_LENGTH(str) == 0) {
5460 Py_DECREF(str);
5461 return 0;
5462 }
5463
5464 if (!state->last_str) {
5465 /* We didn't have a string before, so just remember this one. */
5466 state->last_str = str;
5467 } else {
5468 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005469 PyUnicode_AppendAndDel(&state->last_str, str);
5470 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005471 return -1;
5472 }
5473 FstringParser_check_invariants(state);
5474 return 0;
5475}
5476
Eric V. Smith451d0e32016-09-09 21:56:20 -04005477/* Parse an f-string. The f-string is in *str to end, with no
5478 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005479static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005480FstringParser_ConcatFstring(FstringParser *state, const char **str,
5481 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005482 struct compiling *c, const node *n)
5483{
5484 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005485 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005486
5487 /* Parse the f-string. */
5488 while (1) {
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005489 PyObject *literal = NULL;
5490 PyObject *expr_text = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005491 expr_ty expression = NULL;
5492
5493 /* If there's a zero length literal in front of the
5494 expression, literal will be NULL. If we're at the end of
5495 the f-string, expression will be NULL (unless result == 1,
5496 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005497 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005498 &literal, &expr_text,
Eric V. Smith6f6ff8a2019-05-27 15:31:52 -04005499 &expression, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005500 if (result < 0)
5501 return -1;
5502
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005503 /* Add the literal, if any. */
5504 if (literal && FstringParser_ConcatAndDel(state, literal) < 0) {
5505 Py_XDECREF(expr_text);
5506 return -1;
5507 }
5508 /* Add the expr_text, if any. */
5509 if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) {
5510 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005511 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005512
Eric V. Smithf83d1db2019-05-29 03:55:44 -04005513 /* We've dealt with the literal and expr_text, their ownership has
5514 been transferred to the state object. Don't look at them again. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005515
5516 /* See if we should just loop around to get the next literal
5517 and expression, while ignoring the expression this
5518 time. This is used for un-doubling braces, as an
5519 optimization. */
5520 if (result == 1)
5521 continue;
5522
5523 if (!expression)
5524 /* We're done with this f-string. */
5525 break;
5526
5527 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005528 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005529 if (!state->last_str) {
5530 /* Do nothing. No previous literal. */
5531 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005532 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005533 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5534 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5535 return -1;
5536 }
5537
5538 if (ExprList_Append(&state->expr_list, expression) < 0)
5539 return -1;
5540 }
5541
Eric V. Smith235a6f02015-09-19 14:51:32 -04005542 /* If recurse_lvl is zero, then we must be at the end of the
5543 string. Otherwise, we must be at a right brace. */
5544
Eric V. Smith451d0e32016-09-09 21:56:20 -04005545 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005546 ast_error(c, n, "f-string: unexpected end of string");
5547 return -1;
5548 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005549 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005550 ast_error(c, n, "f-string: expecting '}'");
5551 return -1;
5552 }
5553
5554 FstringParser_check_invariants(state);
5555 return 0;
5556}
5557
5558/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005559 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005560static expr_ty
5561FstringParser_Finish(FstringParser *state, struct compiling *c,
5562 const node *n)
5563{
5564 asdl_seq *seq;
5565
5566 FstringParser_check_invariants(state);
5567
5568 /* If we're just a constant string with no expressions, return
5569 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005570 if (!state->fmode) {
5571 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005572 if (!state->last_str) {
5573 /* Create a zero length string. */
5574 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5575 if (!state->last_str)
5576 goto error;
5577 }
5578 return make_str_node_and_del(&state->last_str, c, n);
5579 }
5580
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005581 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005582 last node in our expression list. */
5583 if (state->last_str) {
5584 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5585 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5586 goto error;
5587 }
5588 /* This has already been freed. */
5589 assert(state->last_str == NULL);
5590
5591 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5592 if (!seq)
5593 goto error;
5594
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005595 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5596 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005597
5598error:
5599 FstringParser_Dealloc(state);
5600 return NULL;
5601}
5602
Eric V. Smith451d0e32016-09-09 21:56:20 -04005603/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5604 at end, parse it into an expr_ty. Return NULL on error. Adjust
5605 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005606static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005607fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005608 struct compiling *c, const node *n)
5609{
5610 FstringParser state;
5611
5612 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005613 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005614 c, n) < 0) {
5615 FstringParser_Dealloc(&state);
5616 return NULL;
5617 }
5618
5619 return FstringParser_Finish(&state, c, n);
5620}
5621
5622/* n is a Python string literal, including the bracketing quote
5623 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005624 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005625 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005626 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5627 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005628*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005629static int
5630parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5631 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005632{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005633 size_t len;
5634 const char *s = STR(n);
5635 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005636 int fmode = 0;
5637 *bytesmode = 0;
5638 *rawmode = 0;
5639 *result = NULL;
5640 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005641 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005642 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005643 if (quote == 'b' || quote == 'B') {
5644 quote = *++s;
5645 *bytesmode = 1;
5646 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005647 else if (quote == 'u' || quote == 'U') {
5648 quote = *++s;
5649 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005650 else if (quote == 'r' || quote == 'R') {
5651 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005652 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005653 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005654 else if (quote == 'f' || quote == 'F') {
5655 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005656 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005657 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005658 else {
5659 break;
5660 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005661 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005662 }
Guido van Rossum495da292019-03-07 12:38:08 -08005663
5664 /* fstrings are only allowed in Python 3.6 and greater */
5665 if (fmode && c->c_feature_version < 6) {
5666 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5667 return -1;
5668 }
5669
Eric V. Smith451d0e32016-09-09 21:56:20 -04005670 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005671 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005672 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005673 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005674 if (quote != '\'' && quote != '\"') {
5675 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005676 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005677 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005678 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005679 s++;
5680 len = strlen(s);
5681 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005682 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005683 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005684 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005685 }
5686 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005687 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005688 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005689 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005690 }
5691 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005692 /* A triple quoted string. We've already skipped one quote at
5693 the start and one at the end of the string. Now skip the
5694 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005695 s += 2;
5696 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005697 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005698 if (s[--len] != quote || s[--len] != quote) {
5699 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005700 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005701 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005702 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005703
Eric V. Smith451d0e32016-09-09 21:56:20 -04005704 if (fmode) {
5705 /* Just return the bytes. The caller will parse the resulting
5706 string. */
5707 *fstr = s;
5708 *fstrlen = len;
5709 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005710 }
5711
Eric V. Smith451d0e32016-09-09 21:56:20 -04005712 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005713 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005714 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005715 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005716 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005717 const char *ch;
5718 for (ch = s; *ch; ch++) {
5719 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005720 ast_error(c, n,
5721 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005722 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005723 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005724 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005725 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005726 if (*rawmode)
5727 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005728 else
Eric V. Smith56466482016-10-31 14:46:26 -04005729 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005730 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005731 if (*rawmode)
5732 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005733 else
Eric V. Smith56466482016-10-31 14:46:26 -04005734 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005735 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005736 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005737}
5738
Eric V. Smith235a6f02015-09-19 14:51:32 -04005739/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5740 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005741 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005742 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005743 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005744 node if there's just an f-string (with no leading or trailing
5745 literals), or a JoinedStr node if there are multiple f-strings or
5746 any literals involved. */
5747static expr_ty
5748parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005749{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005750 int bytesmode = 0;
5751 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005752 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005753
5754 FstringParser state;
5755 FstringParser_Init(&state);
5756
5757 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005758 int this_bytesmode;
5759 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005760 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005761 const char *fstr;
5762 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005763
5764 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005765 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5766 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005767 goto error;
5768
5769 /* Check that we're not mixing bytes with unicode. */
5770 if (i != 0 && bytesmode != this_bytesmode) {
5771 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005772 /* s is NULL if the current string part is an f-string. */
5773 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005774 goto error;
5775 }
5776 bytesmode = this_bytesmode;
5777
Eric V. Smith451d0e32016-09-09 21:56:20 -04005778 if (fstr != NULL) {
5779 int result;
5780 assert(s == NULL && !bytesmode);
5781 /* This is an f-string. Parse and concatenate it. */
5782 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5783 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005784 if (result < 0)
5785 goto error;
5786 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005787 /* A string or byte string. */
5788 assert(s != NULL && fstr == NULL);
5789
Eric V. Smith451d0e32016-09-09 21:56:20 -04005790 assert(bytesmode ? PyBytes_CheckExact(s) :
5791 PyUnicode_CheckExact(s));
5792
Eric V. Smith451d0e32016-09-09 21:56:20 -04005793 if (bytesmode) {
5794 /* For bytes, concat as we go. */
5795 if (i == 0) {
5796 /* First time, just remember this value. */
5797 bytes_str = s;
5798 } else {
5799 PyBytes_ConcatAndDel(&bytes_str, s);
5800 if (!bytes_str)
5801 goto error;
5802 }
5803 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005804 /* This is a regular string. Concatenate it. */
5805 if (FstringParser_ConcatAndDel(&state, s) < 0)
5806 goto error;
5807 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005808 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005809 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005810 if (bytesmode) {
5811 /* Just return the bytes object and we're done. */
5812 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5813 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005814 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005815 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005816 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005817
Eric V. Smith235a6f02015-09-19 14:51:32 -04005818 /* We're not a bytes string, bytes_str should never have been set. */
5819 assert(bytes_str == NULL);
5820
5821 return FstringParser_Finish(&state, c, n);
5822
5823error:
5824 Py_XDECREF(bytes_str);
5825 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005827}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005828
5829PyObject *
5830_PyAST_GetDocString(asdl_seq *body)
5831{
5832 if (!asdl_seq_LEN(body)) {
5833 return NULL;
5834 }
5835 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5836 if (st->kind != Expr_kind) {
5837 return NULL;
5838 }
5839 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005840 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5841 return e->v.Constant.value;
5842 }
5843 return NULL;
5844}