blob: 971b8ddc8c24472711bc9a950e5971ed95256e50 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
Serhiy Storchakaddbce132017-11-15 17:39:37 +020014#include <stdbool.h>
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Serhiy Storchaka58159ef2019-01-12 09:46:50 +020016#define MAXLEVEL 200 /* Max parentheses level */
17
Benjamin Peterson832bfe22011-08-09 16:15:04 -050018static int validate_stmts(asdl_seq *);
19static int validate_exprs(asdl_seq *, expr_context_ty, int);
20static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
21static int validate_stmt(stmt_ty);
22static int validate_expr(expr_ty, expr_context_ty);
23
24static int
25validate_comprehension(asdl_seq *gens)
26{
Victor Stinner4d73ae72018-11-22 14:45:16 +010027 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050028 if (!asdl_seq_LEN(gens)) {
29 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
30 return 0;
31 }
32 for (i = 0; i < asdl_seq_LEN(gens); i++) {
33 comprehension_ty comp = asdl_seq_GET(gens, i);
34 if (!validate_expr(comp->target, Store) ||
35 !validate_expr(comp->iter, Load) ||
36 !validate_exprs(comp->ifs, Load, 0))
37 return 0;
38 }
39 return 1;
40}
41
42static int
43validate_slice(slice_ty slice)
44{
45 switch (slice->kind) {
46 case Slice_kind:
47 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
48 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
49 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
50 case ExtSlice_kind: {
Victor Stinner4d73ae72018-11-22 14:45:16 +010051 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050052 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
53 return 0;
54 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
55 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
56 return 0;
57 return 1;
58 }
59 case Index_kind:
60 return validate_expr(slice->v.Index.value, Load);
61 default:
62 PyErr_SetString(PyExc_SystemError, "unknown slice node");
63 return 0;
64 }
65}
66
67static int
68validate_keywords(asdl_seq *keywords)
69{
Victor Stinner4d73ae72018-11-22 14:45:16 +010070 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050071 for (i = 0; i < asdl_seq_LEN(keywords); i++)
72 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
73 return 0;
74 return 1;
75}
76
77static int
78validate_args(asdl_seq *args)
79{
Victor Stinner4d73ae72018-11-22 14:45:16 +010080 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050081 for (i = 0; i < asdl_seq_LEN(args); i++) {
82 arg_ty arg = asdl_seq_GET(args, i);
83 if (arg->annotation && !validate_expr(arg->annotation, Load))
84 return 0;
85 }
86 return 1;
87}
88
89static const char *
90expr_context_name(expr_context_ty ctx)
91{
92 switch (ctx) {
93 case Load:
94 return "Load";
95 case Store:
96 return "Store";
97 case Del:
98 return "Del";
99 case AugLoad:
100 return "AugLoad";
101 case AugStore:
102 return "AugStore";
103 case Param:
104 return "Param";
105 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700106 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500107 }
108}
109
110static int
111validate_arguments(arguments_ty args)
112{
113 if (!validate_args(args->args))
114 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700115 if (args->vararg && args->vararg->annotation
116 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500117 return 0;
118 }
119 if (!validate_args(args->kwonlyargs))
120 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100121 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700122 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500123 return 0;
124 }
125 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
126 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
127 return 0;
128 }
129 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
130 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
131 "kw_defaults on arguments");
132 return 0;
133 }
134 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
135}
136
137static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100138validate_constant(PyObject *value)
139{
140 if (value == Py_None || value == Py_Ellipsis)
141 return 1;
142
143 if (PyLong_CheckExact(value)
144 || PyFloat_CheckExact(value)
145 || PyComplex_CheckExact(value)
146 || PyBool_Check(value)
147 || PyUnicode_CheckExact(value)
148 || PyBytes_CheckExact(value))
149 return 1;
150
151 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
152 PyObject *it;
153
154 it = PyObject_GetIter(value);
155 if (it == NULL)
156 return 0;
157
158 while (1) {
159 PyObject *item = PyIter_Next(it);
160 if (item == NULL) {
161 if (PyErr_Occurred()) {
162 Py_DECREF(it);
163 return 0;
164 }
165 break;
166 }
167
168 if (!validate_constant(item)) {
169 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 return 0;
172 }
Victor Stinner726f6902016-01-27 00:11:47 +0100173 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100174 }
175
176 Py_DECREF(it);
177 return 1;
178 }
179
180 return 0;
181}
182
183static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500184validate_expr(expr_ty exp, expr_context_ty ctx)
185{
186 int check_ctx = 1;
187 expr_context_ty actual_ctx;
188
189 /* First check expression context. */
190 switch (exp->kind) {
191 case Attribute_kind:
192 actual_ctx = exp->v.Attribute.ctx;
193 break;
194 case Subscript_kind:
195 actual_ctx = exp->v.Subscript.ctx;
196 break;
197 case Starred_kind:
198 actual_ctx = exp->v.Starred.ctx;
199 break;
200 case Name_kind:
201 actual_ctx = exp->v.Name.ctx;
202 break;
203 case List_kind:
204 actual_ctx = exp->v.List.ctx;
205 break;
206 case Tuple_kind:
207 actual_ctx = exp->v.Tuple.ctx;
208 break;
209 default:
210 if (ctx != Load) {
211 PyErr_Format(PyExc_ValueError, "expression which can't be "
212 "assigned to in %s context", expr_context_name(ctx));
213 return 0;
214 }
215 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100216 /* set actual_ctx to prevent gcc warning */
217 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500218 }
219 if (check_ctx && actual_ctx != ctx) {
220 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
221 expr_context_name(ctx), expr_context_name(actual_ctx));
222 return 0;
223 }
224
225 /* Now validate expression. */
226 switch (exp->kind) {
227 case BoolOp_kind:
228 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
229 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
230 return 0;
231 }
232 return validate_exprs(exp->v.BoolOp.values, Load, 0);
233 case BinOp_kind:
234 return validate_expr(exp->v.BinOp.left, Load) &&
235 validate_expr(exp->v.BinOp.right, Load);
236 case UnaryOp_kind:
237 return validate_expr(exp->v.UnaryOp.operand, Load);
238 case Lambda_kind:
239 return validate_arguments(exp->v.Lambda.args) &&
240 validate_expr(exp->v.Lambda.body, Load);
241 case IfExp_kind:
242 return validate_expr(exp->v.IfExp.test, Load) &&
243 validate_expr(exp->v.IfExp.body, Load) &&
244 validate_expr(exp->v.IfExp.orelse, Load);
245 case Dict_kind:
246 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
247 PyErr_SetString(PyExc_ValueError,
248 "Dict doesn't have the same number of keys as values");
249 return 0;
250 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400251 /* null_ok=1 for keys expressions to allow dict unpacking to work in
252 dict literals, i.e. ``{**{a:b}}`` */
253 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
254 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500255 case Set_kind:
256 return validate_exprs(exp->v.Set.elts, Load, 0);
257#define COMP(NAME) \
258 case NAME ## _kind: \
259 return validate_comprehension(exp->v.NAME.generators) && \
260 validate_expr(exp->v.NAME.elt, Load);
261 COMP(ListComp)
262 COMP(SetComp)
263 COMP(GeneratorExp)
264#undef COMP
265 case DictComp_kind:
266 return validate_comprehension(exp->v.DictComp.generators) &&
267 validate_expr(exp->v.DictComp.key, Load) &&
268 validate_expr(exp->v.DictComp.value, Load);
269 case Yield_kind:
270 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500271 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000272 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400273 case Await_kind:
274 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500275 case Compare_kind:
276 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
277 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
278 return 0;
279 }
280 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
281 asdl_seq_LEN(exp->v.Compare.ops)) {
282 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
283 "of comparators and operands");
284 return 0;
285 }
286 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
287 validate_expr(exp->v.Compare.left, Load);
288 case Call_kind:
289 return validate_expr(exp->v.Call.func, Load) &&
290 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400291 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100292 case Constant_kind:
293 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100294 PyErr_Format(PyExc_TypeError,
295 "got an invalid type in Constant: %s",
296 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100297 return 0;
298 }
299 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400300 case JoinedStr_kind:
301 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
302 case FormattedValue_kind:
303 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
304 return 0;
305 if (exp->v.FormattedValue.format_spec)
306 return validate_expr(exp->v.FormattedValue.format_spec, Load);
307 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500308 case Attribute_kind:
309 return validate_expr(exp->v.Attribute.value, Load);
310 case Subscript_kind:
311 return validate_slice(exp->v.Subscript.slice) &&
312 validate_expr(exp->v.Subscript.value, Load);
313 case Starred_kind:
314 return validate_expr(exp->v.Starred.value, ctx);
315 case List_kind:
316 return validate_exprs(exp->v.List.elts, ctx, 0);
317 case Tuple_kind:
318 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300319 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500320 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500321 return 1;
322 default:
323 PyErr_SetString(PyExc_SystemError, "unexpected expression");
324 return 0;
325 }
326}
327
328static int
329validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
330{
331 if (asdl_seq_LEN(seq))
332 return 1;
333 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
334 return 0;
335}
336
337static int
338validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
339{
340 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
341 validate_exprs(targets, ctx, 0);
342}
343
344static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300345validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348}
349
350static int
351validate_stmt(stmt_ty stmt)
352{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100353 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500354 switch (stmt->kind) {
355 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300356 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500357 validate_arguments(stmt->v.FunctionDef.args) &&
358 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
359 (!stmt->v.FunctionDef.returns ||
360 validate_expr(stmt->v.FunctionDef.returns, Load));
361 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300362 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500363 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
364 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400365 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500366 case Return_kind:
367 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
368 case Delete_kind:
369 return validate_assignlist(stmt->v.Delete.targets, Del);
370 case Assign_kind:
371 return validate_assignlist(stmt->v.Assign.targets, Store) &&
372 validate_expr(stmt->v.Assign.value, Load);
373 case AugAssign_kind:
374 return validate_expr(stmt->v.AugAssign.target, Store) &&
375 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700376 case AnnAssign_kind:
377 if (stmt->v.AnnAssign.target->kind != Name_kind &&
378 stmt->v.AnnAssign.simple) {
379 PyErr_SetString(PyExc_TypeError,
380 "AnnAssign with simple non-Name target");
381 return 0;
382 }
383 return validate_expr(stmt->v.AnnAssign.target, Store) &&
384 (!stmt->v.AnnAssign.value ||
385 validate_expr(stmt->v.AnnAssign.value, Load)) &&
386 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500387 case For_kind:
388 return validate_expr(stmt->v.For.target, Store) &&
389 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300390 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500391 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400392 case AsyncFor_kind:
393 return validate_expr(stmt->v.AsyncFor.target, Store) &&
394 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300395 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400396 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500397 case While_kind:
398 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300399 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500400 validate_stmts(stmt->v.While.orelse);
401 case If_kind:
402 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300403 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500404 validate_stmts(stmt->v.If.orelse);
405 case With_kind:
406 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
407 return 0;
408 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
409 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
410 if (!validate_expr(item->context_expr, Load) ||
411 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
412 return 0;
413 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300414 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400415 case AsyncWith_kind:
416 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
417 return 0;
418 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
419 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
420 if (!validate_expr(item->context_expr, Load) ||
421 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
422 return 0;
423 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300424 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500425 case Raise_kind:
426 if (stmt->v.Raise.exc) {
427 return validate_expr(stmt->v.Raise.exc, Load) &&
428 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
429 }
430 if (stmt->v.Raise.cause) {
431 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
432 return 0;
433 }
434 return 1;
435 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300436 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500437 return 0;
438 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
439 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
440 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
441 return 0;
442 }
443 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
444 asdl_seq_LEN(stmt->v.Try.orelse)) {
445 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
446 return 0;
447 }
448 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
449 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
450 if ((handler->v.ExceptHandler.type &&
451 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300452 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500453 return 0;
454 }
455 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
456 validate_stmts(stmt->v.Try.finalbody)) &&
457 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
458 validate_stmts(stmt->v.Try.orelse));
459 case Assert_kind:
460 return validate_expr(stmt->v.Assert.test, Load) &&
461 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
462 case Import_kind:
463 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
464 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300465 if (stmt->v.ImportFrom.level < 0) {
466 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500467 return 0;
468 }
469 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
470 case Global_kind:
471 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
472 case Nonlocal_kind:
473 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
474 case Expr_kind:
475 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400476 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300477 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400478 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
479 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
480 (!stmt->v.AsyncFunctionDef.returns ||
481 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500482 case Pass_kind:
483 case Break_kind:
484 case Continue_kind:
485 return 1;
486 default:
487 PyErr_SetString(PyExc_SystemError, "unexpected statement");
488 return 0;
489 }
490}
491
492static int
493validate_stmts(asdl_seq *seq)
494{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100495 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500496 for (i = 0; i < asdl_seq_LEN(seq); i++) {
497 stmt_ty stmt = asdl_seq_GET(seq, i);
498 if (stmt) {
499 if (!validate_stmt(stmt))
500 return 0;
501 }
502 else {
503 PyErr_SetString(PyExc_ValueError,
504 "None disallowed in statement list");
505 return 0;
506 }
507 }
508 return 1;
509}
510
511static int
512validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
513{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100514 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500515 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
516 expr_ty expr = asdl_seq_GET(exprs, i);
517 if (expr) {
518 if (!validate_expr(expr, ctx))
519 return 0;
520 }
521 else if (!null_ok) {
522 PyErr_SetString(PyExc_ValueError,
523 "None disallowed in expression list");
524 return 0;
525 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100526
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500527 }
528 return 1;
529}
530
531int
532PyAST_Validate(mod_ty mod)
533{
534 int res = 0;
535
536 switch (mod->kind) {
537 case Module_kind:
538 res = validate_stmts(mod->v.Module.body);
539 break;
540 case Interactive_kind:
541 res = validate_stmts(mod->v.Interactive.body);
542 break;
543 case Expression_kind:
544 res = validate_expr(mod->v.Expression.body, Load);
545 break;
546 case Suite_kind:
547 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
548 break;
549 default:
550 PyErr_SetString(PyExc_SystemError, "impossible module node");
551 res = 0;
552 break;
553 }
554 return res;
555}
556
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500557/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500558#include "grammar.h"
559#include "parsetok.h"
560#include "graminit.h"
561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562/* Data structure used internally */
563struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400564 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200565 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500566 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800567 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568};
569
570static asdl_seq *seq_for_testlist(struct compiling *, const node *);
571static expr_ty ast_for_expr(struct compiling *, const node *);
572static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300573static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000574static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
575 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000576static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000577static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578
guoci90fc8982018-09-11 17:45:45 -0400579static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
580static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200583static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000584 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000586static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400587static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000588static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
Nick Coghlan650f0d02007-04-15 12:05:43 +0000590#define COMP_GENEXP 0
591#define COMP_LISTCOMP 1
592#define COMP_SETCOMP 2
593
Benjamin Peterson55e00432012-01-16 17:22:31 -0500594static int
595init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000596{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500597 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
598 if (!m)
599 return 0;
600 c->c_normalize = PyObject_GetAttrString(m, "normalize");
601 Py_DECREF(m);
602 if (!c->c_normalize)
603 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500604 return 1;
605}
606
607static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400608new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500609{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400610 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500611 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000612 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500613 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500614 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000615 /* Check whether there are non-ASCII characters in the
616 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500617 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200618 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300619 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500620 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500621 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200622 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500623 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300624 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
625 if (form == NULL) {
626 Py_DECREF(id);
627 return NULL;
628 }
629 PyObject *args[2] = {form, id};
630 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500631 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200632 if (!id2)
633 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300634 if (!PyUnicode_Check(id2)) {
635 PyErr_Format(PyExc_TypeError,
636 "unicodedata.normalize() must return a string, not "
637 "%.200s",
638 Py_TYPE(id2)->tp_name);
639 Py_DECREF(id2);
640 return NULL;
641 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200642 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000643 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000644 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200645 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
646 Py_DECREF(id);
647 return NULL;
648 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000649 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650}
651
Benjamin Peterson55e00432012-01-16 17:22:31 -0500652#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200655ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400657 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200658 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200660 va_start(va, errmsg);
661 errstr = PyUnicode_FromFormatV(errmsg, va);
662 va_end(va);
663 if (!errstr) {
664 return 0;
665 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200666 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000668 Py_INCREF(Py_None);
669 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 }
Ammar Askar025eb982018-09-24 17:12:49 -0400671 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200672 if (!tmp) {
673 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400674 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000675 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000676 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 Py_DECREF(errstr);
678 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 if (value) {
680 PyErr_SetObject(PyExc_SyntaxError, value);
681 Py_DECREF(value);
682 }
683 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684}
685
686/* num_stmts() returns number of contained statements.
687
688 Use this routine to determine how big a sequence is needed for
689 the statements in a parse tree. Its raison d'etre is this bit of
690 grammar:
691
692 stmt: simple_stmt | compound_stmt
693 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
694
695 A simple_stmt can contain multiple small_stmt elements joined
696 by semicolons. If the arg is a simple_stmt, the number of
697 small_stmt elements is returned.
698*/
699
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800700static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800701new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800702{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800703 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800704 if (res == NULL)
705 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800706 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
707 Py_DECREF(res);
708 return NULL;
709 }
710 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800711}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800712#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800713
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714static int
715num_stmts(const node *n)
716{
717 int i, l;
718 node *ch;
719
720 switch (TYPE(n)) {
721 case single_input:
722 if (TYPE(CHILD(n, 0)) == NEWLINE)
723 return 0;
724 else
725 return num_stmts(CHILD(n, 0));
726 case file_input:
727 l = 0;
728 for (i = 0; i < NCH(n); i++) {
729 ch = CHILD(n, i);
730 if (TYPE(ch) == stmt)
731 l += num_stmts(ch);
732 }
733 return l;
734 case stmt:
735 return num_stmts(CHILD(n, 0));
736 case compound_stmt:
737 return 1;
738 case simple_stmt:
739 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
740 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800741 case func_body_suite:
742 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
743 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 if (NCH(n) == 1)
745 return num_stmts(CHILD(n, 0));
746 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800747 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800749 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
750 i += 2;
751 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 l += num_stmts(CHILD(n, i));
753 return l;
754 }
755 default: {
756 char buf[128];
757
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000758 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 TYPE(n), NCH(n));
760 Py_FatalError(buf);
761 }
762 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700763 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764}
765
766/* Transform the CST rooted at node * to the appropriate AST
767*/
768
769mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200770PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
771 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000773 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800775 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 stmt_ty s;
777 node *ch;
778 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500779 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800780 asdl_seq *argtypes = NULL;
781 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400783 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200784 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400785 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800786 c.c_normalize = NULL;
Guido van Rossum495da292019-03-07 12:38:08 -0800787 c.c_feature_version = flags->cf_feature_version;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800788
789 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Jeremy Hyltona8293132006-02-28 17:58:27 +0000792 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 switch (TYPE(n)) {
794 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200795 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500797 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 for (i = 0; i < NCH(n) - 1; i++) {
799 ch = CHILD(n, i);
800 if (TYPE(ch) == NEWLINE)
801 continue;
802 REQ(ch, stmt);
803 num = num_stmts(ch);
804 if (num == 1) {
805 s = ast_for_stmt(&c, ch);
806 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500807 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000808 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 }
810 else {
811 ch = CHILD(ch, 0);
812 REQ(ch, simple_stmt);
813 for (j = 0; j < num; j++) {
814 s = ast_for_stmt(&c, CHILD(ch, j * 2));
815 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500816 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000817 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 }
819 }
820 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800821
822 /* Type ignores are stored under the ENDMARKER in file_input. */
823 ch = CHILD(n, NCH(n) - 1);
824 REQ(ch, ENDMARKER);
825 num = NCH(ch);
826 type_ignores = _Py_asdl_seq_new(num, arena);
827 if (!type_ignores)
828 goto out;
829
830 for (i = 0; i < num; i++) {
831 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
832 if (!ti)
833 goto out;
834 asdl_seq_SET(type_ignores, i, ti);
835 }
836
837 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 case eval_input: {
840 expr_ty testlist_ast;
841
Nick Coghlan650f0d02007-04-15 12:05:43 +0000842 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000843 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500845 goto out;
846 res = Expression(testlist_ast, arena);
847 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 }
849 case single_input:
850 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200851 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500853 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000855 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000857 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 goto out;
859 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 }
861 else {
862 n = CHILD(n, 0);
863 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200864 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500866 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000868 s = ast_for_stmt(&c, n);
869 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500870 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 asdl_seq_SET(stmts, 0, s);
872 }
873 else {
874 /* Only a simple_stmt can contain multiple statements. */
875 REQ(n, simple_stmt);
876 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 if (TYPE(CHILD(n, i)) == NEWLINE)
878 break;
879 s = ast_for_stmt(&c, CHILD(n, i));
880 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500881 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 asdl_seq_SET(stmts, i / 2, s);
883 }
884 }
885
Benjamin Peterson55e00432012-01-16 17:22:31 -0500886 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500888 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800889 case func_type_input:
890 n = CHILD(n, 0);
891 REQ(n, func_type);
892
893 if (TYPE(CHILD(n, 1)) == typelist) {
894 ch = CHILD(n, 1);
895 /* this is overly permissive -- we don't pay any attention to
896 * stars on the args -- just parse them into an ordered list */
897 num = 0;
898 for (i = 0; i < NCH(ch); i++) {
899 if (TYPE(CHILD(ch, i)) == test) {
900 num++;
901 }
902 }
903
904 argtypes = _Py_asdl_seq_new(num, arena);
905 if (!argtypes)
906 goto out;
907
908 j = 0;
909 for (i = 0; i < NCH(ch); i++) {
910 if (TYPE(CHILD(ch, i)) == test) {
911 arg = ast_for_expr(&c, CHILD(ch, i));
912 if (!arg)
913 goto out;
914 asdl_seq_SET(argtypes, j++, arg);
915 }
916 }
917 }
918 else {
919 argtypes = _Py_asdl_seq_new(0, arena);
920 if (!argtypes)
921 goto out;
922 }
923
924 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
925 if (!ret)
926 goto out;
927 res = FunctionType(argtypes, ret, arena);
928 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000930 PyErr_Format(PyExc_SystemError,
931 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500932 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500934 out:
935 if (c.c_normalize) {
936 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500937 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500938 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939}
940
Victor Stinner14e461d2013-08-26 22:28:21 +0200941mod_ty
942PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
943 PyArena *arena)
944{
945 mod_ty mod;
946 PyObject *filename;
947 filename = PyUnicode_DecodeFSDefault(filename_str);
948 if (filename == NULL)
949 return NULL;
950 mod = PyAST_FromNodeObject(n, flags, filename, arena);
951 Py_DECREF(filename);
952 return mod;
953
954}
955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
957*/
958
959static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800960get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961{
962 switch (TYPE(n)) {
963 case VBAR:
964 return BitOr;
965 case CIRCUMFLEX:
966 return BitXor;
967 case AMPER:
968 return BitAnd;
969 case LEFTSHIFT:
970 return LShift;
971 case RIGHTSHIFT:
972 return RShift;
973 case PLUS:
974 return Add;
975 case MINUS:
976 return Sub;
977 case STAR:
978 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400979 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800980 if (c->c_feature_version < 5) {
981 ast_error(c, n,
982 "The '@' operator is only supported in Python 3.5 and greater");
983 return (operator_ty)0;
984 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400985 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986 case SLASH:
987 return Div;
988 case DOUBLESLASH:
989 return FloorDiv;
990 case PERCENT:
991 return Mod;
992 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000993 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 }
995}
996
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200997static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000998 "None",
999 "True",
1000 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001001 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001002 NULL,
1003};
1004
1005static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001006forbidden_name(struct compiling *c, identifier name, const node *n,
1007 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001008{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001009 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001010 const char * const *p = FORBIDDEN;
1011 if (!full_checks) {
1012 /* In most cases, the parser will protect True, False, and None
1013 from being assign to. */
1014 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001015 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001016 for (; *p; p++) {
1017 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1018 ast_error(c, n, "cannot assign to %U", name);
1019 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001020 }
1021 }
1022 return 0;
1023}
1024
Serhiy Storchakab619b092018-11-27 09:40:29 +02001025static expr_ty
1026copy_location(expr_ty e, const node *n)
1027{
1028 if (e) {
1029 e->lineno = LINENO(n);
1030 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001031 e->end_lineno = n->n_end_lineno;
1032 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001033 }
1034 return e;
1035}
1036
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001037static const char *
1038get_expr_name(expr_ty e)
1039{
1040 switch (e->kind) {
1041 case Attribute_kind:
1042 return "attribute";
1043 case Subscript_kind:
1044 return "subscript";
1045 case Starred_kind:
1046 return "starred";
1047 case Name_kind:
1048 return "name";
1049 case List_kind:
1050 return "list";
1051 case Tuple_kind:
1052 return "tuple";
1053 case Lambda_kind:
1054 return "lambda";
1055 case Call_kind:
1056 return "function call";
1057 case BoolOp_kind:
1058 case BinOp_kind:
1059 case UnaryOp_kind:
1060 return "operator";
1061 case GeneratorExp_kind:
1062 return "generator expression";
1063 case Yield_kind:
1064 case YieldFrom_kind:
1065 return "yield expression";
1066 case Await_kind:
1067 return "await expression";
1068 case ListComp_kind:
1069 return "list comprehension";
1070 case SetComp_kind:
1071 return "set comprehension";
1072 case DictComp_kind:
1073 return "dict comprehension";
1074 case Dict_kind:
1075 return "dict display";
1076 case Set_kind:
1077 return "set display";
1078 case JoinedStr_kind:
1079 case FormattedValue_kind:
1080 return "f-string expression";
1081 case Constant_kind: {
1082 PyObject *value = e->v.Constant.value;
1083 if (value == Py_None) {
1084 return "None";
1085 }
1086 if (value == Py_False) {
1087 return "False";
1088 }
1089 if (value == Py_True) {
1090 return "True";
1091 }
1092 if (value == Py_Ellipsis) {
1093 return "Ellipsis";
1094 }
1095 return "literal";
1096 }
1097 case Compare_kind:
1098 return "comparison";
1099 case IfExp_kind:
1100 return "conditional expression";
1101 case NamedExpr_kind:
1102 return "named expression";
1103 default:
1104 PyErr_Format(PyExc_SystemError,
1105 "unexpected expression in assignment %d (line %d)",
1106 e->kind, e->lineno);
1107 return NULL;
1108 }
1109}
1110
Jeremy Hyltona8293132006-02-28 17:58:27 +00001111/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112
1113 Only sets context for expr kinds that "can appear in assignment context"
1114 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1115 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116*/
1117
1118static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001119set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120{
1121 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001122
1123 /* The ast defines augmented store and load contexts, but the
1124 implementation here doesn't actually use them. The code may be
1125 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001126 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001127 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001128 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001129 */
1130 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131
1132 switch (e->kind) {
1133 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001134 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001135 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001136 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001137 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 e->v.Subscript.ctx = ctx;
1140 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001141 case Starred_kind:
1142 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001143 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001144 return 0;
1145 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001147 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001148 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001149 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001150 }
1151 e->v.Name.ctx = ctx;
1152 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001154 e->v.List.ctx = ctx;
1155 s = e->v.List.elts;
1156 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001158 e->v.Tuple.ctx = ctx;
1159 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001160 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001161 default: {
1162 const char *expr_name = get_expr_name(e);
1163 if (expr_name != NULL) {
1164 ast_error(c, n, "cannot %s %s",
1165 ctx == Store ? "assign to" : "delete",
1166 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001167 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001168 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001169 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001170 }
1171
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 */
1175 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001176 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177
Thomas Wouters89f507f2006-12-13 04:49:30 +00001178 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001179 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001180 return 0;
1181 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 }
1183 return 1;
1184}
1185
1186static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001187ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188{
1189 REQ(n, augassign);
1190 n = CHILD(n, 0);
1191 switch (STR(n)[0]) {
1192 case '+':
1193 return Add;
1194 case '-':
1195 return Sub;
1196 case '/':
1197 if (STR(n)[1] == '/')
1198 return FloorDiv;
1199 else
1200 return Div;
1201 case '%':
1202 return Mod;
1203 case '<':
1204 return LShift;
1205 case '>':
1206 return RShift;
1207 case '&':
1208 return BitAnd;
1209 case '^':
1210 return BitXor;
1211 case '|':
1212 return BitOr;
1213 case '*':
1214 if (STR(n)[1] == '*')
1215 return Pow;
1216 else
1217 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001218 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001219 if (c->c_feature_version < 5) {
1220 ast_error(c, n,
1221 "The '@' operator is only supported in Python 3.5 and greater");
1222 return (operator_ty)0;
1223 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001224 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001226 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001227 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 }
1229}
1230
1231static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001232ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001234 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 |'is' 'not'
1236 */
1237 REQ(n, comp_op);
1238 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001239 n = CHILD(n, 0);
1240 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 case LESS:
1242 return Lt;
1243 case GREATER:
1244 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001245 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 return Eq;
1247 case LESSEQUAL:
1248 return LtE;
1249 case GREATEREQUAL:
1250 return GtE;
1251 case NOTEQUAL:
1252 return NotEq;
1253 case NAME:
1254 if (strcmp(STR(n), "in") == 0)
1255 return In;
1256 if (strcmp(STR(n), "is") == 0)
1257 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001258 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001260 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001262 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001263 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 }
1265 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001266 /* handle "not in" and "is not" */
1267 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 case NAME:
1269 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1270 return NotIn;
1271 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1272 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001273 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001275 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001277 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001278 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 }
Neal Norwitz79792652005-11-14 04:25:03 +00001280 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001282 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283}
1284
1285static asdl_seq *
1286seq_for_testlist(struct compiling *c, const node *n)
1287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001289 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1290 */
Armin Rigo31441302005-10-21 12:57:31 +00001291 asdl_seq *seq;
1292 expr_ty expression;
1293 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001294 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001296 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 if (!seq)
1298 return NULL;
1299
1300 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001302 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303
Benjamin Peterson4905e802009-09-27 02:43:28 +00001304 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001305 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307
1308 assert(i / 2 < seq->size);
1309 asdl_seq_SET(seq, i / 2, expression);
1310 }
1311 return seq;
1312}
1313
Neal Norwitzc1505362006-12-28 06:47:50 +00001314static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001315ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001316{
1317 identifier name;
1318 expr_ty annotation = NULL;
1319 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001320 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001321
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001322 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001323 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001324 name = NEW_IDENTIFIER(ch);
1325 if (!name)
1326 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001327 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001328 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001329
1330 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1331 annotation = ast_for_expr(c, CHILD(n, 2));
1332 if (!annotation)
1333 return NULL;
1334 }
1335
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001336 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001337 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001338 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001339 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001340 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341}
1342
Guido van Rossum4f72a782006-10-27 23:31:49 +00001343/* returns -1 if failed to handle keyword only arguments
1344 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001345 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001346 ^^^
1347 start pointing here
1348 */
1349static int
1350handle_keywordonly_args(struct compiling *c, const node *n, int start,
1351 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1352{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001353 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001354 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001355 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001356 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001357 int i = start;
1358 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001359
1360 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001361 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001362 return -1;
1363 }
1364 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 while (i < NCH(n)) {
1366 ch = CHILD(n, i);
1367 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001368 case vfpdef:
1369 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001371 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001372 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001373 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 asdl_seq_SET(kwdefaults, j, expression);
1375 i += 2; /* '=' and test */
1376 }
1377 else { /* setting NULL if no default value exists */
1378 asdl_seq_SET(kwdefaults, j, NULL);
1379 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001380 if (NCH(ch) == 3) {
1381 /* ch is NAME ':' test */
1382 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001383 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001384 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001385 }
1386 else {
1387 annotation = NULL;
1388 }
1389 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001390 argname = NEW_IDENTIFIER(ch);
1391 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001393 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001394 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001395 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001396 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001397 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001398 if (!arg)
1399 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001400 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001401 i += 1; /* the name */
1402 if (TYPE(CHILD(n, i)) == COMMA)
1403 i += 1; /* the comma, if present */
1404 break;
1405 case TYPE_COMMENT:
1406 /* arg will be equal to the last argument processed */
1407 arg->type_comment = NEW_TYPE_COMMENT(ch);
1408 if (!arg->type_comment)
1409 goto error;
1410 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 break;
1412 case DOUBLESTAR:
1413 return i;
1414 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001415 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 goto error;
1417 }
1418 }
1419 return i;
1420 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001422}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423
Jeremy Hyltona8293132006-02-28 17:58:27 +00001424/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425
1426static arguments_ty
1427ast_for_arguments(struct compiling *c, const node *n)
1428{
Neal Norwitzc1505362006-12-28 06:47:50 +00001429 /* This function handles both typedargslist (function definition)
1430 and varargslist (lambda definition).
1431
1432 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001433 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1434 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1435 | '**' tfpdef [',']]]
1436 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1437 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001438 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001439 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1440 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1441 | '**' vfpdef [',']]]
1442 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1443 | '**' vfpdef [',']
1444 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001445 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1449 int nposdefaults = 0, found_default = 0;
1450 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001451 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001452 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 node *ch;
1454
1455 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001456 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001457 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001458 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001460 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461
Jeremy Hyltone921e022008-07-17 16:37:17 +00001462 /* First count the number of positional args & defaults. The
1463 variable i is the loop index for this for loop and the next.
1464 The next loop picks up where the first leaves off.
1465 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001467 ch = CHILD(n, i);
1468 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001469 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001470 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001471 if (i < NCH(n) && /* skip argument following star */
1472 (TYPE(CHILD(n, i)) == tfpdef ||
1473 TYPE(CHILD(n, i)) == vfpdef)) {
1474 i++;
1475 }
1476 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001477 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001478 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001479 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001480 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001483 defaults for keyword only args */
1484 for ( ; i < NCH(n); ++i) {
1485 ch = CHILD(n, i);
1486 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001487 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001488 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001489 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001490 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001491 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001492 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001493 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001494 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001495 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001497 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001498 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001499 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001501 since we set NULL as default for keyword only argument w/o default
1502 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001503 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001504 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001505 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001506 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001507
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001508 /* tfpdef: NAME [':' test]
1509 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 */
1511 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001512 j = 0; /* index for defaults */
1513 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001515 ch = CHILD(n, i);
1516 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001517 case tfpdef:
1518 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1520 anything other than EQUAL or a comma? */
1521 /* XXX Should NCH(n) check be made a separate check? */
1522 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001523 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1524 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001525 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001526 assert(posdefaults != NULL);
1527 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001529 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001531 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001532 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001533 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001534 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001535 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001536 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001537 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001538 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001539 asdl_seq_SET(posargs, k++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001540 i += 1; /* the name */
1541 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1542 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 break;
1544 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001545 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001546 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1547 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001548 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001549 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001550 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001551 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001552 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001553 if (TYPE(ch) == COMMA) {
1554 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001555 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001556
1557 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1558 ast_error(c, CHILD(n, i),
1559 "bare * has associated type comment");
1560 return NULL;
1561 }
1562
Guido van Rossum4f72a782006-10-27 23:31:49 +00001563 res = handle_keywordonly_args(c, n, i,
1564 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001565 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001566 i = res; /* res has new position to process */
1567 }
1568 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001569 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001570 if (!vararg)
1571 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001572
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001573 i += 2; /* the star and the name */
1574 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1575 i += 1; /* the comma, if present */
1576
1577 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1578 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1579 if (!vararg->type_comment)
1580 return NULL;
1581 i += 1;
1582 }
1583
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001584 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1585 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001586 int res = 0;
1587 res = handle_keywordonly_args(c, n, i,
1588 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001589 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001590 i = res; /* res has new position to process */
1591 }
1592 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 break;
1594 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001595 ch = CHILD(n, i+1); /* tfpdef */
1596 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001597 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001598 if (!kwarg)
1599 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001600 i += 2; /* the double star and the name */
1601 if (TYPE(CHILD(n, i)) == COMMA)
1602 i += 1; /* the comma, if present */
1603 break;
1604 case TYPE_COMMENT:
1605 assert(i);
1606
1607 if (kwarg)
1608 arg = kwarg;
1609
1610 /* arg will be equal to the last argument processed */
1611 arg->type_comment = NEW_TYPE_COMMENT(ch);
1612 if (!arg->type_comment)
1613 return NULL;
1614 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 break;
1616 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001617 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618 "unexpected node in varargslist: %d @ %d",
1619 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001620 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001623 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624}
1625
1626static expr_ty
1627ast_for_dotted_name(struct compiling *c, const node *n)
1628{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001629 expr_ty e;
1630 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001631 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001633 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634
1635 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001636
1637 lineno = LINENO(n);
1638 col_offset = n->n_col_offset;
1639
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001640 ch = CHILD(n, 0);
1641 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001643 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001644 e = Name(id, Load, lineno, col_offset,
1645 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648
1649 for (i = 2; i < NCH(n); i+=2) {
1650 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001651 if (!id)
1652 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001653 e = Attribute(e, id, Load, lineno, col_offset,
1654 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001655 if (!e)
1656 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 }
1658
1659 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660}
1661
1662static expr_ty
1663ast_for_decorator(struct compiling *c, const node *n)
1664{
1665 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1666 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001667 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001670 REQ(CHILD(n, 0), AT);
1671 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1674 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001675 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001678 d = name_expr;
1679 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 }
1681 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001682 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001683 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001684 if (!d)
1685 return NULL;
1686 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 }
1688 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001689 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001690 if (!d)
1691 return NULL;
1692 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 }
1694
1695 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696}
1697
1698static asdl_seq*
1699ast_for_decorators(struct compiling *c, const node *n)
1700{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001701 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001702 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001706 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 if (!decorator_seq)
1708 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001711 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001712 if (!d)
1713 return NULL;
1714 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 }
1716 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717}
1718
1719static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001720ast_for_funcdef_impl(struct compiling *c, const node *n0,
1721 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001723 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001724 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001725 identifier name;
1726 arguments_ty args;
1727 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001728 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001729 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001730 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001731 node *tc;
1732 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733
Guido van Rossum495da292019-03-07 12:38:08 -08001734 if (is_async && c->c_feature_version < 5) {
1735 ast_error(c, n,
1736 "Async functions are only supported in Python 3.5 and greater");
1737 return NULL;
1738 }
1739
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 REQ(n, funcdef);
1741
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 name = NEW_IDENTIFIER(CHILD(n, name_i));
1743 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001744 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001745 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001746 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1748 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001749 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001750 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1751 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1752 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001753 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001754 name_i += 2;
1755 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001756 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1757 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1758 if (!type_comment)
1759 return NULL;
1760 name_i += 1;
1761 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001762 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001764 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001765 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001767 if (NCH(CHILD(n, name_i + 3)) > 1) {
1768 /* Check if the suite has a type comment in it. */
1769 tc = CHILD(CHILD(n, name_i + 3), 1);
1770
1771 if (TYPE(tc) == TYPE_COMMENT) {
1772 if (type_comment != NULL) {
1773 ast_error(c, n, "Cannot have two type comments on def");
1774 return NULL;
1775 }
1776 type_comment = NEW_TYPE_COMMENT(tc);
1777 if (!type_comment)
1778 return NULL;
1779 }
1780 }
1781
Yury Selivanov75445082015-05-11 22:57:16 -04001782 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001783 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001784 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001785 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001786 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001787 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001788}
1789
1790static stmt_ty
1791ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1792{
Guido van Rossum495da292019-03-07 12:38:08 -08001793 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001794 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001795 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001796 REQ(CHILD(n, 1), funcdef);
1797
guoci90fc8982018-09-11 17:45:45 -04001798 return ast_for_funcdef_impl(c, n, decorator_seq,
1799 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001800}
1801
1802static stmt_ty
1803ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1804{
1805 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1806 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001807 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001808}
1809
1810
1811static stmt_ty
1812ast_for_async_stmt(struct compiling *c, const node *n)
1813{
Guido van Rossum495da292019-03-07 12:38:08 -08001814 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001815 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001816 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001817
1818 switch (TYPE(CHILD(n, 1))) {
1819 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001820 return ast_for_funcdef_impl(c, n, NULL,
1821 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001822 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001823 return ast_for_with_stmt(c, n,
1824 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001825
1826 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001827 return ast_for_for_stmt(c, n,
1828 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001829
1830 default:
1831 PyErr_Format(PyExc_SystemError,
1832 "invalid async stament: %s",
1833 STR(CHILD(n, 1)));
1834 return NULL;
1835 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836}
1837
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001838static stmt_ty
1839ast_for_decorated(struct compiling *c, const node *n)
1840{
Yury Selivanov75445082015-05-11 22:57:16 -04001841 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001842 stmt_ty thing = NULL;
1843 asdl_seq *decorator_seq = NULL;
1844
1845 REQ(n, decorated);
1846
1847 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1848 if (!decorator_seq)
1849 return NULL;
1850
1851 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001852 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001854
1855 if (TYPE(CHILD(n, 1)) == funcdef) {
1856 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1857 } else if (TYPE(CHILD(n, 1)) == classdef) {
1858 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001859 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1860 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001861 }
1862 return thing;
1863}
1864
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001866ast_for_namedexpr(struct compiling *c, const node *n)
1867{
1868 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1869 ['else' ':' suite]
1870 namedexpr_test: test [':=' test]
1871 argument: ( test [comp_for] |
1872 test ':=' test |
1873 test '=' test |
1874 '**' test |
1875 '*' test )
1876 */
1877 expr_ty target, value;
1878
1879 target = ast_for_expr(c, CHILD(n, 0));
1880 if (!target)
1881 return NULL;
1882
1883 value = ast_for_expr(c, CHILD(n, 2));
1884 if (!value)
1885 return NULL;
1886
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001887 if (target->kind != Name_kind) {
1888 const char *expr_name = get_expr_name(target);
1889 if (expr_name != NULL) {
1890 ast_error(c, n, "cannot use named assignment with %s", expr_name);
1891 }
1892 return NULL;
1893 }
1894
1895 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001896 return NULL;
1897
1898 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1899 n->n_end_col_offset, c->c_arena);
1900}
1901
1902static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903ast_for_lambdef(struct compiling *c, const node *n)
1904{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001905 /* lambdef: 'lambda' [varargslist] ':' test
1906 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 arguments_ty args;
1908 expr_ty expression;
1909
1910 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001911 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 if (!args)
1913 return NULL;
1914 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001915 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 }
1918 else {
1919 args = ast_for_arguments(c, CHILD(n, 1));
1920 if (!args)
1921 return NULL;
1922 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001923 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 }
1926
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001927 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1928 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929}
1930
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001931static expr_ty
1932ast_for_ifexpr(struct compiling *c, const node *n)
1933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001935 expr_ty expression, body, orelse;
1936
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001937 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001938 body = ast_for_expr(c, CHILD(n, 0));
1939 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001940 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001941 expression = ast_for_expr(c, CHILD(n, 2));
1942 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001943 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001944 orelse = ast_for_expr(c, CHILD(n, 4));
1945 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001946 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001947 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001948 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001949 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001950}
1951
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001953 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954
Nick Coghlan650f0d02007-04-15 12:05:43 +00001955 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956*/
1957
1958static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001959count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001961 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962
Guido van Rossumd8faa362007-04-27 19:54:29 +00001963 count_comp_for:
1964 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001965 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001966 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001967 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001968 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001969 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001970 else if (NCH(n) == 1) {
1971 n = CHILD(n, 0);
1972 }
1973 else {
1974 goto error;
1975 }
1976 if (NCH(n) == (5)) {
1977 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001978 }
1979 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001980 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001981 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001982 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001983 REQ(n, comp_iter);
1984 n = CHILD(n, 0);
1985 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001986 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001987 else if (TYPE(n) == comp_if) {
1988 if (NCH(n) == 3) {
1989 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001990 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001991 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001992 else
1993 return n_fors;
1994 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001995
Jelle Zijlstraac317702017-10-05 20:24:46 -07001996 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001997 /* Should never be reached */
1998 PyErr_SetString(PyExc_SystemError,
1999 "logic error in count_comp_fors");
2000 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001}
2002
Nick Coghlan650f0d02007-04-15 12:05:43 +00002003/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004
Nick Coghlan650f0d02007-04-15 12:05:43 +00002005 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006*/
2007
2008static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002009count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002011 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012
Guido van Rossumd8faa362007-04-27 19:54:29 +00002013 while (1) {
2014 REQ(n, comp_iter);
2015 if (TYPE(CHILD(n, 0)) == comp_for)
2016 return n_ifs;
2017 n = CHILD(n, 0);
2018 REQ(n, comp_if);
2019 n_ifs++;
2020 if (NCH(n) == 2)
2021 return n_ifs;
2022 n = CHILD(n, 2);
2023 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024}
2025
Guido van Rossum992d4a32007-07-11 13:09:30 +00002026static asdl_seq *
2027ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002030 asdl_seq *comps;
2031
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002032 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 if (n_fors == -1)
2034 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002035
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002036 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002037 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002039
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002041 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002043 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002044 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002045 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002046 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047
Guido van Rossum992d4a32007-07-11 13:09:30 +00002048 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049
Jelle Zijlstraac317702017-10-05 20:24:46 -07002050 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002051 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002052 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002053 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002054 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002055 else {
2056 sync_n = CHILD(n, 0);
2057 }
2058 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002059
Guido van Rossum495da292019-03-07 12:38:08 -08002060 /* Async comprehensions only allowed in Python 3.6 and greater */
2061 if (is_async && c->c_feature_version < 6) {
2062 ast_error(c, n,
2063 "Async comprehensions are only supported in Python 3.6 and greater");
2064 return NULL;
2065 }
2066
Jelle Zijlstraac317702017-10-05 20:24:46 -07002067 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002068 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002069 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002071 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002072 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002074
Thomas Wouters89f507f2006-12-13 04:49:30 +00002075 /* Check the # of children rather than the length of t, since
2076 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002077 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002078 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002079 comp = comprehension(first, expression, NULL,
2080 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002082 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2083 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2084 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002085 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002086 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002088
Jelle Zijlstraac317702017-10-05 20:24:46 -07002089 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 int j, n_ifs;
2091 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092
Jelle Zijlstraac317702017-10-05 20:24:46 -07002093 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002094 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002095 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002098 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002103 REQ(n, comp_iter);
2104 n = CHILD(n, 0);
2105 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106
Guido van Rossum992d4a32007-07-11 13:09:30 +00002107 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002108 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002109 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002110 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002111 if (NCH(n) == 3)
2112 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002114 /* on exit, must guarantee that n is a comp_for */
2115 if (TYPE(n) == comp_iter)
2116 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002117 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002119 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002121 return comps;
2122}
2123
2124static expr_ty
2125ast_for_itercomp(struct compiling *c, const node *n, int type)
2126{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002127 /* testlist_comp: (test|star_expr)
2128 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002129 expr_ty elt;
2130 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002131 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132
Guido van Rossum992d4a32007-07-11 13:09:30 +00002133 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002135 ch = CHILD(n, 0);
2136 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002137 if (!elt)
2138 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002139 if (elt->kind == Starred_kind) {
2140 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2141 return NULL;
2142 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143
Guido van Rossum992d4a32007-07-11 13:09:30 +00002144 comps = ast_for_comprehension(c, CHILD(n, 1));
2145 if (!comps)
2146 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002147
2148 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002149 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2150 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002151 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002152 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2153 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002154 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002155 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2156 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002157 else
2158 /* Should never happen */
2159 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160}
2161
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002162/* Fills in the key, value pair corresponding to the dict element. In case
2163 * of an unpacking, key is NULL. *i is advanced by the number of ast
2164 * elements. Iff successful, nonzero is returned.
2165 */
2166static int
2167ast_for_dictelement(struct compiling *c, const node *n, int *i,
2168 expr_ty *key, expr_ty *value)
2169{
2170 expr_ty expression;
2171 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2172 assert(NCH(n) - *i >= 2);
2173
2174 expression = ast_for_expr(c, CHILD(n, *i + 1));
2175 if (!expression)
2176 return 0;
2177 *key = NULL;
2178 *value = expression;
2179
2180 *i += 2;
2181 }
2182 else {
2183 assert(NCH(n) - *i >= 3);
2184
2185 expression = ast_for_expr(c, CHILD(n, *i));
2186 if (!expression)
2187 return 0;
2188 *key = expression;
2189
2190 REQ(CHILD(n, *i + 1), COLON);
2191
2192 expression = ast_for_expr(c, CHILD(n, *i + 2));
2193 if (!expression)
2194 return 0;
2195 *value = expression;
2196
2197 *i += 3;
2198 }
2199 return 1;
2200}
2201
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002203ast_for_dictcomp(struct compiling *c, const node *n)
2204{
2205 expr_ty key, value;
2206 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002207 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002209 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002210 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002211 assert(key);
2212 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002215 if (!comps)
2216 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002218 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2219 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002220}
2221
2222static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002223ast_for_dictdisplay(struct compiling *c, const node *n)
2224{
2225 int i;
2226 int j;
2227 int size;
2228 asdl_seq *keys, *values;
2229
2230 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2231 keys = _Py_asdl_seq_new(size, c->c_arena);
2232 if (!keys)
2233 return NULL;
2234
2235 values = _Py_asdl_seq_new(size, c->c_arena);
2236 if (!values)
2237 return NULL;
2238
2239 j = 0;
2240 for (i = 0; i < NCH(n); i++) {
2241 expr_ty key, value;
2242
2243 if (!ast_for_dictelement(c, n, &i, &key, &value))
2244 return NULL;
2245 asdl_seq_SET(keys, j, key);
2246 asdl_seq_SET(values, j, value);
2247
2248 j++;
2249 }
2250 keys->size = j;
2251 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002252 return Dict(keys, values, LINENO(n), n->n_col_offset,
2253 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002254}
2255
2256static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002257ast_for_genexp(struct compiling *c, const node *n)
2258{
2259 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002260 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002261}
2262
2263static expr_ty
2264ast_for_listcomp(struct compiling *c, const node *n)
2265{
2266 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002267 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002268}
2269
2270static expr_ty
2271ast_for_setcomp(struct compiling *c, const node *n)
2272{
2273 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002274 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002275}
2276
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002277static expr_ty
2278ast_for_setdisplay(struct compiling *c, const node *n)
2279{
2280 int i;
2281 int size;
2282 asdl_seq *elts;
2283
2284 assert(TYPE(n) == (dictorsetmaker));
2285 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2286 elts = _Py_asdl_seq_new(size, c->c_arena);
2287 if (!elts)
2288 return NULL;
2289 for (i = 0; i < NCH(n); i += 2) {
2290 expr_ty expression;
2291 expression = ast_for_expr(c, CHILD(n, i));
2292 if (!expression)
2293 return NULL;
2294 asdl_seq_SET(elts, i / 2, expression);
2295 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002296 return Set(elts, LINENO(n), n->n_col_offset,
2297 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002298}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002299
2300static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301ast_for_atom(struct compiling *c, const node *n)
2302{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002303 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2304 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002305 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 */
2307 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002310 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002311 PyObject *name;
2312 const char *s = STR(ch);
2313 size_t len = strlen(s);
2314 if (len >= 4 && len <= 5) {
2315 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002316 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002317 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002318 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002319 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002320 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002321 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002322 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002323 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002324 }
2325 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002326 if (!name)
2327 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002328 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002329 return Name(name, Load, LINENO(n), n->n_col_offset,
2330 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002331 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002333 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002334 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002335 const char *errtype = NULL;
2336 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2337 errtype = "unicode error";
2338 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2339 errtype = "value error";
2340 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002341 PyObject *type, *value, *tback, *errstr;
2342 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002343 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002344 if (errstr) {
2345 ast_error(c, n, "(%s) %U", errtype, errstr);
2346 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002347 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002348 else {
2349 PyErr_Clear();
2350 ast_error(c, n, "(%s) unknown error", errtype);
2351 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002352 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002353 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002354 Py_XDECREF(tback);
2355 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002356 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002357 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002358 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 }
2360 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002361 PyObject *pynum;
2362 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2363 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2364 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2365 ast_error(c, ch,
2366 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2367 return NULL;
2368 }
2369 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002370 if (!pynum)
2371 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002372
Victor Stinner43d81952013-07-17 00:57:58 +02002373 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2374 Py_DECREF(pynum);
2375 return NULL;
2376 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002377 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002378 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 }
Georg Brandldde00282007-03-18 19:01:53 +00002380 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002381 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002382 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002384 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385
Thomas Wouters89f507f2006-12-13 04:49:30 +00002386 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002387 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2388 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389
Thomas Wouters89f507f2006-12-13 04:49:30 +00002390 if (TYPE(ch) == yield_expr)
2391 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002394 if (NCH(ch) == 1) {
2395 return ast_for_testlist(c, ch);
2396 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002397
Serhiy Storchakab619b092018-11-27 09:40:29 +02002398 if (TYPE(CHILD(ch, 1)) == comp_for) {
2399 return copy_location(ast_for_genexp(c, ch), n);
2400 }
2401 else {
2402 return copy_location(ast_for_testlist(c, ch), n);
2403 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002405 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406
Thomas Wouters89f507f2006-12-13 04:49:30 +00002407 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002408 return List(NULL, Load, LINENO(n), n->n_col_offset,
2409 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410
Nick Coghlan650f0d02007-04-15 12:05:43 +00002411 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002412 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2413 asdl_seq *elts = seq_for_testlist(c, ch);
2414 if (!elts)
2415 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002416
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002417 return List(elts, Load, LINENO(n), n->n_col_offset,
2418 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002419 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002420 else {
2421 return copy_location(ast_for_listcomp(c, ch), n);
2422 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002424 /* dictorsetmaker: ( ((test ':' test | '**' test)
2425 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2426 * ((test | '*' test)
2427 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002428 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002429 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002430 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002431 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002432 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2433 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002434 }
2435 else {
2436 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2437 if (NCH(ch) == 1 ||
2438 (NCH(ch) > 1 &&
2439 TYPE(CHILD(ch, 1)) == COMMA)) {
2440 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002441 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002442 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002443 else if (NCH(ch) > 1 &&
2444 TYPE(CHILD(ch, 1)) == comp_for) {
2445 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002446 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002447 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002448 else if (NCH(ch) > 3 - is_dict &&
2449 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2450 /* It's a dictionary comprehension. */
2451 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002452 ast_error(c, n,
2453 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002454 return NULL;
2455 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002456 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002457 }
2458 else {
2459 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002460 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002461 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002462 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002463 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002466 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2467 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 }
2469}
2470
2471static slice_ty
2472ast_for_slice(struct compiling *c, const node *n)
2473{
2474 node *ch;
2475 expr_ty lower = NULL, upper = NULL, step = NULL;
2476
2477 REQ(n, subscript);
2478
2479 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002480 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 sliceop: ':' [test]
2482 */
2483 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 if (NCH(n) == 1 && TYPE(ch) == test) {
2485 /* 'step' variable hold no significance in terms of being used over
2486 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 if (!step)
2489 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490
Thomas Wouters89f507f2006-12-13 04:49:30 +00002491 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 }
2493
2494 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002495 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 if (!lower)
2497 return NULL;
2498 }
2499
2500 /* If there's an upper bound it's in the second or third position. */
2501 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002502 if (NCH(n) > 1) {
2503 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504
Thomas Wouters89f507f2006-12-13 04:49:30 +00002505 if (TYPE(n2) == test) {
2506 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 if (!upper)
2508 return NULL;
2509 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002512 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513
Thomas Wouters89f507f2006-12-13 04:49:30 +00002514 if (TYPE(n2) == test) {
2515 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 if (!upper)
2517 return NULL;
2518 }
2519 }
2520
2521 ch = CHILD(n, NCH(n) - 1);
2522 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002523 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002524 ch = CHILD(ch, 1);
2525 if (TYPE(ch) == test) {
2526 step = ast_for_expr(c, ch);
2527 if (!step)
2528 return NULL;
2529 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 }
2531 }
2532
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002533 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534}
2535
2536static expr_ty
2537ast_for_binop(struct compiling *c, const node *n)
2538{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002539 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002541 BinOp(BinOp(A, op, B), op, C).
2542 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543
Guido van Rossumd8faa362007-04-27 19:54:29 +00002544 int i, nops;
2545 expr_ty expr1, expr2, result;
2546 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547
Guido van Rossumd8faa362007-04-27 19:54:29 +00002548 expr1 = ast_for_expr(c, CHILD(n, 0));
2549 if (!expr1)
2550 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551
Guido van Rossumd8faa362007-04-27 19:54:29 +00002552 expr2 = ast_for_expr(c, CHILD(n, 2));
2553 if (!expr2)
2554 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555
Guido van Rossum495da292019-03-07 12:38:08 -08002556 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002557 if (!newoperator)
2558 return NULL;
2559
2560 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002561 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002562 c->c_arena);
2563 if (!result)
2564 return NULL;
2565
2566 nops = (NCH(n) - 1) / 2;
2567 for (i = 1; i < nops; i++) {
2568 expr_ty tmp_result, tmp;
2569 const node* next_oper = CHILD(n, i * 2 + 1);
2570
Guido van Rossum495da292019-03-07 12:38:08 -08002571 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002572 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 return NULL;
2574
Guido van Rossumd8faa362007-04-27 19:54:29 +00002575 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2576 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 return NULL;
2578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002580 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002581 CHILD(n, i * 2 + 2)->n_end_lineno,
2582 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002583 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002585 return NULL;
2586 result = tmp_result;
2587 }
2588 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589}
2590
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002591static expr_ty
2592ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002595 subscriptlist: subscript (',' subscript)* [',']
2596 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2597 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002598 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002599 REQ(n, trailer);
2600 if (TYPE(CHILD(n, 0)) == LPAR) {
2601 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002602 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2603 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002604 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002605 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002606 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002607 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002608 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2609 if (!attr_id)
2610 return NULL;
2611 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002612 LINENO(n), n->n_col_offset,
2613 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002614 }
2615 else {
2616 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002617 REQ(CHILD(n, 2), RSQB);
2618 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002619 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002620 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2621 if (!slc)
2622 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002623 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002624 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002625 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002626 }
2627 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002629 by treating the sequence as a tuple literal if there are
2630 no slice features.
2631 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002632 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002633 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002634 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002635 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002636 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002637 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002638 if (!slices)
2639 return NULL;
2640 for (j = 0; j < NCH(n); j += 2) {
2641 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002642 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002643 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002644 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002645 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002646 asdl_seq_SET(slices, j / 2, slc);
2647 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002648 if (!simple) {
2649 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002650 Load, LINENO(n), n->n_col_offset,
2651 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002652 }
2653 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002654 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002655 if (!elts)
2656 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002657 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2658 slc = (slice_ty)asdl_seq_GET(slices, j);
2659 assert(slc->kind == Index_kind && slc->v.Index.value);
2660 asdl_seq_SET(elts, j, slc->v.Index.value);
2661 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002662 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2663 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002664 if (!e)
2665 return NULL;
2666 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002667 Load, LINENO(n), n->n_col_offset,
2668 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002669 }
2670 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002671}
2672
2673static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002674ast_for_factor(struct compiling *c, const node *n)
2675{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002676 expr_ty expression;
2677
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002678 expression = ast_for_expr(c, CHILD(n, 1));
2679 if (!expression)
2680 return NULL;
2681
2682 switch (TYPE(CHILD(n, 0))) {
2683 case PLUS:
2684 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002685 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002686 c->c_arena);
2687 case MINUS:
2688 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002689 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002690 c->c_arena);
2691 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002692 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2693 n->n_end_lineno, n->n_end_col_offset,
2694 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002695 }
2696 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2697 TYPE(CHILD(n, 0)));
2698 return NULL;
2699}
2700
2701static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002702ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002703{
Yury Selivanov75445082015-05-11 22:57:16 -04002704 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002705 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002706
2707 REQ(n, atom_expr);
2708 nch = NCH(n);
2709
Guido van Rossum495da292019-03-07 12:38:08 -08002710 if (TYPE(CHILD(n, 0)) == AWAIT) {
2711 if (c->c_feature_version < 5) {
2712 ast_error(c, n,
2713 "Await expressions are only supported in Python 3.5 and greater");
2714 return NULL;
2715 }
Yury Selivanov75445082015-05-11 22:57:16 -04002716 start = 1;
2717 assert(nch > 1);
2718 }
2719
2720 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002721 if (!e)
2722 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002723 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002724 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002725 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002726 return Await(e, LINENO(n), n->n_col_offset,
2727 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002728 }
2729
2730 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002731 node *ch = CHILD(n, i);
2732 if (TYPE(ch) != trailer)
2733 break;
2734 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002735 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002736 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002737 tmp->lineno = e->lineno;
2738 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002739 e = tmp;
2740 }
Yury Selivanov75445082015-05-11 22:57:16 -04002741
2742 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002743 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002744 return Await(e, LINENO(n), n->n_col_offset,
2745 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002746 }
2747 else {
2748 return e;
2749 }
2750}
2751
2752static expr_ty
2753ast_for_power(struct compiling *c, const node *n)
2754{
2755 /* power: atom trailer* ('**' factor)*
2756 */
2757 expr_ty e;
2758 REQ(n, power);
2759 e = ast_for_atom_expr(c, CHILD(n, 0));
2760 if (!e)
2761 return NULL;
2762 if (NCH(n) == 1)
2763 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002764 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2765 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002766 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002767 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002768 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2769 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002770 }
2771 return e;
2772}
2773
Guido van Rossum0368b722007-05-11 16:50:42 +00002774static expr_ty
2775ast_for_starred(struct compiling *c, const node *n)
2776{
2777 expr_ty tmp;
2778 REQ(n, star_expr);
2779
2780 tmp = ast_for_expr(c, CHILD(n, 1));
2781 if (!tmp)
2782 return NULL;
2783
2784 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002785 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2786 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002787}
2788
2789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790/* Do not name a variable 'expr'! Will cause a compile error.
2791*/
2792
2793static expr_ty
2794ast_for_expr(struct compiling *c, const node *n)
2795{
2796 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002797 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002798 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002799 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 and_test: not_test ('and' not_test)*
2802 not_test: 'not' not_test | comparison
2803 comparison: expr (comp_op expr)*
2804 expr: xor_expr ('|' xor_expr)*
2805 xor_expr: and_expr ('^' and_expr)*
2806 and_expr: shift_expr ('&' shift_expr)*
2807 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2808 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002809 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002811 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002812 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002813 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 */
2815
2816 asdl_seq *seq;
2817 int i;
2818
2819 loop:
2820 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002821 case namedexpr_test:
2822 if (NCH(n) == 3)
2823 return ast_for_namedexpr(c, n);
2824 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002826 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002827 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002828 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002830 else if (NCH(n) > 1)
2831 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002832 /* Fallthrough */
2833 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 case and_test:
2835 if (NCH(n) == 1) {
2836 n = CHILD(n, 0);
2837 goto loop;
2838 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002839 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 if (!seq)
2841 return NULL;
2842 for (i = 0; i < NCH(n); i += 2) {
2843 expr_ty e = ast_for_expr(c, CHILD(n, i));
2844 if (!e)
2845 return NULL;
2846 asdl_seq_SET(seq, i / 2, e);
2847 }
2848 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002849 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002850 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002851 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002852 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002853 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2854 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 case not_test:
2856 if (NCH(n) == 1) {
2857 n = CHILD(n, 0);
2858 goto loop;
2859 }
2860 else {
2861 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2862 if (!expression)
2863 return NULL;
2864
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002865 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002866 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002867 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 }
2869 case comparison:
2870 if (NCH(n) == 1) {
2871 n = CHILD(n, 0);
2872 goto loop;
2873 }
2874 else {
2875 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002876 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002877 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002878 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 if (!ops)
2880 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002881 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 return NULL;
2884 }
2885 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002886 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002888 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002889 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002891 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892
2893 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002894 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002896 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002898 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 asdl_seq_SET(cmps, i / 2, expression);
2900 }
2901 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002902 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002906 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2907 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 }
2909 break;
2910
Guido van Rossum0368b722007-05-11 16:50:42 +00002911 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 /* The next five cases all handle BinOps. The main body of code
2914 is the same in each case, but the switch turned inside out to
2915 reuse the code for each type of operator.
2916 */
2917 case expr:
2918 case xor_expr:
2919 case and_expr:
2920 case shift_expr:
2921 case arith_expr:
2922 case term:
2923 if (NCH(n) == 1) {
2924 n = CHILD(n, 0);
2925 goto loop;
2926 }
2927 return ast_for_binop(c, n);
2928 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002929 node *an = NULL;
2930 node *en = NULL;
2931 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002932 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002933 if (NCH(n) > 1)
2934 an = CHILD(n, 1); /* yield_arg */
2935 if (an) {
2936 en = CHILD(an, NCH(an) - 1);
2937 if (NCH(an) == 2) {
2938 is_from = 1;
2939 exp = ast_for_expr(c, en);
2940 }
2941 else
2942 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002943 if (!exp)
2944 return NULL;
2945 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002946 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002947 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2948 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2949 return Yield(exp, LINENO(n), n->n_col_offset,
2950 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002951 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002952 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 if (NCH(n) == 1) {
2954 n = CHILD(n, 0);
2955 goto loop;
2956 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002957 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002958 case power:
2959 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002961 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 return NULL;
2963 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002964 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 return NULL;
2966}
2967
2968static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002969ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002970 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971{
2972 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002973 arglist: argument (',' argument)* [',']
2974 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 */
2976
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002977 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002978 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002979 asdl_seq *args;
2980 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981
2982 REQ(n, arglist);
2983
2984 nargs = 0;
2985 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002987 node *ch = CHILD(n, i);
2988 if (TYPE(ch) == argument) {
2989 if (NCH(ch) == 1)
2990 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002991 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2992 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002993 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002994 ast_error(c, ch, "invalid syntax");
2995 return NULL;
2996 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002997 if (NCH(n) > 1) {
2998 ast_error(c, ch, "Generator expression must be parenthesized");
2999 return NULL;
3000 }
3001 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003002 else if (TYPE(CHILD(ch, 0)) == STAR)
3003 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003004 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3005 nargs++;
3006 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003008 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003009 nkeywords++;
3010 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003011 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003013 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003015 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003016 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003018 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003019
3020 nargs = 0; /* positional arguments + iterable argument unpackings */
3021 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3022 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003024 node *ch = CHILD(n, i);
3025 if (TYPE(ch) == argument) {
3026 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003027 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003029 /* a positional argument */
3030 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003031 if (ndoublestars) {
3032 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003033 "positional argument follows "
3034 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003035 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003036 else {
3037 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003038 "positional argument follows "
3039 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003040 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003041 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003042 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003043 e = ast_for_expr(c, chch);
3044 if (!e)
3045 return NULL;
3046 asdl_seq_SET(args, nargs++, e);
3047 }
3048 else if (TYPE(chch) == STAR) {
3049 /* an iterable argument unpacking */
3050 expr_ty starred;
3051 if (ndoublestars) {
3052 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003053 "iterable argument unpacking follows "
3054 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003055 return NULL;
3056 }
3057 e = ast_for_expr(c, CHILD(ch, 1));
3058 if (!e)
3059 return NULL;
3060 starred = Starred(e, Load, LINENO(chch),
3061 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003062 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003063 c->c_arena);
3064 if (!starred)
3065 return NULL;
3066 asdl_seq_SET(args, nargs++, starred);
3067
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003068 }
3069 else if (TYPE(chch) == DOUBLESTAR) {
3070 /* a keyword argument unpacking */
3071 keyword_ty kw;
3072 i++;
3073 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003075 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003076 kw = keyword(NULL, e, c->c_arena);
3077 asdl_seq_SET(keywords, nkeywords++, kw);
3078 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003080 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003081 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003082 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003084 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003085 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003087 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3088 /* treat colon equal as positional argument */
3089 if (nkeywords) {
3090 if (ndoublestars) {
3091 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003092 "positional argument follows "
3093 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003094 }
3095 else {
3096 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003097 "positional argument follows "
3098 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003099 }
3100 return NULL;
3101 }
3102 e = ast_for_namedexpr(c, ch);
3103 if (!e)
3104 return NULL;
3105 asdl_seq_SET(args, nargs++, e);
3106 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003107 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003108 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003109 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003110 identifier key, tmp;
3111 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003113 // To remain LL(1), the grammar accepts any test (basically, any
3114 // expression) in the keyword slot of a call site. So, we need
3115 // to manually enforce that the keyword is a NAME here.
3116 static const int name_tree[] = {
3117 test,
3118 or_test,
3119 and_test,
3120 not_test,
3121 comparison,
3122 expr,
3123 xor_expr,
3124 and_expr,
3125 shift_expr,
3126 arith_expr,
3127 term,
3128 factor,
3129 power,
3130 atom_expr,
3131 atom,
3132 0,
3133 };
3134 node *expr_node = chch;
3135 for (int i = 0; name_tree[i]; i++) {
3136 if (TYPE(expr_node) != name_tree[i])
3137 break;
3138 if (NCH(expr_node) != 1)
3139 break;
3140 expr_node = CHILD(expr_node, 0);
3141 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003142 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003143 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003144 "expression cannot contain assignment, "
3145 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003146 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003147 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003148 key = new_identifier(STR(expr_node), c);
3149 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003150 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003152 if (forbidden_name(c, key, chch, 1)) {
3153 return NULL;
3154 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003155 for (k = 0; k < nkeywords; k++) {
3156 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003157 if (tmp && !PyUnicode_Compare(tmp, key)) {
3158 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003159 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003160 return NULL;
3161 }
3162 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003163 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003165 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003166 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003168 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003169 asdl_seq_SET(keywords, nkeywords++, kw);
3170 }
3171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 }
3173
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003174 return Call(func, args, keywords, func->lineno, func->col_offset,
3175 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176}
3177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003179ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003181 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003182 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003184 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003185 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003186 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003187 }
3188 else {
3189 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003190 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003191 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003193 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194 else {
3195 asdl_seq *tmp = seq_for_testlist(c, n);
3196 if (!tmp)
3197 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003198 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3199 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003201}
3202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203static stmt_ty
3204ast_for_expr_stmt(struct compiling *c, const node *n)
3205{
3206 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003207 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003208 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3209 annassign: ':' test ['=' (yield_expr|testlist)]
3210 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3211 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3212 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003213 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003215 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003217 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003218 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 if (!e)
3220 return NULL;
3221
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003222 return Expr(e, LINENO(n), n->n_col_offset,
3223 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 }
3225 else if (TYPE(CHILD(n, 1)) == augassign) {
3226 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003227 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003228 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229
Thomas Wouters89f507f2006-12-13 04:49:30 +00003230 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 if (!expr1)
3232 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003233 if(!set_context(c, expr1, Store, ch))
3234 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003235 /* set_context checks that most expressions are not the left side.
3236 Augmented assignments can only have a name, a subscript, or an
3237 attribute on the left, though, so we have to explicitly check for
3238 those. */
3239 switch (expr1->kind) {
3240 case Name_kind:
3241 case Attribute_kind:
3242 case Subscript_kind:
3243 break;
3244 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003245 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003246 return NULL;
3247 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248
Thomas Wouters89f507f2006-12-13 04:49:30 +00003249 ch = CHILD(n, 2);
3250 if (TYPE(ch) == testlist)
3251 expr2 = ast_for_testlist(c, ch);
3252 else
3253 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003254 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255 return NULL;
3256
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003257 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003258 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 return NULL;
3260
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003261 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3262 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003264 else if (TYPE(CHILD(n, 1)) == annassign) {
3265 expr_ty expr1, expr2, expr3;
3266 node *ch = CHILD(n, 0);
3267 node *deep, *ann = CHILD(n, 1);
3268 int simple = 1;
3269
Guido van Rossum495da292019-03-07 12:38:08 -08003270 /* AnnAssigns are only allowed in Python 3.6 or greater */
3271 if (c->c_feature_version < 6) {
3272 ast_error(c, ch,
3273 "Variable annotation syntax is only supported in Python 3.6 and greater");
3274 return NULL;
3275 }
3276
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003277 /* we keep track of parens to qualify (x) as expression not name */
3278 deep = ch;
3279 while (NCH(deep) == 1) {
3280 deep = CHILD(deep, 0);
3281 }
3282 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3283 simple = 0;
3284 }
3285 expr1 = ast_for_testlist(c, ch);
3286 if (!expr1) {
3287 return NULL;
3288 }
3289 switch (expr1->kind) {
3290 case Name_kind:
3291 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3292 return NULL;
3293 }
3294 expr1->v.Name.ctx = Store;
3295 break;
3296 case Attribute_kind:
3297 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3298 return NULL;
3299 }
3300 expr1->v.Attribute.ctx = Store;
3301 break;
3302 case Subscript_kind:
3303 expr1->v.Subscript.ctx = Store;
3304 break;
3305 case List_kind:
3306 ast_error(c, ch,
3307 "only single target (not list) can be annotated");
3308 return NULL;
3309 case Tuple_kind:
3310 ast_error(c, ch,
3311 "only single target (not tuple) can be annotated");
3312 return NULL;
3313 default:
3314 ast_error(c, ch,
3315 "illegal target for annotation");
3316 return NULL;
3317 }
3318
3319 if (expr1->kind != Name_kind) {
3320 simple = 0;
3321 }
3322 ch = CHILD(ann, 1);
3323 expr2 = ast_for_expr(c, ch);
3324 if (!expr2) {
3325 return NULL;
3326 }
3327 if (NCH(ann) == 2) {
3328 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003329 LINENO(n), n->n_col_offset,
3330 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003331 }
3332 else {
3333 ch = CHILD(ann, 3);
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003334 if (TYPE(ch) == testlist) {
3335 expr3 = ast_for_testlist(c, ch);
3336 }
3337 else {
3338 expr3 = ast_for_expr(c, ch);
3339 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003340 if (!expr3) {
3341 return NULL;
3342 }
3343 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003344 LINENO(n), n->n_col_offset,
3345 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003346 }
3347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003349 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003350 asdl_seq *targets;
3351 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003353 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 /* a normal assignment */
3356 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003357
3358 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3359 nch_minus_type = num - has_type_comment;
3360
3361 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 if (!targets)
3363 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003364 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003365 expr_ty e;
3366 node *ch = CHILD(n, i);
3367 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003368 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003369 return NULL;
3370 }
3371 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003375 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003376 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003377 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378
Thomas Wouters89f507f2006-12-13 04:49:30 +00003379 asdl_seq_SET(targets, i / 2, e);
3380 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003381 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003382 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003383 expression = ast_for_testlist(c, value);
3384 else
3385 expression = ast_for_expr(c, value);
3386 if (!expression)
3387 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003388 if (has_type_comment) {
3389 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3390 if (!type_comment)
3391 return NULL;
3392 }
3393 else
3394 type_comment = NULL;
3395 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003396 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398}
3399
Benjamin Peterson78565b22009-06-28 19:19:51 +00003400
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003402ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403{
3404 asdl_seq *seq;
3405 int i;
3406 expr_ty e;
3407
3408 REQ(n, exprlist);
3409
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003410 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003412 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003414 e = ast_for_expr(c, CHILD(n, i));
3415 if (!e)
3416 return NULL;
3417 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003418 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003419 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 }
3421 return seq;
3422}
3423
3424static stmt_ty
3425ast_for_del_stmt(struct compiling *c, const node *n)
3426{
3427 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003429 /* del_stmt: 'del' exprlist */
3430 REQ(n, del_stmt);
3431
3432 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3433 if (!expr_list)
3434 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003435 return Delete(expr_list, LINENO(n), n->n_col_offset,
3436 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437}
3438
3439static stmt_ty
3440ast_for_flow_stmt(struct compiling *c, const node *n)
3441{
3442 /*
3443 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3444 | yield_stmt
3445 break_stmt: 'break'
3446 continue_stmt: 'continue'
3447 return_stmt: 'return' [testlist]
3448 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003449 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 raise_stmt: 'raise' [test [',' test [',' test]]]
3451 */
3452 node *ch;
3453
3454 REQ(n, flow_stmt);
3455 ch = CHILD(n, 0);
3456 switch (TYPE(ch)) {
3457 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003458 return Break(LINENO(n), n->n_col_offset,
3459 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003461 return Continue(LINENO(n), n->n_col_offset,
3462 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003464 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3465 if (!exp)
3466 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003467 return Expr(exp, LINENO(n), n->n_col_offset,
3468 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 }
3470 case return_stmt:
3471 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003472 return Return(NULL, LINENO(n), n->n_col_offset,
3473 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003475 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 if (!expression)
3477 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003478 return Return(expression, LINENO(n), n->n_col_offset,
3479 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 }
3481 case raise_stmt:
3482 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003483 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3484 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003485 else if (NCH(ch) >= 2) {
3486 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3488 if (!expression)
3489 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003490 if (NCH(ch) == 4) {
3491 cause = ast_for_expr(c, CHILD(ch, 3));
3492 if (!cause)
3493 return NULL;
3494 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003495 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3496 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 }
Stefan Krahf432a322017-08-21 13:09:59 +02003498 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003500 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 "unexpected flow_stmt: %d", TYPE(ch));
3502 return NULL;
3503 }
3504}
3505
3506static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003507alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508{
3509 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003510 import_as_name: NAME ['as' NAME]
3511 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512 dotted_name: NAME ('.' NAME)*
3513 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003514 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003515
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 loop:
3517 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003518 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003519 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003520 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003521 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003522 if (!name)
3523 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003524 if (NCH(n) == 3) {
3525 node *str_node = CHILD(n, 2);
3526 str = NEW_IDENTIFIER(str_node);
3527 if (!str)
3528 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003529 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003530 return NULL;
3531 }
3532 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003533 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003534 return NULL;
3535 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003536 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003537 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 case dotted_as_name:
3539 if (NCH(n) == 1) {
3540 n = CHILD(n, 0);
3541 goto loop;
3542 }
3543 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003544 node *asname_node = CHILD(n, 2);
3545 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003546 if (!a)
3547 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003549 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003550 if (!a->asname)
3551 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003552 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003553 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554 return a;
3555 }
3556 break;
3557 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003558 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003559 node *name_node = CHILD(n, 0);
3560 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003561 if (!name)
3562 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003563 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003564 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003565 return alias(name, NULL, c->c_arena);
3566 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 else {
3568 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003569 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003570 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573
3574 len = 0;
3575 for (i = 0; i < NCH(n); i += 2)
3576 /* length of string plus one for the dot */
3577 len += strlen(STR(CHILD(n, i))) + 1;
3578 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003579 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 if (!str)
3581 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003582 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583 if (!s)
3584 return NULL;
3585 for (i = 0; i < NCH(n); i += 2) {
3586 char *sch = STR(CHILD(n, i));
3587 strcpy(s, STR(CHILD(n, i)));
3588 s += strlen(sch);
3589 *s++ = '.';
3590 }
3591 --s;
3592 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3594 PyBytes_GET_SIZE(str),
3595 NULL);
3596 Py_DECREF(str);
3597 if (!uni)
3598 return NULL;
3599 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003600 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003601 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3602 Py_DECREF(str);
3603 return NULL;
3604 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003605 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 }
3607 break;
3608 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003609 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003610 if (!str)
3611 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003612 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3613 Py_DECREF(str);
3614 return NULL;
3615 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003616 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003618 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 "unexpected import name: %d", TYPE(n));
3620 return NULL;
3621 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003622
3623 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 return NULL;
3625}
3626
3627static stmt_ty
3628ast_for_import_stmt(struct compiling *c, const node *n)
3629{
3630 /*
3631 import_stmt: import_name | import_from
3632 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003633 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3634 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003636 int lineno;
3637 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 int i;
3639 asdl_seq *aliases;
3640
3641 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003642 lineno = LINENO(n);
3643 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003645 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003647 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003648 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003649 if (!aliases)
3650 return NULL;
3651 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003652 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003653 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003655 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003657 // Even though n is modified above, the end position is not changed
3658 return Import(aliases, lineno, col_offset,
3659 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003661 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003663 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003664 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003665 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003666 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003668 /* Count the number of dots (for relative imports) and check for the
3669 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003670 for (idx = 1; idx < NCH(n); idx++) {
3671 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003672 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3673 if (!mod)
3674 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003675 idx++;
3676 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003677 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003679 ndots += 3;
3680 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003681 } else if (TYPE(CHILD(n, idx)) != DOT) {
3682 break;
3683 }
3684 ndots++;
3685 }
3686 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003687 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003688 case STAR:
3689 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003690 n = CHILD(n, idx);
3691 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003692 break;
3693 case LPAR:
3694 /* from ... import (x, y, z) */
3695 n = CHILD(n, idx + 1);
3696 n_children = NCH(n);
3697 break;
3698 case import_as_names:
3699 /* from ... import x, y, z */
3700 n = CHILD(n, idx);
3701 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003702 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003703 ast_error(c, n,
3704 "trailing comma not allowed without"
3705 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 return NULL;
3707 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003708 break;
3709 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003710 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003711 return NULL;
3712 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003714 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003715 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717
3718 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003719 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003720 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003721 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003723 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003725 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003726 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003727 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003728 if (!import_alias)
3729 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003730 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003731 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003733 if (mod != NULL)
3734 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003735 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003736 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003737 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 }
Neal Norwitz79792652005-11-14 04:25:03 +00003739 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740 "unknown import statement: starts with command '%s'",
3741 STR(CHILD(n, 0)));
3742 return NULL;
3743}
3744
3745static stmt_ty
3746ast_for_global_stmt(struct compiling *c, const node *n)
3747{
3748 /* global_stmt: 'global' NAME (',' NAME)* */
3749 identifier name;
3750 asdl_seq *s;
3751 int i;
3752
3753 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003754 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003756 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003758 name = NEW_IDENTIFIER(CHILD(n, i));
3759 if (!name)
3760 return NULL;
3761 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003763 return Global(s, LINENO(n), n->n_col_offset,
3764 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765}
3766
3767static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003768ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3769{
3770 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3771 identifier name;
3772 asdl_seq *s;
3773 int i;
3774
3775 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003776 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003777 if (!s)
3778 return NULL;
3779 for (i = 1; i < NCH(n); i += 2) {
3780 name = NEW_IDENTIFIER(CHILD(n, i));
3781 if (!name)
3782 return NULL;
3783 asdl_seq_SET(s, i / 2, name);
3784 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003785 return Nonlocal(s, LINENO(n), n->n_col_offset,
3786 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003787}
3788
3789static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790ast_for_assert_stmt(struct compiling *c, const node *n)
3791{
3792 /* assert_stmt: 'assert' test [',' test] */
3793 REQ(n, assert_stmt);
3794 if (NCH(n) == 2) {
3795 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3796 if (!expression)
3797 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003798 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3799 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 }
3801 else if (NCH(n) == 4) {
3802 expr_ty expr1, expr2;
3803
3804 expr1 = ast_for_expr(c, CHILD(n, 1));
3805 if (!expr1)
3806 return NULL;
3807 expr2 = ast_for_expr(c, CHILD(n, 3));
3808 if (!expr2)
3809 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003811 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3812 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813 }
Neal Norwitz79792652005-11-14 04:25:03 +00003814 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 "improper number of parts to 'assert' statement: %d",
3816 NCH(n));
3817 return NULL;
3818}
3819
3820static asdl_seq *
3821ast_for_suite(struct compiling *c, const node *n)
3822{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003823 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003824 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 stmt_ty s;
3826 int i, total, num, end, pos = 0;
3827 node *ch;
3828
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003829 if (TYPE(n) != func_body_suite) {
3830 REQ(n, suite);
3831 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832
3833 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003834 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003838 n = CHILD(n, 0);
3839 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003841 */
3842 end = NCH(n) - 1;
3843 if (TYPE(CHILD(n, end - 1)) == SEMI)
3844 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003846 for (i = 0; i < end; i += 2) {
3847 ch = CHILD(n, i);
3848 s = ast_for_stmt(c, ch);
3849 if (!s)
3850 return NULL;
3851 asdl_seq_SET(seq, pos++, s);
3852 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 }
3854 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003855 i = 2;
3856 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3857 i += 2;
3858 REQ(CHILD(n, 2), NEWLINE);
3859 }
3860
3861 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003862 ch = CHILD(n, i);
3863 REQ(ch, stmt);
3864 num = num_stmts(ch);
3865 if (num == 1) {
3866 /* small_stmt or compound_stmt with only one child */
3867 s = ast_for_stmt(c, ch);
3868 if (!s)
3869 return NULL;
3870 asdl_seq_SET(seq, pos++, s);
3871 }
3872 else {
3873 int j;
3874 ch = CHILD(ch, 0);
3875 REQ(ch, simple_stmt);
3876 for (j = 0; j < NCH(ch); j += 2) {
3877 /* statement terminates with a semi-colon ';' */
3878 if (NCH(CHILD(ch, j)) == 0) {
3879 assert((j + 1) == NCH(ch));
3880 break;
3881 }
3882 s = ast_for_stmt(c, CHILD(ch, j));
3883 if (!s)
3884 return NULL;
3885 asdl_seq_SET(seq, pos++, s);
3886 }
3887 }
3888 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 }
3890 assert(pos == seq->size);
3891 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003892}
3893
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003894static void
3895get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3896{
Pablo Galindo46a97922019-02-19 22:51:53 +00003897 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003898 // There must be no empty suites.
3899 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003900 stmt_ty last = asdl_seq_GET(s, tot - 1);
3901 *end_lineno = last->end_lineno;
3902 *end_col_offset = last->end_col_offset;
3903}
3904
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905static stmt_ty
3906ast_for_if_stmt(struct compiling *c, const node *n)
3907{
3908 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3909 ['else' ':' suite]
3910 */
3911 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003912 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913
3914 REQ(n, if_stmt);
3915
3916 if (NCH(n) == 4) {
3917 expr_ty expression;
3918 asdl_seq *suite_seq;
3919
3920 expression = ast_for_expr(c, CHILD(n, 1));
3921 if (!expression)
3922 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003924 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003926 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927
Guido van Rossumd8faa362007-04-27 19:54:29 +00003928 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003929 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 s = STR(CHILD(n, 4));
3933 /* s[2], the third character in the string, will be
3934 's' for el_s_e, or
3935 'i' for el_i_f
3936 */
3937 if (s[2] == 's') {
3938 expr_ty expression;
3939 asdl_seq *seq1, *seq2;
3940
3941 expression = ast_for_expr(c, CHILD(n, 1));
3942 if (!expression)
3943 return NULL;
3944 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003945 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946 return NULL;
3947 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003948 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003949 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003950 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951
Guido van Rossumd8faa362007-04-27 19:54:29 +00003952 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003953 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 }
3955 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003956 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003957 expr_ty expression;
3958 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003959 asdl_seq *orelse = NULL;
3960 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961 /* must reference the child n_elif+1 since 'else' token is third,
3962 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003963 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3964 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3965 has_else = 1;
3966 n_elif -= 3;
3967 }
3968 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969
Thomas Wouters89f507f2006-12-13 04:49:30 +00003970 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003971 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003973 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003974 if (!orelse)
3975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003977 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003979 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3980 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003982 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3983 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003985 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 asdl_seq_SET(orelse, 0,
3988 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003989 LINENO(CHILD(n, NCH(n) - 6)),
3990 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003991 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003992 /* the just-created orelse handled the last elif */
3993 n_elif--;
3994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995
Thomas Wouters89f507f2006-12-13 04:49:30 +00003996 for (i = 0; i < n_elif; i++) {
3997 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003998 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003999 if (!newobj)
4000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004002 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004005 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004008 if (orelse != NULL) {
4009 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4010 } else {
4011 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4012 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004013 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004015 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004016 CHILD(n, off)->n_col_offset,
4017 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004018 orelse = newobj;
4019 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004020 expression = ast_for_expr(c, CHILD(n, 1));
4021 if (!expression)
4022 return NULL;
4023 suite_seq = ast_for_suite(c, CHILD(n, 3));
4024 if (!suite_seq)
4025 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004026 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004027 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004028 LINENO(n), n->n_col_offset,
4029 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004031
4032 PyErr_Format(PyExc_SystemError,
4033 "unexpected token in 'if' statement: %s", s);
4034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035}
4036
4037static stmt_ty
4038ast_for_while_stmt(struct compiling *c, const node *n)
4039{
4040 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4041 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004042 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043
4044 if (NCH(n) == 4) {
4045 expr_ty expression;
4046 asdl_seq *suite_seq;
4047
4048 expression = ast_for_expr(c, CHILD(n, 1));
4049 if (!expression)
4050 return NULL;
4051 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004052 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004054 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4055 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4056 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057 }
4058 else if (NCH(n) == 7) {
4059 expr_ty expression;
4060 asdl_seq *seq1, *seq2;
4061
4062 expression = ast_for_expr(c, CHILD(n, 1));
4063 if (!expression)
4064 return NULL;
4065 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004066 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 return NULL;
4068 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004069 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004071 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004073 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4074 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004076
4077 PyErr_Format(PyExc_SystemError,
4078 "wrong number of tokens for 'while' statement: %d",
4079 NCH(n));
4080 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004081}
4082
4083static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004084ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004085{
guoci90fc8982018-09-11 17:45:45 -04004086 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004087 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004088 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004089 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004090 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004091 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004092 int has_type_comment;
4093 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004094
4095 if (is_async && c->c_feature_version < 5) {
4096 ast_error(c, n,
4097 "Async for loops are only supported in Python 3.5 and greater");
4098 return NULL;
4099 }
4100
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004101 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004102 REQ(n, for_stmt);
4103
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004104 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4105
4106 if (NCH(n) == 9 + has_type_comment) {
4107 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108 if (!seq)
4109 return NULL;
4110 }
4111
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004112 node_target = CHILD(n, 1);
4113 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004114 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004116 /* Check the # of children rather than the length of _target, since
4117 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004118 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004119 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004120 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004121 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004122 target = Tuple(_target, Store, first->lineno, first->col_offset,
4123 node_target->n_end_lineno, node_target->n_end_col_offset,
4124 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004125
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004126 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004127 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004129 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004130 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 return NULL;
4132
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004133 if (seq != NULL) {
4134 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4135 } else {
4136 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4137 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004138
4139 if (has_type_comment) {
4140 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4141 if (!type_comment)
4142 return NULL;
4143 }
4144 else
4145 type_comment = NULL;
4146
Yury Selivanov75445082015-05-11 22:57:16 -04004147 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004148 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004149 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004150 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004151 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004152 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004153 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004154 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155}
4156
4157static excepthandler_ty
4158ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4159{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004160 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004161 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004162 REQ(exc, except_clause);
4163 REQ(body, suite);
4164
4165 if (NCH(exc) == 1) {
4166 asdl_seq *suite_seq = ast_for_suite(c, body);
4167 if (!suite_seq)
4168 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004169 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004170
Neal Norwitzad74aa82008-03-31 05:14:30 +00004171 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004172 exc->n_col_offset,
4173 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004174 }
4175 else if (NCH(exc) == 2) {
4176 expr_ty expression;
4177 asdl_seq *suite_seq;
4178
4179 expression = ast_for_expr(c, CHILD(exc, 1));
4180 if (!expression)
4181 return NULL;
4182 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004183 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004184 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004185 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004186
Neal Norwitzad74aa82008-03-31 05:14:30 +00004187 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004188 exc->n_col_offset,
4189 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004190 }
4191 else if (NCH(exc) == 4) {
4192 asdl_seq *suite_seq;
4193 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004194 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004195 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004196 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004197 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004199 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004200 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004201 return NULL;
4202 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004203 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004204 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004205 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004206
Neal Norwitzad74aa82008-03-31 05:14:30 +00004207 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004208 exc->n_col_offset,
4209 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004210 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004211
4212 PyErr_Format(PyExc_SystemError,
4213 "wrong number of children for 'except' clause: %d",
4214 NCH(exc));
4215 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004216}
4217
4218static stmt_ty
4219ast_for_try_stmt(struct compiling *c, const node *n)
4220{
Neal Norwitzf599f422005-12-17 21:33:47 +00004221 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004222 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004223 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004224 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004225
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004226 REQ(n, try_stmt);
4227
Neal Norwitzf599f422005-12-17 21:33:47 +00004228 body = ast_for_suite(c, CHILD(n, 2));
4229 if (body == NULL)
4230 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004231
Neal Norwitzf599f422005-12-17 21:33:47 +00004232 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4233 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4234 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4235 /* we can assume it's an "else",
4236 because nch >= 9 for try-else-finally and
4237 it would otherwise have a type of except_clause */
4238 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4239 if (orelse == NULL)
4240 return NULL;
4241 n_except--;
4242 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004243
Neal Norwitzf599f422005-12-17 21:33:47 +00004244 finally = ast_for_suite(c, CHILD(n, nch - 1));
4245 if (finally == NULL)
4246 return NULL;
4247 n_except--;
4248 }
4249 else {
4250 /* we can assume it's an "else",
4251 otherwise it would have a type of except_clause */
4252 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4253 if (orelse == NULL)
4254 return NULL;
4255 n_except--;
4256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004257 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004258 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004259 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004260 return NULL;
4261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262
Neal Norwitzf599f422005-12-17 21:33:47 +00004263 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004264 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004265 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004266 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004267 if (handlers == NULL)
4268 return NULL;
4269
4270 for (i = 0; i < n_except; i++) {
4271 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4272 CHILD(n, 5 + i * 3));
4273 if (!e)
4274 return NULL;
4275 asdl_seq_SET(handlers, i, e);
4276 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004277 }
4278
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004279 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004280 if (finally != NULL) {
4281 // finally is always last
4282 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4283 } else if (orelse != NULL) {
4284 // otherwise else is last
4285 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4286 } else {
4287 // inline the get_last_end_pos logic due to layout mismatch
4288 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4289 end_lineno = last_handler->end_lineno;
4290 end_col_offset = last_handler->end_col_offset;
4291 }
4292 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4293 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004294}
4295
Georg Brandl0c315622009-05-25 21:10:36 +00004296/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004297static withitem_ty
4298ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004299{
4300 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004301
Georg Brandl0c315622009-05-25 21:10:36 +00004302 REQ(n, with_item);
4303 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004304 if (!context_expr)
4305 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004306 if (NCH(n) == 3) {
4307 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004308
4309 if (!optional_vars) {
4310 return NULL;
4311 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004312 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004313 return NULL;
4314 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004315 }
4316
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004317 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004318}
4319
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004320/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004321static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004322ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004323{
guoci90fc8982018-09-11 17:45:45 -04004324 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004325 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004326 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004327 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004328
Guido van Rossum495da292019-03-07 12:38:08 -08004329 if (is_async && c->c_feature_version < 5) {
4330 ast_error(c, n,
4331 "Async with statements are only supported in Python 3.5 and greater");
4332 return NULL;
4333 }
4334
Georg Brandl0c315622009-05-25 21:10:36 +00004335 REQ(n, with_stmt);
4336
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004337 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4338 nch_minus_type = NCH(n) - has_type_comment;
4339
4340 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004341 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004342 if (!items)
4343 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004344 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004345 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4346 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004347 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004348 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004349 }
4350
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004351 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4352 if (!body)
4353 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004354 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004355
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004356 if (has_type_comment) {
4357 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4358 if (!type_comment)
4359 return NULL;
4360 }
4361 else
4362 type_comment = NULL;
4363
Yury Selivanov75445082015-05-11 22:57:16 -04004364 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004365 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004366 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004367 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004368 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004369 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004370}
4371
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004372static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004373ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004374{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004375 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004376 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004377 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004378 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004379 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004380
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004381 REQ(n, classdef);
4382
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004383 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004384 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004385 if (!s)
4386 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004387 get_last_end_pos(s, &end_lineno, &end_col_offset);
4388
Benjamin Peterson30760062008-11-25 04:02:28 +00004389 classname = NEW_IDENTIFIER(CHILD(n, 1));
4390 if (!classname)
4391 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004392 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004393 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004394 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004395 LINENO(n), n->n_col_offset,
4396 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004397 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004398
4399 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004400 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004401 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004402 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004403 get_last_end_pos(s, &end_lineno, &end_col_offset);
4404
Benjamin Peterson30760062008-11-25 04:02:28 +00004405 classname = NEW_IDENTIFIER(CHILD(n, 1));
4406 if (!classname)
4407 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004408 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004409 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004410 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004411 LINENO(n), n->n_col_offset,
4412 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004413 }
4414
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004415 /* class NAME '(' arglist ')' ':' suite */
4416 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004417 {
4418 PyObject *dummy_name;
4419 expr_ty dummy;
4420 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4421 if (!dummy_name)
4422 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004423 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4424 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4425 c->c_arena);
4426 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004427 if (!call)
4428 return NULL;
4429 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004430 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004431 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004432 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004433 get_last_end_pos(s, &end_lineno, &end_col_offset);
4434
Benjamin Peterson30760062008-11-25 04:02:28 +00004435 classname = NEW_IDENTIFIER(CHILD(n, 1));
4436 if (!classname)
4437 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004438 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004439 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004440
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004441 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004442 decorator_seq, LINENO(n), n->n_col_offset,
4443 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004444}
4445
4446static stmt_ty
4447ast_for_stmt(struct compiling *c, const node *n)
4448{
4449 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004450 assert(NCH(n) == 1);
4451 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004452 }
4453 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004454 assert(num_stmts(n) == 1);
4455 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004456 }
4457 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004458 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004459 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4460 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004461 */
4462 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004463 case expr_stmt:
4464 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004465 case del_stmt:
4466 return ast_for_del_stmt(c, n);
4467 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004468 return Pass(LINENO(n), n->n_col_offset,
4469 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004470 case flow_stmt:
4471 return ast_for_flow_stmt(c, n);
4472 case import_stmt:
4473 return ast_for_import_stmt(c, n);
4474 case global_stmt:
4475 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004476 case nonlocal_stmt:
4477 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004478 case assert_stmt:
4479 return ast_for_assert_stmt(c, n);
4480 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004481 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004482 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4483 TYPE(n), NCH(n));
4484 return NULL;
4485 }
4486 }
4487 else {
4488 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004489 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004490 */
4491 node *ch = CHILD(n, 0);
4492 REQ(n, compound_stmt);
4493 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004494 case if_stmt:
4495 return ast_for_if_stmt(c, ch);
4496 case while_stmt:
4497 return ast_for_while_stmt(c, ch);
4498 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004499 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004500 case try_stmt:
4501 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004502 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004503 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004504 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004505 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004506 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004507 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 case decorated:
4509 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004510 case async_stmt:
4511 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004512 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004513 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004514 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004515 TYPE(n), NCH(n));
4516 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004518 }
4519}
4520
4521static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004522parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004523{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004524 const char *end;
4525 long x;
4526 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004527 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004528 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004529
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004530 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004531 errno = 0;
4532 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004533 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004534 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004535 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004536 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004537 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004538 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004539 }
4540 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004541 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004542 if (*end == '\0') {
4543 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004544 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004545 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004546 }
4547 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004548 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004549 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004550 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4551 if (compl.imag == -1.0 && PyErr_Occurred())
4552 return NULL;
4553 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004554 }
4555 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004556 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004557 dx = PyOS_string_to_double(s, NULL, NULL);
4558 if (dx == -1.0 && PyErr_Occurred())
4559 return NULL;
4560 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004561 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004562}
4563
4564static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004565parsenumber(struct compiling *c, const char *s)
4566{
4567 char *dup, *end;
4568 PyObject *res = NULL;
4569
4570 assert(s != NULL);
4571
4572 if (strchr(s, '_') == NULL) {
4573 return parsenumber_raw(c, s);
4574 }
4575 /* Create a duplicate without underscores. */
4576 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004577 if (dup == NULL) {
4578 return PyErr_NoMemory();
4579 }
Brett Cannona721aba2016-09-09 14:57:09 -07004580 end = dup;
4581 for (; *s; s++) {
4582 if (*s != '_') {
4583 *end++ = *s;
4584 }
4585 }
4586 *end = '\0';
4587 res = parsenumber_raw(c, dup);
4588 PyMem_Free(dup);
4589 return res;
4590}
4591
4592static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004593decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004594{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004595 const char *s, *t;
4596 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004597 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4598 while (s < end && (*s & 0x80)) s++;
4599 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004600 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004601}
4602
Eric V. Smith56466482016-10-31 14:46:26 -04004603static int
4604warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004605 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004606{
4607 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4608 first_invalid_escape_char);
4609 if (msg == NULL) {
4610 return -1;
4611 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004612 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004613 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004614 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004615 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004616 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004617 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004618 to get a more accurate error report */
4619 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004620 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004621 }
4622 Py_DECREF(msg);
4623 return -1;
4624 }
4625 Py_DECREF(msg);
4626 return 0;
4627}
4628
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004629static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004630decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4631 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004632{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004633 PyObject *v, *u;
4634 char *buf;
4635 char *p;
4636 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004637
Benjamin Peterson202803a2016-02-25 22:34:45 -08004638 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004639 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004640 return NULL;
4641 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4642 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4643 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4644 if (u == NULL)
4645 return NULL;
4646 p = buf = PyBytes_AsString(u);
4647 end = s + len;
4648 while (s < end) {
4649 if (*s == '\\') {
4650 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004651 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004652 strcpy(p, "u005c");
4653 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004654 if (s >= end)
4655 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004656 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004657 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004658 if (*s & 0x80) { /* XXX inefficient */
4659 PyObject *w;
4660 int kind;
4661 void *data;
4662 Py_ssize_t len, i;
4663 w = decode_utf8(c, &s, end);
4664 if (w == NULL) {
4665 Py_DECREF(u);
4666 return NULL;
4667 }
4668 kind = PyUnicode_KIND(w);
4669 data = PyUnicode_DATA(w);
4670 len = PyUnicode_GET_LENGTH(w);
4671 for (i = 0; i < len; i++) {
4672 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4673 sprintf(p, "\\U%08x", chr);
4674 p += 10;
4675 }
4676 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004677 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004678 Py_DECREF(w);
4679 } else {
4680 *p++ = *s++;
4681 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004682 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004683 len = p - buf;
4684 s = buf;
4685
Eric V. Smith56466482016-10-31 14:46:26 -04004686 const char *first_invalid_escape;
4687 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4688
4689 if (v != NULL && first_invalid_escape != NULL) {
4690 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4691 /* We have not decref u before because first_invalid_escape points
4692 inside u. */
4693 Py_XDECREF(u);
4694 Py_DECREF(v);
4695 return NULL;
4696 }
4697 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004698 Py_XDECREF(u);
4699 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004700}
4701
Eric V. Smith56466482016-10-31 14:46:26 -04004702static PyObject *
4703decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4704 size_t len)
4705{
4706 const char *first_invalid_escape;
4707 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4708 &first_invalid_escape);
4709 if (result == NULL)
4710 return NULL;
4711
4712 if (first_invalid_escape != NULL) {
4713 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4714 Py_DECREF(result);
4715 return NULL;
4716 }
4717 }
4718 return result;
4719}
4720
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004721/* Shift locations for the given node and all its children by adding `lineno`
4722 and `col_offset` to existing locations. */
4723static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4724{
4725 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004726 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004727 for (int i = 0; i < NCH(n); ++i) {
4728 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4729 /* Shifting column offsets unnecessary if there's been newlines. */
4730 col_offset = 0;
4731 }
4732 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4733 }
4734 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004735 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004736}
4737
4738/* Fix locations for the given node and its children.
4739
4740 `parent` is the enclosing node.
4741 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004742 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004743*/
4744static void
4745fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4746{
4747 char *substr = NULL;
4748 char *start;
4749 int lines = LINENO(parent) - 1;
4750 int cols = parent->n_col_offset;
4751 /* Find the full fstring to fix location information in `n`. */
4752 while (parent && parent->n_type != STRING)
4753 parent = parent->n_child;
4754 if (parent && parent->n_str) {
4755 substr = strstr(parent->n_str, expr_str);
4756 if (substr) {
4757 start = substr;
4758 while (start > parent->n_str) {
4759 if (start[0] == '\n')
4760 break;
4761 start--;
4762 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004763 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004764 /* adjust the start based on the number of newlines encountered
4765 before the f-string expression */
4766 for (char* p = parent->n_str; p < substr; p++) {
4767 if (*p == '\n') {
4768 lines++;
4769 }
4770 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004771 }
4772 }
4773 fstring_shift_node_locations(n, lines, cols);
4774}
4775
Eric V. Smith451d0e32016-09-09 21:56:20 -04004776/* Compile this expression in to an expr_ty. Add parens around the
4777 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004778static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004779fstring_compile_expr(const char *expr_start, const char *expr_end,
4780 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004781
Eric V. Smith235a6f02015-09-19 14:51:32 -04004782{
4783 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004784 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004785 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004786 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004787 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004788 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004789
Eric V. Smith1d44c412015-09-23 07:49:00 -04004790 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004791 assert(*(expr_start-1) == '{');
4792 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004793
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004794 /* If the substring is all whitespace, it's an error. We need to catch this
4795 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4796 because turning the expression '' in to '()' would go from being invalid
4797 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004798 for (s = expr_start; s != expr_end; s++) {
4799 char c = *s;
4800 /* The Python parser ignores only the following whitespace
4801 characters (\r already is converted to \n). */
4802 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004803 break;
4804 }
4805 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004806 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004807 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004808 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004809 }
4810
Eric V. Smith451d0e32016-09-09 21:56:20 -04004811 len = expr_end - expr_start;
4812 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4813 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004814 if (str == NULL) {
4815 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004816 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004817 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004818
Eric V. Smith451d0e32016-09-09 21:56:20 -04004819 str[0] = '(';
4820 memcpy(str+1, expr_start, len);
4821 str[len+1] = ')';
4822 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004823
4824 cf.cf_flags = PyCF_ONLY_AST;
Guido van Rossum495da292019-03-07 12:38:08 -08004825 cf.cf_feature_version = PY_MINOR_VERSION;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004826 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4827 Py_eval_input, 0);
4828 if (!mod_n) {
4829 PyMem_RawFree(str);
4830 return NULL;
4831 }
4832 /* Reuse str to find the correct column offset. */
4833 str[0] = '{';
4834 str[len+1] = '}';
4835 fstring_fix_node_location(n, mod_n, str);
4836 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004837 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004838 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004839 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004840 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004841 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004842}
4843
4844/* Return -1 on error.
4845
4846 Return 0 if we reached the end of the literal.
4847
4848 Return 1 if we haven't reached the end of the literal, but we want
4849 the caller to process the literal up to this point. Used for
4850 doubled braces.
4851*/
4852static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004853fstring_find_literal(const char **str, const char *end, int raw,
4854 PyObject **literal, int recurse_lvl,
4855 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004856{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004857 /* Get any literal string. It ends when we hit an un-doubled left
4858 brace (which isn't part of a unicode name escape such as
4859 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004860
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004861 const char *s = *str;
4862 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004863 int result = 0;
4864
Eric V. Smith235a6f02015-09-19 14:51:32 -04004865 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004866 while (s < end) {
4867 char ch = *s++;
4868 if (!raw && ch == '\\' && s < end) {
4869 ch = *s++;
4870 if (ch == 'N') {
4871 if (s < end && *s++ == '{') {
4872 while (s < end && *s++ != '}') {
4873 }
4874 continue;
4875 }
4876 break;
4877 }
4878 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4879 return -1;
4880 }
4881 }
4882 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004883 /* Check for doubled braces, but only at the top level. If
4884 we checked at every level, then f'{0:{3}}' would fail
4885 with the two closing braces. */
4886 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004887 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888 /* We're going to tell the caller that the literal ends
4889 here, but that they should continue scanning. But also
4890 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004891 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004892 result = 1;
4893 goto done;
4894 }
4895
4896 /* Where a single '{' is the start of a new expression, a
4897 single '}' is not allowed. */
4898 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004899 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900 ast_error(c, n, "f-string: single '}' is not allowed");
4901 return -1;
4902 }
4903 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004904 /* We're either at a '{', which means we're starting another
4905 expression; or a '}', which means we're at the end of this
4906 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004907 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004908 break;
4909 }
4910 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004911 *str = s;
4912 assert(s <= end);
4913 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004914done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004915 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004916 if (raw)
4917 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004918 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004919 NULL, NULL);
4920 else
Eric V. Smith56466482016-10-31 14:46:26 -04004921 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004922 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004923 if (!*literal)
4924 return -1;
4925 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004926 return result;
4927}
4928
4929/* Forward declaration because parsing is recursive. */
4930static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004931fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004932 struct compiling *c, const node *n);
4933
Eric V. Smith451d0e32016-09-09 21:56:20 -04004934/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004935 expression (so it must be a '{'). Returns the FormattedValue node,
4936 which includes the expression, conversion character, and
4937 format_spec expression.
4938
4939 Note that I don't do a perfect job here: I don't make sure that a
4940 closing brace doesn't match an opening paren, for example. It
4941 doesn't need to error on all invalid expressions, just correctly
4942 find the end of all valid ones. Any errors inside the expression
4943 will be caught when we parse it later. */
4944static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004945fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004946 expr_ty *expression, struct compiling *c, const node *n)
4947{
4948 /* Return -1 on error, else 0. */
4949
Eric V. Smith451d0e32016-09-09 21:56:20 -04004950 const char *expr_start;
4951 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004952 expr_ty simple_expression;
4953 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004954 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004955
4956 /* 0 if we're not in a string, else the quote char we're trying to
4957 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004958 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004959
4960 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4961 int string_type = 0;
4962
4963 /* Keep track of nesting level for braces/parens/brackets in
4964 expressions. */
4965 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004966 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004967
4968 /* Can only nest one level deep. */
4969 if (recurse_lvl >= 2) {
4970 ast_error(c, n, "f-string: expressions nested too deeply");
4971 return -1;
4972 }
4973
4974 /* The first char must be a left brace, or we wouldn't have gotten
4975 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004976 assert(**str == '{');
4977 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004978
Eric V. Smith451d0e32016-09-09 21:56:20 -04004979 expr_start = *str;
4980 for (; *str < end; (*str)++) {
4981 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004982
4983 /* Loop invariants. */
4984 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004985 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004986 if (quote_char)
4987 assert(string_type == 1 || string_type == 3);
4988 else
4989 assert(string_type == 0);
4990
Eric V. Smith451d0e32016-09-09 21:56:20 -04004991 ch = **str;
4992 /* Nowhere inside an expression is a backslash allowed. */
4993 if (ch == '\\') {
4994 /* Error: can't include a backslash character, inside
4995 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004996 ast_error(c, n,
4997 "f-string expression part "
4998 "cannot include a backslash");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004999 return -1;
5000 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005001 if (quote_char) {
5002 /* We're inside a string. See if we're at the end. */
5003 /* This code needs to implement the same non-error logic
5004 as tok_get from tokenizer.c, at the letter_quote
5005 label. To actually share that code would be a
5006 nightmare. But, it's unlikely to change and is small,
5007 so duplicate it here. Note we don't need to catch all
5008 of the errors, since they'll be caught when parsing the
5009 expression. We just need to match the non-error
5010 cases. Thus we can ignore \n in single-quoted strings,
5011 for example. Or non-terminated strings. */
5012 if (ch == quote_char) {
5013 /* Does this match the string_type (single or triple
5014 quoted)? */
5015 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005016 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005017 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005018 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005019 string_type = 0;
5020 quote_char = 0;
5021 continue;
5022 }
5023 } else {
5024 /* We're at the end of a normal string. */
5025 quote_char = 0;
5026 string_type = 0;
5027 continue;
5028 }
5029 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005030 } else if (ch == '\'' || ch == '"') {
5031 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005032 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005033 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005034 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005035 } else {
5036 /* Start of a normal string. */
5037 string_type = 1;
5038 }
5039 /* Start looking for the end of the string. */
5040 quote_char = ch;
5041 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005042 if (nested_depth >= MAXLEVEL) {
5043 ast_error(c, n, "f-string: too many nested parenthesis");
5044 return -1;
5045 }
5046 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005047 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005048 } else if (ch == '#') {
5049 /* Error: can't include a comment character, inside parens
5050 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005051 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005052 return -1;
5053 } else if (nested_depth == 0 &&
5054 (ch == '!' || ch == ':' || ch == '}')) {
5055 /* First, test for the special case of "!=". Since '=' is
5056 not an allowed conversion character, nothing is lost in
5057 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005058 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005059 /* This isn't a conversion character, just continue. */
5060 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005061 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005062 /* Normal way out of this loop. */
5063 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005064 } else if (ch == ']' || ch == '}' || ch == ')') {
5065 if (!nested_depth) {
5066 ast_error(c, n, "f-string: unmatched '%c'", ch);
5067 return -1;
5068 }
5069 nested_depth--;
5070 int opening = parenstack[nested_depth];
5071 if (!((opening == '(' && ch == ')') ||
5072 (opening == '[' && ch == ']') ||
5073 (opening == '{' && ch == '}')))
5074 {
5075 ast_error(c, n,
5076 "f-string: closing parenthesis '%c' "
5077 "does not match opening parenthesis '%c'",
5078 ch, opening);
5079 return -1;
5080 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005081 } else {
5082 /* Just consume this char and loop around. */
5083 }
5084 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005085 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005086 /* If we leave this loop in a string or with mismatched parens, we
5087 don't care. We'll get a syntax error when compiling the
5088 expression. But, we can produce a better error message, so
5089 let's just do that.*/
5090 if (quote_char) {
5091 ast_error(c, n, "f-string: unterminated string");
5092 return -1;
5093 }
5094 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005095 int opening = parenstack[nested_depth - 1];
5096 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005097 return -1;
5098 }
5099
Eric V. Smith451d0e32016-09-09 21:56:20 -04005100 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005101 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005102
5103 /* Compile the expression as soon as possible, so we show errors
5104 related to the expression before errors related to the
5105 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005106 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005107 if (!simple_expression)
5108 return -1;
5109
5110 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005111 if (**str == '!') {
5112 *str += 1;
5113 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005114 goto unexpected_end_of_string;
5115
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 conversion = **str;
5117 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118
5119 /* Validate the conversion. */
5120 if (!(conversion == 's' || conversion == 'r'
5121 || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005122 ast_error(c, n,
5123 "f-string: invalid conversion character: "
5124 "expected 's', 'r', or 'a'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005125 return -1;
5126 }
5127 }
5128
5129 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005130 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005131 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005132 if (**str == ':') {
5133 *str += 1;
5134 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005135 goto unexpected_end_of_string;
5136
5137 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005138 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005139 if (!format_spec)
5140 return -1;
5141 }
5142
Eric V. Smith451d0e32016-09-09 21:56:20 -04005143 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005144 goto unexpected_end_of_string;
5145
5146 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005147 assert(*str < end);
5148 assert(**str == '}');
5149 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005150
Eric V. Smith451d0e32016-09-09 21:56:20 -04005151 /* And now create the FormattedValue node that represents this
5152 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005153 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005154 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005155 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005156 c->c_arena);
5157 if (!*expression)
5158 return -1;
5159
5160 return 0;
5161
5162unexpected_end_of_string:
5163 ast_error(c, n, "f-string: expecting '}'");
5164 return -1;
5165}
5166
5167/* Return -1 on error.
5168
5169 Return 0 if we have a literal (possible zero length) and an
5170 expression (zero length if at the end of the string.
5171
5172 Return 1 if we have a literal, but no expression, and we want the
5173 caller to call us again. This is used to deal with doubled
5174 braces.
5175
5176 When called multiple times on the string 'a{{b{0}c', this function
5177 will return:
5178
5179 1. the literal 'a{' with no expression, and a return value
5180 of 1. Despite the fact that there's no expression, the return
5181 value of 1 means we're not finished yet.
5182
5183 2. the literal 'b' and the expression '0', with a return value of
5184 0. The fact that there's an expression means we're not finished.
5185
5186 3. literal 'c' with no expression and a return value of 0. The
5187 combination of the return value of 0 with no expression means
5188 we're finished.
5189*/
5190static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005191fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5192 int recurse_lvl, PyObject **literal,
5193 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005194 struct compiling *c, const node *n)
5195{
5196 int result;
5197
5198 assert(*literal == NULL && *expression == NULL);
5199
5200 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005201 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005202 if (result < 0)
5203 goto error;
5204
5205 assert(result == 0 || result == 1);
5206
5207 if (result == 1)
5208 /* We have a literal, but don't look at the expression. */
5209 return 1;
5210
Eric V. Smith451d0e32016-09-09 21:56:20 -04005211 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212 /* We're at the end of the string or the end of a nested
5213 f-string: no expression. The top-level error case where we
5214 expect to be at the end of the string but we're at a '}' is
5215 handled later. */
5216 return 0;
5217
5218 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005219 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005220
Eric V. Smith451d0e32016-09-09 21:56:20 -04005221 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005222 goto error;
5223
5224 return 0;
5225
5226error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005227 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005228 return -1;
5229}
5230
5231#define EXPRLIST_N_CACHED 64
5232
5233typedef struct {
5234 /* Incrementally build an array of expr_ty, so be used in an
5235 asdl_seq. Cache some small but reasonably sized number of
5236 expr_ty's, and then after that start dynamically allocating,
5237 doubling the number allocated each time. Note that the f-string
5238 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005239 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005240 fast as you add exressions in an f-string. */
5241
5242 Py_ssize_t allocated; /* Number we've allocated. */
5243 Py_ssize_t size; /* Number we've used. */
5244 expr_ty *p; /* Pointer to the memory we're actually
5245 using. Will point to 'data' until we
5246 start dynamically allocating. */
5247 expr_ty data[EXPRLIST_N_CACHED];
5248} ExprList;
5249
5250#ifdef NDEBUG
5251#define ExprList_check_invariants(l)
5252#else
5253static void
5254ExprList_check_invariants(ExprList *l)
5255{
5256 /* Check our invariants. Make sure this object is "live", and
5257 hasn't been deallocated. */
5258 assert(l->size >= 0);
5259 assert(l->p != NULL);
5260 if (l->size <= EXPRLIST_N_CACHED)
5261 assert(l->data == l->p);
5262}
5263#endif
5264
5265static void
5266ExprList_Init(ExprList *l)
5267{
5268 l->allocated = EXPRLIST_N_CACHED;
5269 l->size = 0;
5270
5271 /* Until we start allocating dynamically, p points to data. */
5272 l->p = l->data;
5273
5274 ExprList_check_invariants(l);
5275}
5276
5277static int
5278ExprList_Append(ExprList *l, expr_ty exp)
5279{
5280 ExprList_check_invariants(l);
5281 if (l->size >= l->allocated) {
5282 /* We need to alloc (or realloc) the memory. */
5283 Py_ssize_t new_size = l->allocated * 2;
5284
5285 /* See if we've ever allocated anything dynamically. */
5286 if (l->p == l->data) {
5287 Py_ssize_t i;
5288 /* We're still using the cached data. Switch to
5289 alloc-ing. */
5290 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5291 if (!l->p)
5292 return -1;
5293 /* Copy the cached data into the new buffer. */
5294 for (i = 0; i < l->size; i++)
5295 l->p[i] = l->data[i];
5296 } else {
5297 /* Just realloc. */
5298 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5299 if (!tmp) {
5300 PyMem_RawFree(l->p);
5301 l->p = NULL;
5302 return -1;
5303 }
5304 l->p = tmp;
5305 }
5306
5307 l->allocated = new_size;
5308 assert(l->allocated == 2 * l->size);
5309 }
5310
5311 l->p[l->size++] = exp;
5312
5313 ExprList_check_invariants(l);
5314 return 0;
5315}
5316
5317static void
5318ExprList_Dealloc(ExprList *l)
5319{
5320 ExprList_check_invariants(l);
5321
5322 /* If there's been an error, or we've never dynamically allocated,
5323 do nothing. */
5324 if (!l->p || l->p == l->data) {
5325 /* Do nothing. */
5326 } else {
5327 /* We have dynamically allocated. Free the memory. */
5328 PyMem_RawFree(l->p);
5329 }
5330 l->p = NULL;
5331 l->size = -1;
5332}
5333
5334static asdl_seq *
5335ExprList_Finish(ExprList *l, PyArena *arena)
5336{
5337 asdl_seq *seq;
5338
5339 ExprList_check_invariants(l);
5340
5341 /* Allocate the asdl_seq and copy the expressions in to it. */
5342 seq = _Py_asdl_seq_new(l->size, arena);
5343 if (seq) {
5344 Py_ssize_t i;
5345 for (i = 0; i < l->size; i++)
5346 asdl_seq_SET(seq, i, l->p[i]);
5347 }
5348 ExprList_Dealloc(l);
5349 return seq;
5350}
5351
5352/* The FstringParser is designed to add a mix of strings and
5353 f-strings, and concat them together as needed. Ultimately, it
5354 generates an expr_ty. */
5355typedef struct {
5356 PyObject *last_str;
5357 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005358 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005359} FstringParser;
5360
5361#ifdef NDEBUG
5362#define FstringParser_check_invariants(state)
5363#else
5364static void
5365FstringParser_check_invariants(FstringParser *state)
5366{
5367 if (state->last_str)
5368 assert(PyUnicode_CheckExact(state->last_str));
5369 ExprList_check_invariants(&state->expr_list);
5370}
5371#endif
5372
5373static void
5374FstringParser_Init(FstringParser *state)
5375{
5376 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005377 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005378 ExprList_Init(&state->expr_list);
5379 FstringParser_check_invariants(state);
5380}
5381
5382static void
5383FstringParser_Dealloc(FstringParser *state)
5384{
5385 FstringParser_check_invariants(state);
5386
5387 Py_XDECREF(state->last_str);
5388 ExprList_Dealloc(&state->expr_list);
5389}
5390
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005391/* Constants for the following */
5392static PyObject *u_kind;
5393
5394/* Compute 'kind' field for string Constant (either 'u' or None) */
5395static PyObject *
5396make_kind(struct compiling *c, const node *n)
5397{
5398 char *s = NULL;
5399 PyObject *kind = NULL;
5400
5401 /* Find the first string literal, if any */
5402 while (TYPE(n) != STRING) {
5403 if (NCH(n) == 0)
5404 return NULL;
5405 n = CHILD(n, 0);
5406 }
5407 REQ(n, STRING);
5408
5409 /* If it starts with 'u', return a PyUnicode "u" string */
5410 s = STR(n);
5411 if (s && *s == 'u') {
5412 if (!u_kind) {
5413 u_kind = PyUnicode_InternFromString("u");
5414 if (!u_kind)
5415 return NULL;
5416 }
5417 kind = u_kind;
5418 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5419 return NULL;
5420 }
5421 Py_INCREF(kind);
5422 }
5423 return kind;
5424}
5425
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005426/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005427static expr_ty
5428make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5429{
5430 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005431 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005432 *str = NULL;
5433 assert(PyUnicode_CheckExact(s));
5434 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5435 Py_DECREF(s);
5436 return NULL;
5437 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005438 kind = make_kind(c, n);
5439 if (kind == NULL && PyErr_Occurred())
5440 return NULL;
5441 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005442 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005443}
5444
5445/* Add a non-f-string (that is, a regular literal string). str is
5446 decref'd. */
5447static int
5448FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5449{
5450 FstringParser_check_invariants(state);
5451
5452 assert(PyUnicode_CheckExact(str));
5453
5454 if (PyUnicode_GET_LENGTH(str) == 0) {
5455 Py_DECREF(str);
5456 return 0;
5457 }
5458
5459 if (!state->last_str) {
5460 /* We didn't have a string before, so just remember this one. */
5461 state->last_str = str;
5462 } else {
5463 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005464 PyUnicode_AppendAndDel(&state->last_str, str);
5465 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005466 return -1;
5467 }
5468 FstringParser_check_invariants(state);
5469 return 0;
5470}
5471
Eric V. Smith451d0e32016-09-09 21:56:20 -04005472/* Parse an f-string. The f-string is in *str to end, with no
5473 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005474static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005475FstringParser_ConcatFstring(FstringParser *state, const char **str,
5476 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005477 struct compiling *c, const node *n)
5478{
5479 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005480 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005481
5482 /* Parse the f-string. */
5483 while (1) {
5484 PyObject *literal = NULL;
5485 expr_ty expression = NULL;
5486
5487 /* If there's a zero length literal in front of the
5488 expression, literal will be NULL. If we're at the end of
5489 the f-string, expression will be NULL (unless result == 1,
5490 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005491 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005492 &literal, &expression,
5493 c, n);
5494 if (result < 0)
5495 return -1;
5496
5497 /* Add the literal, if any. */
5498 if (!literal) {
5499 /* Do nothing. Just leave last_str alone (and possibly
5500 NULL). */
5501 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005502 /* Note that the literal can be zero length, if the
5503 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005504 state->last_str = literal;
5505 literal = NULL;
5506 } else {
5507 /* We have a literal, concatenate it. */
5508 assert(PyUnicode_GET_LENGTH(literal) != 0);
5509 if (FstringParser_ConcatAndDel(state, literal) < 0)
5510 return -1;
5511 literal = NULL;
5512 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005513
5514 /* We've dealt with the literal now. It can't be leaked on further
5515 errors. */
5516 assert(literal == NULL);
5517
5518 /* See if we should just loop around to get the next literal
5519 and expression, while ignoring the expression this
5520 time. This is used for un-doubling braces, as an
5521 optimization. */
5522 if (result == 1)
5523 continue;
5524
5525 if (!expression)
5526 /* We're done with this f-string. */
5527 break;
5528
5529 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005530 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005531 if (!state->last_str) {
5532 /* Do nothing. No previous literal. */
5533 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005534 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005535 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5536 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5537 return -1;
5538 }
5539
5540 if (ExprList_Append(&state->expr_list, expression) < 0)
5541 return -1;
5542 }
5543
Eric V. Smith235a6f02015-09-19 14:51:32 -04005544 /* If recurse_lvl is zero, then we must be at the end of the
5545 string. Otherwise, we must be at a right brace. */
5546
Eric V. Smith451d0e32016-09-09 21:56:20 -04005547 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005548 ast_error(c, n, "f-string: unexpected end of string");
5549 return -1;
5550 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005551 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005552 ast_error(c, n, "f-string: expecting '}'");
5553 return -1;
5554 }
5555
5556 FstringParser_check_invariants(state);
5557 return 0;
5558}
5559
5560/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005561 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005562static expr_ty
5563FstringParser_Finish(FstringParser *state, struct compiling *c,
5564 const node *n)
5565{
5566 asdl_seq *seq;
5567
5568 FstringParser_check_invariants(state);
5569
5570 /* If we're just a constant string with no expressions, return
5571 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005572 if (!state->fmode) {
5573 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005574 if (!state->last_str) {
5575 /* Create a zero length string. */
5576 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5577 if (!state->last_str)
5578 goto error;
5579 }
5580 return make_str_node_and_del(&state->last_str, c, n);
5581 }
5582
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005583 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005584 last node in our expression list. */
5585 if (state->last_str) {
5586 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5587 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5588 goto error;
5589 }
5590 /* This has already been freed. */
5591 assert(state->last_str == NULL);
5592
5593 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5594 if (!seq)
5595 goto error;
5596
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005597 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5598 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005599
5600error:
5601 FstringParser_Dealloc(state);
5602 return NULL;
5603}
5604
Eric V. Smith451d0e32016-09-09 21:56:20 -04005605/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5606 at end, parse it into an expr_ty. Return NULL on error. Adjust
5607 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005608static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005609fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005610 struct compiling *c, const node *n)
5611{
5612 FstringParser state;
5613
5614 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005615 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005616 c, n) < 0) {
5617 FstringParser_Dealloc(&state);
5618 return NULL;
5619 }
5620
5621 return FstringParser_Finish(&state, c, n);
5622}
5623
5624/* n is a Python string literal, including the bracketing quote
5625 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005626 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005627 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005628 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5629 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005630*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005631static int
5632parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5633 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005634{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005635 size_t len;
5636 const char *s = STR(n);
5637 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005638 int fmode = 0;
5639 *bytesmode = 0;
5640 *rawmode = 0;
5641 *result = NULL;
5642 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005643 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005644 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005645 if (quote == 'b' || quote == 'B') {
5646 quote = *++s;
5647 *bytesmode = 1;
5648 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005649 else if (quote == 'u' || quote == 'U') {
5650 quote = *++s;
5651 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005652 else if (quote == 'r' || quote == 'R') {
5653 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005654 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005655 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005656 else if (quote == 'f' || quote == 'F') {
5657 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005658 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005659 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005660 else {
5661 break;
5662 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005663 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005664 }
Guido van Rossum495da292019-03-07 12:38:08 -08005665
5666 /* fstrings are only allowed in Python 3.6 and greater */
5667 if (fmode && c->c_feature_version < 6) {
5668 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5669 return -1;
5670 }
5671
Eric V. Smith451d0e32016-09-09 21:56:20 -04005672 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005673 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005674 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005675 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005676 if (quote != '\'' && quote != '\"') {
5677 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005678 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005679 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005680 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005681 s++;
5682 len = strlen(s);
5683 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005684 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005685 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005686 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005687 }
5688 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005689 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005690 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005691 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005692 }
5693 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005694 /* A triple quoted string. We've already skipped one quote at
5695 the start and one at the end of the string. Now skip the
5696 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005697 s += 2;
5698 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005699 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005700 if (s[--len] != quote || s[--len] != quote) {
5701 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005702 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005703 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005704 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005705
Eric V. Smith451d0e32016-09-09 21:56:20 -04005706 if (fmode) {
5707 /* Just return the bytes. The caller will parse the resulting
5708 string. */
5709 *fstr = s;
5710 *fstrlen = len;
5711 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005712 }
5713
Eric V. Smith451d0e32016-09-09 21:56:20 -04005714 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005715 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005716 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005717 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005718 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005719 const char *ch;
5720 for (ch = s; *ch; ch++) {
5721 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005722 ast_error(c, n,
5723 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005724 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005725 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005726 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005727 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005728 if (*rawmode)
5729 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005730 else
Eric V. Smith56466482016-10-31 14:46:26 -04005731 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005732 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005733 if (*rawmode)
5734 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005735 else
Eric V. Smith56466482016-10-31 14:46:26 -04005736 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005737 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005738 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005739}
5740
Eric V. Smith235a6f02015-09-19 14:51:32 -04005741/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5742 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005743 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005744 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005745 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005746 node if there's just an f-string (with no leading or trailing
5747 literals), or a JoinedStr node if there are multiple f-strings or
5748 any literals involved. */
5749static expr_ty
5750parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005751{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005752 int bytesmode = 0;
5753 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005754 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005755
5756 FstringParser state;
5757 FstringParser_Init(&state);
5758
5759 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005760 int this_bytesmode;
5761 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005762 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005763 const char *fstr;
5764 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005765
5766 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005767 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5768 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005769 goto error;
5770
5771 /* Check that we're not mixing bytes with unicode. */
5772 if (i != 0 && bytesmode != this_bytesmode) {
5773 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005774 /* s is NULL if the current string part is an f-string. */
5775 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005776 goto error;
5777 }
5778 bytesmode = this_bytesmode;
5779
Eric V. Smith451d0e32016-09-09 21:56:20 -04005780 if (fstr != NULL) {
5781 int result;
5782 assert(s == NULL && !bytesmode);
5783 /* This is an f-string. Parse and concatenate it. */
5784 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5785 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005786 if (result < 0)
5787 goto error;
5788 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005789 /* A string or byte string. */
5790 assert(s != NULL && fstr == NULL);
5791
Eric V. Smith451d0e32016-09-09 21:56:20 -04005792 assert(bytesmode ? PyBytes_CheckExact(s) :
5793 PyUnicode_CheckExact(s));
5794
Eric V. Smith451d0e32016-09-09 21:56:20 -04005795 if (bytesmode) {
5796 /* For bytes, concat as we go. */
5797 if (i == 0) {
5798 /* First time, just remember this value. */
5799 bytes_str = s;
5800 } else {
5801 PyBytes_ConcatAndDel(&bytes_str, s);
5802 if (!bytes_str)
5803 goto error;
5804 }
5805 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005806 /* This is a regular string. Concatenate it. */
5807 if (FstringParser_ConcatAndDel(&state, s) < 0)
5808 goto error;
5809 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005810 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005811 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005812 if (bytesmode) {
5813 /* Just return the bytes object and we're done. */
5814 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5815 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005816 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005817 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005819
Eric V. Smith235a6f02015-09-19 14:51:32 -04005820 /* We're not a bytes string, bytes_str should never have been set. */
5821 assert(bytes_str == NULL);
5822
5823 return FstringParser_Finish(&state, c, n);
5824
5825error:
5826 Py_XDECREF(bytes_str);
5827 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005829}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005830
5831PyObject *
5832_PyAST_GetDocString(asdl_seq *body)
5833{
5834 if (!asdl_seq_LEN(body)) {
5835 return NULL;
5836 }
5837 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5838 if (st->kind != Expr_kind) {
5839 return NULL;
5840 }
5841 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005842 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5843 return e->v.Constant.value;
5844 }
5845 return NULL;
5846}