blob: e9154fecff064b33f7fee23b06d700be6105f301 [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);
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000319 case NamedExpr_kind:
320 return validate_expr(exp->v.NamedExpr.value, Load);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300321 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500324 }
Pablo Galindo0c9258a2019-03-18 13:51:53 +0000325 PyErr_SetString(PyExc_SystemError, "unexpected expression");
326 return 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500327}
328
329static int
330validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
331{
332 if (asdl_seq_LEN(seq))
333 return 1;
334 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
335 return 0;
336}
337
338static int
339validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
340{
341 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
342 validate_exprs(targets, ctx, 0);
343}
344
345static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300346validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500347{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300348 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500349}
350
351static int
352validate_stmt(stmt_ty stmt)
353{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100354 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500355 switch (stmt->kind) {
356 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300357 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500358 validate_arguments(stmt->v.FunctionDef.args) &&
359 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
360 (!stmt->v.FunctionDef.returns ||
361 validate_expr(stmt->v.FunctionDef.returns, Load));
362 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300363 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500364 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
365 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400366 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500367 case Return_kind:
368 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
369 case Delete_kind:
370 return validate_assignlist(stmt->v.Delete.targets, Del);
371 case Assign_kind:
372 return validate_assignlist(stmt->v.Assign.targets, Store) &&
373 validate_expr(stmt->v.Assign.value, Load);
374 case AugAssign_kind:
375 return validate_expr(stmt->v.AugAssign.target, Store) &&
376 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700377 case AnnAssign_kind:
378 if (stmt->v.AnnAssign.target->kind != Name_kind &&
379 stmt->v.AnnAssign.simple) {
380 PyErr_SetString(PyExc_TypeError,
381 "AnnAssign with simple non-Name target");
382 return 0;
383 }
384 return validate_expr(stmt->v.AnnAssign.target, Store) &&
385 (!stmt->v.AnnAssign.value ||
386 validate_expr(stmt->v.AnnAssign.value, Load)) &&
387 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500388 case For_kind:
389 return validate_expr(stmt->v.For.target, Store) &&
390 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300391 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500392 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400393 case AsyncFor_kind:
394 return validate_expr(stmt->v.AsyncFor.target, Store) &&
395 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300396 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400397 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500398 case While_kind:
399 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300400 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500401 validate_stmts(stmt->v.While.orelse);
402 case If_kind:
403 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300404 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500405 validate_stmts(stmt->v.If.orelse);
406 case With_kind:
407 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
408 return 0;
409 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
410 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
411 if (!validate_expr(item->context_expr, Load) ||
412 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
413 return 0;
414 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300415 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400416 case AsyncWith_kind:
417 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
418 return 0;
419 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
420 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
421 if (!validate_expr(item->context_expr, Load) ||
422 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
423 return 0;
424 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300425 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500426 case Raise_kind:
427 if (stmt->v.Raise.exc) {
428 return validate_expr(stmt->v.Raise.exc, Load) &&
429 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
430 }
431 if (stmt->v.Raise.cause) {
432 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
433 return 0;
434 }
435 return 1;
436 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300437 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500438 return 0;
439 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
440 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
441 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
442 return 0;
443 }
444 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
445 asdl_seq_LEN(stmt->v.Try.orelse)) {
446 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
447 return 0;
448 }
449 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
450 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
451 if ((handler->v.ExceptHandler.type &&
452 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300453 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500454 return 0;
455 }
456 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
457 validate_stmts(stmt->v.Try.finalbody)) &&
458 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
459 validate_stmts(stmt->v.Try.orelse));
460 case Assert_kind:
461 return validate_expr(stmt->v.Assert.test, Load) &&
462 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
463 case Import_kind:
464 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
465 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300466 if (stmt->v.ImportFrom.level < 0) {
467 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500468 return 0;
469 }
470 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
471 case Global_kind:
472 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
473 case Nonlocal_kind:
474 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
475 case Expr_kind:
476 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400477 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300478 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400479 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
480 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
481 (!stmt->v.AsyncFunctionDef.returns ||
482 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500483 case Pass_kind:
484 case Break_kind:
485 case Continue_kind:
486 return 1;
487 default:
488 PyErr_SetString(PyExc_SystemError, "unexpected statement");
489 return 0;
490 }
491}
492
493static int
494validate_stmts(asdl_seq *seq)
495{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100496 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500497 for (i = 0; i < asdl_seq_LEN(seq); i++) {
498 stmt_ty stmt = asdl_seq_GET(seq, i);
499 if (stmt) {
500 if (!validate_stmt(stmt))
501 return 0;
502 }
503 else {
504 PyErr_SetString(PyExc_ValueError,
505 "None disallowed in statement list");
506 return 0;
507 }
508 }
509 return 1;
510}
511
512static int
513validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
514{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100515 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500516 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
517 expr_ty expr = asdl_seq_GET(exprs, i);
518 if (expr) {
519 if (!validate_expr(expr, ctx))
520 return 0;
521 }
522 else if (!null_ok) {
523 PyErr_SetString(PyExc_ValueError,
524 "None disallowed in expression list");
525 return 0;
526 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100527
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500528 }
529 return 1;
530}
531
532int
533PyAST_Validate(mod_ty mod)
534{
535 int res = 0;
536
537 switch (mod->kind) {
538 case Module_kind:
539 res = validate_stmts(mod->v.Module.body);
540 break;
541 case Interactive_kind:
542 res = validate_stmts(mod->v.Interactive.body);
543 break;
544 case Expression_kind:
545 res = validate_expr(mod->v.Expression.body, Load);
546 break;
547 case Suite_kind:
548 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
549 break;
550 default:
551 PyErr_SetString(PyExc_SystemError, "impossible module node");
552 res = 0;
553 break;
554 }
555 return res;
556}
557
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500558/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500559#include "grammar.h"
560#include "parsetok.h"
561#include "graminit.h"
562
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563/* Data structure used internally */
564struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400565 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200566 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500567 PyObject *c_normalize; /* Normalization function from unicodedata. */
Guido van Rossum495da292019-03-07 12:38:08 -0800568 int c_feature_version; /* Latest minor version of Python for allowed features */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569};
570
571static asdl_seq *seq_for_testlist(struct compiling *, const node *);
572static expr_ty ast_for_expr(struct compiling *, const node *);
573static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300574static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000575static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
576 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000577static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000578static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
guoci90fc8982018-09-11 17:45:45 -0400580static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
581static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200584static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000585 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000587static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400588static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000589static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Nick Coghlan650f0d02007-04-15 12:05:43 +0000591#define COMP_GENEXP 0
592#define COMP_LISTCOMP 1
593#define COMP_SETCOMP 2
594
Benjamin Peterson55e00432012-01-16 17:22:31 -0500595static int
596init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000597{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500598 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
599 if (!m)
600 return 0;
601 c->c_normalize = PyObject_GetAttrString(m, "normalize");
602 Py_DECREF(m);
603 if (!c->c_normalize)
604 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500605 return 1;
606}
607
608static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400609new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500610{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400611 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500612 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000613 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500614 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500615 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000616 /* Check whether there are non-ASCII characters in the
617 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500618 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200619 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300620 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500621 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500622 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200623 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500624 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300625 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
626 if (form == NULL) {
627 Py_DECREF(id);
628 return NULL;
629 }
630 PyObject *args[2] = {form, id};
631 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500632 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633 if (!id2)
634 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300635 if (!PyUnicode_Check(id2)) {
636 PyErr_Format(PyExc_TypeError,
637 "unicodedata.normalize() must return a string, not "
638 "%.200s",
639 Py_TYPE(id2)->tp_name);
640 Py_DECREF(id2);
641 return NULL;
642 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000645 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200646 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
647 Py_DECREF(id);
648 return NULL;
649 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000650 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651}
652
Benjamin Peterson55e00432012-01-16 17:22:31 -0500653#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200656ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400658 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200659 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200661 va_start(va, errmsg);
662 errstr = PyUnicode_FromFormatV(errmsg, va);
663 va_end(va);
664 if (!errstr) {
665 return 0;
666 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200667 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000669 Py_INCREF(Py_None);
670 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 }
Ammar Askar025eb982018-09-24 17:12:49 -0400672 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200673 if (!tmp) {
674 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400675 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000676 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 Py_DECREF(errstr);
679 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400680 if (value) {
681 PyErr_SetObject(PyExc_SyntaxError, value);
682 Py_DECREF(value);
683 }
684 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685}
686
687/* num_stmts() returns number of contained statements.
688
689 Use this routine to determine how big a sequence is needed for
690 the statements in a parse tree. Its raison d'etre is this bit of
691 grammar:
692
693 stmt: simple_stmt | compound_stmt
694 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
695
696 A simple_stmt can contain multiple small_stmt elements joined
697 by semicolons. If the arg is a simple_stmt, the number of
698 small_stmt elements is returned.
699*/
700
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800701static string
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800702new_type_comment(const char *s, struct compiling *c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800703{
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800704 PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL);
Guido van Rossum4b250fc2019-02-11 08:10:42 -0800705 if (res == NULL)
706 return NULL;
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800707 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
708 Py_DECREF(res);
709 return NULL;
710 }
711 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800712}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800713#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800714
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715static int
716num_stmts(const node *n)
717{
718 int i, l;
719 node *ch;
720
721 switch (TYPE(n)) {
722 case single_input:
723 if (TYPE(CHILD(n, 0)) == NEWLINE)
724 return 0;
725 else
726 return num_stmts(CHILD(n, 0));
727 case file_input:
728 l = 0;
729 for (i = 0; i < NCH(n); i++) {
730 ch = CHILD(n, i);
731 if (TYPE(ch) == stmt)
732 l += num_stmts(ch);
733 }
734 return l;
735 case stmt:
736 return num_stmts(CHILD(n, 0));
737 case compound_stmt:
738 return 1;
739 case simple_stmt:
740 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
741 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800742 case func_body_suite:
743 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
744 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 if (NCH(n) == 1)
746 return num_stmts(CHILD(n, 0));
747 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800748 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800750 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
751 i += 2;
752 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 l += num_stmts(CHILD(n, i));
754 return l;
755 }
756 default: {
757 char buf[128];
758
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000759 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 TYPE(n), NCH(n));
761 Py_FatalError(buf);
762 }
763 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700764 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765}
766
767/* Transform the CST rooted at node * to the appropriate AST
768*/
769
770mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200771PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
772 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000774 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800776 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 stmt_ty s;
778 node *ch;
779 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500780 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800781 asdl_seq *argtypes = NULL;
782 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400784 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200785 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400786 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800787 c.c_normalize = NULL;
Guido van Rossum495da292019-03-07 12:38:08 -0800788 c.c_feature_version = flags->cf_feature_version;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800789
790 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Jeremy Hyltona8293132006-02-28 17:58:27 +0000793 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 switch (TYPE(n)) {
795 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200796 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500798 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 for (i = 0; i < NCH(n) - 1; i++) {
800 ch = CHILD(n, i);
801 if (TYPE(ch) == NEWLINE)
802 continue;
803 REQ(ch, stmt);
804 num = num_stmts(ch);
805 if (num == 1) {
806 s = ast_for_stmt(&c, ch);
807 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500808 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000809 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 else {
812 ch = CHILD(ch, 0);
813 REQ(ch, simple_stmt);
814 for (j = 0; j < num; j++) {
815 s = ast_for_stmt(&c, CHILD(ch, j * 2));
816 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000818 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 }
820 }
821 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800822
823 /* Type ignores are stored under the ENDMARKER in file_input. */
824 ch = CHILD(n, NCH(n) - 1);
825 REQ(ch, ENDMARKER);
826 num = NCH(ch);
827 type_ignores = _Py_asdl_seq_new(num, arena);
828 if (!type_ignores)
829 goto out;
830
831 for (i = 0; i < num; i++) {
832 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
833 if (!ti)
834 goto out;
835 asdl_seq_SET(type_ignores, i, ti);
836 }
837
838 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500839 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 case eval_input: {
841 expr_ty testlist_ast;
842
Nick Coghlan650f0d02007-04-15 12:05:43 +0000843 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000844 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500846 goto out;
847 res = Expression(testlist_ast, arena);
848 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 }
850 case single_input:
851 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200852 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500854 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000856 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000857 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000858 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500859 goto out;
860 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 }
862 else {
863 n = CHILD(n, 0);
864 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200865 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500867 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000869 s = ast_for_stmt(&c, n);
870 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500871 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 asdl_seq_SET(stmts, 0, s);
873 }
874 else {
875 /* Only a simple_stmt can contain multiple statements. */
876 REQ(n, simple_stmt);
877 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 if (TYPE(CHILD(n, i)) == NEWLINE)
879 break;
880 s = ast_for_stmt(&c, CHILD(n, i));
881 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500882 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 asdl_seq_SET(stmts, i / 2, s);
884 }
885 }
886
Benjamin Peterson55e00432012-01-16 17:22:31 -0500887 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500889 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800890 case func_type_input:
891 n = CHILD(n, 0);
892 REQ(n, func_type);
893
894 if (TYPE(CHILD(n, 1)) == typelist) {
895 ch = CHILD(n, 1);
896 /* this is overly permissive -- we don't pay any attention to
897 * stars on the args -- just parse them into an ordered list */
898 num = 0;
899 for (i = 0; i < NCH(ch); i++) {
900 if (TYPE(CHILD(ch, i)) == test) {
901 num++;
902 }
903 }
904
905 argtypes = _Py_asdl_seq_new(num, arena);
906 if (!argtypes)
907 goto out;
908
909 j = 0;
910 for (i = 0; i < NCH(ch); i++) {
911 if (TYPE(CHILD(ch, i)) == test) {
912 arg = ast_for_expr(&c, CHILD(ch, i));
913 if (!arg)
914 goto out;
915 asdl_seq_SET(argtypes, j++, arg);
916 }
917 }
918 }
919 else {
920 argtypes = _Py_asdl_seq_new(0, arena);
921 if (!argtypes)
922 goto out;
923 }
924
925 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
926 if (!ret)
927 goto out;
928 res = FunctionType(argtypes, ret, arena);
929 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000931 PyErr_Format(PyExc_SystemError,
932 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500933 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500935 out:
936 if (c.c_normalize) {
937 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500938 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500939 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940}
941
Victor Stinner14e461d2013-08-26 22:28:21 +0200942mod_ty
943PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
944 PyArena *arena)
945{
946 mod_ty mod;
947 PyObject *filename;
948 filename = PyUnicode_DecodeFSDefault(filename_str);
949 if (filename == NULL)
950 return NULL;
951 mod = PyAST_FromNodeObject(n, flags, filename, arena);
952 Py_DECREF(filename);
953 return mod;
954
955}
956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
958*/
959
960static operator_ty
Guido van Rossum495da292019-03-07 12:38:08 -0800961get_operator(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962{
963 switch (TYPE(n)) {
964 case VBAR:
965 return BitOr;
966 case CIRCUMFLEX:
967 return BitXor;
968 case AMPER:
969 return BitAnd;
970 case LEFTSHIFT:
971 return LShift;
972 case RIGHTSHIFT:
973 return RShift;
974 case PLUS:
975 return Add;
976 case MINUS:
977 return Sub;
978 case STAR:
979 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400980 case AT:
Guido van Rossum495da292019-03-07 12:38:08 -0800981 if (c->c_feature_version < 5) {
982 ast_error(c, n,
983 "The '@' operator is only supported in Python 3.5 and greater");
984 return (operator_ty)0;
985 }
Benjamin Petersond51374e2014-04-09 23:55:56 -0400986 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 case SLASH:
988 return Div;
989 case DOUBLESLASH:
990 return FloorDiv;
991 case PERCENT:
992 return Mod;
993 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000994 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 }
996}
997
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200998static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000999 "None",
1000 "True",
1001 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001002 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +00001003 NULL,
1004};
1005
1006static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001007forbidden_name(struct compiling *c, identifier name, const node *n,
1008 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001009{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001010 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001011 const char * const *p = FORBIDDEN;
1012 if (!full_checks) {
1013 /* In most cases, the parser will protect True, False, and None
1014 from being assign to. */
1015 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001016 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001017 for (; *p; p++) {
1018 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1019 ast_error(c, n, "cannot assign to %U", name);
1020 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001021 }
1022 }
1023 return 0;
1024}
1025
Serhiy Storchakab619b092018-11-27 09:40:29 +02001026static expr_ty
1027copy_location(expr_ty e, const node *n)
1028{
1029 if (e) {
1030 e->lineno = LINENO(n);
1031 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001032 e->end_lineno = n->n_end_lineno;
1033 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001034 }
1035 return e;
1036}
1037
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001038static const char *
1039get_expr_name(expr_ty e)
1040{
1041 switch (e->kind) {
1042 case Attribute_kind:
1043 return "attribute";
1044 case Subscript_kind:
1045 return "subscript";
1046 case Starred_kind:
1047 return "starred";
1048 case Name_kind:
1049 return "name";
1050 case List_kind:
1051 return "list";
1052 case Tuple_kind:
1053 return "tuple";
1054 case Lambda_kind:
1055 return "lambda";
1056 case Call_kind:
1057 return "function call";
1058 case BoolOp_kind:
1059 case BinOp_kind:
1060 case UnaryOp_kind:
1061 return "operator";
1062 case GeneratorExp_kind:
1063 return "generator expression";
1064 case Yield_kind:
1065 case YieldFrom_kind:
1066 return "yield expression";
1067 case Await_kind:
1068 return "await expression";
1069 case ListComp_kind:
1070 return "list comprehension";
1071 case SetComp_kind:
1072 return "set comprehension";
1073 case DictComp_kind:
1074 return "dict comprehension";
1075 case Dict_kind:
1076 return "dict display";
1077 case Set_kind:
1078 return "set display";
1079 case JoinedStr_kind:
1080 case FormattedValue_kind:
1081 return "f-string expression";
1082 case Constant_kind: {
1083 PyObject *value = e->v.Constant.value;
1084 if (value == Py_None) {
1085 return "None";
1086 }
1087 if (value == Py_False) {
1088 return "False";
1089 }
1090 if (value == Py_True) {
1091 return "True";
1092 }
1093 if (value == Py_Ellipsis) {
1094 return "Ellipsis";
1095 }
1096 return "literal";
1097 }
1098 case Compare_kind:
1099 return "comparison";
1100 case IfExp_kind:
1101 return "conditional expression";
1102 case NamedExpr_kind:
1103 return "named expression";
1104 default:
1105 PyErr_Format(PyExc_SystemError,
1106 "unexpected expression in assignment %d (line %d)",
1107 e->kind, e->lineno);
1108 return NULL;
1109 }
1110}
1111
Jeremy Hyltona8293132006-02-28 17:58:27 +00001112/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113
1114 Only sets context for expr kinds that "can appear in assignment context"
1115 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1116 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117*/
1118
1119static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001120set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121{
1122 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001123
1124 /* The ast defines augmented store and load contexts, but the
1125 implementation here doesn't actually use them. The code may be
1126 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001127 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001128 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001129 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001130 */
1131 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132
1133 switch (e->kind) {
1134 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001135 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001136 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001137 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001138 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001140 e->v.Subscript.ctx = ctx;
1141 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001142 case Starred_kind:
1143 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001144 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001145 return 0;
1146 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 case Name_kind:
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001148 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001149 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001150 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001151 }
1152 e->v.Name.ctx = ctx;
1153 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001155 e->v.List.ctx = ctx;
1156 s = e->v.List.elts;
1157 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001159 e->v.Tuple.ctx = ctx;
1160 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001161 break;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001162 default: {
1163 const char *expr_name = get_expr_name(e);
1164 if (expr_name != NULL) {
1165 ast_error(c, n, "cannot %s %s",
1166 ctx == Store ? "assign to" : "delete",
1167 expr_name);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001168 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001169 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001170 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001171 }
1172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 */
1176 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001177 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178
Thomas Wouters89f507f2006-12-13 04:49:30 +00001179 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001180 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001181 return 0;
1182 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 }
1184 return 1;
1185}
1186
1187static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001188ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189{
1190 REQ(n, augassign);
1191 n = CHILD(n, 0);
1192 switch (STR(n)[0]) {
1193 case '+':
1194 return Add;
1195 case '-':
1196 return Sub;
1197 case '/':
1198 if (STR(n)[1] == '/')
1199 return FloorDiv;
1200 else
1201 return Div;
1202 case '%':
1203 return Mod;
1204 case '<':
1205 return LShift;
1206 case '>':
1207 return RShift;
1208 case '&':
1209 return BitAnd;
1210 case '^':
1211 return BitXor;
1212 case '|':
1213 return BitOr;
1214 case '*':
1215 if (STR(n)[1] == '*')
1216 return Pow;
1217 else
1218 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001219 case '@':
Guido van Rossum495da292019-03-07 12:38:08 -08001220 if (c->c_feature_version < 5) {
1221 ast_error(c, n,
1222 "The '@' operator is only supported in Python 3.5 and greater");
1223 return (operator_ty)0;
1224 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04001225 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001227 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 }
1230}
1231
1232static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001233ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001235 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 |'is' 'not'
1237 */
1238 REQ(n, comp_op);
1239 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001240 n = CHILD(n, 0);
1241 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 case LESS:
1243 return Lt;
1244 case GREATER:
1245 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001246 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 return Eq;
1248 case LESSEQUAL:
1249 return LtE;
1250 case GREATEREQUAL:
1251 return GtE;
1252 case NOTEQUAL:
1253 return NotEq;
1254 case NAME:
1255 if (strcmp(STR(n), "in") == 0)
1256 return In;
1257 if (strcmp(STR(n), "is") == 0)
1258 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001259 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001261 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001263 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001264 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 }
1266 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001267 /* handle "not in" and "is not" */
1268 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 case NAME:
1270 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1271 return NotIn;
1272 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1273 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001274 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001276 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001278 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001279 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 }
Neal Norwitz79792652005-11-14 04:25:03 +00001281 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001283 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284}
1285
1286static asdl_seq *
1287seq_for_testlist(struct compiling *c, const node *n)
1288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001290 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1291 */
Armin Rigo31441302005-10-21 12:57:31 +00001292 asdl_seq *seq;
1293 expr_ty expression;
1294 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001295 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001297 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 if (!seq)
1299 return NULL;
1300
1301 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001303 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304
Benjamin Peterson4905e802009-09-27 02:43:28 +00001305 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001306 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308
1309 assert(i / 2 < seq->size);
1310 asdl_seq_SET(seq, i / 2, expression);
1311 }
1312 return seq;
1313}
1314
Neal Norwitzc1505362006-12-28 06:47:50 +00001315static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001316ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001317{
1318 identifier name;
1319 expr_ty annotation = NULL;
1320 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001321 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001322
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001323 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001324 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001325 name = NEW_IDENTIFIER(ch);
1326 if (!name)
1327 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001328 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001329 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001330
1331 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1332 annotation = ast_for_expr(c, CHILD(n, 2));
1333 if (!annotation)
1334 return NULL;
1335 }
1336
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001337 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001338 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001339 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001340 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001341 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342}
1343
Guido van Rossum4f72a782006-10-27 23:31:49 +00001344/* returns -1 if failed to handle keyword only arguments
1345 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001346 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001347 ^^^
1348 start pointing here
1349 */
1350static int
1351handle_keywordonly_args(struct compiling *c, const node *n, int start,
1352 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1353{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001354 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001355 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001356 expr_ty expression, annotation;
Pablo Galindo164686f2019-02-10 20:37:07 +00001357 arg_ty arg = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001358 int i = start;
1359 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001360
1361 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001362 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001363 return -1;
1364 }
1365 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001366 while (i < NCH(n)) {
1367 ch = CHILD(n, i);
1368 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001369 case vfpdef:
1370 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001372 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001373 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001374 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 asdl_seq_SET(kwdefaults, j, expression);
1376 i += 2; /* '=' and test */
1377 }
1378 else { /* setting NULL if no default value exists */
1379 asdl_seq_SET(kwdefaults, j, NULL);
1380 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001381 if (NCH(ch) == 3) {
1382 /* ch is NAME ':' test */
1383 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001384 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001385 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001386 }
1387 else {
1388 annotation = NULL;
1389 }
1390 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001391 argname = NEW_IDENTIFIER(ch);
1392 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001393 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001394 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001395 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001396 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001397 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001398 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001399 if (!arg)
1400 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001401 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001402 i += 1; /* the name */
1403 if (TYPE(CHILD(n, i)) == COMMA)
1404 i += 1; /* the comma, if present */
1405 break;
1406 case TYPE_COMMENT:
1407 /* arg will be equal to the last argument processed */
1408 arg->type_comment = NEW_TYPE_COMMENT(ch);
1409 if (!arg->type_comment)
1410 goto error;
1411 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001412 break;
1413 case DOUBLESTAR:
1414 return i;
1415 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001416 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 goto error;
1418 }
1419 }
1420 return i;
1421 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424
Jeremy Hyltona8293132006-02-28 17:58:27 +00001425/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426
1427static arguments_ty
1428ast_for_arguments(struct compiling *c, const node *n)
1429{
Neal Norwitzc1505362006-12-28 06:47:50 +00001430 /* This function handles both typedargslist (function definition)
1431 and varargslist (lambda definition).
1432
1433 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001434 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1435 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1436 | '**' tfpdef [',']]]
1437 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1438 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001439 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001440 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1441 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1442 | '**' vfpdef [',']]]
1443 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1444 | '**' vfpdef [',']
1445 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001446 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001447
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1450 int nposdefaults = 0, found_default = 0;
1451 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001452 arg_ty vararg = NULL, kwarg = NULL;
Pablo Galindo164686f2019-02-10 20:37:07 +00001453 arg_ty arg = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 node *ch;
1455
1456 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001457 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001458 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001459 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001461 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462
Jeremy Hyltone921e022008-07-17 16:37:17 +00001463 /* First count the number of positional args & defaults. The
1464 variable i is the loop index for this for loop and the next.
1465 The next loop picks up where the first leaves off.
1466 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001468 ch = CHILD(n, i);
1469 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001470 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001471 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001472 if (i < NCH(n) && /* skip argument following star */
1473 (TYPE(CHILD(n, i)) == tfpdef ||
1474 TYPE(CHILD(n, i)) == vfpdef)) {
1475 i++;
1476 }
1477 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001478 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001479 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001480 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001481 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001484 defaults for keyword only args */
1485 for ( ; i < NCH(n); ++i) {
1486 ch = CHILD(n, i);
1487 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001488 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001489 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001490 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001491 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001492 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001494 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001495 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001496 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001498 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001499 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001500 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001502 since we set NULL as default for keyword only argument w/o default
1503 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001504 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001505 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001506 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001507 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001508
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001509 /* tfpdef: NAME [':' test]
1510 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 */
1512 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001513 j = 0; /* index for defaults */
1514 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001516 ch = CHILD(n, i);
1517 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001518 case tfpdef:
1519 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1521 anything other than EQUAL or a comma? */
1522 /* XXX Should NCH(n) check be made a separate check? */
1523 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001524 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1525 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001526 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001527 assert(posdefaults != NULL);
1528 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001530 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001532 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001533 ast_error(c, n,
Guido van Rossum495da292019-03-07 12:38:08 -08001534 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001535 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001536 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001537 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001538 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001539 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001540 asdl_seq_SET(posargs, k++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001541 i += 1; /* the name */
1542 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1543 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 break;
1545 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001546 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001547 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1548 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001549 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001550 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001551 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001552 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001553 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001554 if (TYPE(ch) == COMMA) {
1555 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001556 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001557
1558 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1559 ast_error(c, CHILD(n, i),
1560 "bare * has associated type comment");
1561 return NULL;
1562 }
1563
Guido van Rossum4f72a782006-10-27 23:31:49 +00001564 res = handle_keywordonly_args(c, n, i,
1565 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001566 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001567 i = res; /* res has new position to process */
1568 }
1569 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001570 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001571 if (!vararg)
1572 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001573
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001574 i += 2; /* the star and the name */
1575 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1576 i += 1; /* the comma, if present */
1577
1578 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1579 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1580 if (!vararg->type_comment)
1581 return NULL;
1582 i += 1;
1583 }
1584
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001585 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1586 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001587 int res = 0;
1588 res = handle_keywordonly_args(c, n, i,
1589 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001590 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001591 i = res; /* res has new position to process */
1592 }
1593 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 break;
1595 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001596 ch = CHILD(n, i+1); /* tfpdef */
1597 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001598 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001599 if (!kwarg)
1600 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001601 i += 2; /* the double star and the name */
1602 if (TYPE(CHILD(n, i)) == COMMA)
1603 i += 1; /* the comma, if present */
1604 break;
1605 case TYPE_COMMENT:
1606 assert(i);
1607
1608 if (kwarg)
1609 arg = kwarg;
1610
1611 /* arg will be equal to the last argument processed */
1612 arg->type_comment = NEW_TYPE_COMMENT(ch);
1613 if (!arg->type_comment)
1614 return NULL;
1615 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616 break;
1617 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001618 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619 "unexpected node in varargslist: %d @ %d",
1620 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001621 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001624 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625}
1626
1627static expr_ty
1628ast_for_dotted_name(struct compiling *c, const node *n)
1629{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001630 expr_ty e;
1631 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001632 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001634 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635
1636 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001637
1638 lineno = LINENO(n);
1639 col_offset = n->n_col_offset;
1640
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001641 ch = CHILD(n, 0);
1642 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001644 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001645 e = Name(id, Load, lineno, col_offset,
1646 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001648 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649
1650 for (i = 2; i < NCH(n); i+=2) {
1651 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001652 if (!id)
1653 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001654 e = Attribute(e, id, Load, lineno, col_offset,
1655 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001656 if (!e)
1657 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 }
1659
1660 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661}
1662
1663static expr_ty
1664ast_for_decorator(struct compiling *c, const node *n)
1665{
1666 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1667 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001668 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001671 REQ(CHILD(n, 0), AT);
1672 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1675 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001676 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001679 d = name_expr;
1680 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 }
1682 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001683 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001684 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001685 if (!d)
1686 return NULL;
1687 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 }
1689 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001690 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001691 if (!d)
1692 return NULL;
1693 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 }
1695
1696 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697}
1698
1699static asdl_seq*
1700ast_for_decorators(struct compiling *c, const node *n)
1701{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001702 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001703 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001707 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 if (!decorator_seq)
1709 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001712 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001713 if (!d)
1714 return NULL;
1715 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 }
1717 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718}
1719
1720static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001721ast_for_funcdef_impl(struct compiling *c, const node *n0,
1722 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001724 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001725 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001726 identifier name;
1727 arguments_ty args;
1728 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001729 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001730 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001731 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001732 node *tc;
1733 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734
Guido van Rossum495da292019-03-07 12:38:08 -08001735 if (is_async && c->c_feature_version < 5) {
1736 ast_error(c, n,
1737 "Async functions are only supported in Python 3.5 and greater");
1738 return NULL;
1739 }
1740
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 REQ(n, funcdef);
1742
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 name = NEW_IDENTIFIER(CHILD(n, name_i));
1744 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001745 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001746 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001747 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1749 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001750 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001751 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1752 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1753 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001754 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001755 name_i += 2;
1756 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001757 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1758 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1759 if (!type_comment)
1760 return NULL;
1761 name_i += 1;
1762 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001763 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001765 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001766 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001768 if (NCH(CHILD(n, name_i + 3)) > 1) {
1769 /* Check if the suite has a type comment in it. */
1770 tc = CHILD(CHILD(n, name_i + 3), 1);
1771
1772 if (TYPE(tc) == TYPE_COMMENT) {
1773 if (type_comment != NULL) {
1774 ast_error(c, n, "Cannot have two type comments on def");
1775 return NULL;
1776 }
1777 type_comment = NEW_TYPE_COMMENT(tc);
1778 if (!type_comment)
1779 return NULL;
1780 }
1781 }
1782
Yury Selivanov75445082015-05-11 22:57:16 -04001783 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001784 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001785 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001786 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001787 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001788 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001789}
1790
1791static stmt_ty
1792ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1793{
Guido van Rossum495da292019-03-07 12:38:08 -08001794 /* async_funcdef: ASYNC funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001795 REQ(n, async_funcdef);
Guido van Rossum495da292019-03-07 12:38:08 -08001796 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001797 REQ(CHILD(n, 1), funcdef);
1798
guoci90fc8982018-09-11 17:45:45 -04001799 return ast_for_funcdef_impl(c, n, decorator_seq,
1800 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001801}
1802
1803static stmt_ty
1804ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1805{
1806 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1807 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001808 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001809}
1810
1811
1812static stmt_ty
1813ast_for_async_stmt(struct compiling *c, const node *n)
1814{
Guido van Rossum495da292019-03-07 12:38:08 -08001815 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001816 REQ(n, async_stmt);
Guido van Rossum495da292019-03-07 12:38:08 -08001817 REQ(CHILD(n, 0), ASYNC);
Yury Selivanov75445082015-05-11 22:57:16 -04001818
1819 switch (TYPE(CHILD(n, 1))) {
1820 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001821 return ast_for_funcdef_impl(c, n, NULL,
1822 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001823 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001824 return ast_for_with_stmt(c, n,
1825 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001826
1827 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001828 return ast_for_for_stmt(c, n,
1829 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001830
1831 default:
1832 PyErr_Format(PyExc_SystemError,
1833 "invalid async stament: %s",
1834 STR(CHILD(n, 1)));
1835 return NULL;
1836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837}
1838
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001839static stmt_ty
1840ast_for_decorated(struct compiling *c, const node *n)
1841{
Yury Selivanov75445082015-05-11 22:57:16 -04001842 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001843 stmt_ty thing = NULL;
1844 asdl_seq *decorator_seq = NULL;
1845
1846 REQ(n, decorated);
1847
1848 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1849 if (!decorator_seq)
1850 return NULL;
1851
1852 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001853 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001855
1856 if (TYPE(CHILD(n, 1)) == funcdef) {
1857 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1858 } else if (TYPE(CHILD(n, 1)) == classdef) {
1859 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001860 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1861 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001862 }
1863 return thing;
1864}
1865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001867ast_for_namedexpr(struct compiling *c, const node *n)
1868{
1869 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1870 ['else' ':' suite]
1871 namedexpr_test: test [':=' test]
1872 argument: ( test [comp_for] |
1873 test ':=' test |
1874 test '=' test |
1875 '**' test |
1876 '*' test )
1877 */
1878 expr_ty target, value;
1879
1880 target = ast_for_expr(c, CHILD(n, 0));
1881 if (!target)
1882 return NULL;
1883
1884 value = ast_for_expr(c, CHILD(n, 2));
1885 if (!value)
1886 return NULL;
1887
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02001888 if (target->kind != Name_kind) {
1889 const char *expr_name = get_expr_name(target);
1890 if (expr_name != NULL) {
1891 ast_error(c, n, "cannot use named assignment with %s", expr_name);
1892 }
1893 return NULL;
1894 }
1895
1896 if (!set_context(c, target, Store, n))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001897 return NULL;
1898
1899 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1900 n->n_end_col_offset, c->c_arena);
1901}
1902
1903static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904ast_for_lambdef(struct compiling *c, const node *n)
1905{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001906 /* lambdef: 'lambda' [varargslist] ':' test
1907 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 arguments_ty args;
1909 expr_ty expression;
1910
1911 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001912 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 if (!args)
1914 return NULL;
1915 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001916 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 }
1919 else {
1920 args = ast_for_arguments(c, CHILD(n, 1));
1921 if (!args)
1922 return NULL;
1923 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001924 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926 }
1927
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001928 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1929 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930}
1931
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001932static expr_ty
1933ast_for_ifexpr(struct compiling *c, const node *n)
1934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001936 expr_ty expression, body, orelse;
1937
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001938 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001939 body = ast_for_expr(c, CHILD(n, 0));
1940 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001941 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001942 expression = ast_for_expr(c, CHILD(n, 2));
1943 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001944 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001945 orelse = ast_for_expr(c, CHILD(n, 4));
1946 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001947 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001948 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001949 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001950 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001951}
1952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001954 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955
Nick Coghlan650f0d02007-04-15 12:05:43 +00001956 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957*/
1958
1959static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001960count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001962 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963
Guido van Rossumd8faa362007-04-27 19:54:29 +00001964 count_comp_for:
1965 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001966 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001967 if (NCH(n) == 2) {
Guido van Rossum495da292019-03-07 12:38:08 -08001968 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001969 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001970 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001971 else if (NCH(n) == 1) {
1972 n = CHILD(n, 0);
1973 }
1974 else {
1975 goto error;
1976 }
1977 if (NCH(n) == (5)) {
1978 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001979 }
1980 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001981 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001982 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001983 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001984 REQ(n, comp_iter);
1985 n = CHILD(n, 0);
1986 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001987 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001988 else if (TYPE(n) == comp_if) {
1989 if (NCH(n) == 3) {
1990 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001991 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001992 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001993 else
1994 return n_fors;
1995 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001996
Jelle Zijlstraac317702017-10-05 20:24:46 -07001997 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001998 /* Should never be reached */
1999 PyErr_SetString(PyExc_SystemError,
2000 "logic error in count_comp_fors");
2001 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002}
2003
Nick Coghlan650f0d02007-04-15 12:05:43 +00002004/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005
Nick Coghlan650f0d02007-04-15 12:05:43 +00002006 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007*/
2008
2009static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002010count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002012 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013
Guido van Rossumd8faa362007-04-27 19:54:29 +00002014 while (1) {
2015 REQ(n, comp_iter);
2016 if (TYPE(CHILD(n, 0)) == comp_for)
2017 return n_ifs;
2018 n = CHILD(n, 0);
2019 REQ(n, comp_if);
2020 n_ifs++;
2021 if (NCH(n) == 2)
2022 return n_ifs;
2023 n = CHILD(n, 2);
2024 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025}
2026
Guido van Rossum992d4a32007-07-11 13:09:30 +00002027static asdl_seq *
2028ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002031 asdl_seq *comps;
2032
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002033 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 if (n_fors == -1)
2035 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002036
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002037 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002038 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002040
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002042 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002044 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002045 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002046 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002047 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048
Guido van Rossum992d4a32007-07-11 13:09:30 +00002049 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050
Jelle Zijlstraac317702017-10-05 20:24:46 -07002051 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002052 is_async = 1;
Guido van Rossum495da292019-03-07 12:38:08 -08002053 REQ(CHILD(n, 0), ASYNC);
Jelle Zijlstraac317702017-10-05 20:24:46 -07002054 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002055 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002056 else {
2057 sync_n = CHILD(n, 0);
2058 }
2059 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002060
Guido van Rossum495da292019-03-07 12:38:08 -08002061 /* Async comprehensions only allowed in Python 3.6 and greater */
2062 if (is_async && c->c_feature_version < 6) {
2063 ast_error(c, n,
2064 "Async comprehensions are only supported in Python 3.6 and greater");
2065 return NULL;
2066 }
2067
Jelle Zijlstraac317702017-10-05 20:24:46 -07002068 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002069 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002070 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002072 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002073 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002075
Thomas Wouters89f507f2006-12-13 04:49:30 +00002076 /* Check the # of children rather than the length of t, since
2077 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002078 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002079 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002080 comp = comprehension(first, expression, NULL,
2081 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002083 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2084 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2085 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002086 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002087 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002089
Jelle Zijlstraac317702017-10-05 20:24:46 -07002090 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 int j, n_ifs;
2092 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093
Jelle Zijlstraac317702017-10-05 20:24:46 -07002094 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002095 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002096 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002098
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002099 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002100 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002104 REQ(n, comp_iter);
2105 n = CHILD(n, 0);
2106 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107
Guido van Rossum992d4a32007-07-11 13:09:30 +00002108 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002109 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002110 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002111 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002112 if (NCH(n) == 3)
2113 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002115 /* on exit, must guarantee that n is a comp_for */
2116 if (TYPE(n) == comp_iter)
2117 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002118 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002120 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002122 return comps;
2123}
2124
2125static expr_ty
2126ast_for_itercomp(struct compiling *c, const node *n, int type)
2127{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002128 /* testlist_comp: (test|star_expr)
2129 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002130 expr_ty elt;
2131 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002132 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133
Guido van Rossum992d4a32007-07-11 13:09:30 +00002134 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002136 ch = CHILD(n, 0);
2137 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002138 if (!elt)
2139 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002140 if (elt->kind == Starred_kind) {
2141 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2142 return NULL;
2143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144
Guido van Rossum992d4a32007-07-11 13:09:30 +00002145 comps = ast_for_comprehension(c, CHILD(n, 1));
2146 if (!comps)
2147 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002148
2149 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002150 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2151 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002152 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002153 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2154 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002155 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002156 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2157 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002158 else
2159 /* Should never happen */
2160 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161}
2162
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002163/* Fills in the key, value pair corresponding to the dict element. In case
2164 * of an unpacking, key is NULL. *i is advanced by the number of ast
2165 * elements. Iff successful, nonzero is returned.
2166 */
2167static int
2168ast_for_dictelement(struct compiling *c, const node *n, int *i,
2169 expr_ty *key, expr_ty *value)
2170{
2171 expr_ty expression;
2172 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2173 assert(NCH(n) - *i >= 2);
2174
2175 expression = ast_for_expr(c, CHILD(n, *i + 1));
2176 if (!expression)
2177 return 0;
2178 *key = NULL;
2179 *value = expression;
2180
2181 *i += 2;
2182 }
2183 else {
2184 assert(NCH(n) - *i >= 3);
2185
2186 expression = ast_for_expr(c, CHILD(n, *i));
2187 if (!expression)
2188 return 0;
2189 *key = expression;
2190
2191 REQ(CHILD(n, *i + 1), COLON);
2192
2193 expression = ast_for_expr(c, CHILD(n, *i + 2));
2194 if (!expression)
2195 return 0;
2196 *value = expression;
2197
2198 *i += 3;
2199 }
2200 return 1;
2201}
2202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002204ast_for_dictcomp(struct compiling *c, const node *n)
2205{
2206 expr_ty key, value;
2207 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002208 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002210 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002211 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002212 assert(key);
2213 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002215 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002216 if (!comps)
2217 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002219 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2220 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002221}
2222
2223static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002224ast_for_dictdisplay(struct compiling *c, const node *n)
2225{
2226 int i;
2227 int j;
2228 int size;
2229 asdl_seq *keys, *values;
2230
2231 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2232 keys = _Py_asdl_seq_new(size, c->c_arena);
2233 if (!keys)
2234 return NULL;
2235
2236 values = _Py_asdl_seq_new(size, c->c_arena);
2237 if (!values)
2238 return NULL;
2239
2240 j = 0;
2241 for (i = 0; i < NCH(n); i++) {
2242 expr_ty key, value;
2243
2244 if (!ast_for_dictelement(c, n, &i, &key, &value))
2245 return NULL;
2246 asdl_seq_SET(keys, j, key);
2247 asdl_seq_SET(values, j, value);
2248
2249 j++;
2250 }
2251 keys->size = j;
2252 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002253 return Dict(keys, values, LINENO(n), n->n_col_offset,
2254 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002255}
2256
2257static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002258ast_for_genexp(struct compiling *c, const node *n)
2259{
2260 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002261 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002262}
2263
2264static expr_ty
2265ast_for_listcomp(struct compiling *c, const node *n)
2266{
2267 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002268 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002269}
2270
2271static expr_ty
2272ast_for_setcomp(struct compiling *c, const node *n)
2273{
2274 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002275 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002276}
2277
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002278static expr_ty
2279ast_for_setdisplay(struct compiling *c, const node *n)
2280{
2281 int i;
2282 int size;
2283 asdl_seq *elts;
2284
2285 assert(TYPE(n) == (dictorsetmaker));
2286 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2287 elts = _Py_asdl_seq_new(size, c->c_arena);
2288 if (!elts)
2289 return NULL;
2290 for (i = 0; i < NCH(n); i += 2) {
2291 expr_ty expression;
2292 expression = ast_for_expr(c, CHILD(n, i));
2293 if (!expression)
2294 return NULL;
2295 asdl_seq_SET(elts, i / 2, expression);
2296 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002297 return Set(elts, LINENO(n), n->n_col_offset,
2298 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002299}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002300
2301static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302ast_for_atom(struct compiling *c, const node *n)
2303{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002304 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2305 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002306 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 */
2308 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002311 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002312 PyObject *name;
2313 const char *s = STR(ch);
2314 size_t len = strlen(s);
2315 if (len >= 4 && len <= 5) {
2316 if (!strcmp(s, "None"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002317 return Constant(Py_None, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002318 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002319 if (!strcmp(s, "True"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002320 return Constant(Py_True, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002321 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002322 if (!strcmp(s, "False"))
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002323 return Constant(Py_False, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002324 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002325 }
2326 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002327 if (!name)
2328 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002329 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002330 return Name(name, Load, LINENO(n), n->n_col_offset,
2331 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002332 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002334 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002335 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002336 const char *errtype = NULL;
2337 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2338 errtype = "unicode error";
2339 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2340 errtype = "value error";
2341 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002342 PyObject *type, *value, *tback, *errstr;
2343 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002344 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002345 if (errstr) {
2346 ast_error(c, n, "(%s) %U", errtype, errstr);
2347 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002348 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002349 else {
2350 PyErr_Clear();
2351 ast_error(c, n, "(%s) unknown error", errtype);
2352 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002353 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002354 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002355 Py_XDECREF(tback);
2356 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002357 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002358 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002359 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 }
2361 case NUMBER: {
Guido van Rossum495da292019-03-07 12:38:08 -08002362 PyObject *pynum;
2363 /* Underscores in numeric literals are only allowed in Python 3.6 or greater */
2364 /* Check for underscores here rather than in parse_number so we can report a line number on error */
2365 if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) {
2366 ast_error(c, ch,
2367 "Underscores in numeric literals are only supported in Python 3.6 and greater");
2368 return NULL;
2369 }
2370 pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002371 if (!pynum)
2372 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002373
Victor Stinner43d81952013-07-17 00:57:58 +02002374 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2375 Py_DECREF(pynum);
2376 return NULL;
2377 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002378 return Constant(pynum, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002379 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 }
Georg Brandldde00282007-03-18 19:01:53 +00002381 case ELLIPSIS: /* Ellipsis */
Guido van Rossum10f8ce62019-03-13 13:00:46 -07002382 return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002383 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002385 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386
Thomas Wouters89f507f2006-12-13 04:49:30 +00002387 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002388 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2389 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390
Thomas Wouters89f507f2006-12-13 04:49:30 +00002391 if (TYPE(ch) == yield_expr)
2392 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002395 if (NCH(ch) == 1) {
2396 return ast_for_testlist(c, ch);
2397 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002398
Serhiy Storchakab619b092018-11-27 09:40:29 +02002399 if (TYPE(CHILD(ch, 1)) == comp_for) {
2400 return copy_location(ast_for_genexp(c, ch), n);
2401 }
2402 else {
2403 return copy_location(ast_for_testlist(c, ch), n);
2404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002406 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407
Thomas Wouters89f507f2006-12-13 04:49:30 +00002408 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002409 return List(NULL, Load, LINENO(n), n->n_col_offset,
2410 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411
Nick Coghlan650f0d02007-04-15 12:05:43 +00002412 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002413 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2414 asdl_seq *elts = seq_for_testlist(c, ch);
2415 if (!elts)
2416 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002417
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002418 return List(elts, Load, LINENO(n), n->n_col_offset,
2419 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002420 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002421 else {
2422 return copy_location(ast_for_listcomp(c, ch), n);
2423 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002425 /* dictorsetmaker: ( ((test ':' test | '**' test)
2426 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2427 * ((test | '*' test)
2428 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002429 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002430 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002431 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002432 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002433 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2434 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002435 }
2436 else {
2437 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2438 if (NCH(ch) == 1 ||
2439 (NCH(ch) > 1 &&
2440 TYPE(CHILD(ch, 1)) == COMMA)) {
2441 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002442 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002443 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002444 else if (NCH(ch) > 1 &&
2445 TYPE(CHILD(ch, 1)) == comp_for) {
2446 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002447 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002448 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002449 else if (NCH(ch) > 3 - is_dict &&
2450 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2451 /* It's a dictionary comprehension. */
2452 if (is_dict) {
Guido van Rossum495da292019-03-07 12:38:08 -08002453 ast_error(c, n,
2454 "dict unpacking cannot be used in dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002455 return NULL;
2456 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002457 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002458 }
2459 else {
2460 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002461 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002462 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002463 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002467 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2468 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 }
2470}
2471
2472static slice_ty
2473ast_for_slice(struct compiling *c, const node *n)
2474{
2475 node *ch;
2476 expr_ty lower = NULL, upper = NULL, step = NULL;
2477
2478 REQ(n, subscript);
2479
2480 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002481 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 sliceop: ':' [test]
2483 */
2484 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 if (NCH(n) == 1 && TYPE(ch) == test) {
2486 /* 'step' variable hold no significance in terms of being used over
2487 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 if (!step)
2490 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491
Thomas Wouters89f507f2006-12-13 04:49:30 +00002492 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 }
2494
2495 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002496 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 if (!lower)
2498 return NULL;
2499 }
2500
2501 /* If there's an upper bound it's in the second or third position. */
2502 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002503 if (NCH(n) > 1) {
2504 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505
Thomas Wouters89f507f2006-12-13 04:49:30 +00002506 if (TYPE(n2) == test) {
2507 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 if (!upper)
2509 return NULL;
2510 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002513 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514
Thomas Wouters89f507f2006-12-13 04:49:30 +00002515 if (TYPE(n2) == test) {
2516 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 if (!upper)
2518 return NULL;
2519 }
2520 }
2521
2522 ch = CHILD(n, NCH(n) - 1);
2523 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002524 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002525 ch = CHILD(ch, 1);
2526 if (TYPE(ch) == test) {
2527 step = ast_for_expr(c, ch);
2528 if (!step)
2529 return NULL;
2530 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 }
2532 }
2533
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002534 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535}
2536
2537static expr_ty
2538ast_for_binop(struct compiling *c, const node *n)
2539{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002540 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002542 BinOp(BinOp(A, op, B), op, C).
2543 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544
Guido van Rossumd8faa362007-04-27 19:54:29 +00002545 int i, nops;
2546 expr_ty expr1, expr2, result;
2547 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548
Guido van Rossumd8faa362007-04-27 19:54:29 +00002549 expr1 = ast_for_expr(c, CHILD(n, 0));
2550 if (!expr1)
2551 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552
Guido van Rossumd8faa362007-04-27 19:54:29 +00002553 expr2 = ast_for_expr(c, CHILD(n, 2));
2554 if (!expr2)
2555 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556
Guido van Rossum495da292019-03-07 12:38:08 -08002557 newoperator = get_operator(c, CHILD(n, 1));
Guido van Rossumd8faa362007-04-27 19:54:29 +00002558 if (!newoperator)
2559 return NULL;
2560
2561 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002562 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002563 c->c_arena);
2564 if (!result)
2565 return NULL;
2566
2567 nops = (NCH(n) - 1) / 2;
2568 for (i = 1; i < nops; i++) {
2569 expr_ty tmp_result, tmp;
2570 const node* next_oper = CHILD(n, i * 2 + 1);
2571
Guido van Rossum495da292019-03-07 12:38:08 -08002572 newoperator = get_operator(c, next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002573 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 return NULL;
2575
Guido van Rossumd8faa362007-04-27 19:54:29 +00002576 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2577 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 return NULL;
2579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002581 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002582 CHILD(n, i * 2 + 2)->n_end_lineno,
2583 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002584 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002586 return NULL;
2587 result = tmp_result;
2588 }
2589 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590}
2591
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002592static expr_ty
2593ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002596 subscriptlist: subscript (',' subscript)* [',']
2597 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2598 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002599 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002600 REQ(n, trailer);
2601 if (TYPE(CHILD(n, 0)) == LPAR) {
2602 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002603 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2604 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002605 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002606 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002607 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002608 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002609 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2610 if (!attr_id)
2611 return NULL;
2612 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002613 LINENO(n), n->n_col_offset,
2614 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002615 }
2616 else {
2617 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002618 REQ(CHILD(n, 2), RSQB);
2619 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002620 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002621 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2622 if (!slc)
2623 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002624 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002625 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002626 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002627 }
2628 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002630 by treating the sequence as a tuple literal if there are
2631 no slice features.
2632 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002633 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002634 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002635 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002636 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002637 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002638 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002639 if (!slices)
2640 return NULL;
2641 for (j = 0; j < NCH(n); j += 2) {
2642 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002643 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002644 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002645 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002646 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002647 asdl_seq_SET(slices, j / 2, slc);
2648 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002649 if (!simple) {
2650 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002651 Load, LINENO(n), n->n_col_offset,
2652 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002653 }
2654 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002655 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002656 if (!elts)
2657 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002658 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2659 slc = (slice_ty)asdl_seq_GET(slices, j);
2660 assert(slc->kind == Index_kind && slc->v.Index.value);
2661 asdl_seq_SET(elts, j, slc->v.Index.value);
2662 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002663 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2664 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002665 if (!e)
2666 return NULL;
2667 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002668 Load, LINENO(n), n->n_col_offset,
2669 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002670 }
2671 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002672}
2673
2674static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002675ast_for_factor(struct compiling *c, const node *n)
2676{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002677 expr_ty expression;
2678
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002679 expression = ast_for_expr(c, CHILD(n, 1));
2680 if (!expression)
2681 return NULL;
2682
2683 switch (TYPE(CHILD(n, 0))) {
2684 case PLUS:
2685 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002686 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002687 c->c_arena);
2688 case MINUS:
2689 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002690 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002691 c->c_arena);
2692 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002693 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2694 n->n_end_lineno, n->n_end_col_offset,
2695 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002696 }
2697 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2698 TYPE(CHILD(n, 0)));
2699 return NULL;
2700}
2701
2702static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002703ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002704{
Yury Selivanov75445082015-05-11 22:57:16 -04002705 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002706 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002707
2708 REQ(n, atom_expr);
2709 nch = NCH(n);
2710
Guido van Rossum495da292019-03-07 12:38:08 -08002711 if (TYPE(CHILD(n, 0)) == AWAIT) {
2712 if (c->c_feature_version < 5) {
2713 ast_error(c, n,
2714 "Await expressions are only supported in Python 3.5 and greater");
2715 return NULL;
2716 }
Yury Selivanov75445082015-05-11 22:57:16 -04002717 start = 1;
2718 assert(nch > 1);
2719 }
2720
2721 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002722 if (!e)
2723 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002724 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002725 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002726 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002727 return Await(e, LINENO(n), n->n_col_offset,
2728 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002729 }
2730
2731 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002732 node *ch = CHILD(n, i);
2733 if (TYPE(ch) != trailer)
2734 break;
2735 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002736 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002737 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002738 tmp->lineno = e->lineno;
2739 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002740 e = tmp;
2741 }
Yury Selivanov75445082015-05-11 22:57:16 -04002742
2743 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002744 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002745 return Await(e, LINENO(n), n->n_col_offset,
2746 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002747 }
2748 else {
2749 return e;
2750 }
2751}
2752
2753static expr_ty
2754ast_for_power(struct compiling *c, const node *n)
2755{
2756 /* power: atom trailer* ('**' factor)*
2757 */
2758 expr_ty e;
2759 REQ(n, power);
2760 e = ast_for_atom_expr(c, CHILD(n, 0));
2761 if (!e)
2762 return NULL;
2763 if (NCH(n) == 1)
2764 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002765 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2766 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002767 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002768 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002769 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2770 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002771 }
2772 return e;
2773}
2774
Guido van Rossum0368b722007-05-11 16:50:42 +00002775static expr_ty
2776ast_for_starred(struct compiling *c, const node *n)
2777{
2778 expr_ty tmp;
2779 REQ(n, star_expr);
2780
2781 tmp = ast_for_expr(c, CHILD(n, 1));
2782 if (!tmp)
2783 return NULL;
2784
2785 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002786 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2787 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002788}
2789
2790
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791/* Do not name a variable 'expr'! Will cause a compile error.
2792*/
2793
2794static expr_ty
2795ast_for_expr(struct compiling *c, const node *n)
2796{
2797 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002798 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002799 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002800 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 and_test: not_test ('and' not_test)*
2803 not_test: 'not' not_test | comparison
2804 comparison: expr (comp_op expr)*
2805 expr: xor_expr ('|' xor_expr)*
2806 xor_expr: and_expr ('^' and_expr)*
2807 and_expr: shift_expr ('&' shift_expr)*
2808 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2809 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002810 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002812 power: atom_expr ['**' factor]
Guido van Rossum495da292019-03-07 12:38:08 -08002813 atom_expr: [AWAIT] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002814 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 */
2816
2817 asdl_seq *seq;
2818 int i;
2819
2820 loop:
2821 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002822 case namedexpr_test:
2823 if (NCH(n) == 3)
2824 return ast_for_namedexpr(c, n);
2825 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002827 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002828 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002829 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002831 else if (NCH(n) > 1)
2832 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002833 /* Fallthrough */
2834 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 case and_test:
2836 if (NCH(n) == 1) {
2837 n = CHILD(n, 0);
2838 goto loop;
2839 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002840 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 if (!seq)
2842 return NULL;
2843 for (i = 0; i < NCH(n); i += 2) {
2844 expr_ty e = ast_for_expr(c, CHILD(n, i));
2845 if (!e)
2846 return NULL;
2847 asdl_seq_SET(seq, i / 2, e);
2848 }
2849 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002850 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002851 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002852 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002853 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002854 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2855 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 case not_test:
2857 if (NCH(n) == 1) {
2858 n = CHILD(n, 0);
2859 goto loop;
2860 }
2861 else {
2862 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2863 if (!expression)
2864 return NULL;
2865
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002866 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002867 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002868 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 }
2870 case comparison:
2871 if (NCH(n) == 1) {
2872 n = CHILD(n, 0);
2873 goto loop;
2874 }
2875 else {
2876 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002877 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002878 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002879 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 if (!ops)
2881 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002882 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 return NULL;
2885 }
2886 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002887 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002889 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002890 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002892 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893
2894 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002895 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002899 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 asdl_seq_SET(cmps, i / 2, expression);
2901 }
2902 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002903 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002907 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2908 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 }
2910 break;
2911
Guido van Rossum0368b722007-05-11 16:50:42 +00002912 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 /* The next five cases all handle BinOps. The main body of code
2915 is the same in each case, but the switch turned inside out to
2916 reuse the code for each type of operator.
2917 */
2918 case expr:
2919 case xor_expr:
2920 case and_expr:
2921 case shift_expr:
2922 case arith_expr:
2923 case term:
2924 if (NCH(n) == 1) {
2925 n = CHILD(n, 0);
2926 goto loop;
2927 }
2928 return ast_for_binop(c, n);
2929 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002930 node *an = NULL;
2931 node *en = NULL;
2932 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002933 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002934 if (NCH(n) > 1)
2935 an = CHILD(n, 1); /* yield_arg */
2936 if (an) {
2937 en = CHILD(an, NCH(an) - 1);
2938 if (NCH(an) == 2) {
2939 is_from = 1;
2940 exp = ast_for_expr(c, en);
2941 }
2942 else
2943 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002944 if (!exp)
2945 return NULL;
2946 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002947 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002948 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2949 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2950 return Yield(exp, LINENO(n), n->n_col_offset,
2951 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002952 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002953 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954 if (NCH(n) == 1) {
2955 n = CHILD(n, 0);
2956 goto loop;
2957 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002958 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002959 case power:
2960 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002962 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 return NULL;
2964 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002965 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 return NULL;
2967}
2968
2969static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002970ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002971 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972{
2973 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002974 arglist: argument (',' argument)* [',']
2975 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976 */
2977
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002978 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002979 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002980 asdl_seq *args;
2981 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982
2983 REQ(n, arglist);
2984
2985 nargs = 0;
2986 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002988 node *ch = CHILD(n, i);
2989 if (TYPE(ch) == argument) {
2990 if (NCH(ch) == 1)
2991 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002992 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2993 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002994 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002995 ast_error(c, ch, "invalid syntax");
2996 return NULL;
2997 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002998 if (NCH(n) > 1) {
2999 ast_error(c, ch, "Generator expression must be parenthesized");
3000 return NULL;
3001 }
3002 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003003 else if (TYPE(CHILD(ch, 0)) == STAR)
3004 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003005 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3006 nargs++;
3007 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003009 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003010 nkeywords++;
3011 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013
Serhiy Storchaka9165f772017-11-15 08:49:40 +02003014 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003016 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003017 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003019 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003020
3021 nargs = 0; /* positional arguments + iterable argument unpackings */
3022 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3023 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003025 node *ch = CHILD(n, i);
3026 if (TYPE(ch) == argument) {
3027 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003028 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003029 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003030 /* a positional argument */
3031 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003032 if (ndoublestars) {
3033 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003034 "positional argument follows "
3035 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003036 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003037 else {
3038 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003039 "positional argument follows "
3040 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003041 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003042 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003043 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003044 e = ast_for_expr(c, chch);
3045 if (!e)
3046 return NULL;
3047 asdl_seq_SET(args, nargs++, e);
3048 }
3049 else if (TYPE(chch) == STAR) {
3050 /* an iterable argument unpacking */
3051 expr_ty starred;
3052 if (ndoublestars) {
3053 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003054 "iterable argument unpacking follows "
3055 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003056 return NULL;
3057 }
3058 e = ast_for_expr(c, CHILD(ch, 1));
3059 if (!e)
3060 return NULL;
3061 starred = Starred(e, Load, LINENO(chch),
3062 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003063 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003064 c->c_arena);
3065 if (!starred)
3066 return NULL;
3067 asdl_seq_SET(args, nargs++, starred);
3068
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003069 }
3070 else if (TYPE(chch) == DOUBLESTAR) {
3071 /* a keyword argument unpacking */
3072 keyword_ty kw;
3073 i++;
3074 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003076 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003077 kw = keyword(NULL, e, c->c_arena);
3078 asdl_seq_SET(keywords, nkeywords++, kw);
3079 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003081 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003082 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003083 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003085 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003086 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003088 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3089 /* treat colon equal as positional argument */
3090 if (nkeywords) {
3091 if (ndoublestars) {
3092 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003093 "positional argument follows "
3094 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003095 }
3096 else {
3097 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003098 "positional argument follows "
3099 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003100 }
3101 return NULL;
3102 }
3103 e = ast_for_namedexpr(c, ch);
3104 if (!e)
3105 return NULL;
3106 asdl_seq_SET(args, nargs++, e);
3107 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003108 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003109 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003110 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003111 identifier key, tmp;
3112 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003114 // To remain LL(1), the grammar accepts any test (basically, any
3115 // expression) in the keyword slot of a call site. So, we need
3116 // to manually enforce that the keyword is a NAME here.
3117 static const int name_tree[] = {
3118 test,
3119 or_test,
3120 and_test,
3121 not_test,
3122 comparison,
3123 expr,
3124 xor_expr,
3125 and_expr,
3126 shift_expr,
3127 arith_expr,
3128 term,
3129 factor,
3130 power,
3131 atom_expr,
3132 atom,
3133 0,
3134 };
3135 node *expr_node = chch;
3136 for (int i = 0; name_tree[i]; i++) {
3137 if (TYPE(expr_node) != name_tree[i])
3138 break;
3139 if (NCH(expr_node) != 1)
3140 break;
3141 expr_node = CHILD(expr_node, 0);
3142 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003143 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003144 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003145 "expression cannot contain assignment, "
3146 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003147 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003148 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003149 key = new_identifier(STR(expr_node), c);
3150 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003151 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003153 if (forbidden_name(c, key, chch, 1)) {
3154 return NULL;
3155 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003156 for (k = 0; k < nkeywords; k++) {
3157 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003158 if (tmp && !PyUnicode_Compare(tmp, key)) {
3159 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003160 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003161 return NULL;
3162 }
3163 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003164 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003166 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003167 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003169 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003170 asdl_seq_SET(keywords, nkeywords++, kw);
3171 }
3172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173 }
3174
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003175 return Call(func, args, keywords, func->lineno, func->col_offset,
3176 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177}
3178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003180ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003182 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003183 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003185 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003186 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003187 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003188 }
3189 else {
3190 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003191 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003194 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 else {
3196 asdl_seq *tmp = seq_for_testlist(c, n);
3197 if (!tmp)
3198 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003199 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3200 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003202}
3203
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003204static stmt_ty
3205ast_for_expr_stmt(struct compiling *c, const node *n)
3206{
3207 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003208 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003209 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3210 annassign: ':' test ['=' (yield_expr|testlist)]
3211 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3212 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3213 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003214 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003216 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003218 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003219 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 if (!e)
3221 return NULL;
3222
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003223 return Expr(e, LINENO(n), n->n_col_offset,
3224 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225 }
3226 else if (TYPE(CHILD(n, 1)) == augassign) {
3227 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003228 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003229 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230
Thomas Wouters89f507f2006-12-13 04:49:30 +00003231 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 if (!expr1)
3233 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003234 if(!set_context(c, expr1, Store, ch))
3235 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003236 /* set_context checks that most expressions are not the left side.
3237 Augmented assignments can only have a name, a subscript, or an
3238 attribute on the left, though, so we have to explicitly check for
3239 those. */
3240 switch (expr1->kind) {
3241 case Name_kind:
3242 case Attribute_kind:
3243 case Subscript_kind:
3244 break;
3245 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003246 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003247 return NULL;
3248 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249
Thomas Wouters89f507f2006-12-13 04:49:30 +00003250 ch = CHILD(n, 2);
3251 if (TYPE(ch) == testlist)
3252 expr2 = ast_for_testlist(c, ch);
3253 else
3254 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003255 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 return NULL;
3257
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003258 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003259 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 return NULL;
3261
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003262 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3263 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003265 else if (TYPE(CHILD(n, 1)) == annassign) {
3266 expr_ty expr1, expr2, expr3;
3267 node *ch = CHILD(n, 0);
3268 node *deep, *ann = CHILD(n, 1);
3269 int simple = 1;
3270
Guido van Rossum495da292019-03-07 12:38:08 -08003271 /* AnnAssigns are only allowed in Python 3.6 or greater */
3272 if (c->c_feature_version < 6) {
3273 ast_error(c, ch,
3274 "Variable annotation syntax is only supported in Python 3.6 and greater");
3275 return NULL;
3276 }
3277
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003278 /* we keep track of parens to qualify (x) as expression not name */
3279 deep = ch;
3280 while (NCH(deep) == 1) {
3281 deep = CHILD(deep, 0);
3282 }
3283 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3284 simple = 0;
3285 }
3286 expr1 = ast_for_testlist(c, ch);
3287 if (!expr1) {
3288 return NULL;
3289 }
3290 switch (expr1->kind) {
3291 case Name_kind:
3292 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3293 return NULL;
3294 }
3295 expr1->v.Name.ctx = Store;
3296 break;
3297 case Attribute_kind:
3298 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3299 return NULL;
3300 }
3301 expr1->v.Attribute.ctx = Store;
3302 break;
3303 case Subscript_kind:
3304 expr1->v.Subscript.ctx = Store;
3305 break;
3306 case List_kind:
3307 ast_error(c, ch,
3308 "only single target (not list) can be annotated");
3309 return NULL;
3310 case Tuple_kind:
3311 ast_error(c, ch,
3312 "only single target (not tuple) can be annotated");
3313 return NULL;
3314 default:
3315 ast_error(c, ch,
3316 "illegal target for annotation");
3317 return NULL;
3318 }
3319
3320 if (expr1->kind != Name_kind) {
3321 simple = 0;
3322 }
3323 ch = CHILD(ann, 1);
3324 expr2 = ast_for_expr(c, ch);
3325 if (!expr2) {
3326 return NULL;
3327 }
3328 if (NCH(ann) == 2) {
3329 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003330 LINENO(n), n->n_col_offset,
3331 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003332 }
3333 else {
3334 ch = CHILD(ann, 3);
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003335 if (TYPE(ch) == testlist) {
3336 expr3 = ast_for_testlist(c, ch);
3337 }
3338 else {
3339 expr3 = ast_for_expr(c, ch);
3340 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003341 if (!expr3) {
3342 return NULL;
3343 }
3344 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003345 LINENO(n), n->n_col_offset,
3346 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003347 }
3348 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003350 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003351 asdl_seq *targets;
3352 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003354 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355
Thomas Wouters89f507f2006-12-13 04:49:30 +00003356 /* a normal assignment */
3357 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003358
3359 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3360 nch_minus_type = num - has_type_comment;
3361
3362 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003363 if (!targets)
3364 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003365 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003366 expr_ty e;
3367 node *ch = CHILD(n, i);
3368 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003369 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003370 return NULL;
3371 }
3372 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003374 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003376 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003377 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003378 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379
Thomas Wouters89f507f2006-12-13 04:49:30 +00003380 asdl_seq_SET(targets, i / 2, e);
3381 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003382 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003383 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003384 expression = ast_for_testlist(c, value);
3385 else
3386 expression = ast_for_expr(c, value);
3387 if (!expression)
3388 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003389 if (has_type_comment) {
3390 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3391 if (!type_comment)
3392 return NULL;
3393 }
3394 else
3395 type_comment = NULL;
3396 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003397 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399}
3400
Benjamin Peterson78565b22009-06-28 19:19:51 +00003401
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003403ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404{
3405 asdl_seq *seq;
3406 int i;
3407 expr_ty e;
3408
3409 REQ(n, exprlist);
3410
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003411 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003413 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003415 e = ast_for_expr(c, CHILD(n, i));
3416 if (!e)
3417 return NULL;
3418 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003419 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003420 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 }
3422 return seq;
3423}
3424
3425static stmt_ty
3426ast_for_del_stmt(struct compiling *c, const node *n)
3427{
3428 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 /* del_stmt: 'del' exprlist */
3431 REQ(n, del_stmt);
3432
3433 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3434 if (!expr_list)
3435 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003436 return Delete(expr_list, LINENO(n), n->n_col_offset,
3437 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438}
3439
3440static stmt_ty
3441ast_for_flow_stmt(struct compiling *c, const node *n)
3442{
3443 /*
3444 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3445 | yield_stmt
3446 break_stmt: 'break'
3447 continue_stmt: 'continue'
3448 return_stmt: 'return' [testlist]
3449 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003450 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451 raise_stmt: 'raise' [test [',' test [',' test]]]
3452 */
3453 node *ch;
3454
3455 REQ(n, flow_stmt);
3456 ch = CHILD(n, 0);
3457 switch (TYPE(ch)) {
3458 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003459 return Break(LINENO(n), n->n_col_offset,
3460 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003462 return Continue(LINENO(n), n->n_col_offset,
3463 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003465 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3466 if (!exp)
3467 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003468 return Expr(exp, LINENO(n), n->n_col_offset,
3469 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 }
3471 case return_stmt:
3472 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003473 return Return(NULL, LINENO(n), n->n_col_offset,
3474 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003476 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 if (!expression)
3478 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003479 return Return(expression, LINENO(n), n->n_col_offset,
3480 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 }
3482 case raise_stmt:
3483 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003484 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3485 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003486 else if (NCH(ch) >= 2) {
3487 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3489 if (!expression)
3490 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003491 if (NCH(ch) == 4) {
3492 cause = ast_for_expr(c, CHILD(ch, 3));
3493 if (!cause)
3494 return NULL;
3495 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003496 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3497 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 }
Stefan Krahf432a322017-08-21 13:09:59 +02003499 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003501 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502 "unexpected flow_stmt: %d", TYPE(ch));
3503 return NULL;
3504 }
3505}
3506
3507static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003508alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509{
3510 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003511 import_as_name: NAME ['as' NAME]
3512 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 dotted_name: NAME ('.' NAME)*
3514 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003515 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517 loop:
3518 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003519 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003520 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003521 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003522 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003523 if (!name)
3524 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003525 if (NCH(n) == 3) {
3526 node *str_node = CHILD(n, 2);
3527 str = NEW_IDENTIFIER(str_node);
3528 if (!str)
3529 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003530 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003531 return NULL;
3532 }
3533 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003534 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003535 return NULL;
3536 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003537 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003538 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 case dotted_as_name:
3540 if (NCH(n) == 1) {
3541 n = CHILD(n, 0);
3542 goto loop;
3543 }
3544 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003545 node *asname_node = CHILD(n, 2);
3546 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003547 if (!a)
3548 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003550 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003551 if (!a->asname)
3552 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003553 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003554 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 return a;
3556 }
3557 break;
3558 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003559 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003560 node *name_node = CHILD(n, 0);
3561 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003562 if (!name)
3563 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003564 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003565 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003566 return alias(name, NULL, c->c_arena);
3567 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568 else {
3569 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003570 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003571 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574
3575 len = 0;
3576 for (i = 0; i < NCH(n); i += 2)
3577 /* length of string plus one for the dot */
3578 len += strlen(STR(CHILD(n, i))) + 1;
3579 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003580 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 if (!str)
3582 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003583 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 if (!s)
3585 return NULL;
3586 for (i = 0; i < NCH(n); i += 2) {
3587 char *sch = STR(CHILD(n, i));
3588 strcpy(s, STR(CHILD(n, i)));
3589 s += strlen(sch);
3590 *s++ = '.';
3591 }
3592 --s;
3593 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3595 PyBytes_GET_SIZE(str),
3596 NULL);
3597 Py_DECREF(str);
3598 if (!uni)
3599 return NULL;
3600 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003601 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003602 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3603 Py_DECREF(str);
3604 return NULL;
3605 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003606 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 }
3608 break;
3609 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003610 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003611 if (!str)
3612 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003613 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3614 Py_DECREF(str);
3615 return NULL;
3616 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003617 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003619 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 "unexpected import name: %d", TYPE(n));
3621 return NULL;
3622 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003623
3624 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return NULL;
3626}
3627
3628static stmt_ty
3629ast_for_import_stmt(struct compiling *c, const node *n)
3630{
3631 /*
3632 import_stmt: import_name | import_from
3633 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003634 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3635 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003637 int lineno;
3638 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003639 int i;
3640 asdl_seq *aliases;
3641
3642 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003643 lineno = LINENO(n);
3644 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003646 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003648 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003649 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003650 if (!aliases)
3651 return NULL;
3652 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003653 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003654 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003656 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003657 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003658 // Even though n is modified above, the end position is not changed
3659 return Import(aliases, lineno, col_offset,
3660 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003662 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003664 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003665 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003666 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003667 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003669 /* Count the number of dots (for relative imports) and check for the
3670 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003671 for (idx = 1; idx < NCH(n); idx++) {
3672 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003673 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3674 if (!mod)
3675 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003676 idx++;
3677 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003678 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003680 ndots += 3;
3681 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003682 } else if (TYPE(CHILD(n, idx)) != DOT) {
3683 break;
3684 }
3685 ndots++;
3686 }
3687 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003688 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003689 case STAR:
3690 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003691 n = CHILD(n, idx);
3692 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003693 break;
3694 case LPAR:
3695 /* from ... import (x, y, z) */
3696 n = CHILD(n, idx + 1);
3697 n_children = NCH(n);
3698 break;
3699 case import_as_names:
3700 /* from ... import x, y, z */
3701 n = CHILD(n, idx);
3702 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003703 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003704 ast_error(c, n,
3705 "trailing comma not allowed without"
3706 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707 return NULL;
3708 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003709 break;
3710 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003711 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003712 return NULL;
3713 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003715 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003716 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718
3719 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003720 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003721 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003722 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003724 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003726 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003727 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003728 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003729 if (!import_alias)
3730 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003731 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003732 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003734 if (mod != NULL)
3735 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003736 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003737 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003738 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739 }
Neal Norwitz79792652005-11-14 04:25:03 +00003740 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 "unknown import statement: starts with command '%s'",
3742 STR(CHILD(n, 0)));
3743 return NULL;
3744}
3745
3746static stmt_ty
3747ast_for_global_stmt(struct compiling *c, const node *n)
3748{
3749 /* global_stmt: 'global' NAME (',' NAME)* */
3750 identifier name;
3751 asdl_seq *s;
3752 int i;
3753
3754 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003755 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003757 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003759 name = NEW_IDENTIFIER(CHILD(n, i));
3760 if (!name)
3761 return NULL;
3762 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003764 return Global(s, LINENO(n), n->n_col_offset,
3765 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766}
3767
3768static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003769ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3770{
3771 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3772 identifier name;
3773 asdl_seq *s;
3774 int i;
3775
3776 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003777 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003778 if (!s)
3779 return NULL;
3780 for (i = 1; i < NCH(n); i += 2) {
3781 name = NEW_IDENTIFIER(CHILD(n, i));
3782 if (!name)
3783 return NULL;
3784 asdl_seq_SET(s, i / 2, name);
3785 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003786 return Nonlocal(s, LINENO(n), n->n_col_offset,
3787 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003788}
3789
3790static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791ast_for_assert_stmt(struct compiling *c, const node *n)
3792{
3793 /* assert_stmt: 'assert' test [',' test] */
3794 REQ(n, assert_stmt);
3795 if (NCH(n) == 2) {
3796 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3797 if (!expression)
3798 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003799 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3800 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 }
3802 else if (NCH(n) == 4) {
3803 expr_ty expr1, expr2;
3804
3805 expr1 = ast_for_expr(c, CHILD(n, 1));
3806 if (!expr1)
3807 return NULL;
3808 expr2 = ast_for_expr(c, CHILD(n, 3));
3809 if (!expr2)
3810 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003812 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3813 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814 }
Neal Norwitz79792652005-11-14 04:25:03 +00003815 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 "improper number of parts to 'assert' statement: %d",
3817 NCH(n));
3818 return NULL;
3819}
3820
3821static asdl_seq *
3822ast_for_suite(struct compiling *c, const node *n)
3823{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003824 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003825 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 stmt_ty s;
3827 int i, total, num, end, pos = 0;
3828 node *ch;
3829
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003830 if (TYPE(n) != func_body_suite) {
3831 REQ(n, suite);
3832 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833
3834 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003835 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003837 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003839 n = CHILD(n, 0);
3840 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003842 */
3843 end = NCH(n) - 1;
3844 if (TYPE(CHILD(n, end - 1)) == SEMI)
3845 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003847 for (i = 0; i < end; i += 2) {
3848 ch = CHILD(n, i);
3849 s = ast_for_stmt(c, ch);
3850 if (!s)
3851 return NULL;
3852 asdl_seq_SET(seq, pos++, s);
3853 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854 }
3855 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003856 i = 2;
3857 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3858 i += 2;
3859 REQ(CHILD(n, 2), NEWLINE);
3860 }
3861
3862 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003863 ch = CHILD(n, i);
3864 REQ(ch, stmt);
3865 num = num_stmts(ch);
3866 if (num == 1) {
3867 /* small_stmt or compound_stmt with only one child */
3868 s = ast_for_stmt(c, ch);
3869 if (!s)
3870 return NULL;
3871 asdl_seq_SET(seq, pos++, s);
3872 }
3873 else {
3874 int j;
3875 ch = CHILD(ch, 0);
3876 REQ(ch, simple_stmt);
3877 for (j = 0; j < NCH(ch); j += 2) {
3878 /* statement terminates with a semi-colon ';' */
3879 if (NCH(CHILD(ch, j)) == 0) {
3880 assert((j + 1) == NCH(ch));
3881 break;
3882 }
3883 s = ast_for_stmt(c, CHILD(ch, j));
3884 if (!s)
3885 return NULL;
3886 asdl_seq_SET(seq, pos++, s);
3887 }
3888 }
3889 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003890 }
3891 assert(pos == seq->size);
3892 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893}
3894
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003895static void
3896get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3897{
Pablo Galindo46a97922019-02-19 22:51:53 +00003898 Py_ssize_t tot = asdl_seq_LEN(s);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003899 // There must be no empty suites.
3900 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003901 stmt_ty last = asdl_seq_GET(s, tot - 1);
3902 *end_lineno = last->end_lineno;
3903 *end_col_offset = last->end_col_offset;
3904}
3905
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906static stmt_ty
3907ast_for_if_stmt(struct compiling *c, const node *n)
3908{
3909 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3910 ['else' ':' suite]
3911 */
3912 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003913 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914
3915 REQ(n, if_stmt);
3916
3917 if (NCH(n) == 4) {
3918 expr_ty expression;
3919 asdl_seq *suite_seq;
3920
3921 expression = ast_for_expr(c, CHILD(n, 1));
3922 if (!expression)
3923 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003925 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003927 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928
Guido van Rossumd8faa362007-04-27 19:54:29 +00003929 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003930 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003932
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 s = STR(CHILD(n, 4));
3934 /* s[2], the third character in the string, will be
3935 's' for el_s_e, or
3936 'i' for el_i_f
3937 */
3938 if (s[2] == 's') {
3939 expr_ty expression;
3940 asdl_seq *seq1, *seq2;
3941
3942 expression = ast_for_expr(c, CHILD(n, 1));
3943 if (!expression)
3944 return NULL;
3945 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003946 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003947 return NULL;
3948 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003949 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003951 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952
Guido van Rossumd8faa362007-04-27 19:54:29 +00003953 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003954 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 }
3956 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003957 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003958 expr_ty expression;
3959 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003960 asdl_seq *orelse = NULL;
3961 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962 /* must reference the child n_elif+1 since 'else' token is third,
3963 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003964 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3965 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3966 has_else = 1;
3967 n_elif -= 3;
3968 }
3969 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970
Thomas Wouters89f507f2006-12-13 04:49:30 +00003971 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003972 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003974 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003975 if (!orelse)
3976 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003978 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003980 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3981 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003983 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3984 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003986 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 asdl_seq_SET(orelse, 0,
3989 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003990 LINENO(CHILD(n, NCH(n) - 6)),
3991 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003992 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003993 /* the just-created orelse handled the last elif */
3994 n_elif--;
3995 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003996
Thomas Wouters89f507f2006-12-13 04:49:30 +00003997 for (i = 0; i < n_elif; i++) {
3998 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003999 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004000 if (!newobj)
4001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004003 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004006 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004009 if (orelse != NULL) {
4010 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4011 } else {
4012 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4013 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004014 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00004016 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004017 CHILD(n, off)->n_col_offset,
4018 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004019 orelse = newobj;
4020 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004021 expression = ast_for_expr(c, CHILD(n, 1));
4022 if (!expression)
4023 return NULL;
4024 suite_seq = ast_for_suite(c, CHILD(n, 3));
4025 if (!suite_seq)
4026 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004027 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004028 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004029 LINENO(n), n->n_col_offset,
4030 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004032
4033 PyErr_Format(PyExc_SystemError,
4034 "unexpected token in 'if' statement: %s", s);
4035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036}
4037
4038static stmt_ty
4039ast_for_while_stmt(struct compiling *c, const node *n)
4040{
4041 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4042 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004043 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004044
4045 if (NCH(n) == 4) {
4046 expr_ty expression;
4047 asdl_seq *suite_seq;
4048
4049 expression = ast_for_expr(c, CHILD(n, 1));
4050 if (!expression)
4051 return NULL;
4052 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004053 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004055 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4056 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4057 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004058 }
4059 else if (NCH(n) == 7) {
4060 expr_ty expression;
4061 asdl_seq *seq1, *seq2;
4062
4063 expression = ast_for_expr(c, CHILD(n, 1));
4064 if (!expression)
4065 return NULL;
4066 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004067 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 return NULL;
4069 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004070 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004072 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004074 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4075 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004077
4078 PyErr_Format(PyExc_SystemError,
4079 "wrong number of tokens for 'while' statement: %d",
4080 NCH(n));
4081 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082}
4083
4084static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004085ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086{
guoci90fc8982018-09-11 17:45:45 -04004087 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004088 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004089 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004090 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004091 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004092 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004093 int has_type_comment;
4094 string type_comment;
Guido van Rossum495da292019-03-07 12:38:08 -08004095
4096 if (is_async && c->c_feature_version < 5) {
4097 ast_error(c, n,
4098 "Async for loops are only supported in Python 3.5 and greater");
4099 return NULL;
4100 }
4101
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004102 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004103 REQ(n, for_stmt);
4104
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004105 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4106
4107 if (NCH(n) == 9 + has_type_comment) {
4108 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109 if (!seq)
4110 return NULL;
4111 }
4112
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004113 node_target = CHILD(n, 1);
4114 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004115 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004117 /* Check the # of children rather than the length of _target, since
4118 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004119 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004120 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004121 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004123 target = Tuple(_target, Store, first->lineno, first->col_offset,
4124 node_target->n_end_lineno, node_target->n_end_col_offset,
4125 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004127 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004128 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004129 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004130 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004131 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004132 return NULL;
4133
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004134 if (seq != NULL) {
4135 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4136 } else {
4137 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4138 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004139
4140 if (has_type_comment) {
4141 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4142 if (!type_comment)
4143 return NULL;
4144 }
4145 else
4146 type_comment = NULL;
4147
Yury Selivanov75445082015-05-11 22:57:16 -04004148 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004149 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004150 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004151 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004152 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004153 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004154 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004155 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004156}
4157
4158static excepthandler_ty
4159ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4160{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004161 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004162 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004163 REQ(exc, except_clause);
4164 REQ(body, suite);
4165
4166 if (NCH(exc) == 1) {
4167 asdl_seq *suite_seq = ast_for_suite(c, body);
4168 if (!suite_seq)
4169 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004170 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004171
Neal Norwitzad74aa82008-03-31 05:14:30 +00004172 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004173 exc->n_col_offset,
4174 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175 }
4176 else if (NCH(exc) == 2) {
4177 expr_ty expression;
4178 asdl_seq *suite_seq;
4179
4180 expression = ast_for_expr(c, CHILD(exc, 1));
4181 if (!expression)
4182 return NULL;
4183 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004184 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004185 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004186 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004187
Neal Norwitzad74aa82008-03-31 05:14:30 +00004188 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004189 exc->n_col_offset,
4190 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004191 }
4192 else if (NCH(exc) == 4) {
4193 asdl_seq *suite_seq;
4194 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004195 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004196 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004197 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004198 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004200 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004201 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004202 return NULL;
4203 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004204 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004205 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004206 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004207
Neal Norwitzad74aa82008-03-31 05:14:30 +00004208 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004209 exc->n_col_offset,
4210 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004211 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004212
4213 PyErr_Format(PyExc_SystemError,
4214 "wrong number of children for 'except' clause: %d",
4215 NCH(exc));
4216 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004217}
4218
4219static stmt_ty
4220ast_for_try_stmt(struct compiling *c, const node *n)
4221{
Neal Norwitzf599f422005-12-17 21:33:47 +00004222 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004223 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004224 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004225 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004226
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004227 REQ(n, try_stmt);
4228
Neal Norwitzf599f422005-12-17 21:33:47 +00004229 body = ast_for_suite(c, CHILD(n, 2));
4230 if (body == NULL)
4231 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004232
Neal Norwitzf599f422005-12-17 21:33:47 +00004233 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4234 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4235 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4236 /* we can assume it's an "else",
4237 because nch >= 9 for try-else-finally and
4238 it would otherwise have a type of except_clause */
4239 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4240 if (orelse == NULL)
4241 return NULL;
4242 n_except--;
4243 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004244
Neal Norwitzf599f422005-12-17 21:33:47 +00004245 finally = ast_for_suite(c, CHILD(n, nch - 1));
4246 if (finally == NULL)
4247 return NULL;
4248 n_except--;
4249 }
4250 else {
4251 /* we can assume it's an "else",
4252 otherwise it would have a type of except_clause */
4253 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4254 if (orelse == NULL)
4255 return NULL;
4256 n_except--;
4257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004258 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004259 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004260 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004261 return NULL;
4262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263
Neal Norwitzf599f422005-12-17 21:33:47 +00004264 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004265 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004266 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004267 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004268 if (handlers == NULL)
4269 return NULL;
4270
4271 for (i = 0; i < n_except; i++) {
4272 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4273 CHILD(n, 5 + i * 3));
4274 if (!e)
4275 return NULL;
4276 asdl_seq_SET(handlers, i, e);
4277 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004278 }
4279
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004280 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004281 if (finally != NULL) {
4282 // finally is always last
4283 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4284 } else if (orelse != NULL) {
4285 // otherwise else is last
4286 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4287 } else {
4288 // inline the get_last_end_pos logic due to layout mismatch
4289 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4290 end_lineno = last_handler->end_lineno;
4291 end_col_offset = last_handler->end_col_offset;
4292 }
4293 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4294 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004295}
4296
Georg Brandl0c315622009-05-25 21:10:36 +00004297/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004298static withitem_ty
4299ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004300{
4301 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004302
Georg Brandl0c315622009-05-25 21:10:36 +00004303 REQ(n, with_item);
4304 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004305 if (!context_expr)
4306 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004307 if (NCH(n) == 3) {
4308 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004309
4310 if (!optional_vars) {
4311 return NULL;
4312 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004313 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004314 return NULL;
4315 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004316 }
4317
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004318 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004319}
4320
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004321/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004322static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004323ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004324{
guoci90fc8982018-09-11 17:45:45 -04004325 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004326 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004327 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004328 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004329
Guido van Rossum495da292019-03-07 12:38:08 -08004330 if (is_async && c->c_feature_version < 5) {
4331 ast_error(c, n,
4332 "Async with statements are only supported in Python 3.5 and greater");
4333 return NULL;
4334 }
4335
Georg Brandl0c315622009-05-25 21:10:36 +00004336 REQ(n, with_stmt);
4337
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004338 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4339 nch_minus_type = NCH(n) - has_type_comment;
4340
4341 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004342 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004343 if (!items)
4344 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004345 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004346 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4347 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004348 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004349 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004350 }
4351
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004352 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4353 if (!body)
4354 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004355 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004356
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004357 if (has_type_comment) {
4358 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4359 if (!type_comment)
4360 return NULL;
4361 }
4362 else
4363 type_comment = NULL;
4364
Yury Selivanov75445082015-05-11 22:57:16 -04004365 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004366 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004367 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004368 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004369 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004370 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004371}
4372
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004373static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004374ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004375{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004376 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004377 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004378 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004379 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004380 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004381
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004382 REQ(n, classdef);
4383
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004384 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004385 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004386 if (!s)
4387 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004388 get_last_end_pos(s, &end_lineno, &end_col_offset);
4389
Benjamin Peterson30760062008-11-25 04:02:28 +00004390 classname = NEW_IDENTIFIER(CHILD(n, 1));
4391 if (!classname)
4392 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004393 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004394 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004395 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004396 LINENO(n), n->n_col_offset,
4397 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004398 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004399
4400 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004401 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004402 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004403 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004404 get_last_end_pos(s, &end_lineno, &end_col_offset);
4405
Benjamin Peterson30760062008-11-25 04:02:28 +00004406 classname = NEW_IDENTIFIER(CHILD(n, 1));
4407 if (!classname)
4408 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004409 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004410 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004411 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004412 LINENO(n), n->n_col_offset,
4413 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004414 }
4415
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004416 /* class NAME '(' arglist ')' ':' suite */
4417 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004418 {
4419 PyObject *dummy_name;
4420 expr_ty dummy;
4421 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4422 if (!dummy_name)
4423 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004424 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4425 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4426 c->c_arena);
4427 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004428 if (!call)
4429 return NULL;
4430 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004431 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004432 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004433 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004434 get_last_end_pos(s, &end_lineno, &end_col_offset);
4435
Benjamin Peterson30760062008-11-25 04:02:28 +00004436 classname = NEW_IDENTIFIER(CHILD(n, 1));
4437 if (!classname)
4438 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004439 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004440 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004441
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004442 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004443 decorator_seq, LINENO(n), n->n_col_offset,
4444 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445}
4446
4447static stmt_ty
4448ast_for_stmt(struct compiling *c, const node *n)
4449{
4450 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004451 assert(NCH(n) == 1);
4452 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004453 }
4454 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004455 assert(num_stmts(n) == 1);
4456 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004457 }
4458 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004459 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004460 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4461 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004462 */
4463 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004464 case expr_stmt:
4465 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004466 case del_stmt:
4467 return ast_for_del_stmt(c, n);
4468 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004469 return Pass(LINENO(n), n->n_col_offset,
4470 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004471 case flow_stmt:
4472 return ast_for_flow_stmt(c, n);
4473 case import_stmt:
4474 return ast_for_import_stmt(c, n);
4475 case global_stmt:
4476 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004477 case nonlocal_stmt:
4478 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004479 case assert_stmt:
4480 return ast_for_assert_stmt(c, n);
4481 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004482 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004483 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4484 TYPE(n), NCH(n));
4485 return NULL;
4486 }
4487 }
4488 else {
4489 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004490 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004491 */
4492 node *ch = CHILD(n, 0);
4493 REQ(n, compound_stmt);
4494 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004495 case if_stmt:
4496 return ast_for_if_stmt(c, ch);
4497 case while_stmt:
4498 return ast_for_while_stmt(c, ch);
4499 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004500 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004501 case try_stmt:
4502 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004503 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004504 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004505 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004506 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004507 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004508 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 case decorated:
4510 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004511 case async_stmt:
4512 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004513 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004514 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004515 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004516 TYPE(n), NCH(n));
4517 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004519 }
4520}
4521
4522static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004523parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004524{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004525 const char *end;
4526 long x;
4527 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004528 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004529 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004530
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004531 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004532 errno = 0;
4533 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004534 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004535 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004536 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004537 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004538 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004539 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004540 }
4541 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004542 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004543 if (*end == '\0') {
4544 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004545 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004546 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004547 }
4548 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004549 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004550 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004551 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4552 if (compl.imag == -1.0 && PyErr_Occurred())
4553 return NULL;
4554 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004555 }
4556 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004557 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004558 dx = PyOS_string_to_double(s, NULL, NULL);
4559 if (dx == -1.0 && PyErr_Occurred())
4560 return NULL;
4561 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004562 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004563}
4564
4565static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004566parsenumber(struct compiling *c, const char *s)
4567{
4568 char *dup, *end;
4569 PyObject *res = NULL;
4570
4571 assert(s != NULL);
4572
4573 if (strchr(s, '_') == NULL) {
4574 return parsenumber_raw(c, s);
4575 }
4576 /* Create a duplicate without underscores. */
4577 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004578 if (dup == NULL) {
4579 return PyErr_NoMemory();
4580 }
Brett Cannona721aba2016-09-09 14:57:09 -07004581 end = dup;
4582 for (; *s; s++) {
4583 if (*s != '_') {
4584 *end++ = *s;
4585 }
4586 }
4587 *end = '\0';
4588 res = parsenumber_raw(c, dup);
4589 PyMem_Free(dup);
4590 return res;
4591}
4592
4593static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004594decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004595{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004596 const char *s, *t;
4597 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004598 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4599 while (s < end && (*s & 0x80)) s++;
4600 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004601 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004602}
4603
Eric V. Smith56466482016-10-31 14:46:26 -04004604static int
4605warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004606 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004607{
4608 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4609 first_invalid_escape_char);
4610 if (msg == NULL) {
4611 return -1;
4612 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004613 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004614 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004615 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004616 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004617 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004618 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004619 to get a more accurate error report */
4620 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004621 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004622 }
4623 Py_DECREF(msg);
4624 return -1;
4625 }
4626 Py_DECREF(msg);
4627 return 0;
4628}
4629
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004630static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004631decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4632 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004633{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004634 PyObject *v, *u;
4635 char *buf;
4636 char *p;
4637 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004638
Benjamin Peterson202803a2016-02-25 22:34:45 -08004639 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004640 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004641 return NULL;
4642 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4643 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4644 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4645 if (u == NULL)
4646 return NULL;
4647 p = buf = PyBytes_AsString(u);
4648 end = s + len;
4649 while (s < end) {
4650 if (*s == '\\') {
4651 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004652 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004653 strcpy(p, "u005c");
4654 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004655 if (s >= end)
4656 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004657 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004658 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004659 if (*s & 0x80) { /* XXX inefficient */
4660 PyObject *w;
4661 int kind;
4662 void *data;
4663 Py_ssize_t len, i;
4664 w = decode_utf8(c, &s, end);
4665 if (w == NULL) {
4666 Py_DECREF(u);
4667 return NULL;
4668 }
4669 kind = PyUnicode_KIND(w);
4670 data = PyUnicode_DATA(w);
4671 len = PyUnicode_GET_LENGTH(w);
4672 for (i = 0; i < len; i++) {
4673 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4674 sprintf(p, "\\U%08x", chr);
4675 p += 10;
4676 }
4677 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004678 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004679 Py_DECREF(w);
4680 } else {
4681 *p++ = *s++;
4682 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004683 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004684 len = p - buf;
4685 s = buf;
4686
Eric V. Smith56466482016-10-31 14:46:26 -04004687 const char *first_invalid_escape;
4688 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4689
4690 if (v != NULL && first_invalid_escape != NULL) {
4691 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4692 /* We have not decref u before because first_invalid_escape points
4693 inside u. */
4694 Py_XDECREF(u);
4695 Py_DECREF(v);
4696 return NULL;
4697 }
4698 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004699 Py_XDECREF(u);
4700 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004701}
4702
Eric V. Smith56466482016-10-31 14:46:26 -04004703static PyObject *
4704decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4705 size_t len)
4706{
4707 const char *first_invalid_escape;
4708 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4709 &first_invalid_escape);
4710 if (result == NULL)
4711 return NULL;
4712
4713 if (first_invalid_escape != NULL) {
4714 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4715 Py_DECREF(result);
4716 return NULL;
4717 }
4718 }
4719 return result;
4720}
4721
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004722/* Shift locations for the given node and all its children by adding `lineno`
4723 and `col_offset` to existing locations. */
4724static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4725{
4726 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004727 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004728 for (int i = 0; i < NCH(n); ++i) {
4729 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4730 /* Shifting column offsets unnecessary if there's been newlines. */
4731 col_offset = 0;
4732 }
4733 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4734 }
4735 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004736 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004737}
4738
4739/* Fix locations for the given node and its children.
4740
4741 `parent` is the enclosing node.
4742 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004743 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004744*/
4745static void
4746fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4747{
4748 char *substr = NULL;
4749 char *start;
4750 int lines = LINENO(parent) - 1;
4751 int cols = parent->n_col_offset;
4752 /* Find the full fstring to fix location information in `n`. */
4753 while (parent && parent->n_type != STRING)
4754 parent = parent->n_child;
4755 if (parent && parent->n_str) {
4756 substr = strstr(parent->n_str, expr_str);
4757 if (substr) {
4758 start = substr;
4759 while (start > parent->n_str) {
4760 if (start[0] == '\n')
4761 break;
4762 start--;
4763 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004764 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004765 /* adjust the start based on the number of newlines encountered
4766 before the f-string expression */
4767 for (char* p = parent->n_str; p < substr; p++) {
4768 if (*p == '\n') {
4769 lines++;
4770 }
4771 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004772 }
4773 }
4774 fstring_shift_node_locations(n, lines, cols);
4775}
4776
Eric V. Smith451d0e32016-09-09 21:56:20 -04004777/* Compile this expression in to an expr_ty. Add parens around the
4778 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004779static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004780fstring_compile_expr(const char *expr_start, const char *expr_end,
4781 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004782
Eric V. Smith235a6f02015-09-19 14:51:32 -04004783{
4784 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004785 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004786 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004787 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004788 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004789 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004790
Eric V. Smith1d44c412015-09-23 07:49:00 -04004791 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004792 assert(*(expr_start-1) == '{');
4793 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004794
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004795 /* If the substring is all whitespace, it's an error. We need to catch this
4796 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4797 because turning the expression '' in to '()' would go from being invalid
4798 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004799 for (s = expr_start; s != expr_end; s++) {
4800 char c = *s;
4801 /* The Python parser ignores only the following whitespace
4802 characters (\r already is converted to \n). */
4803 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004804 break;
4805 }
4806 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004807 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004808 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004809 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004810 }
4811
Eric V. Smith451d0e32016-09-09 21:56:20 -04004812 len = expr_end - expr_start;
4813 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4814 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004815 if (str == NULL) {
4816 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004817 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004818 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004819
Eric V. Smith451d0e32016-09-09 21:56:20 -04004820 str[0] = '(';
4821 memcpy(str+1, expr_start, len);
4822 str[len+1] = ')';
4823 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004824
4825 cf.cf_flags = PyCF_ONLY_AST;
Guido van Rossum495da292019-03-07 12:38:08 -08004826 cf.cf_feature_version = PY_MINOR_VERSION;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004827 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4828 Py_eval_input, 0);
4829 if (!mod_n) {
4830 PyMem_RawFree(str);
4831 return NULL;
4832 }
4833 /* Reuse str to find the correct column offset. */
4834 str[0] = '{';
4835 str[len+1] = '}';
4836 fstring_fix_node_location(n, mod_n, str);
4837 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004838 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004839 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004840 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004841 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004842 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004843}
4844
4845/* Return -1 on error.
4846
4847 Return 0 if we reached the end of the literal.
4848
4849 Return 1 if we haven't reached the end of the literal, but we want
4850 the caller to process the literal up to this point. Used for
4851 doubled braces.
4852*/
4853static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004854fstring_find_literal(const char **str, const char *end, int raw,
4855 PyObject **literal, int recurse_lvl,
4856 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004857{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004858 /* Get any literal string. It ends when we hit an un-doubled left
4859 brace (which isn't part of a unicode name escape such as
4860 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004861
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004862 const char *s = *str;
4863 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004864 int result = 0;
4865
Eric V. Smith235a6f02015-09-19 14:51:32 -04004866 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004867 while (s < end) {
4868 char ch = *s++;
4869 if (!raw && ch == '\\' && s < end) {
4870 ch = *s++;
4871 if (ch == 'N') {
4872 if (s < end && *s++ == '{') {
4873 while (s < end && *s++ != '}') {
4874 }
4875 continue;
4876 }
4877 break;
4878 }
4879 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4880 return -1;
4881 }
4882 }
4883 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004884 /* Check for doubled braces, but only at the top level. If
4885 we checked at every level, then f'{0:{3}}' would fail
4886 with the two closing braces. */
4887 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004888 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004889 /* We're going to tell the caller that the literal ends
4890 here, but that they should continue scanning. But also
4891 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004892 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004893 result = 1;
4894 goto done;
4895 }
4896
4897 /* Where a single '{' is the start of a new expression, a
4898 single '}' is not allowed. */
4899 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004900 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004901 ast_error(c, n, "f-string: single '}' is not allowed");
4902 return -1;
4903 }
4904 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004905 /* We're either at a '{', which means we're starting another
4906 expression; or a '}', which means we're at the end of this
4907 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004908 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004909 break;
4910 }
4911 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004912 *str = s;
4913 assert(s <= end);
4914 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004915done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004916 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004917 if (raw)
4918 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004919 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004920 NULL, NULL);
4921 else
Eric V. Smith56466482016-10-31 14:46:26 -04004922 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004923 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924 if (!*literal)
4925 return -1;
4926 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927 return result;
4928}
4929
4930/* Forward declaration because parsing is recursive. */
4931static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004932fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004933 struct compiling *c, const node *n);
4934
Eric V. Smith451d0e32016-09-09 21:56:20 -04004935/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004936 expression (so it must be a '{'). Returns the FormattedValue node,
4937 which includes the expression, conversion character, and
4938 format_spec expression.
4939
4940 Note that I don't do a perfect job here: I don't make sure that a
4941 closing brace doesn't match an opening paren, for example. It
4942 doesn't need to error on all invalid expressions, just correctly
4943 find the end of all valid ones. Any errors inside the expression
4944 will be caught when we parse it later. */
4945static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004946fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004947 expr_ty *expression, struct compiling *c, const node *n)
4948{
4949 /* Return -1 on error, else 0. */
4950
Eric V. Smith451d0e32016-09-09 21:56:20 -04004951 const char *expr_start;
4952 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004953 expr_ty simple_expression;
4954 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004955 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004956
4957 /* 0 if we're not in a string, else the quote char we're trying to
4958 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004959 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004960
4961 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4962 int string_type = 0;
4963
4964 /* Keep track of nesting level for braces/parens/brackets in
4965 expressions. */
4966 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004967 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004968
4969 /* Can only nest one level deep. */
4970 if (recurse_lvl >= 2) {
4971 ast_error(c, n, "f-string: expressions nested too deeply");
4972 return -1;
4973 }
4974
4975 /* The first char must be a left brace, or we wouldn't have gotten
4976 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004977 assert(**str == '{');
4978 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979
Eric V. Smith451d0e32016-09-09 21:56:20 -04004980 expr_start = *str;
4981 for (; *str < end; (*str)++) {
4982 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004983
4984 /* Loop invariants. */
4985 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004986 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 if (quote_char)
4988 assert(string_type == 1 || string_type == 3);
4989 else
4990 assert(string_type == 0);
4991
Eric V. Smith451d0e32016-09-09 21:56:20 -04004992 ch = **str;
4993 /* Nowhere inside an expression is a backslash allowed. */
4994 if (ch == '\\') {
4995 /* Error: can't include a backslash character, inside
4996 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004997 ast_error(c, n,
4998 "f-string expression part "
4999 "cannot include a backslash");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005000 return -1;
5001 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005002 if (quote_char) {
5003 /* We're inside a string. See if we're at the end. */
5004 /* This code needs to implement the same non-error logic
5005 as tok_get from tokenizer.c, at the letter_quote
5006 label. To actually share that code would be a
5007 nightmare. But, it's unlikely to change and is small,
5008 so duplicate it here. Note we don't need to catch all
5009 of the errors, since they'll be caught when parsing the
5010 expression. We just need to match the non-error
5011 cases. Thus we can ignore \n in single-quoted strings,
5012 for example. Or non-terminated strings. */
5013 if (ch == quote_char) {
5014 /* Does this match the string_type (single or triple
5015 quoted)? */
5016 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005017 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005018 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005019 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005020 string_type = 0;
5021 quote_char = 0;
5022 continue;
5023 }
5024 } else {
5025 /* We're at the end of a normal string. */
5026 quote_char = 0;
5027 string_type = 0;
5028 continue;
5029 }
5030 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005031 } else if (ch == '\'' || ch == '"') {
5032 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005033 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005034 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005035 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005036 } else {
5037 /* Start of a normal string. */
5038 string_type = 1;
5039 }
5040 /* Start looking for the end of the string. */
5041 quote_char = ch;
5042 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005043 if (nested_depth >= MAXLEVEL) {
5044 ast_error(c, n, "f-string: too many nested parenthesis");
5045 return -1;
5046 }
5047 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005048 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005049 } else if (ch == '#') {
5050 /* Error: can't include a comment character, inside parens
5051 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005052 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005053 return -1;
5054 } else if (nested_depth == 0 &&
5055 (ch == '!' || ch == ':' || ch == '}')) {
5056 /* First, test for the special case of "!=". Since '=' is
5057 not an allowed conversion character, nothing is lost in
5058 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005059 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005060 /* This isn't a conversion character, just continue. */
5061 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005062 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063 /* Normal way out of this loop. */
5064 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005065 } else if (ch == ']' || ch == '}' || ch == ')') {
5066 if (!nested_depth) {
5067 ast_error(c, n, "f-string: unmatched '%c'", ch);
5068 return -1;
5069 }
5070 nested_depth--;
5071 int opening = parenstack[nested_depth];
5072 if (!((opening == '(' && ch == ')') ||
5073 (opening == '[' && ch == ']') ||
5074 (opening == '{' && ch == '}')))
5075 {
5076 ast_error(c, n,
5077 "f-string: closing parenthesis '%c' "
5078 "does not match opening parenthesis '%c'",
5079 ch, opening);
5080 return -1;
5081 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005082 } else {
5083 /* Just consume this char and loop around. */
5084 }
5085 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005086 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005087 /* If we leave this loop in a string or with mismatched parens, we
5088 don't care. We'll get a syntax error when compiling the
5089 expression. But, we can produce a better error message, so
5090 let's just do that.*/
5091 if (quote_char) {
5092 ast_error(c, n, "f-string: unterminated string");
5093 return -1;
5094 }
5095 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005096 int opening = parenstack[nested_depth - 1];
5097 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005098 return -1;
5099 }
5100
Eric V. Smith451d0e32016-09-09 21:56:20 -04005101 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005102 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005103
5104 /* Compile the expression as soon as possible, so we show errors
5105 related to the expression before errors related to the
5106 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005108 if (!simple_expression)
5109 return -1;
5110
5111 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005112 if (**str == '!') {
5113 *str += 1;
5114 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115 goto unexpected_end_of_string;
5116
Eric V. Smith451d0e32016-09-09 21:56:20 -04005117 conversion = **str;
5118 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119
5120 /* Validate the conversion. */
5121 if (!(conversion == 's' || conversion == 'r'
5122 || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005123 ast_error(c, n,
5124 "f-string: invalid conversion character: "
5125 "expected 's', 'r', or 'a'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005126 return -1;
5127 }
5128 }
5129
5130 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005131 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005132 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005133 if (**str == ':') {
5134 *str += 1;
5135 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005136 goto unexpected_end_of_string;
5137
5138 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005139 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005140 if (!format_spec)
5141 return -1;
5142 }
5143
Eric V. Smith451d0e32016-09-09 21:56:20 -04005144 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005145 goto unexpected_end_of_string;
5146
5147 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005148 assert(*str < end);
5149 assert(**str == '}');
5150 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005151
Eric V. Smith451d0e32016-09-09 21:56:20 -04005152 /* And now create the FormattedValue node that represents this
5153 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005154 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005155 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005156 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005157 c->c_arena);
5158 if (!*expression)
5159 return -1;
5160
5161 return 0;
5162
5163unexpected_end_of_string:
5164 ast_error(c, n, "f-string: expecting '}'");
5165 return -1;
5166}
5167
5168/* Return -1 on error.
5169
5170 Return 0 if we have a literal (possible zero length) and an
5171 expression (zero length if at the end of the string.
5172
5173 Return 1 if we have a literal, but no expression, and we want the
5174 caller to call us again. This is used to deal with doubled
5175 braces.
5176
5177 When called multiple times on the string 'a{{b{0}c', this function
5178 will return:
5179
5180 1. the literal 'a{' with no expression, and a return value
5181 of 1. Despite the fact that there's no expression, the return
5182 value of 1 means we're not finished yet.
5183
5184 2. the literal 'b' and the expression '0', with a return value of
5185 0. The fact that there's an expression means we're not finished.
5186
5187 3. literal 'c' with no expression and a return value of 0. The
5188 combination of the return value of 0 with no expression means
5189 we're finished.
5190*/
5191static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005192fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5193 int recurse_lvl, PyObject **literal,
5194 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005195 struct compiling *c, const node *n)
5196{
5197 int result;
5198
5199 assert(*literal == NULL && *expression == NULL);
5200
5201 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005202 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005203 if (result < 0)
5204 goto error;
5205
5206 assert(result == 0 || result == 1);
5207
5208 if (result == 1)
5209 /* We have a literal, but don't look at the expression. */
5210 return 1;
5211
Eric V. Smith451d0e32016-09-09 21:56:20 -04005212 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005213 /* We're at the end of the string or the end of a nested
5214 f-string: no expression. The top-level error case where we
5215 expect to be at the end of the string but we're at a '}' is
5216 handled later. */
5217 return 0;
5218
5219 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005220 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005221
Eric V. Smith451d0e32016-09-09 21:56:20 -04005222 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005223 goto error;
5224
5225 return 0;
5226
5227error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005228 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005229 return -1;
5230}
5231
5232#define EXPRLIST_N_CACHED 64
5233
5234typedef struct {
5235 /* Incrementally build an array of expr_ty, so be used in an
5236 asdl_seq. Cache some small but reasonably sized number of
5237 expr_ty's, and then after that start dynamically allocating,
5238 doubling the number allocated each time. Note that the f-string
5239 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005240 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005241 fast as you add exressions in an f-string. */
5242
5243 Py_ssize_t allocated; /* Number we've allocated. */
5244 Py_ssize_t size; /* Number we've used. */
5245 expr_ty *p; /* Pointer to the memory we're actually
5246 using. Will point to 'data' until we
5247 start dynamically allocating. */
5248 expr_ty data[EXPRLIST_N_CACHED];
5249} ExprList;
5250
5251#ifdef NDEBUG
5252#define ExprList_check_invariants(l)
5253#else
5254static void
5255ExprList_check_invariants(ExprList *l)
5256{
5257 /* Check our invariants. Make sure this object is "live", and
5258 hasn't been deallocated. */
5259 assert(l->size >= 0);
5260 assert(l->p != NULL);
5261 if (l->size <= EXPRLIST_N_CACHED)
5262 assert(l->data == l->p);
5263}
5264#endif
5265
5266static void
5267ExprList_Init(ExprList *l)
5268{
5269 l->allocated = EXPRLIST_N_CACHED;
5270 l->size = 0;
5271
5272 /* Until we start allocating dynamically, p points to data. */
5273 l->p = l->data;
5274
5275 ExprList_check_invariants(l);
5276}
5277
5278static int
5279ExprList_Append(ExprList *l, expr_ty exp)
5280{
5281 ExprList_check_invariants(l);
5282 if (l->size >= l->allocated) {
5283 /* We need to alloc (or realloc) the memory. */
5284 Py_ssize_t new_size = l->allocated * 2;
5285
5286 /* See if we've ever allocated anything dynamically. */
5287 if (l->p == l->data) {
5288 Py_ssize_t i;
5289 /* We're still using the cached data. Switch to
5290 alloc-ing. */
5291 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5292 if (!l->p)
5293 return -1;
5294 /* Copy the cached data into the new buffer. */
5295 for (i = 0; i < l->size; i++)
5296 l->p[i] = l->data[i];
5297 } else {
5298 /* Just realloc. */
5299 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5300 if (!tmp) {
5301 PyMem_RawFree(l->p);
5302 l->p = NULL;
5303 return -1;
5304 }
5305 l->p = tmp;
5306 }
5307
5308 l->allocated = new_size;
5309 assert(l->allocated == 2 * l->size);
5310 }
5311
5312 l->p[l->size++] = exp;
5313
5314 ExprList_check_invariants(l);
5315 return 0;
5316}
5317
5318static void
5319ExprList_Dealloc(ExprList *l)
5320{
5321 ExprList_check_invariants(l);
5322
5323 /* If there's been an error, or we've never dynamically allocated,
5324 do nothing. */
5325 if (!l->p || l->p == l->data) {
5326 /* Do nothing. */
5327 } else {
5328 /* We have dynamically allocated. Free the memory. */
5329 PyMem_RawFree(l->p);
5330 }
5331 l->p = NULL;
5332 l->size = -1;
5333}
5334
5335static asdl_seq *
5336ExprList_Finish(ExprList *l, PyArena *arena)
5337{
5338 asdl_seq *seq;
5339
5340 ExprList_check_invariants(l);
5341
5342 /* Allocate the asdl_seq and copy the expressions in to it. */
5343 seq = _Py_asdl_seq_new(l->size, arena);
5344 if (seq) {
5345 Py_ssize_t i;
5346 for (i = 0; i < l->size; i++)
5347 asdl_seq_SET(seq, i, l->p[i]);
5348 }
5349 ExprList_Dealloc(l);
5350 return seq;
5351}
5352
5353/* The FstringParser is designed to add a mix of strings and
5354 f-strings, and concat them together as needed. Ultimately, it
5355 generates an expr_ty. */
5356typedef struct {
5357 PyObject *last_str;
5358 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005359 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005360} FstringParser;
5361
5362#ifdef NDEBUG
5363#define FstringParser_check_invariants(state)
5364#else
5365static void
5366FstringParser_check_invariants(FstringParser *state)
5367{
5368 if (state->last_str)
5369 assert(PyUnicode_CheckExact(state->last_str));
5370 ExprList_check_invariants(&state->expr_list);
5371}
5372#endif
5373
5374static void
5375FstringParser_Init(FstringParser *state)
5376{
5377 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005378 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005379 ExprList_Init(&state->expr_list);
5380 FstringParser_check_invariants(state);
5381}
5382
5383static void
5384FstringParser_Dealloc(FstringParser *state)
5385{
5386 FstringParser_check_invariants(state);
5387
5388 Py_XDECREF(state->last_str);
5389 ExprList_Dealloc(&state->expr_list);
5390}
5391
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005392/* Constants for the following */
5393static PyObject *u_kind;
5394
5395/* Compute 'kind' field for string Constant (either 'u' or None) */
5396static PyObject *
5397make_kind(struct compiling *c, const node *n)
5398{
5399 char *s = NULL;
5400 PyObject *kind = NULL;
5401
5402 /* Find the first string literal, if any */
5403 while (TYPE(n) != STRING) {
5404 if (NCH(n) == 0)
5405 return NULL;
5406 n = CHILD(n, 0);
5407 }
5408 REQ(n, STRING);
5409
5410 /* If it starts with 'u', return a PyUnicode "u" string */
5411 s = STR(n);
5412 if (s && *s == 'u') {
5413 if (!u_kind) {
5414 u_kind = PyUnicode_InternFromString("u");
5415 if (!u_kind)
5416 return NULL;
5417 }
5418 kind = u_kind;
5419 if (PyArena_AddPyObject(c->c_arena, kind) < 0) {
5420 return NULL;
5421 }
5422 Py_INCREF(kind);
5423 }
5424 return kind;
5425}
5426
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005427/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005428static expr_ty
5429make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5430{
5431 PyObject *s = *str;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005432 PyObject *kind = NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005433 *str = NULL;
5434 assert(PyUnicode_CheckExact(s));
5435 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5436 Py_DECREF(s);
5437 return NULL;
5438 }
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005439 kind = make_kind(c, n);
5440 if (kind == NULL && PyErr_Occurred())
5441 return NULL;
5442 return Constant(s, kind, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005443 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005444}
5445
5446/* Add a non-f-string (that is, a regular literal string). str is
5447 decref'd. */
5448static int
5449FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5450{
5451 FstringParser_check_invariants(state);
5452
5453 assert(PyUnicode_CheckExact(str));
5454
5455 if (PyUnicode_GET_LENGTH(str) == 0) {
5456 Py_DECREF(str);
5457 return 0;
5458 }
5459
5460 if (!state->last_str) {
5461 /* We didn't have a string before, so just remember this one. */
5462 state->last_str = str;
5463 } else {
5464 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005465 PyUnicode_AppendAndDel(&state->last_str, str);
5466 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005467 return -1;
5468 }
5469 FstringParser_check_invariants(state);
5470 return 0;
5471}
5472
Eric V. Smith451d0e32016-09-09 21:56:20 -04005473/* Parse an f-string. The f-string is in *str to end, with no
5474 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005475static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005476FstringParser_ConcatFstring(FstringParser *state, const char **str,
5477 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005478 struct compiling *c, const node *n)
5479{
5480 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005481 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005482
5483 /* Parse the f-string. */
5484 while (1) {
5485 PyObject *literal = NULL;
5486 expr_ty expression = NULL;
5487
5488 /* If there's a zero length literal in front of the
5489 expression, literal will be NULL. If we're at the end of
5490 the f-string, expression will be NULL (unless result == 1,
5491 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005492 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005493 &literal, &expression,
5494 c, n);
5495 if (result < 0)
5496 return -1;
5497
5498 /* Add the literal, if any. */
5499 if (!literal) {
5500 /* Do nothing. Just leave last_str alone (and possibly
5501 NULL). */
5502 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005503 /* Note that the literal can be zero length, if the
5504 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005505 state->last_str = literal;
5506 literal = NULL;
5507 } else {
5508 /* We have a literal, concatenate it. */
5509 assert(PyUnicode_GET_LENGTH(literal) != 0);
5510 if (FstringParser_ConcatAndDel(state, literal) < 0)
5511 return -1;
5512 literal = NULL;
5513 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005514
5515 /* We've dealt with the literal now. It can't be leaked on further
5516 errors. */
5517 assert(literal == NULL);
5518
5519 /* See if we should just loop around to get the next literal
5520 and expression, while ignoring the expression this
5521 time. This is used for un-doubling braces, as an
5522 optimization. */
5523 if (result == 1)
5524 continue;
5525
5526 if (!expression)
5527 /* We're done with this f-string. */
5528 break;
5529
5530 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005531 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005532 if (!state->last_str) {
5533 /* Do nothing. No previous literal. */
5534 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005535 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005536 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5537 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5538 return -1;
5539 }
5540
5541 if (ExprList_Append(&state->expr_list, expression) < 0)
5542 return -1;
5543 }
5544
Eric V. Smith235a6f02015-09-19 14:51:32 -04005545 /* If recurse_lvl is zero, then we must be at the end of the
5546 string. Otherwise, we must be at a right brace. */
5547
Eric V. Smith451d0e32016-09-09 21:56:20 -04005548 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005549 ast_error(c, n, "f-string: unexpected end of string");
5550 return -1;
5551 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005552 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005553 ast_error(c, n, "f-string: expecting '}'");
5554 return -1;
5555 }
5556
5557 FstringParser_check_invariants(state);
5558 return 0;
5559}
5560
5561/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005562 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005563static expr_ty
5564FstringParser_Finish(FstringParser *state, struct compiling *c,
5565 const node *n)
5566{
5567 asdl_seq *seq;
5568
5569 FstringParser_check_invariants(state);
5570
5571 /* If we're just a constant string with no expressions, return
5572 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005573 if (!state->fmode) {
5574 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005575 if (!state->last_str) {
5576 /* Create a zero length string. */
5577 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5578 if (!state->last_str)
5579 goto error;
5580 }
5581 return make_str_node_and_del(&state->last_str, c, n);
5582 }
5583
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005584 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005585 last node in our expression list. */
5586 if (state->last_str) {
5587 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5588 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5589 goto error;
5590 }
5591 /* This has already been freed. */
5592 assert(state->last_str == NULL);
5593
5594 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5595 if (!seq)
5596 goto error;
5597
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005598 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5599 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005600
5601error:
5602 FstringParser_Dealloc(state);
5603 return NULL;
5604}
5605
Eric V. Smith451d0e32016-09-09 21:56:20 -04005606/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5607 at end, parse it into an expr_ty. Return NULL on error. Adjust
5608 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005609static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005610fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005611 struct compiling *c, const node *n)
5612{
5613 FstringParser state;
5614
5615 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005616 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005617 c, n) < 0) {
5618 FstringParser_Dealloc(&state);
5619 return NULL;
5620 }
5621
5622 return FstringParser_Finish(&state, c, n);
5623}
5624
5625/* n is a Python string literal, including the bracketing quote
5626 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005627 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005628 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005629 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5630 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005631*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005632static int
5633parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5634 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005635{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005636 size_t len;
5637 const char *s = STR(n);
5638 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005639 int fmode = 0;
5640 *bytesmode = 0;
5641 *rawmode = 0;
5642 *result = NULL;
5643 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005644 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005645 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005646 if (quote == 'b' || quote == 'B') {
5647 quote = *++s;
5648 *bytesmode = 1;
5649 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005650 else if (quote == 'u' || quote == 'U') {
5651 quote = *++s;
5652 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005653 else if (quote == 'r' || quote == 'R') {
5654 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005655 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005656 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005657 else if (quote == 'f' || quote == 'F') {
5658 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005659 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005660 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005661 else {
5662 break;
5663 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005664 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005665 }
Guido van Rossum495da292019-03-07 12:38:08 -08005666
5667 /* fstrings are only allowed in Python 3.6 and greater */
5668 if (fmode && c->c_feature_version < 6) {
5669 ast_error(c, n, "Format strings are only supported in Python 3.6 and greater");
5670 return -1;
5671 }
5672
Eric V. Smith451d0e32016-09-09 21:56:20 -04005673 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005674 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005675 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005676 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005677 if (quote != '\'' && quote != '\"') {
5678 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005679 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005680 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005681 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005682 s++;
5683 len = strlen(s);
5684 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005685 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005686 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005687 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005688 }
5689 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005690 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005691 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005692 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005693 }
5694 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005695 /* A triple quoted string. We've already skipped one quote at
5696 the start and one at the end of the string. Now skip the
5697 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005698 s += 2;
5699 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005700 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005701 if (s[--len] != quote || s[--len] != quote) {
5702 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005703 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005704 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005705 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005706
Eric V. Smith451d0e32016-09-09 21:56:20 -04005707 if (fmode) {
5708 /* Just return the bytes. The caller will parse the resulting
5709 string. */
5710 *fstr = s;
5711 *fstrlen = len;
5712 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005713 }
5714
Eric V. Smith451d0e32016-09-09 21:56:20 -04005715 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005716 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005717 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005718 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005719 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005720 const char *ch;
5721 for (ch = s; *ch; ch++) {
5722 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005723 ast_error(c, n,
5724 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005725 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005726 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005727 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005728 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005729 if (*rawmode)
5730 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005731 else
Eric V. Smith56466482016-10-31 14:46:26 -04005732 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005733 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005734 if (*rawmode)
5735 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005736 else
Eric V. Smith56466482016-10-31 14:46:26 -04005737 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005738 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005739 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005740}
5741
Eric V. Smith235a6f02015-09-19 14:51:32 -04005742/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5743 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005744 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005745 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005746 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005747 node if there's just an f-string (with no leading or trailing
5748 literals), or a JoinedStr node if there are multiple f-strings or
5749 any literals involved. */
5750static expr_ty
5751parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005752{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005753 int bytesmode = 0;
5754 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005755 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005756
5757 FstringParser state;
5758 FstringParser_Init(&state);
5759
5760 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005761 int this_bytesmode;
5762 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005763 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005764 const char *fstr;
5765 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005766
5767 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005768 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5769 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005770 goto error;
5771
5772 /* Check that we're not mixing bytes with unicode. */
5773 if (i != 0 && bytesmode != this_bytesmode) {
5774 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005775 /* s is NULL if the current string part is an f-string. */
5776 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005777 goto error;
5778 }
5779 bytesmode = this_bytesmode;
5780
Eric V. Smith451d0e32016-09-09 21:56:20 -04005781 if (fstr != NULL) {
5782 int result;
5783 assert(s == NULL && !bytesmode);
5784 /* This is an f-string. Parse and concatenate it. */
5785 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5786 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005787 if (result < 0)
5788 goto error;
5789 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005790 /* A string or byte string. */
5791 assert(s != NULL && fstr == NULL);
5792
Eric V. Smith451d0e32016-09-09 21:56:20 -04005793 assert(bytesmode ? PyBytes_CheckExact(s) :
5794 PyUnicode_CheckExact(s));
5795
Eric V. Smith451d0e32016-09-09 21:56:20 -04005796 if (bytesmode) {
5797 /* For bytes, concat as we go. */
5798 if (i == 0) {
5799 /* First time, just remember this value. */
5800 bytes_str = s;
5801 } else {
5802 PyBytes_ConcatAndDel(&bytes_str, s);
5803 if (!bytes_str)
5804 goto error;
5805 }
5806 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005807 /* This is a regular string. Concatenate it. */
5808 if (FstringParser_ConcatAndDel(&state, s) < 0)
5809 goto error;
5810 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005811 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005812 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005813 if (bytesmode) {
5814 /* Just return the bytes object and we're done. */
5815 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5816 goto error;
Guido van Rossum10f8ce62019-03-13 13:00:46 -07005817 return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005818 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005819 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005820
Eric V. Smith235a6f02015-09-19 14:51:32 -04005821 /* We're not a bytes string, bytes_str should never have been set. */
5822 assert(bytes_str == NULL);
5823
5824 return FstringParser_Finish(&state, c, n);
5825
5826error:
5827 Py_XDECREF(bytes_str);
5828 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005830}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005831
5832PyObject *
5833_PyAST_GetDocString(asdl_seq *body)
5834{
5835 if (!asdl_seq_LEN(body)) {
5836 return NULL;
5837 }
5838 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5839 if (st->kind != Expr_kind) {
5840 return NULL;
5841 }
5842 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005843 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5844 return e->v.Constant.value;
5845 }
5846 return NULL;
5847}