blob: 45578a850f77cd4c50cb8a6fc8431090ad521958 [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";
Emily Morehouse8f59ee02019-01-24 16:49:56 -070097 case NamedStore:
98 return "NamedStore";
Benjamin Peterson832bfe22011-08-09 16:15:04 -050099 case Del:
100 return "Del";
101 case AugLoad:
102 return "AugLoad";
103 case AugStore:
104 return "AugStore";
105 case Param:
106 return "Param";
107 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700108 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500109 }
110}
111
112static int
113validate_arguments(arguments_ty args)
114{
115 if (!validate_args(args->args))
116 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700117 if (args->vararg && args->vararg->annotation
118 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500119 return 0;
120 }
121 if (!validate_args(args->kwonlyargs))
122 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100123 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700124 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500125 return 0;
126 }
127 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
128 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
129 return 0;
130 }
131 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
132 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
133 "kw_defaults on arguments");
134 return 0;
135 }
136 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
137}
138
139static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100140validate_constant(PyObject *value)
141{
142 if (value == Py_None || value == Py_Ellipsis)
143 return 1;
144
145 if (PyLong_CheckExact(value)
146 || PyFloat_CheckExact(value)
147 || PyComplex_CheckExact(value)
148 || PyBool_Check(value)
149 || PyUnicode_CheckExact(value)
150 || PyBytes_CheckExact(value))
151 return 1;
152
153 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
154 PyObject *it;
155
156 it = PyObject_GetIter(value);
157 if (it == NULL)
158 return 0;
159
160 while (1) {
161 PyObject *item = PyIter_Next(it);
162 if (item == NULL) {
163 if (PyErr_Occurred()) {
164 Py_DECREF(it);
165 return 0;
166 }
167 break;
168 }
169
170 if (!validate_constant(item)) {
171 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100172 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100173 return 0;
174 }
Victor Stinner726f6902016-01-27 00:11:47 +0100175 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100176 }
177
178 Py_DECREF(it);
179 return 1;
180 }
181
182 return 0;
183}
184
185static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500186validate_expr(expr_ty exp, expr_context_ty ctx)
187{
188 int check_ctx = 1;
189 expr_context_ty actual_ctx;
190
191 /* First check expression context. */
192 switch (exp->kind) {
193 case Attribute_kind:
194 actual_ctx = exp->v.Attribute.ctx;
195 break;
196 case Subscript_kind:
197 actual_ctx = exp->v.Subscript.ctx;
198 break;
199 case Starred_kind:
200 actual_ctx = exp->v.Starred.ctx;
201 break;
202 case Name_kind:
203 actual_ctx = exp->v.Name.ctx;
204 break;
205 case List_kind:
206 actual_ctx = exp->v.List.ctx;
207 break;
208 case Tuple_kind:
209 actual_ctx = exp->v.Tuple.ctx;
210 break;
211 default:
212 if (ctx != Load) {
213 PyErr_Format(PyExc_ValueError, "expression which can't be "
214 "assigned to in %s context", expr_context_name(ctx));
215 return 0;
216 }
217 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100218 /* set actual_ctx to prevent gcc warning */
219 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500220 }
221 if (check_ctx && actual_ctx != ctx) {
222 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
223 expr_context_name(ctx), expr_context_name(actual_ctx));
224 return 0;
225 }
226
227 /* Now validate expression. */
228 switch (exp->kind) {
229 case BoolOp_kind:
230 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
231 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
232 return 0;
233 }
234 return validate_exprs(exp->v.BoolOp.values, Load, 0);
235 case BinOp_kind:
236 return validate_expr(exp->v.BinOp.left, Load) &&
237 validate_expr(exp->v.BinOp.right, Load);
238 case UnaryOp_kind:
239 return validate_expr(exp->v.UnaryOp.operand, Load);
240 case Lambda_kind:
241 return validate_arguments(exp->v.Lambda.args) &&
242 validate_expr(exp->v.Lambda.body, Load);
243 case IfExp_kind:
244 return validate_expr(exp->v.IfExp.test, Load) &&
245 validate_expr(exp->v.IfExp.body, Load) &&
246 validate_expr(exp->v.IfExp.orelse, Load);
247 case Dict_kind:
248 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
249 PyErr_SetString(PyExc_ValueError,
250 "Dict doesn't have the same number of keys as values");
251 return 0;
252 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400253 /* null_ok=1 for keys expressions to allow dict unpacking to work in
254 dict literals, i.e. ``{**{a:b}}`` */
255 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
256 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500257 case Set_kind:
258 return validate_exprs(exp->v.Set.elts, Load, 0);
259#define COMP(NAME) \
260 case NAME ## _kind: \
261 return validate_comprehension(exp->v.NAME.generators) && \
262 validate_expr(exp->v.NAME.elt, Load);
263 COMP(ListComp)
264 COMP(SetComp)
265 COMP(GeneratorExp)
266#undef COMP
267 case DictComp_kind:
268 return validate_comprehension(exp->v.DictComp.generators) &&
269 validate_expr(exp->v.DictComp.key, Load) &&
270 validate_expr(exp->v.DictComp.value, Load);
271 case Yield_kind:
272 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500273 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000274 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400275 case Await_kind:
276 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500277 case Compare_kind:
278 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
279 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
280 return 0;
281 }
282 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
283 asdl_seq_LEN(exp->v.Compare.ops)) {
284 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
285 "of comparators and operands");
286 return 0;
287 }
288 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
289 validate_expr(exp->v.Compare.left, Load);
290 case Call_kind:
291 return validate_expr(exp->v.Call.func, Load) &&
292 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400293 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 case Constant_kind:
295 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100296 PyErr_Format(PyExc_TypeError,
297 "got an invalid type in Constant: %s",
298 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100299 return 0;
300 }
301 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400302 case JoinedStr_kind:
303 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
304 case FormattedValue_kind:
305 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
306 return 0;
307 if (exp->v.FormattedValue.format_spec)
308 return validate_expr(exp->v.FormattedValue.format_spec, Load);
309 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500310 case Attribute_kind:
311 return validate_expr(exp->v.Attribute.value, Load);
312 case Subscript_kind:
313 return validate_slice(exp->v.Subscript.slice) &&
314 validate_expr(exp->v.Subscript.value, Load);
315 case Starred_kind:
316 return validate_expr(exp->v.Starred.value, ctx);
317 case List_kind:
318 return validate_exprs(exp->v.List.elts, ctx, 0);
319 case Tuple_kind:
320 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
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;
324 default:
325 PyErr_SetString(PyExc_SystemError, "unexpected expression");
326 return 0;
327 }
328}
329
330static int
331validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
332{
333 if (asdl_seq_LEN(seq))
334 return 1;
335 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
336 return 0;
337}
338
339static int
340validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
341{
342 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
343 validate_exprs(targets, ctx, 0);
344}
345
346static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300349 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500350}
351
352static int
353validate_stmt(stmt_ty stmt)
354{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100355 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 switch (stmt->kind) {
357 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300358 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500359 validate_arguments(stmt->v.FunctionDef.args) &&
360 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
361 (!stmt->v.FunctionDef.returns ||
362 validate_expr(stmt->v.FunctionDef.returns, Load));
363 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300364 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500365 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
366 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400367 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 case Return_kind:
369 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
370 case Delete_kind:
371 return validate_assignlist(stmt->v.Delete.targets, Del);
372 case Assign_kind:
373 return validate_assignlist(stmt->v.Assign.targets, Store) &&
374 validate_expr(stmt->v.Assign.value, Load);
375 case AugAssign_kind:
376 return validate_expr(stmt->v.AugAssign.target, Store) &&
377 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700378 case AnnAssign_kind:
379 if (stmt->v.AnnAssign.target->kind != Name_kind &&
380 stmt->v.AnnAssign.simple) {
381 PyErr_SetString(PyExc_TypeError,
382 "AnnAssign with simple non-Name target");
383 return 0;
384 }
385 return validate_expr(stmt->v.AnnAssign.target, Store) &&
386 (!stmt->v.AnnAssign.value ||
387 validate_expr(stmt->v.AnnAssign.value, Load)) &&
388 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 case For_kind:
390 return validate_expr(stmt->v.For.target, Store) &&
391 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300392 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400394 case AsyncFor_kind:
395 return validate_expr(stmt->v.AsyncFor.target, Store) &&
396 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400398 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500399 case While_kind:
400 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.While.orelse);
403 case If_kind:
404 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300405 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 validate_stmts(stmt->v.If.orelse);
407 case With_kind:
408 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
409 return 0;
410 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
411 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
412 if (!validate_expr(item->context_expr, Load) ||
413 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
414 return 0;
415 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300416 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncWith_kind:
418 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
419 return 0;
420 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
421 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
422 if (!validate_expr(item->context_expr, Load) ||
423 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
424 return 0;
425 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300426 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case Raise_kind:
428 if (stmt->v.Raise.exc) {
429 return validate_expr(stmt->v.Raise.exc, Load) &&
430 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
431 }
432 if (stmt->v.Raise.cause) {
433 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
434 return 0;
435 }
436 return 1;
437 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300438 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500439 return 0;
440 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
441 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
442 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
443 return 0;
444 }
445 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
446 asdl_seq_LEN(stmt->v.Try.orelse)) {
447 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
448 return 0;
449 }
450 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
451 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
452 if ((handler->v.ExceptHandler.type &&
453 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300454 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 return 0;
456 }
457 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
458 validate_stmts(stmt->v.Try.finalbody)) &&
459 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
460 validate_stmts(stmt->v.Try.orelse));
461 case Assert_kind:
462 return validate_expr(stmt->v.Assert.test, Load) &&
463 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
464 case Import_kind:
465 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
466 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300467 if (stmt->v.ImportFrom.level < 0) {
468 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500469 return 0;
470 }
471 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
472 case Global_kind:
473 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
474 case Nonlocal_kind:
475 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
476 case Expr_kind:
477 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400478 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300479 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400480 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
481 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
482 (!stmt->v.AsyncFunctionDef.returns ||
483 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500484 case Pass_kind:
485 case Break_kind:
486 case Continue_kind:
487 return 1;
488 default:
489 PyErr_SetString(PyExc_SystemError, "unexpected statement");
490 return 0;
491 }
492}
493
494static int
495validate_stmts(asdl_seq *seq)
496{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100497 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500498 for (i = 0; i < asdl_seq_LEN(seq); i++) {
499 stmt_ty stmt = asdl_seq_GET(seq, i);
500 if (stmt) {
501 if (!validate_stmt(stmt))
502 return 0;
503 }
504 else {
505 PyErr_SetString(PyExc_ValueError,
506 "None disallowed in statement list");
507 return 0;
508 }
509 }
510 return 1;
511}
512
513static int
514validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
515{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100516 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
518 expr_ty expr = asdl_seq_GET(exprs, i);
519 if (expr) {
520 if (!validate_expr(expr, ctx))
521 return 0;
522 }
523 else if (!null_ok) {
524 PyErr_SetString(PyExc_ValueError,
525 "None disallowed in expression list");
526 return 0;
527 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100528
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500529 }
530 return 1;
531}
532
533int
534PyAST_Validate(mod_ty mod)
535{
536 int res = 0;
537
538 switch (mod->kind) {
539 case Module_kind:
540 res = validate_stmts(mod->v.Module.body);
541 break;
542 case Interactive_kind:
543 res = validate_stmts(mod->v.Interactive.body);
544 break;
545 case Expression_kind:
546 res = validate_expr(mod->v.Expression.body, Load);
547 break;
548 case Suite_kind:
549 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
550 break;
551 default:
552 PyErr_SetString(PyExc_SystemError, "impossible module node");
553 res = 0;
554 break;
555 }
556 return res;
557}
558
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500559/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500560#include "grammar.h"
561#include "parsetok.h"
562#include "graminit.h"
563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564/* Data structure used internally */
565struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400566 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200567 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 PyObject *c_normalize; /* Normalization function from unicodedata. */
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);
705 if (PyArena_AddPyObject(c->c_arena, res) < 0) {
706 Py_DECREF(res);
707 return NULL;
708 }
709 return res;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800710}
Guido van Rossumd2b4c192019-02-01 15:28:13 -0800711#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c)
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713static int
714num_stmts(const node *n)
715{
716 int i, l;
717 node *ch;
718
719 switch (TYPE(n)) {
720 case single_input:
721 if (TYPE(CHILD(n, 0)) == NEWLINE)
722 return 0;
723 else
724 return num_stmts(CHILD(n, 0));
725 case file_input:
726 l = 0;
727 for (i = 0; i < NCH(n); i++) {
728 ch = CHILD(n, i);
729 if (TYPE(ch) == stmt)
730 l += num_stmts(ch);
731 }
732 return l;
733 case stmt:
734 return num_stmts(CHILD(n, 0));
735 case compound_stmt:
736 return 1;
737 case simple_stmt:
738 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
739 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800740 case func_body_suite:
741 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
742 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 if (NCH(n) == 1)
744 return num_stmts(CHILD(n, 0));
745 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800746 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800748 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
749 i += 2;
750 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 l += num_stmts(CHILD(n, i));
752 return l;
753 }
754 default: {
755 char buf[128];
756
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000757 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 TYPE(n), NCH(n));
759 Py_FatalError(buf);
760 }
761 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700762 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763}
764
765/* Transform the CST rooted at node * to the appropriate AST
766*/
767
768mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200769PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
770 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000772 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800774 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 stmt_ty s;
776 node *ch;
777 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500778 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800779 asdl_seq *argtypes = NULL;
780 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400782 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200783 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400784 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800785 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800786
787 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789
Jeremy Hyltona8293132006-02-28 17:58:27 +0000790 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 switch (TYPE(n)) {
792 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200793 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500795 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 for (i = 0; i < NCH(n) - 1; i++) {
797 ch = CHILD(n, i);
798 if (TYPE(ch) == NEWLINE)
799 continue;
800 REQ(ch, stmt);
801 num = num_stmts(ch);
802 if (num == 1) {
803 s = ast_for_stmt(&c, ch);
804 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500805 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000806 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808 else {
809 ch = CHILD(ch, 0);
810 REQ(ch, simple_stmt);
811 for (j = 0; j < num; j++) {
812 s = ast_for_stmt(&c, CHILD(ch, j * 2));
813 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500814 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000815 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 }
817 }
818 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800819
820 /* Type ignores are stored under the ENDMARKER in file_input. */
821 ch = CHILD(n, NCH(n) - 1);
822 REQ(ch, ENDMARKER);
823 num = NCH(ch);
824 type_ignores = _Py_asdl_seq_new(num, arena);
825 if (!type_ignores)
826 goto out;
827
828 for (i = 0; i < num; i++) {
829 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
830 if (!ti)
831 goto out;
832 asdl_seq_SET(type_ignores, i, ti);
833 }
834
835 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500836 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 case eval_input: {
838 expr_ty testlist_ast;
839
Nick Coghlan650f0d02007-04-15 12:05:43 +0000840 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000841 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 goto out;
844 res = Expression(testlist_ast, arena);
845 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 }
847 case single_input:
848 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200849 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500851 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000852 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000853 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000854 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000855 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500856 goto out;
857 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 }
859 else {
860 n = CHILD(n, 0);
861 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200862 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500864 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000866 s = ast_for_stmt(&c, n);
867 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500868 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 asdl_seq_SET(stmts, 0, s);
870 }
871 else {
872 /* Only a simple_stmt can contain multiple statements. */
873 REQ(n, simple_stmt);
874 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 if (TYPE(CHILD(n, i)) == NEWLINE)
876 break;
877 s = ast_for_stmt(&c, CHILD(n, i));
878 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500879 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 asdl_seq_SET(stmts, i / 2, s);
881 }
882 }
883
Benjamin Peterson55e00432012-01-16 17:22:31 -0500884 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500886 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800887 case func_type_input:
888 n = CHILD(n, 0);
889 REQ(n, func_type);
890
891 if (TYPE(CHILD(n, 1)) == typelist) {
892 ch = CHILD(n, 1);
893 /* this is overly permissive -- we don't pay any attention to
894 * stars on the args -- just parse them into an ordered list */
895 num = 0;
896 for (i = 0; i < NCH(ch); i++) {
897 if (TYPE(CHILD(ch, i)) == test) {
898 num++;
899 }
900 }
901
902 argtypes = _Py_asdl_seq_new(num, arena);
903 if (!argtypes)
904 goto out;
905
906 j = 0;
907 for (i = 0; i < NCH(ch); i++) {
908 if (TYPE(CHILD(ch, i)) == test) {
909 arg = ast_for_expr(&c, CHILD(ch, i));
910 if (!arg)
911 goto out;
912 asdl_seq_SET(argtypes, j++, arg);
913 }
914 }
915 }
916 else {
917 argtypes = _Py_asdl_seq_new(0, arena);
918 if (!argtypes)
919 goto out;
920 }
921
922 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
923 if (!ret)
924 goto out;
925 res = FunctionType(argtypes, ret, arena);
926 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000928 PyErr_Format(PyExc_SystemError,
929 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500930 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500932 out:
933 if (c.c_normalize) {
934 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500935 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500936 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937}
938
Victor Stinner14e461d2013-08-26 22:28:21 +0200939mod_ty
940PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
941 PyArena *arena)
942{
943 mod_ty mod;
944 PyObject *filename;
945 filename = PyUnicode_DecodeFSDefault(filename_str);
946 if (filename == NULL)
947 return NULL;
948 mod = PyAST_FromNodeObject(n, flags, filename, arena);
949 Py_DECREF(filename);
950 return mod;
951
952}
953
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
955*/
956
957static operator_ty
958get_operator(const node *n)
959{
960 switch (TYPE(n)) {
961 case VBAR:
962 return BitOr;
963 case CIRCUMFLEX:
964 return BitXor;
965 case AMPER:
966 return BitAnd;
967 case LEFTSHIFT:
968 return LShift;
969 case RIGHTSHIFT:
970 return RShift;
971 case PLUS:
972 return Add;
973 case MINUS:
974 return Sub;
975 case STAR:
976 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400977 case AT:
978 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 case SLASH:
980 return Div;
981 case DOUBLESLASH:
982 return FloorDiv;
983 case PERCENT:
984 return Mod;
985 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000986 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 }
988}
989
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200990static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000991 "None",
992 "True",
993 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200994 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000995 NULL,
996};
997
998static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400999forbidden_name(struct compiling *c, identifier name, const node *n,
1000 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +00001001{
Benjamin Peterson78565b22009-06-28 19:19:51 +00001002 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001003 const char * const *p = FORBIDDEN;
1004 if (!full_checks) {
1005 /* In most cases, the parser will protect True, False, and None
1006 from being assign to. */
1007 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001008 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001009 for (; *p; p++) {
1010 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1011 ast_error(c, n, "cannot assign to %U", name);
1012 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001013 }
1014 }
1015 return 0;
1016}
1017
Serhiy Storchakab619b092018-11-27 09:40:29 +02001018static expr_ty
1019copy_location(expr_ty e, const node *n)
1020{
1021 if (e) {
1022 e->lineno = LINENO(n);
1023 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001024 e->end_lineno = n->n_end_lineno;
1025 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001026 }
1027 return e;
1028}
1029
Jeremy Hyltona8293132006-02-28 17:58:27 +00001030/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031
1032 Only sets context for expr kinds that "can appear in assignment context"
1033 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1034 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035*/
1036
1037static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001038set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039{
1040 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001041 /* If a particular expression type can't be used for assign / delete,
1042 set expr_name to its name and an error message will be generated.
1043 */
1044 const char* expr_name = NULL;
1045
1046 /* The ast defines augmented store and load contexts, but the
1047 implementation here doesn't actually use them. The code may be
1048 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001049 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001050 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001051 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001052 */
1053 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054
1055 switch (e->kind) {
1056 case Attribute_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001057 if (ctx == NamedStore) {
1058 expr_name = "attribute";
1059 break;
1060 }
1061
Thomas Wouters89f507f2006-12-13 04:49:30 +00001062 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001063 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001064 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001065 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 case Subscript_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001067 if (ctx == NamedStore) {
1068 expr_name = "subscript";
1069 break;
1070 }
1071
Thomas Wouters89f507f2006-12-13 04:49:30 +00001072 e->v.Subscript.ctx = ctx;
1073 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001074 case Starred_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001075 if (ctx == NamedStore) {
1076 expr_name = "starred";
1077 break;
1078 }
1079
Guido van Rossum0368b722007-05-11 16:50:42 +00001080 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001081 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001082 return 0;
1083 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001085 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001086 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001087 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001088 }
1089 e->v.Name.ctx = ctx;
1090 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 case List_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001092 if (ctx == NamedStore) {
1093 expr_name = "list";
1094 break;
1095 }
1096
Thomas Wouters89f507f2006-12-13 04:49:30 +00001097 e->v.List.ctx = ctx;
1098 s = e->v.List.elts;
1099 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 case Tuple_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001101 if (ctx == NamedStore) {
1102 expr_name = "tuple";
1103 break;
1104 }
1105
Berker Peksag094c9c92016-05-18 08:44:29 +03001106 e->v.Tuple.ctx = ctx;
1107 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001108 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001109 case Lambda_kind:
1110 expr_name = "lambda";
1111 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001113 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001114 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001115 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001117 case UnaryOp_kind:
1118 expr_name = "operator";
1119 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001121 expr_name = "generator expression";
1122 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001123 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001124 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001125 expr_name = "yield expression";
1126 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001127 case Await_kind:
1128 expr_name = "await expression";
1129 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001130 case ListComp_kind:
1131 expr_name = "list comprehension";
1132 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001133 case SetComp_kind:
1134 expr_name = "set comprehension";
1135 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001136 case DictComp_kind:
1137 expr_name = "dict comprehension";
1138 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001139 case Dict_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001140 expr_name = "dict display";
1141 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001142 case Set_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001143 expr_name = "set display";
1144 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001145 case JoinedStr_kind:
1146 case FormattedValue_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001147 expr_name = "f-string expression";
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001148 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001149 case Constant_kind: {
1150 PyObject *value = e->v.Constant.value;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001151 if (value == Py_None || value == Py_False || value == Py_True
1152 || value == Py_Ellipsis)
1153 {
1154 return ast_error(c, n, "cannot %s %R",
1155 ctx == Store ? "assign to" : "delete",
1156 value);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001157 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001158 expr_name = "literal";
Benjamin Peterson442f2092012-12-06 17:41:04 -05001159 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001160 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001161 case Compare_kind:
1162 expr_name = "comparison";
1163 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001164 case IfExp_kind:
1165 expr_name = "conditional expression";
1166 break;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001167 case NamedExpr_kind:
1168 expr_name = "named expression";
1169 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001170 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 PyErr_Format(PyExc_SystemError,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001172 "unexpected expression in %sassignment %d (line %d)",
1173 ctx == NamedStore ? "named ": "",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001174 e->kind, e->lineno);
1175 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001177 /* Check for error string set by switch */
1178 if (expr_name) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001179 if (ctx == NamedStore) {
1180 return ast_error(c, n, "cannot use named assignment with %s",
1181 expr_name);
1182 }
1183 else {
1184 return ast_error(c, n, "cannot %s %s",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001185 ctx == Store ? "assign to" : "delete",
1186 expr_name);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001187 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001188 }
1189
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 */
1193 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001194 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195
Thomas Wouters89f507f2006-12-13 04:49:30 +00001196 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001197 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001198 return 0;
1199 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 }
1201 return 1;
1202}
1203
1204static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001205ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206{
1207 REQ(n, augassign);
1208 n = CHILD(n, 0);
1209 switch (STR(n)[0]) {
1210 case '+':
1211 return Add;
1212 case '-':
1213 return Sub;
1214 case '/':
1215 if (STR(n)[1] == '/')
1216 return FloorDiv;
1217 else
1218 return Div;
1219 case '%':
1220 return Mod;
1221 case '<':
1222 return LShift;
1223 case '>':
1224 return RShift;
1225 case '&':
1226 return BitAnd;
1227 case '^':
1228 return BitXor;
1229 case '|':
1230 return BitOr;
1231 case '*':
1232 if (STR(n)[1] == '*')
1233 return Pow;
1234 else
1235 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001236 case '@':
1237 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001239 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001240 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 }
1242}
1243
1244static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001245ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001247 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 |'is' 'not'
1249 */
1250 REQ(n, comp_op);
1251 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001252 n = CHILD(n, 0);
1253 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 case LESS:
1255 return Lt;
1256 case GREATER:
1257 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001258 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 return Eq;
1260 case LESSEQUAL:
1261 return LtE;
1262 case GREATEREQUAL:
1263 return GtE;
1264 case NOTEQUAL:
1265 return NotEq;
1266 case NAME:
1267 if (strcmp(STR(n), "in") == 0)
1268 return In;
1269 if (strcmp(STR(n), "is") == 0)
1270 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001271 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001273 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001276 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 }
1278 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001279 /* handle "not in" and "is not" */
1280 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 case NAME:
1282 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1283 return NotIn;
1284 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1285 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001286 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001288 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001290 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001291 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 }
Neal Norwitz79792652005-11-14 04:25:03 +00001293 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001295 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296}
1297
1298static asdl_seq *
1299seq_for_testlist(struct compiling *c, const node *n)
1300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001302 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1303 */
Armin Rigo31441302005-10-21 12:57:31 +00001304 asdl_seq *seq;
1305 expr_ty expression;
1306 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001307 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001309 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 if (!seq)
1311 return NULL;
1312
1313 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001315 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316
Benjamin Peterson4905e802009-09-27 02:43:28 +00001317 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001318 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320
1321 assert(i / 2 < seq->size);
1322 asdl_seq_SET(seq, i / 2, expression);
1323 }
1324 return seq;
1325}
1326
Neal Norwitzc1505362006-12-28 06:47:50 +00001327static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001328ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001329{
1330 identifier name;
1331 expr_ty annotation = NULL;
1332 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001333 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001334
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001337 name = NEW_IDENTIFIER(ch);
1338 if (!name)
1339 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001340 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001341 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001342
1343 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1344 annotation = ast_for_expr(c, CHILD(n, 2));
1345 if (!annotation)
1346 return NULL;
1347 }
1348
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001349 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001350 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001351 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001352 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001353 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354}
1355
Guido van Rossum4f72a782006-10-27 23:31:49 +00001356/* returns -1 if failed to handle keyword only arguments
1357 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001358 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 ^^^
1360 start pointing here
1361 */
1362static int
1363handle_keywordonly_args(struct compiling *c, const node *n, int start,
1364 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1365{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001366 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001368 expr_ty expression, annotation;
1369 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 int i = start;
1371 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001372
1373 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001374 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001375 return -1;
1376 }
1377 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 while (i < NCH(n)) {
1379 ch = CHILD(n, i);
1380 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001381 case vfpdef:
1382 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001384 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001385 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001386 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001387 asdl_seq_SET(kwdefaults, j, expression);
1388 i += 2; /* '=' and test */
1389 }
1390 else { /* setting NULL if no default value exists */
1391 asdl_seq_SET(kwdefaults, j, NULL);
1392 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001393 if (NCH(ch) == 3) {
1394 /* ch is NAME ':' test */
1395 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001396 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001397 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001398 }
1399 else {
1400 annotation = NULL;
1401 }
1402 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001403 argname = NEW_IDENTIFIER(ch);
1404 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001406 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001407 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001408 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001409 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001410 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001411 if (!arg)
1412 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001413 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001414 i += 1; /* the name */
1415 if (TYPE(CHILD(n, i)) == COMMA)
1416 i += 1; /* the comma, if present */
1417 break;
1418 case TYPE_COMMENT:
1419 /* arg will be equal to the last argument processed */
1420 arg->type_comment = NEW_TYPE_COMMENT(ch);
1421 if (!arg->type_comment)
1422 goto error;
1423 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 break;
1425 case DOUBLESTAR:
1426 return i;
1427 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001428 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001429 goto error;
1430 }
1431 }
1432 return i;
1433 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001435}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436
Jeremy Hyltona8293132006-02-28 17:58:27 +00001437/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438
1439static arguments_ty
1440ast_for_arguments(struct compiling *c, const node *n)
1441{
Neal Norwitzc1505362006-12-28 06:47:50 +00001442 /* This function handles both typedargslist (function definition)
1443 and varargslist (lambda definition).
1444
1445 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001446 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1447 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1448 | '**' tfpdef [',']]]
1449 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1450 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001451 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001452 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1453 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1454 | '**' vfpdef [',']]]
1455 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1456 | '**' vfpdef [',']
1457 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001458 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001461 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1462 int nposdefaults = 0, found_default = 0;
1463 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001464 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001465 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 node *ch;
1467
1468 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001469 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001470 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001471 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001473 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474
Jeremy Hyltone921e022008-07-17 16:37:17 +00001475 /* First count the number of positional args & defaults. The
1476 variable i is the loop index for this for loop and the next.
1477 The next loop picks up where the first leaves off.
1478 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001480 ch = CHILD(n, i);
1481 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001482 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001483 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001484 if (i < NCH(n) && /* skip argument following star */
1485 (TYPE(CHILD(n, i)) == tfpdef ||
1486 TYPE(CHILD(n, i)) == vfpdef)) {
1487 i++;
1488 }
1489 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001490 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001491 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001492 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496 defaults for keyword only args */
1497 for ( ; i < NCH(n); ++i) {
1498 ch = CHILD(n, i);
1499 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001500 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001501 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001502 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001504 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001505 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001506 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001507 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001508 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001510 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001511 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001512 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001514 since we set NULL as default for keyword only argument w/o default
1515 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001516 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001517 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001518 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001519 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001520
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001521 /* tfpdef: NAME [':' test]
1522 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 */
1524 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001525 j = 0; /* index for defaults */
1526 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001528 ch = CHILD(n, i);
1529 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001530 case tfpdef:
1531 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1533 anything other than EQUAL or a comma? */
1534 /* XXX Should NCH(n) check be made a separate check? */
1535 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001536 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1537 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001538 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001539 assert(posdefaults != NULL);
1540 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001542 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001544 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001545 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001546 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001547 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001548 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001549 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001550 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001551 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001552 asdl_seq_SET(posargs, k++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001553 i += 1; /* the name */
1554 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1555 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 break;
1557 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001558 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001559 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1560 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001561 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001562 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001563 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001564 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001565 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001566 if (TYPE(ch) == COMMA) {
1567 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001568 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001569
1570 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1571 ast_error(c, CHILD(n, i),
1572 "bare * has associated type comment");
1573 return NULL;
1574 }
1575
Guido van Rossum4f72a782006-10-27 23:31:49 +00001576 res = handle_keywordonly_args(c, n, i,
1577 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001578 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001579 i = res; /* res has new position to process */
1580 }
1581 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001582 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001583 if (!vararg)
1584 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001585
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001586 i += 2; /* the star and the name */
1587 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1588 i += 1; /* the comma, if present */
1589
1590 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1591 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1592 if (!vararg->type_comment)
1593 return NULL;
1594 i += 1;
1595 }
1596
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001597 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1598 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001599 int res = 0;
1600 res = handle_keywordonly_args(c, n, i,
1601 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001602 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001603 i = res; /* res has new position to process */
1604 }
1605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 break;
1607 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001608 ch = CHILD(n, i+1); /* tfpdef */
1609 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001610 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001611 if (!kwarg)
1612 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001613 i += 2; /* the double star and the name */
1614 if (TYPE(CHILD(n, i)) == COMMA)
1615 i += 1; /* the comma, if present */
1616 break;
1617 case TYPE_COMMENT:
1618 assert(i);
1619
1620 if (kwarg)
1621 arg = kwarg;
1622
1623 /* arg will be equal to the last argument processed */
1624 arg->type_comment = NEW_TYPE_COMMENT(ch);
1625 if (!arg->type_comment)
1626 return NULL;
1627 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 break;
1629 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001630 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 "unexpected node in varargslist: %d @ %d",
1632 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001633 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001636 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637}
1638
1639static expr_ty
1640ast_for_dotted_name(struct compiling *c, const node *n)
1641{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001642 expr_ty e;
1643 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001644 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001646 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647
1648 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001649
1650 lineno = LINENO(n);
1651 col_offset = n->n_col_offset;
1652
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001653 ch = CHILD(n, 0);
1654 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001656 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001657 e = Name(id, Load, lineno, col_offset,
1658 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001660 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661
1662 for (i = 2; i < NCH(n); i+=2) {
1663 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001664 if (!id)
1665 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001666 e = Attribute(e, id, Load, lineno, col_offset,
1667 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001668 if (!e)
1669 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 }
1671
1672 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673}
1674
1675static expr_ty
1676ast_for_decorator(struct compiling *c, const node *n)
1677{
1678 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1679 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001680 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001683 REQ(CHILD(n, 0), AT);
1684 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1687 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001688 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001691 d = name_expr;
1692 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 }
1694 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001695 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001696 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001697 if (!d)
1698 return NULL;
1699 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 }
1701 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001702 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001703 if (!d)
1704 return NULL;
1705 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 }
1707
1708 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709}
1710
1711static asdl_seq*
1712ast_for_decorators(struct compiling *c, const node *n)
1713{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001714 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001715 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001719 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 if (!decorator_seq)
1721 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001724 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001725 if (!d)
1726 return NULL;
1727 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 }
1729 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730}
1731
1732static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001733ast_for_funcdef_impl(struct compiling *c, const node *n0,
1734 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001736 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001737 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001738 identifier name;
1739 arguments_ty args;
1740 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001741 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001742 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001743 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001744 node *tc;
1745 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746
1747 REQ(n, funcdef);
1748
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 name = NEW_IDENTIFIER(CHILD(n, name_i));
1750 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001751 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001752 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001753 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1755 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001756 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001757 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1758 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1759 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001760 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001761 name_i += 2;
1762 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001763 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1764 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1765 if (!type_comment)
1766 return NULL;
1767 name_i += 1;
1768 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001769 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001771 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001772 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001774 if (NCH(CHILD(n, name_i + 3)) > 1) {
1775 /* Check if the suite has a type comment in it. */
1776 tc = CHILD(CHILD(n, name_i + 3), 1);
1777
1778 if (TYPE(tc) == TYPE_COMMENT) {
1779 if (type_comment != NULL) {
1780 ast_error(c, n, "Cannot have two type comments on def");
1781 return NULL;
1782 }
1783 type_comment = NEW_TYPE_COMMENT(tc);
1784 if (!type_comment)
1785 return NULL;
1786 }
1787 }
1788
Yury Selivanov75445082015-05-11 22:57:16 -04001789 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001790 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001791 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001792 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001793 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001794 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001795}
1796
1797static stmt_ty
1798ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1799{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001800 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001801 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001802 REQ(CHILD(n, 0), NAME);
1803 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001804 REQ(CHILD(n, 1), funcdef);
1805
guoci90fc8982018-09-11 17:45:45 -04001806 return ast_for_funcdef_impl(c, n, decorator_seq,
1807 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001808}
1809
1810static stmt_ty
1811ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1812{
1813 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1814 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001815 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001816}
1817
1818
1819static stmt_ty
1820ast_for_async_stmt(struct compiling *c, const node *n)
1821{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001822 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001823 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001824 REQ(CHILD(n, 0), NAME);
1825 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001826
1827 switch (TYPE(CHILD(n, 1))) {
1828 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001829 return ast_for_funcdef_impl(c, n, NULL,
1830 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001831 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001832 return ast_for_with_stmt(c, n,
1833 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001834
1835 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001836 return ast_for_for_stmt(c, n,
1837 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001838
1839 default:
1840 PyErr_Format(PyExc_SystemError,
1841 "invalid async stament: %s",
1842 STR(CHILD(n, 1)));
1843 return NULL;
1844 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845}
1846
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001847static stmt_ty
1848ast_for_decorated(struct compiling *c, const node *n)
1849{
Yury Selivanov75445082015-05-11 22:57:16 -04001850 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001851 stmt_ty thing = NULL;
1852 asdl_seq *decorator_seq = NULL;
1853
1854 REQ(n, decorated);
1855
1856 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1857 if (!decorator_seq)
1858 return NULL;
1859
1860 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001861 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001863
1864 if (TYPE(CHILD(n, 1)) == funcdef) {
1865 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1866 } else if (TYPE(CHILD(n, 1)) == classdef) {
1867 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001868 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1869 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001870 }
1871 return thing;
1872}
1873
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001875ast_for_namedexpr(struct compiling *c, const node *n)
1876{
1877 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1878 ['else' ':' suite]
1879 namedexpr_test: test [':=' test]
1880 argument: ( test [comp_for] |
1881 test ':=' test |
1882 test '=' test |
1883 '**' test |
1884 '*' test )
1885 */
1886 expr_ty target, value;
1887
1888 target = ast_for_expr(c, CHILD(n, 0));
1889 if (!target)
1890 return NULL;
1891
1892 value = ast_for_expr(c, CHILD(n, 2));
1893 if (!value)
1894 return NULL;
1895
1896 if (!set_context(c, target, NamedStore, n))
1897 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) {
1968 REQ(CHILD(n, 0), NAME);
1969 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1970 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001971 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001972 else if (NCH(n) == 1) {
1973 n = CHILD(n, 0);
1974 }
1975 else {
1976 goto error;
1977 }
1978 if (NCH(n) == (5)) {
1979 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001980 }
1981 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001982 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001983 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001984 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001985 REQ(n, comp_iter);
1986 n = CHILD(n, 0);
1987 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001988 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001989 else if (TYPE(n) == comp_if) {
1990 if (NCH(n) == 3) {
1991 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001992 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001993 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001994 else
1995 return n_fors;
1996 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001997
Jelle Zijlstraac317702017-10-05 20:24:46 -07001998 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001999 /* Should never be reached */
2000 PyErr_SetString(PyExc_SystemError,
2001 "logic error in count_comp_fors");
2002 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003}
2004
Nick Coghlan650f0d02007-04-15 12:05:43 +00002005/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006
Nick Coghlan650f0d02007-04-15 12:05:43 +00002007 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008*/
2009
2010static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002011count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002013 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014
Guido van Rossumd8faa362007-04-27 19:54:29 +00002015 while (1) {
2016 REQ(n, comp_iter);
2017 if (TYPE(CHILD(n, 0)) == comp_for)
2018 return n_ifs;
2019 n = CHILD(n, 0);
2020 REQ(n, comp_if);
2021 n_ifs++;
2022 if (NCH(n) == 2)
2023 return n_ifs;
2024 n = CHILD(n, 2);
2025 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026}
2027
Guido van Rossum992d4a32007-07-11 13:09:30 +00002028static asdl_seq *
2029ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002032 asdl_seq *comps;
2033
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002034 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 if (n_fors == -1)
2036 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002037
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002038 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002039 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002043 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002044 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002045 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002046 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002047 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002048 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049
Guido van Rossum992d4a32007-07-11 13:09:30 +00002050 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051
Jelle Zijlstraac317702017-10-05 20:24:46 -07002052 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002053 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002054 REQ(CHILD(n, 0), NAME);
2055 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
2056 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002057 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002058 else {
2059 sync_n = CHILD(n, 0);
2060 }
2061 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002062
Jelle Zijlstraac317702017-10-05 20:24:46 -07002063 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002064 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002067 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002068 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002070
Thomas Wouters89f507f2006-12-13 04:49:30 +00002071 /* Check the # of children rather than the length of t, since
2072 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002073 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002074 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002075 comp = comprehension(first, expression, NULL,
2076 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002078 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2079 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2080 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002081 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002082 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002084
Jelle Zijlstraac317702017-10-05 20:24:46 -07002085 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 int j, n_ifs;
2087 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088
Jelle Zijlstraac317702017-10-05 20:24:46 -07002089 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002090 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002091 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002093
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002094 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002095 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002099 REQ(n, comp_iter);
2100 n = CHILD(n, 0);
2101 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102
Guido van Rossum992d4a32007-07-11 13:09:30 +00002103 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002104 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002105 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002106 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002107 if (NCH(n) == 3)
2108 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002110 /* on exit, must guarantee that n is a comp_for */
2111 if (TYPE(n) == comp_iter)
2112 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002113 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002115 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002117 return comps;
2118}
2119
2120static expr_ty
2121ast_for_itercomp(struct compiling *c, const node *n, int type)
2122{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002123 /* testlist_comp: (test|star_expr)
2124 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002125 expr_ty elt;
2126 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002127 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128
Guido van Rossum992d4a32007-07-11 13:09:30 +00002129 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002131 ch = CHILD(n, 0);
2132 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002133 if (!elt)
2134 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002135 if (elt->kind == Starred_kind) {
2136 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2137 return NULL;
2138 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139
Guido van Rossum992d4a32007-07-11 13:09:30 +00002140 comps = ast_for_comprehension(c, CHILD(n, 1));
2141 if (!comps)
2142 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002143
2144 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002145 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2146 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002147 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002148 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2149 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002150 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002151 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2152 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002153 else
2154 /* Should never happen */
2155 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156}
2157
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002158/* Fills in the key, value pair corresponding to the dict element. In case
2159 * of an unpacking, key is NULL. *i is advanced by the number of ast
2160 * elements. Iff successful, nonzero is returned.
2161 */
2162static int
2163ast_for_dictelement(struct compiling *c, const node *n, int *i,
2164 expr_ty *key, expr_ty *value)
2165{
2166 expr_ty expression;
2167 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2168 assert(NCH(n) - *i >= 2);
2169
2170 expression = ast_for_expr(c, CHILD(n, *i + 1));
2171 if (!expression)
2172 return 0;
2173 *key = NULL;
2174 *value = expression;
2175
2176 *i += 2;
2177 }
2178 else {
2179 assert(NCH(n) - *i >= 3);
2180
2181 expression = ast_for_expr(c, CHILD(n, *i));
2182 if (!expression)
2183 return 0;
2184 *key = expression;
2185
2186 REQ(CHILD(n, *i + 1), COLON);
2187
2188 expression = ast_for_expr(c, CHILD(n, *i + 2));
2189 if (!expression)
2190 return 0;
2191 *value = expression;
2192
2193 *i += 3;
2194 }
2195 return 1;
2196}
2197
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002199ast_for_dictcomp(struct compiling *c, const node *n)
2200{
2201 expr_ty key, value;
2202 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002203 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002206 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002207 assert(key);
2208 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002210 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002211 if (!comps)
2212 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002214 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2215 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002216}
2217
2218static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002219ast_for_dictdisplay(struct compiling *c, const node *n)
2220{
2221 int i;
2222 int j;
2223 int size;
2224 asdl_seq *keys, *values;
2225
2226 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2227 keys = _Py_asdl_seq_new(size, c->c_arena);
2228 if (!keys)
2229 return NULL;
2230
2231 values = _Py_asdl_seq_new(size, c->c_arena);
2232 if (!values)
2233 return NULL;
2234
2235 j = 0;
2236 for (i = 0; i < NCH(n); i++) {
2237 expr_ty key, value;
2238
2239 if (!ast_for_dictelement(c, n, &i, &key, &value))
2240 return NULL;
2241 asdl_seq_SET(keys, j, key);
2242 asdl_seq_SET(values, j, value);
2243
2244 j++;
2245 }
2246 keys->size = j;
2247 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002248 return Dict(keys, values, LINENO(n), n->n_col_offset,
2249 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002250}
2251
2252static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002253ast_for_genexp(struct compiling *c, const node *n)
2254{
2255 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002256 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002257}
2258
2259static expr_ty
2260ast_for_listcomp(struct compiling *c, const node *n)
2261{
2262 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002263 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002264}
2265
2266static expr_ty
2267ast_for_setcomp(struct compiling *c, const node *n)
2268{
2269 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002270 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002271}
2272
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002273static expr_ty
2274ast_for_setdisplay(struct compiling *c, const node *n)
2275{
2276 int i;
2277 int size;
2278 asdl_seq *elts;
2279
2280 assert(TYPE(n) == (dictorsetmaker));
2281 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2282 elts = _Py_asdl_seq_new(size, c->c_arena);
2283 if (!elts)
2284 return NULL;
2285 for (i = 0; i < NCH(n); i += 2) {
2286 expr_ty expression;
2287 expression = ast_for_expr(c, CHILD(n, i));
2288 if (!expression)
2289 return NULL;
2290 asdl_seq_SET(elts, i / 2, expression);
2291 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002292 return Set(elts, LINENO(n), n->n_col_offset,
2293 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002294}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002295
2296static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297ast_for_atom(struct compiling *c, const node *n)
2298{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002299 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2300 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002301 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 */
2303 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002306 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002307 PyObject *name;
2308 const char *s = STR(ch);
2309 size_t len = strlen(s);
2310 if (len >= 4 && len <= 5) {
2311 if (!strcmp(s, "None"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002312 return Constant(Py_None, LINENO(n), n->n_col_offset,
2313 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002314 if (!strcmp(s, "True"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002315 return Constant(Py_True, LINENO(n), n->n_col_offset,
2316 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002317 if (!strcmp(s, "False"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002318 return Constant(Py_False, LINENO(n), n->n_col_offset,
2319 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002320 }
2321 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002322 if (!name)
2323 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002324 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002325 return Name(name, Load, LINENO(n), n->n_col_offset,
2326 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002327 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002329 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002330 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002331 const char *errtype = NULL;
2332 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2333 errtype = "unicode error";
2334 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2335 errtype = "value error";
2336 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002337 PyObject *type, *value, *tback, *errstr;
2338 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002339 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002340 if (errstr) {
2341 ast_error(c, n, "(%s) %U", errtype, errstr);
2342 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002343 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002344 else {
2345 PyErr_Clear();
2346 ast_error(c, n, "(%s) unknown error", errtype);
2347 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002348 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002349 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002350 Py_XDECREF(tback);
2351 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002352 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002353 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002354 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355 }
2356 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002357 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002358 if (!pynum)
2359 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002360
Victor Stinner43d81952013-07-17 00:57:58 +02002361 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2362 Py_DECREF(pynum);
2363 return NULL;
2364 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002365 return Constant(pynum, LINENO(n), n->n_col_offset,
2366 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367 }
Georg Brandldde00282007-03-18 19:01:53 +00002368 case ELLIPSIS: /* Ellipsis */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002369 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset,
2370 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002372 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373
Thomas Wouters89f507f2006-12-13 04:49:30 +00002374 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002375 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2376 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377
Thomas Wouters89f507f2006-12-13 04:49:30 +00002378 if (TYPE(ch) == yield_expr)
2379 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002382 if (NCH(ch) == 1) {
2383 return ast_for_testlist(c, ch);
2384 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002385
Serhiy Storchakab619b092018-11-27 09:40:29 +02002386 if (TYPE(CHILD(ch, 1)) == comp_for) {
2387 return copy_location(ast_for_genexp(c, ch), n);
2388 }
2389 else {
2390 return copy_location(ast_for_testlist(c, ch), n);
2391 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002393 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394
Thomas Wouters89f507f2006-12-13 04:49:30 +00002395 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002396 return List(NULL, Load, LINENO(n), n->n_col_offset,
2397 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398
Nick Coghlan650f0d02007-04-15 12:05:43 +00002399 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002400 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2401 asdl_seq *elts = seq_for_testlist(c, ch);
2402 if (!elts)
2403 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002404
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002405 return List(elts, Load, LINENO(n), n->n_col_offset,
2406 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002407 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002408 else {
2409 return copy_location(ast_for_listcomp(c, ch), n);
2410 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002412 /* dictorsetmaker: ( ((test ':' test | '**' test)
2413 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2414 * ((test | '*' test)
2415 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002416 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002417 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002418 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002419 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002420 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2421 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002422 }
2423 else {
2424 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2425 if (NCH(ch) == 1 ||
2426 (NCH(ch) > 1 &&
2427 TYPE(CHILD(ch, 1)) == COMMA)) {
2428 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002429 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002430 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002431 else if (NCH(ch) > 1 &&
2432 TYPE(CHILD(ch, 1)) == comp_for) {
2433 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002434 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002435 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002436 else if (NCH(ch) > 3 - is_dict &&
2437 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2438 /* It's a dictionary comprehension. */
2439 if (is_dict) {
2440 ast_error(c, n, "dict unpacking cannot be used in "
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002441 "dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002442 return NULL;
2443 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002444 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002445 }
2446 else {
2447 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002448 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002449 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002450 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002451 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002454 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2455 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002456 }
2457}
2458
2459static slice_ty
2460ast_for_slice(struct compiling *c, const node *n)
2461{
2462 node *ch;
2463 expr_ty lower = NULL, upper = NULL, step = NULL;
2464
2465 REQ(n, subscript);
2466
2467 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002468 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 sliceop: ':' [test]
2470 */
2471 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 if (NCH(n) == 1 && TYPE(ch) == test) {
2473 /* 'step' variable hold no significance in terms of being used over
2474 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 if (!step)
2477 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478
Thomas Wouters89f507f2006-12-13 04:49:30 +00002479 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 }
2481
2482 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002483 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 if (!lower)
2485 return NULL;
2486 }
2487
2488 /* If there's an upper bound it's in the second or third position. */
2489 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002490 if (NCH(n) > 1) {
2491 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492
Thomas Wouters89f507f2006-12-13 04:49:30 +00002493 if (TYPE(n2) == test) {
2494 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 if (!upper)
2496 return NULL;
2497 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002498 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002500 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501
Thomas Wouters89f507f2006-12-13 04:49:30 +00002502 if (TYPE(n2) == test) {
2503 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 if (!upper)
2505 return NULL;
2506 }
2507 }
2508
2509 ch = CHILD(n, NCH(n) - 1);
2510 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002511 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002512 ch = CHILD(ch, 1);
2513 if (TYPE(ch) == test) {
2514 step = ast_for_expr(c, ch);
2515 if (!step)
2516 return NULL;
2517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 }
2519 }
2520
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002521 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522}
2523
2524static expr_ty
2525ast_for_binop(struct compiling *c, const node *n)
2526{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002527 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002529 BinOp(BinOp(A, op, B), op, C).
2530 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531
Guido van Rossumd8faa362007-04-27 19:54:29 +00002532 int i, nops;
2533 expr_ty expr1, expr2, result;
2534 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535
Guido van Rossumd8faa362007-04-27 19:54:29 +00002536 expr1 = ast_for_expr(c, CHILD(n, 0));
2537 if (!expr1)
2538 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539
Guido van Rossumd8faa362007-04-27 19:54:29 +00002540 expr2 = ast_for_expr(c, CHILD(n, 2));
2541 if (!expr2)
2542 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543
Guido van Rossumd8faa362007-04-27 19:54:29 +00002544 newoperator = get_operator(CHILD(n, 1));
2545 if (!newoperator)
2546 return NULL;
2547
2548 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002549 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002550 c->c_arena);
2551 if (!result)
2552 return NULL;
2553
2554 nops = (NCH(n) - 1) / 2;
2555 for (i = 1; i < nops; i++) {
2556 expr_ty tmp_result, tmp;
2557 const node* next_oper = CHILD(n, i * 2 + 1);
2558
2559 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002560 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 return NULL;
2562
Guido van Rossumd8faa362007-04-27 19:54:29 +00002563 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2564 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 return NULL;
2566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002568 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002569 CHILD(n, i * 2 + 2)->n_end_lineno,
2570 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002571 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002573 return NULL;
2574 result = tmp_result;
2575 }
2576 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577}
2578
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002579static expr_ty
2580ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002583 subscriptlist: subscript (',' subscript)* [',']
2584 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2585 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002586 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002587 REQ(n, trailer);
2588 if (TYPE(CHILD(n, 0)) == LPAR) {
2589 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002590 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2591 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002592 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002593 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002594 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002595 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002596 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2597 if (!attr_id)
2598 return NULL;
2599 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002600 LINENO(n), n->n_col_offset,
2601 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002602 }
2603 else {
2604 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002605 REQ(CHILD(n, 2), RSQB);
2606 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002607 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002608 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2609 if (!slc)
2610 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002611 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002612 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002613 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002614 }
2615 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002617 by treating the sequence as a tuple literal if there are
2618 no slice features.
2619 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002620 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002621 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002622 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002623 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002624 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002625 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002626 if (!slices)
2627 return NULL;
2628 for (j = 0; j < NCH(n); j += 2) {
2629 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002630 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002631 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002632 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002633 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002634 asdl_seq_SET(slices, j / 2, slc);
2635 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002636 if (!simple) {
2637 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002638 Load, LINENO(n), n->n_col_offset,
2639 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002640 }
2641 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002642 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002643 if (!elts)
2644 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002645 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2646 slc = (slice_ty)asdl_seq_GET(slices, j);
2647 assert(slc->kind == Index_kind && slc->v.Index.value);
2648 asdl_seq_SET(elts, j, slc->v.Index.value);
2649 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002650 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2651 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002652 if (!e)
2653 return NULL;
2654 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002655 Load, LINENO(n), n->n_col_offset,
2656 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002657 }
2658 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002659}
2660
2661static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002662ast_for_factor(struct compiling *c, const node *n)
2663{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002664 expr_ty expression;
2665
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002666 expression = ast_for_expr(c, CHILD(n, 1));
2667 if (!expression)
2668 return NULL;
2669
2670 switch (TYPE(CHILD(n, 0))) {
2671 case PLUS:
2672 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002673 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002674 c->c_arena);
2675 case MINUS:
2676 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002677 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002678 c->c_arena);
2679 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002680 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2681 n->n_end_lineno, n->n_end_col_offset,
2682 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002683 }
2684 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2685 TYPE(CHILD(n, 0)));
2686 return NULL;
2687}
2688
2689static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002690ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002691{
Yury Selivanov75445082015-05-11 22:57:16 -04002692 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002693 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002694
2695 REQ(n, atom_expr);
2696 nch = NCH(n);
2697
Jelle Zijlstraac317702017-10-05 20:24:46 -07002698 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002699 start = 1;
2700 assert(nch > 1);
2701 }
2702
2703 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002704 if (!e)
2705 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002706 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002707 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002708 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002709 return Await(e, LINENO(n), n->n_col_offset,
2710 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002711 }
2712
2713 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002714 node *ch = CHILD(n, i);
2715 if (TYPE(ch) != trailer)
2716 break;
2717 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002718 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002719 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002720 tmp->lineno = e->lineno;
2721 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002722 e = tmp;
2723 }
Yury Selivanov75445082015-05-11 22:57:16 -04002724
2725 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002726 /* there was an 'await' */
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 else {
2731 return e;
2732 }
2733}
2734
2735static expr_ty
2736ast_for_power(struct compiling *c, const node *n)
2737{
2738 /* power: atom trailer* ('**' factor)*
2739 */
2740 expr_ty e;
2741 REQ(n, power);
2742 e = ast_for_atom_expr(c, CHILD(n, 0));
2743 if (!e)
2744 return NULL;
2745 if (NCH(n) == 1)
2746 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002747 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2748 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002749 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002750 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002751 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2752 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002753 }
2754 return e;
2755}
2756
Guido van Rossum0368b722007-05-11 16:50:42 +00002757static expr_ty
2758ast_for_starred(struct compiling *c, const node *n)
2759{
2760 expr_ty tmp;
2761 REQ(n, star_expr);
2762
2763 tmp = ast_for_expr(c, CHILD(n, 1));
2764 if (!tmp)
2765 return NULL;
2766
2767 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002768 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2769 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002770}
2771
2772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773/* Do not name a variable 'expr'! Will cause a compile error.
2774*/
2775
2776static expr_ty
2777ast_for_expr(struct compiling *c, const node *n)
2778{
2779 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002780 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002781 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002782 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 and_test: not_test ('and' not_test)*
2785 not_test: 'not' not_test | comparison
2786 comparison: expr (comp_op expr)*
2787 expr: xor_expr ('|' xor_expr)*
2788 xor_expr: and_expr ('^' and_expr)*
2789 and_expr: shift_expr ('&' shift_expr)*
2790 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2791 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002792 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002794 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002795 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002796 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 */
2798
2799 asdl_seq *seq;
2800 int i;
2801
2802 loop:
2803 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002804 case namedexpr_test:
2805 if (NCH(n) == 3)
2806 return ast_for_namedexpr(c, n);
2807 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002809 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002810 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002811 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002813 else if (NCH(n) > 1)
2814 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 /* Fallthrough */
2816 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 case and_test:
2818 if (NCH(n) == 1) {
2819 n = CHILD(n, 0);
2820 goto loop;
2821 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002822 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (!seq)
2824 return NULL;
2825 for (i = 0; i < NCH(n); i += 2) {
2826 expr_ty e = ast_for_expr(c, CHILD(n, i));
2827 if (!e)
2828 return NULL;
2829 asdl_seq_SET(seq, i / 2, e);
2830 }
2831 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002832 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002833 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002834 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002835 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002836 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2837 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838 case not_test:
2839 if (NCH(n) == 1) {
2840 n = CHILD(n, 0);
2841 goto loop;
2842 }
2843 else {
2844 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2845 if (!expression)
2846 return NULL;
2847
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002848 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002849 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002850 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 }
2852 case comparison:
2853 if (NCH(n) == 1) {
2854 n = CHILD(n, 0);
2855 goto loop;
2856 }
2857 else {
2858 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002859 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002860 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002861 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 if (!ops)
2863 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002864 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 return NULL;
2867 }
2868 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002869 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002871 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002872 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002874 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875
2876 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002877 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002879 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002881 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 asdl_seq_SET(cmps, i / 2, expression);
2883 }
2884 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002885 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002887 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002889 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2890 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 }
2892 break;
2893
Guido van Rossum0368b722007-05-11 16:50:42 +00002894 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 /* The next five cases all handle BinOps. The main body of code
2897 is the same in each case, but the switch turned inside out to
2898 reuse the code for each type of operator.
2899 */
2900 case expr:
2901 case xor_expr:
2902 case and_expr:
2903 case shift_expr:
2904 case arith_expr:
2905 case term:
2906 if (NCH(n) == 1) {
2907 n = CHILD(n, 0);
2908 goto loop;
2909 }
2910 return ast_for_binop(c, n);
2911 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002912 node *an = NULL;
2913 node *en = NULL;
2914 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002916 if (NCH(n) > 1)
2917 an = CHILD(n, 1); /* yield_arg */
2918 if (an) {
2919 en = CHILD(an, NCH(an) - 1);
2920 if (NCH(an) == 2) {
2921 is_from = 1;
2922 exp = ast_for_expr(c, en);
2923 }
2924 else
2925 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002926 if (!exp)
2927 return NULL;
2928 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002929 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002930 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2931 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2932 return Yield(exp, LINENO(n), n->n_col_offset,
2933 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002934 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002935 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 if (NCH(n) == 1) {
2937 n = CHILD(n, 0);
2938 goto loop;
2939 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002940 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002941 case power:
2942 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002944 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002945 return NULL;
2946 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002947 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 return NULL;
2949}
2950
2951static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002952ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002953 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954{
2955 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002956 arglist: argument (',' argument)* [',']
2957 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 */
2959
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002960 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002961 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002962 asdl_seq *args;
2963 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964
2965 REQ(n, arglist);
2966
2967 nargs = 0;
2968 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002970 node *ch = CHILD(n, i);
2971 if (TYPE(ch) == argument) {
2972 if (NCH(ch) == 1)
2973 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002974 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2975 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002976 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002977 ast_error(c, ch, "invalid syntax");
2978 return NULL;
2979 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002980 if (NCH(n) > 1) {
2981 ast_error(c, ch, "Generator expression must be parenthesized");
2982 return NULL;
2983 }
2984 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002985 else if (TYPE(CHILD(ch, 0)) == STAR)
2986 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002987 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2988 nargs++;
2989 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002991 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002992 nkeywords++;
2993 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002996 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002998 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002999 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003001 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003002
3003 nargs = 0; /* positional arguments + iterable argument unpackings */
3004 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3005 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003007 node *ch = CHILD(n, i);
3008 if (TYPE(ch) == argument) {
3009 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003010 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003011 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003012 /* a positional argument */
3013 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003014 if (ndoublestars) {
3015 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003016 "positional argument follows "
3017 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003018 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003019 else {
3020 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003021 "positional argument follows "
3022 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003023 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003024 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003025 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003026 e = ast_for_expr(c, chch);
3027 if (!e)
3028 return NULL;
3029 asdl_seq_SET(args, nargs++, e);
3030 }
3031 else if (TYPE(chch) == STAR) {
3032 /* an iterable argument unpacking */
3033 expr_ty starred;
3034 if (ndoublestars) {
3035 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003036 "iterable argument unpacking follows "
3037 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003038 return NULL;
3039 }
3040 e = ast_for_expr(c, CHILD(ch, 1));
3041 if (!e)
3042 return NULL;
3043 starred = Starred(e, Load, LINENO(chch),
3044 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003045 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003046 c->c_arena);
3047 if (!starred)
3048 return NULL;
3049 asdl_seq_SET(args, nargs++, starred);
3050
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003051 }
3052 else if (TYPE(chch) == DOUBLESTAR) {
3053 /* a keyword argument unpacking */
3054 keyword_ty kw;
3055 i++;
3056 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003058 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003059 kw = keyword(NULL, e, c->c_arena);
3060 asdl_seq_SET(keywords, nkeywords++, kw);
3061 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003063 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003064 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003065 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003067 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003068 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003069 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003070 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3071 /* treat colon equal as positional argument */
3072 if (nkeywords) {
3073 if (ndoublestars) {
3074 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003075 "positional argument follows "
3076 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003077 }
3078 else {
3079 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003080 "positional argument follows "
3081 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003082 }
3083 return NULL;
3084 }
3085 e = ast_for_namedexpr(c, ch);
3086 if (!e)
3087 return NULL;
3088 asdl_seq_SET(args, nargs++, e);
3089 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003090 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003091 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003092 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003093 identifier key, tmp;
3094 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003096 // To remain LL(1), the grammar accepts any test (basically, any
3097 // expression) in the keyword slot of a call site. So, we need
3098 // to manually enforce that the keyword is a NAME here.
3099 static const int name_tree[] = {
3100 test,
3101 or_test,
3102 and_test,
3103 not_test,
3104 comparison,
3105 expr,
3106 xor_expr,
3107 and_expr,
3108 shift_expr,
3109 arith_expr,
3110 term,
3111 factor,
3112 power,
3113 atom_expr,
3114 atom,
3115 0,
3116 };
3117 node *expr_node = chch;
3118 for (int i = 0; name_tree[i]; i++) {
3119 if (TYPE(expr_node) != name_tree[i])
3120 break;
3121 if (NCH(expr_node) != 1)
3122 break;
3123 expr_node = CHILD(expr_node, 0);
3124 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003125 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003126 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003127 "expression cannot contain assignment, "
3128 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003129 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003130 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003131 key = new_identifier(STR(expr_node), c);
3132 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003133 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003135 if (forbidden_name(c, key, chch, 1)) {
3136 return NULL;
3137 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003138 for (k = 0; k < nkeywords; k++) {
3139 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003140 if (tmp && !PyUnicode_Compare(tmp, key)) {
3141 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003142 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003143 return NULL;
3144 }
3145 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003146 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003148 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003149 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003151 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003152 asdl_seq_SET(keywords, nkeywords++, kw);
3153 }
3154 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 }
3156
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003157 return Call(func, args, keywords, func->lineno, func->col_offset,
3158 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159}
3160
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003162ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003164 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003165 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003167 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003168 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003169 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003170 }
3171 else {
3172 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003173 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003174 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003176 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 else {
3178 asdl_seq *tmp = seq_for_testlist(c, n);
3179 if (!tmp)
3180 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003181 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3182 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003184}
3185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186static stmt_ty
3187ast_for_expr_stmt(struct compiling *c, const node *n)
3188{
3189 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003190 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003191 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3192 annassign: ':' test ['=' (yield_expr|testlist)]
3193 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3194 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3195 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003196 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003198 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003200 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003201 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 if (!e)
3203 return NULL;
3204
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003205 return Expr(e, LINENO(n), n->n_col_offset,
3206 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 }
3208 else if (TYPE(CHILD(n, 1)) == augassign) {
3209 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003210 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003211 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212
Thomas Wouters89f507f2006-12-13 04:49:30 +00003213 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 if (!expr1)
3215 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003216 if(!set_context(c, expr1, Store, ch))
3217 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003218 /* set_context checks that most expressions are not the left side.
3219 Augmented assignments can only have a name, a subscript, or an
3220 attribute on the left, though, so we have to explicitly check for
3221 those. */
3222 switch (expr1->kind) {
3223 case Name_kind:
3224 case Attribute_kind:
3225 case Subscript_kind:
3226 break;
3227 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003228 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003229 return NULL;
3230 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231
Thomas Wouters89f507f2006-12-13 04:49:30 +00003232 ch = CHILD(n, 2);
3233 if (TYPE(ch) == testlist)
3234 expr2 = ast_for_testlist(c, ch);
3235 else
3236 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003237 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 return NULL;
3239
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003240 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003241 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242 return NULL;
3243
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003244 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3245 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003247 else if (TYPE(CHILD(n, 1)) == annassign) {
3248 expr_ty expr1, expr2, expr3;
3249 node *ch = CHILD(n, 0);
3250 node *deep, *ann = CHILD(n, 1);
3251 int simple = 1;
3252
3253 /* we keep track of parens to qualify (x) as expression not name */
3254 deep = ch;
3255 while (NCH(deep) == 1) {
3256 deep = CHILD(deep, 0);
3257 }
3258 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3259 simple = 0;
3260 }
3261 expr1 = ast_for_testlist(c, ch);
3262 if (!expr1) {
3263 return NULL;
3264 }
3265 switch (expr1->kind) {
3266 case Name_kind:
3267 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3268 return NULL;
3269 }
3270 expr1->v.Name.ctx = Store;
3271 break;
3272 case Attribute_kind:
3273 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3274 return NULL;
3275 }
3276 expr1->v.Attribute.ctx = Store;
3277 break;
3278 case Subscript_kind:
3279 expr1->v.Subscript.ctx = Store;
3280 break;
3281 case List_kind:
3282 ast_error(c, ch,
3283 "only single target (not list) can be annotated");
3284 return NULL;
3285 case Tuple_kind:
3286 ast_error(c, ch,
3287 "only single target (not tuple) can be annotated");
3288 return NULL;
3289 default:
3290 ast_error(c, ch,
3291 "illegal target for annotation");
3292 return NULL;
3293 }
3294
3295 if (expr1->kind != Name_kind) {
3296 simple = 0;
3297 }
3298 ch = CHILD(ann, 1);
3299 expr2 = ast_for_expr(c, ch);
3300 if (!expr2) {
3301 return NULL;
3302 }
3303 if (NCH(ann) == 2) {
3304 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003305 LINENO(n), n->n_col_offset,
3306 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003307 }
3308 else {
3309 ch = CHILD(ann, 3);
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003310 if (TYPE(ch) == testlist) {
3311 expr3 = ast_for_testlist(c, ch);
3312 }
3313 else {
3314 expr3 = ast_for_expr(c, ch);
3315 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003316 if (!expr3) {
3317 return NULL;
3318 }
3319 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003320 LINENO(n), n->n_col_offset,
3321 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003322 }
3323 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003325 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003326 asdl_seq *targets;
3327 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003329 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330
Thomas Wouters89f507f2006-12-13 04:49:30 +00003331 /* a normal assignment */
3332 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003333
3334 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3335 nch_minus_type = num - has_type_comment;
3336
3337 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 if (!targets)
3339 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003340 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003341 expr_ty e;
3342 node *ch = CHILD(n, i);
3343 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003344 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003345 return NULL;
3346 }
3347 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003349 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003351 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003352 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 asdl_seq_SET(targets, i / 2, e);
3356 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003357 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003358 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003359 expression = ast_for_testlist(c, value);
3360 else
3361 expression = ast_for_expr(c, value);
3362 if (!expression)
3363 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003364 if (has_type_comment) {
3365 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3366 if (!type_comment)
3367 return NULL;
3368 }
3369 else
3370 type_comment = NULL;
3371 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003372 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374}
3375
Benjamin Peterson78565b22009-06-28 19:19:51 +00003376
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003378ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379{
3380 asdl_seq *seq;
3381 int i;
3382 expr_ty e;
3383
3384 REQ(n, exprlist);
3385
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003386 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003388 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003390 e = ast_for_expr(c, CHILD(n, i));
3391 if (!e)
3392 return NULL;
3393 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003394 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003395 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396 }
3397 return seq;
3398}
3399
3400static stmt_ty
3401ast_for_del_stmt(struct compiling *c, const node *n)
3402{
3403 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 /* del_stmt: 'del' exprlist */
3406 REQ(n, del_stmt);
3407
3408 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3409 if (!expr_list)
3410 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003411 return Delete(expr_list, LINENO(n), n->n_col_offset,
3412 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413}
3414
3415static stmt_ty
3416ast_for_flow_stmt(struct compiling *c, const node *n)
3417{
3418 /*
3419 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3420 | yield_stmt
3421 break_stmt: 'break'
3422 continue_stmt: 'continue'
3423 return_stmt: 'return' [testlist]
3424 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003425 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426 raise_stmt: 'raise' [test [',' test [',' test]]]
3427 */
3428 node *ch;
3429
3430 REQ(n, flow_stmt);
3431 ch = CHILD(n, 0);
3432 switch (TYPE(ch)) {
3433 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003434 return Break(LINENO(n), n->n_col_offset,
3435 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003437 return Continue(LINENO(n), n->n_col_offset,
3438 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003440 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3441 if (!exp)
3442 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003443 return Expr(exp, LINENO(n), n->n_col_offset,
3444 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445 }
3446 case return_stmt:
3447 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003448 return Return(NULL, LINENO(n), n->n_col_offset,
3449 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003451 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 if (!expression)
3453 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003454 return Return(expression, LINENO(n), n->n_col_offset,
3455 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 }
3457 case raise_stmt:
3458 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003459 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3460 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003461 else if (NCH(ch) >= 2) {
3462 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3464 if (!expression)
3465 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003466 if (NCH(ch) == 4) {
3467 cause = ast_for_expr(c, CHILD(ch, 3));
3468 if (!cause)
3469 return NULL;
3470 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003471 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3472 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473 }
Stefan Krahf432a322017-08-21 13:09:59 +02003474 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003476 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 "unexpected flow_stmt: %d", TYPE(ch));
3478 return NULL;
3479 }
3480}
3481
3482static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003483alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484{
3485 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003486 import_as_name: NAME ['as' NAME]
3487 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 dotted_name: NAME ('.' NAME)*
3489 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003490 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003491
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492 loop:
3493 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003494 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003495 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003496 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003497 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003498 if (!name)
3499 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003500 if (NCH(n) == 3) {
3501 node *str_node = CHILD(n, 2);
3502 str = NEW_IDENTIFIER(str_node);
3503 if (!str)
3504 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003505 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003506 return NULL;
3507 }
3508 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003509 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003510 return NULL;
3511 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003512 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514 case dotted_as_name:
3515 if (NCH(n) == 1) {
3516 n = CHILD(n, 0);
3517 goto loop;
3518 }
3519 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003520 node *asname_node = CHILD(n, 2);
3521 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003522 if (!a)
3523 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003525 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003526 if (!a->asname)
3527 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003528 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003529 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530 return a;
3531 }
3532 break;
3533 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003534 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003535 node *name_node = CHILD(n, 0);
3536 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003537 if (!name)
3538 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003539 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003540 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003541 return alias(name, NULL, c->c_arena);
3542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543 else {
3544 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003545 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003546 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549
3550 len = 0;
3551 for (i = 0; i < NCH(n); i += 2)
3552 /* length of string plus one for the dot */
3553 len += strlen(STR(CHILD(n, i))) + 1;
3554 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003555 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 if (!str)
3557 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003558 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 if (!s)
3560 return NULL;
3561 for (i = 0; i < NCH(n); i += 2) {
3562 char *sch = STR(CHILD(n, i));
3563 strcpy(s, STR(CHILD(n, i)));
3564 s += strlen(sch);
3565 *s++ = '.';
3566 }
3567 --s;
3568 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3570 PyBytes_GET_SIZE(str),
3571 NULL);
3572 Py_DECREF(str);
3573 if (!uni)
3574 return NULL;
3575 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003576 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003577 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3578 Py_DECREF(str);
3579 return NULL;
3580 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003581 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 }
3583 break;
3584 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003585 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003586 if (!str)
3587 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003588 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3589 Py_DECREF(str);
3590 return NULL;
3591 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003592 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003594 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 "unexpected import name: %d", TYPE(n));
3596 return NULL;
3597 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003598
3599 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 return NULL;
3601}
3602
3603static stmt_ty
3604ast_for_import_stmt(struct compiling *c, const node *n)
3605{
3606 /*
3607 import_stmt: import_name | import_from
3608 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003609 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3610 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003612 int lineno;
3613 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 int i;
3615 asdl_seq *aliases;
3616
3617 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003618 lineno = LINENO(n);
3619 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003621 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003623 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003624 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003625 if (!aliases)
3626 return NULL;
3627 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003628 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003629 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003631 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003633 // Even though n is modified above, the end position is not changed
3634 return Import(aliases, lineno, col_offset,
3635 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003637 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003639 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003640 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003641 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003642 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003644 /* Count the number of dots (for relative imports) and check for the
3645 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003646 for (idx = 1; idx < NCH(n); idx++) {
3647 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003648 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3649 if (!mod)
3650 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003651 idx++;
3652 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003653 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003655 ndots += 3;
3656 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003657 } else if (TYPE(CHILD(n, idx)) != DOT) {
3658 break;
3659 }
3660 ndots++;
3661 }
3662 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003663 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003664 case STAR:
3665 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003666 n = CHILD(n, idx);
3667 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003668 break;
3669 case LPAR:
3670 /* from ... import (x, y, z) */
3671 n = CHILD(n, idx + 1);
3672 n_children = NCH(n);
3673 break;
3674 case import_as_names:
3675 /* from ... import x, y, z */
3676 n = CHILD(n, idx);
3677 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003678 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003679 ast_error(c, n,
3680 "trailing comma not allowed without"
3681 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003682 return NULL;
3683 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003684 break;
3685 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003686 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003687 return NULL;
3688 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003690 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003691 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003692 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693
3694 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003695 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003696 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003697 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003698 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003699 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003701 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003702 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003703 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003704 if (!import_alias)
3705 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003706 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003709 if (mod != NULL)
3710 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003711 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003712 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003713 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 }
Neal Norwitz79792652005-11-14 04:25:03 +00003715 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716 "unknown import statement: starts with command '%s'",
3717 STR(CHILD(n, 0)));
3718 return NULL;
3719}
3720
3721static stmt_ty
3722ast_for_global_stmt(struct compiling *c, const node *n)
3723{
3724 /* global_stmt: 'global' NAME (',' NAME)* */
3725 identifier name;
3726 asdl_seq *s;
3727 int i;
3728
3729 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003730 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003731 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003732 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003734 name = NEW_IDENTIFIER(CHILD(n, i));
3735 if (!name)
3736 return NULL;
3737 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003739 return Global(s, LINENO(n), n->n_col_offset,
3740 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741}
3742
3743static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003744ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3745{
3746 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3747 identifier name;
3748 asdl_seq *s;
3749 int i;
3750
3751 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003752 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003753 if (!s)
3754 return NULL;
3755 for (i = 1; i < NCH(n); i += 2) {
3756 name = NEW_IDENTIFIER(CHILD(n, i));
3757 if (!name)
3758 return NULL;
3759 asdl_seq_SET(s, i / 2, name);
3760 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003761 return Nonlocal(s, LINENO(n), n->n_col_offset,
3762 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003763}
3764
3765static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766ast_for_assert_stmt(struct compiling *c, const node *n)
3767{
3768 /* assert_stmt: 'assert' test [',' test] */
3769 REQ(n, assert_stmt);
3770 if (NCH(n) == 2) {
3771 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3772 if (!expression)
3773 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003774 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3775 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 }
3777 else if (NCH(n) == 4) {
3778 expr_ty expr1, expr2;
3779
3780 expr1 = ast_for_expr(c, CHILD(n, 1));
3781 if (!expr1)
3782 return NULL;
3783 expr2 = ast_for_expr(c, CHILD(n, 3));
3784 if (!expr2)
3785 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003787 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3788 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789 }
Neal Norwitz79792652005-11-14 04:25:03 +00003790 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 "improper number of parts to 'assert' statement: %d",
3792 NCH(n));
3793 return NULL;
3794}
3795
3796static asdl_seq *
3797ast_for_suite(struct compiling *c, const node *n)
3798{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003799 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003800 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 stmt_ty s;
3802 int i, total, num, end, pos = 0;
3803 node *ch;
3804
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003805 if (TYPE(n) != func_body_suite) {
3806 REQ(n, suite);
3807 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808
3809 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003810 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003812 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003814 n = CHILD(n, 0);
3815 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003817 */
3818 end = NCH(n) - 1;
3819 if (TYPE(CHILD(n, end - 1)) == SEMI)
3820 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003822 for (i = 0; i < end; i += 2) {
3823 ch = CHILD(n, i);
3824 s = ast_for_stmt(c, ch);
3825 if (!s)
3826 return NULL;
3827 asdl_seq_SET(seq, pos++, s);
3828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 }
3830 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003831 i = 2;
3832 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3833 i += 2;
3834 REQ(CHILD(n, 2), NEWLINE);
3835 }
3836
3837 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003838 ch = CHILD(n, i);
3839 REQ(ch, stmt);
3840 num = num_stmts(ch);
3841 if (num == 1) {
3842 /* small_stmt or compound_stmt with only one child */
3843 s = ast_for_stmt(c, ch);
3844 if (!s)
3845 return NULL;
3846 asdl_seq_SET(seq, pos++, s);
3847 }
3848 else {
3849 int j;
3850 ch = CHILD(ch, 0);
3851 REQ(ch, simple_stmt);
3852 for (j = 0; j < NCH(ch); j += 2) {
3853 /* statement terminates with a semi-colon ';' */
3854 if (NCH(CHILD(ch, j)) == 0) {
3855 assert((j + 1) == NCH(ch));
3856 break;
3857 }
3858 s = ast_for_stmt(c, CHILD(ch, j));
3859 if (!s)
3860 return NULL;
3861 asdl_seq_SET(seq, pos++, s);
3862 }
3863 }
3864 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 }
3866 assert(pos == seq->size);
3867 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868}
3869
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003870static void
3871get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3872{
3873 int tot = asdl_seq_LEN(s);
3874 // Suite should not be empty, but it is safe to just ignore it
3875 // if it will ever occur.
3876 if (!tot) {
3877 return;
3878 }
3879 stmt_ty last = asdl_seq_GET(s, tot - 1);
3880 *end_lineno = last->end_lineno;
3881 *end_col_offset = last->end_col_offset;
3882}
3883
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884static stmt_ty
3885ast_for_if_stmt(struct compiling *c, const node *n)
3886{
3887 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3888 ['else' ':' suite]
3889 */
3890 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003891 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003892
3893 REQ(n, if_stmt);
3894
3895 if (NCH(n) == 4) {
3896 expr_ty expression;
3897 asdl_seq *suite_seq;
3898
3899 expression = ast_for_expr(c, CHILD(n, 1));
3900 if (!expression)
3901 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003903 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003905 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906
Guido van Rossumd8faa362007-04-27 19:54:29 +00003907 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003908 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003909 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911 s = STR(CHILD(n, 4));
3912 /* s[2], the third character in the string, will be
3913 's' for el_s_e, or
3914 'i' for el_i_f
3915 */
3916 if (s[2] == 's') {
3917 expr_ty expression;
3918 asdl_seq *seq1, *seq2;
3919
3920 expression = ast_for_expr(c, CHILD(n, 1));
3921 if (!expression)
3922 return NULL;
3923 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003924 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 return NULL;
3926 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003927 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003929 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930
Guido van Rossumd8faa362007-04-27 19:54:29 +00003931 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003932 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 }
3934 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003935 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003936 expr_ty expression;
3937 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003938 asdl_seq *orelse = NULL;
3939 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 /* must reference the child n_elif+1 since 'else' token is third,
3941 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003942 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3943 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3944 has_else = 1;
3945 n_elif -= 3;
3946 }
3947 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003948
Thomas Wouters89f507f2006-12-13 04:49:30 +00003949 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003950 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003952 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003953 if (!orelse)
3954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003956 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003958 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3959 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003961 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3962 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003964 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 asdl_seq_SET(orelse, 0,
3967 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003968 LINENO(CHILD(n, NCH(n) - 6)),
3969 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003970 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003971 /* the just-created orelse handled the last elif */
3972 n_elif--;
3973 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974
Thomas Wouters89f507f2006-12-13 04:49:30 +00003975 for (i = 0; i < n_elif; i++) {
3976 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003977 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003978 if (!newobj)
3979 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003981 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003984 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003987 if (orelse != NULL) {
3988 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3989 } else {
3990 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3991 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003992 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003994 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003995 CHILD(n, off)->n_col_offset,
3996 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003997 orelse = newobj;
3998 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003999 expression = ast_for_expr(c, CHILD(n, 1));
4000 if (!expression)
4001 return NULL;
4002 suite_seq = ast_for_suite(c, CHILD(n, 3));
4003 if (!suite_seq)
4004 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004005 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004006 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004007 LINENO(n), n->n_col_offset,
4008 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004009 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004010
4011 PyErr_Format(PyExc_SystemError,
4012 "unexpected token in 'if' statement: %s", s);
4013 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014}
4015
4016static stmt_ty
4017ast_for_while_stmt(struct compiling *c, const node *n)
4018{
4019 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4020 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004021 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022
4023 if (NCH(n) == 4) {
4024 expr_ty expression;
4025 asdl_seq *suite_seq;
4026
4027 expression = ast_for_expr(c, CHILD(n, 1));
4028 if (!expression)
4029 return NULL;
4030 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004031 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004032 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004033 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4034 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4035 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036 }
4037 else if (NCH(n) == 7) {
4038 expr_ty expression;
4039 asdl_seq *seq1, *seq2;
4040
4041 expression = ast_for_expr(c, CHILD(n, 1));
4042 if (!expression)
4043 return NULL;
4044 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004045 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046 return NULL;
4047 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004048 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004050 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004052 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4053 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004055
4056 PyErr_Format(PyExc_SystemError,
4057 "wrong number of tokens for 'while' statement: %d",
4058 NCH(n));
4059 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060}
4061
4062static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004063ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064{
guoci90fc8982018-09-11 17:45:45 -04004065 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004066 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004068 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004069 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004070 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004071 int has_type_comment;
4072 string type_comment;
4073 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074 REQ(n, for_stmt);
4075
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004076 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4077
4078 if (NCH(n) == 9 + has_type_comment) {
4079 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080 if (!seq)
4081 return NULL;
4082 }
4083
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004084 node_target = CHILD(n, 1);
4085 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004086 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004087 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004088 /* Check the # of children rather than the length of _target, since
4089 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004090 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004091 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004092 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004094 target = Tuple(_target, Store, first->lineno, first->col_offset,
4095 node_target->n_end_lineno, node_target->n_end_col_offset,
4096 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004097
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004098 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004099 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004100 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004101 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004102 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004103 return NULL;
4104
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004105 if (seq != NULL) {
4106 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4107 } else {
4108 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4109 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004110
4111 if (has_type_comment) {
4112 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4113 if (!type_comment)
4114 return NULL;
4115 }
4116 else
4117 type_comment = NULL;
4118
Yury Selivanov75445082015-05-11 22:57:16 -04004119 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004120 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004121 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004122 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004123 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004124 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004125 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004126 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004127}
4128
4129static excepthandler_ty
4130ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4131{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004132 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004133 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004134 REQ(exc, except_clause);
4135 REQ(body, suite);
4136
4137 if (NCH(exc) == 1) {
4138 asdl_seq *suite_seq = ast_for_suite(c, body);
4139 if (!suite_seq)
4140 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004141 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004142
Neal Norwitzad74aa82008-03-31 05:14:30 +00004143 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004144 exc->n_col_offset,
4145 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146 }
4147 else if (NCH(exc) == 2) {
4148 expr_ty expression;
4149 asdl_seq *suite_seq;
4150
4151 expression = ast_for_expr(c, CHILD(exc, 1));
4152 if (!expression)
4153 return NULL;
4154 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004155 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004156 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004157 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004158
Neal Norwitzad74aa82008-03-31 05:14:30 +00004159 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004160 exc->n_col_offset,
4161 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004162 }
4163 else if (NCH(exc) == 4) {
4164 asdl_seq *suite_seq;
4165 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004166 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004167 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004168 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004169 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004170 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004171 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004172 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173 return NULL;
4174 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004175 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004176 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004177 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178
Neal Norwitzad74aa82008-03-31 05:14:30 +00004179 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004180 exc->n_col_offset,
4181 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004182 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004183
4184 PyErr_Format(PyExc_SystemError,
4185 "wrong number of children for 'except' clause: %d",
4186 NCH(exc));
4187 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004188}
4189
4190static stmt_ty
4191ast_for_try_stmt(struct compiling *c, const node *n)
4192{
Neal Norwitzf599f422005-12-17 21:33:47 +00004193 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004194 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004195 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004196 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004197
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004198 REQ(n, try_stmt);
4199
Neal Norwitzf599f422005-12-17 21:33:47 +00004200 body = ast_for_suite(c, CHILD(n, 2));
4201 if (body == NULL)
4202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004203
Neal Norwitzf599f422005-12-17 21:33:47 +00004204 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4205 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4206 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4207 /* we can assume it's an "else",
4208 because nch >= 9 for try-else-finally and
4209 it would otherwise have a type of except_clause */
4210 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4211 if (orelse == NULL)
4212 return NULL;
4213 n_except--;
4214 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004215
Neal Norwitzf599f422005-12-17 21:33:47 +00004216 finally = ast_for_suite(c, CHILD(n, nch - 1));
4217 if (finally == NULL)
4218 return NULL;
4219 n_except--;
4220 }
4221 else {
4222 /* we can assume it's an "else",
4223 otherwise it would have a type of except_clause */
4224 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4225 if (orelse == NULL)
4226 return NULL;
4227 n_except--;
4228 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004229 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004230 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004231 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004232 return NULL;
4233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234
Neal Norwitzf599f422005-12-17 21:33:47 +00004235 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004236 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004237 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004238 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004239 if (handlers == NULL)
4240 return NULL;
4241
4242 for (i = 0; i < n_except; i++) {
4243 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4244 CHILD(n, 5 + i * 3));
4245 if (!e)
4246 return NULL;
4247 asdl_seq_SET(handlers, i, e);
4248 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004249 }
4250
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004251 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004252 if (finally != NULL) {
4253 // finally is always last
4254 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4255 } else if (orelse != NULL) {
4256 // otherwise else is last
4257 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4258 } else {
4259 // inline the get_last_end_pos logic due to layout mismatch
4260 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4261 end_lineno = last_handler->end_lineno;
4262 end_col_offset = last_handler->end_col_offset;
4263 }
4264 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4265 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004266}
4267
Georg Brandl0c315622009-05-25 21:10:36 +00004268/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004269static withitem_ty
4270ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004271{
4272 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004273
Georg Brandl0c315622009-05-25 21:10:36 +00004274 REQ(n, with_item);
4275 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004276 if (!context_expr)
4277 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004278 if (NCH(n) == 3) {
4279 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004280
4281 if (!optional_vars) {
4282 return NULL;
4283 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004284 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004285 return NULL;
4286 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004287 }
4288
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004289 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004290}
4291
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004292/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004293static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004294ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004295{
guoci90fc8982018-09-11 17:45:45 -04004296 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004297 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004298 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004299 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004300
4301 REQ(n, with_stmt);
4302
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004303 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4304 nch_minus_type = NCH(n) - has_type_comment;
4305
4306 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004307 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004308 if (!items)
4309 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004310 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004311 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4312 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004313 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004314 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004315 }
4316
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004317 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4318 if (!body)
4319 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004320 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004321
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004322 if (has_type_comment) {
4323 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4324 if (!type_comment)
4325 return NULL;
4326 }
4327 else
4328 type_comment = NULL;
4329
Yury Selivanov75445082015-05-11 22:57:16 -04004330 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004331 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004332 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004333 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004334 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004335 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004336}
4337
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004338static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004339ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004340{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004341 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004342 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004343 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004344 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004345 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004346
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004347 REQ(n, classdef);
4348
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004349 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004350 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004351 if (!s)
4352 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004353 get_last_end_pos(s, &end_lineno, &end_col_offset);
4354
Benjamin Peterson30760062008-11-25 04:02:28 +00004355 classname = NEW_IDENTIFIER(CHILD(n, 1));
4356 if (!classname)
4357 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004358 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004359 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004360 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004361 LINENO(n), n->n_col_offset,
4362 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004363 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004364
4365 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004366 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004367 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004368 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004369 get_last_end_pos(s, &end_lineno, &end_col_offset);
4370
Benjamin Peterson30760062008-11-25 04:02:28 +00004371 classname = NEW_IDENTIFIER(CHILD(n, 1));
4372 if (!classname)
4373 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004374 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004375 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004376 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004377 LINENO(n), n->n_col_offset,
4378 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004379 }
4380
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004381 /* class NAME '(' arglist ')' ':' suite */
4382 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004383 {
4384 PyObject *dummy_name;
4385 expr_ty dummy;
4386 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4387 if (!dummy_name)
4388 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004389 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4390 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4391 c->c_arena);
4392 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004393 if (!call)
4394 return NULL;
4395 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004396 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004397 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004398 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004399 get_last_end_pos(s, &end_lineno, &end_col_offset);
4400
Benjamin Peterson30760062008-11-25 04:02:28 +00004401 classname = NEW_IDENTIFIER(CHILD(n, 1));
4402 if (!classname)
4403 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004404 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004405 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004406
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004407 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004408 decorator_seq, LINENO(n), n->n_col_offset,
4409 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004410}
4411
4412static stmt_ty
4413ast_for_stmt(struct compiling *c, const node *n)
4414{
4415 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004416 assert(NCH(n) == 1);
4417 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004418 }
4419 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004420 assert(num_stmts(n) == 1);
4421 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004422 }
4423 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004424 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004425 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4426 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004427 */
4428 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004429 case expr_stmt:
4430 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004431 case del_stmt:
4432 return ast_for_del_stmt(c, n);
4433 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004434 return Pass(LINENO(n), n->n_col_offset,
4435 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004436 case flow_stmt:
4437 return ast_for_flow_stmt(c, n);
4438 case import_stmt:
4439 return ast_for_import_stmt(c, n);
4440 case global_stmt:
4441 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004442 case nonlocal_stmt:
4443 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004444 case assert_stmt:
4445 return ast_for_assert_stmt(c, n);
4446 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004447 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004448 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4449 TYPE(n), NCH(n));
4450 return NULL;
4451 }
4452 }
4453 else {
4454 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004455 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004456 */
4457 node *ch = CHILD(n, 0);
4458 REQ(n, compound_stmt);
4459 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004460 case if_stmt:
4461 return ast_for_if_stmt(c, ch);
4462 case while_stmt:
4463 return ast_for_while_stmt(c, ch);
4464 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004465 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004466 case try_stmt:
4467 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004468 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004469 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004470 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004471 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004472 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004473 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 case decorated:
4475 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004476 case async_stmt:
4477 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004478 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004479 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004480 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004481 TYPE(n), NCH(n));
4482 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004483 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004484 }
4485}
4486
4487static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004488parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004489{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004490 const char *end;
4491 long x;
4492 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004493 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004494 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004495
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004496 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004497 errno = 0;
4498 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004499 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004500 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004501 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004502 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004503 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004504 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004505 }
4506 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004507 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004508 if (*end == '\0') {
4509 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004510 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004511 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004512 }
4513 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004514 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004515 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004516 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4517 if (compl.imag == -1.0 && PyErr_Occurred())
4518 return NULL;
4519 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004520 }
4521 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004522 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004523 dx = PyOS_string_to_double(s, NULL, NULL);
4524 if (dx == -1.0 && PyErr_Occurred())
4525 return NULL;
4526 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004527 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004528}
4529
4530static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004531parsenumber(struct compiling *c, const char *s)
4532{
4533 char *dup, *end;
4534 PyObject *res = NULL;
4535
4536 assert(s != NULL);
4537
4538 if (strchr(s, '_') == NULL) {
4539 return parsenumber_raw(c, s);
4540 }
4541 /* Create a duplicate without underscores. */
4542 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004543 if (dup == NULL) {
4544 return PyErr_NoMemory();
4545 }
Brett Cannona721aba2016-09-09 14:57:09 -07004546 end = dup;
4547 for (; *s; s++) {
4548 if (*s != '_') {
4549 *end++ = *s;
4550 }
4551 }
4552 *end = '\0';
4553 res = parsenumber_raw(c, dup);
4554 PyMem_Free(dup);
4555 return res;
4556}
4557
4558static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004559decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004560{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004561 const char *s, *t;
4562 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004563 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4564 while (s < end && (*s & 0x80)) s++;
4565 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004566 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004567}
4568
Eric V. Smith56466482016-10-31 14:46:26 -04004569static int
4570warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004571 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004572{
4573 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4574 first_invalid_escape_char);
4575 if (msg == NULL) {
4576 return -1;
4577 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004578 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004579 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004580 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004581 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004582 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004583 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004584 to get a more accurate error report */
4585 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004586 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004587 }
4588 Py_DECREF(msg);
4589 return -1;
4590 }
4591 Py_DECREF(msg);
4592 return 0;
4593}
4594
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004595static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004596decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4597 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004598{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004599 PyObject *v, *u;
4600 char *buf;
4601 char *p;
4602 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004603
Benjamin Peterson202803a2016-02-25 22:34:45 -08004604 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004605 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004606 return NULL;
4607 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4608 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4609 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4610 if (u == NULL)
4611 return NULL;
4612 p = buf = PyBytes_AsString(u);
4613 end = s + len;
4614 while (s < end) {
4615 if (*s == '\\') {
4616 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004617 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004618 strcpy(p, "u005c");
4619 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004620 if (s >= end)
4621 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004622 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004623 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004624 if (*s & 0x80) { /* XXX inefficient */
4625 PyObject *w;
4626 int kind;
4627 void *data;
4628 Py_ssize_t len, i;
4629 w = decode_utf8(c, &s, end);
4630 if (w == NULL) {
4631 Py_DECREF(u);
4632 return NULL;
4633 }
4634 kind = PyUnicode_KIND(w);
4635 data = PyUnicode_DATA(w);
4636 len = PyUnicode_GET_LENGTH(w);
4637 for (i = 0; i < len; i++) {
4638 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4639 sprintf(p, "\\U%08x", chr);
4640 p += 10;
4641 }
4642 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004643 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004644 Py_DECREF(w);
4645 } else {
4646 *p++ = *s++;
4647 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004648 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004649 len = p - buf;
4650 s = buf;
4651
Eric V. Smith56466482016-10-31 14:46:26 -04004652 const char *first_invalid_escape;
4653 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4654
4655 if (v != NULL && first_invalid_escape != NULL) {
4656 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4657 /* We have not decref u before because first_invalid_escape points
4658 inside u. */
4659 Py_XDECREF(u);
4660 Py_DECREF(v);
4661 return NULL;
4662 }
4663 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004664 Py_XDECREF(u);
4665 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004666}
4667
Eric V. Smith56466482016-10-31 14:46:26 -04004668static PyObject *
4669decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4670 size_t len)
4671{
4672 const char *first_invalid_escape;
4673 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4674 &first_invalid_escape);
4675 if (result == NULL)
4676 return NULL;
4677
4678 if (first_invalid_escape != NULL) {
4679 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4680 Py_DECREF(result);
4681 return NULL;
4682 }
4683 }
4684 return result;
4685}
4686
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004687/* Shift locations for the given node and all its children by adding `lineno`
4688 and `col_offset` to existing locations. */
4689static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4690{
4691 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004692 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004693 for (int i = 0; i < NCH(n); ++i) {
4694 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4695 /* Shifting column offsets unnecessary if there's been newlines. */
4696 col_offset = 0;
4697 }
4698 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4699 }
4700 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004701 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004702}
4703
4704/* Fix locations for the given node and its children.
4705
4706 `parent` is the enclosing node.
4707 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004708 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004709*/
4710static void
4711fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4712{
4713 char *substr = NULL;
4714 char *start;
4715 int lines = LINENO(parent) - 1;
4716 int cols = parent->n_col_offset;
4717 /* Find the full fstring to fix location information in `n`. */
4718 while (parent && parent->n_type != STRING)
4719 parent = parent->n_child;
4720 if (parent && parent->n_str) {
4721 substr = strstr(parent->n_str, expr_str);
4722 if (substr) {
4723 start = substr;
4724 while (start > parent->n_str) {
4725 if (start[0] == '\n')
4726 break;
4727 start--;
4728 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004729 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004730 /* adjust the start based on the number of newlines encountered
4731 before the f-string expression */
4732 for (char* p = parent->n_str; p < substr; p++) {
4733 if (*p == '\n') {
4734 lines++;
4735 }
4736 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004737 }
4738 }
4739 fstring_shift_node_locations(n, lines, cols);
4740}
4741
Eric V. Smith451d0e32016-09-09 21:56:20 -04004742/* Compile this expression in to an expr_ty. Add parens around the
4743 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004744static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004745fstring_compile_expr(const char *expr_start, const char *expr_end,
4746 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004747
Eric V. Smith235a6f02015-09-19 14:51:32 -04004748{
4749 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004750 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004751 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004752 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004753 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004754 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004755
Eric V. Smith1d44c412015-09-23 07:49:00 -04004756 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004757 assert(*(expr_start-1) == '{');
4758 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004759
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004760 /* If the substring is all whitespace, it's an error. We need to catch this
4761 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4762 because turning the expression '' in to '()' would go from being invalid
4763 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004764 for (s = expr_start; s != expr_end; s++) {
4765 char c = *s;
4766 /* The Python parser ignores only the following whitespace
4767 characters (\r already is converted to \n). */
4768 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004769 break;
4770 }
4771 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004772 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004773 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004774 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004775 }
4776
Eric V. Smith451d0e32016-09-09 21:56:20 -04004777 len = expr_end - expr_start;
4778 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4779 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004780 if (str == NULL) {
4781 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004782 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004783 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004784
Eric V. Smith451d0e32016-09-09 21:56:20 -04004785 str[0] = '(';
4786 memcpy(str+1, expr_start, len);
4787 str[len+1] = ')';
4788 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004789
4790 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004791 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4792 Py_eval_input, 0);
4793 if (!mod_n) {
4794 PyMem_RawFree(str);
4795 return NULL;
4796 }
4797 /* Reuse str to find the correct column offset. */
4798 str[0] = '{';
4799 str[len+1] = '}';
4800 fstring_fix_node_location(n, mod_n, str);
4801 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004802 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004803 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004804 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004805 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004806 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004807}
4808
4809/* Return -1 on error.
4810
4811 Return 0 if we reached the end of the literal.
4812
4813 Return 1 if we haven't reached the end of the literal, but we want
4814 the caller to process the literal up to this point. Used for
4815 doubled braces.
4816*/
4817static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004818fstring_find_literal(const char **str, const char *end, int raw,
4819 PyObject **literal, int recurse_lvl,
4820 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004821{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004822 /* Get any literal string. It ends when we hit an un-doubled left
4823 brace (which isn't part of a unicode name escape such as
4824 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004825
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004826 const char *s = *str;
4827 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004828 int result = 0;
4829
Eric V. Smith235a6f02015-09-19 14:51:32 -04004830 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004831 while (s < end) {
4832 char ch = *s++;
4833 if (!raw && ch == '\\' && s < end) {
4834 ch = *s++;
4835 if (ch == 'N') {
4836 if (s < end && *s++ == '{') {
4837 while (s < end && *s++ != '}') {
4838 }
4839 continue;
4840 }
4841 break;
4842 }
4843 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4844 return -1;
4845 }
4846 }
4847 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004848 /* Check for doubled braces, but only at the top level. If
4849 we checked at every level, then f'{0:{3}}' would fail
4850 with the two closing braces. */
4851 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004852 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004853 /* We're going to tell the caller that the literal ends
4854 here, but that they should continue scanning. But also
4855 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004856 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004857 result = 1;
4858 goto done;
4859 }
4860
4861 /* Where a single '{' is the start of a new expression, a
4862 single '}' is not allowed. */
4863 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004864 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004865 ast_error(c, n, "f-string: single '}' is not allowed");
4866 return -1;
4867 }
4868 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004869 /* We're either at a '{', which means we're starting another
4870 expression; or a '}', which means we're at the end of this
4871 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004872 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004873 break;
4874 }
4875 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004876 *str = s;
4877 assert(s <= end);
4878 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004879done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004880 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004881 if (raw)
4882 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004883 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004884 NULL, NULL);
4885 else
Eric V. Smith56466482016-10-31 14:46:26 -04004886 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004887 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888 if (!*literal)
4889 return -1;
4890 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004891 return result;
4892}
4893
4894/* Forward declaration because parsing is recursive. */
4895static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004896fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897 struct compiling *c, const node *n);
4898
Eric V. Smith451d0e32016-09-09 21:56:20 -04004899/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900 expression (so it must be a '{'). Returns the FormattedValue node,
4901 which includes the expression, conversion character, and
4902 format_spec expression.
4903
4904 Note that I don't do a perfect job here: I don't make sure that a
4905 closing brace doesn't match an opening paren, for example. It
4906 doesn't need to error on all invalid expressions, just correctly
4907 find the end of all valid ones. Any errors inside the expression
4908 will be caught when we parse it later. */
4909static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004910fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004911 expr_ty *expression, struct compiling *c, const node *n)
4912{
4913 /* Return -1 on error, else 0. */
4914
Eric V. Smith451d0e32016-09-09 21:56:20 -04004915 const char *expr_start;
4916 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917 expr_ty simple_expression;
4918 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004919 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004920
4921 /* 0 if we're not in a string, else the quote char we're trying to
4922 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004923 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924
4925 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4926 int string_type = 0;
4927
4928 /* Keep track of nesting level for braces/parens/brackets in
4929 expressions. */
4930 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004931 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004932
4933 /* Can only nest one level deep. */
4934 if (recurse_lvl >= 2) {
4935 ast_error(c, n, "f-string: expressions nested too deeply");
4936 return -1;
4937 }
4938
4939 /* The first char must be a left brace, or we wouldn't have gotten
4940 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004941 assert(**str == '{');
4942 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004943
Eric V. Smith451d0e32016-09-09 21:56:20 -04004944 expr_start = *str;
4945 for (; *str < end; (*str)++) {
4946 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004947
4948 /* Loop invariants. */
4949 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004950 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004951 if (quote_char)
4952 assert(string_type == 1 || string_type == 3);
4953 else
4954 assert(string_type == 0);
4955
Eric V. Smith451d0e32016-09-09 21:56:20 -04004956 ch = **str;
4957 /* Nowhere inside an expression is a backslash allowed. */
4958 if (ch == '\\') {
4959 /* Error: can't include a backslash character, inside
4960 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004961 ast_error(c, n,
4962 "f-string expression part "
4963 "cannot include a backslash");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004964 return -1;
4965 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004966 if (quote_char) {
4967 /* We're inside a string. See if we're at the end. */
4968 /* This code needs to implement the same non-error logic
4969 as tok_get from tokenizer.c, at the letter_quote
4970 label. To actually share that code would be a
4971 nightmare. But, it's unlikely to change and is small,
4972 so duplicate it here. Note we don't need to catch all
4973 of the errors, since they'll be caught when parsing the
4974 expression. We just need to match the non-error
4975 cases. Thus we can ignore \n in single-quoted strings,
4976 for example. Or non-terminated strings. */
4977 if (ch == quote_char) {
4978 /* Does this match the string_type (single or triple
4979 quoted)? */
4980 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004981 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004982 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004983 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 string_type = 0;
4985 quote_char = 0;
4986 continue;
4987 }
4988 } else {
4989 /* We're at the end of a normal string. */
4990 quote_char = 0;
4991 string_type = 0;
4992 continue;
4993 }
4994 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 } else if (ch == '\'' || ch == '"') {
4996 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004997 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004998 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004999 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005000 } else {
5001 /* Start of a normal string. */
5002 string_type = 1;
5003 }
5004 /* Start looking for the end of the string. */
5005 quote_char = ch;
5006 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005007 if (nested_depth >= MAXLEVEL) {
5008 ast_error(c, n, "f-string: too many nested parenthesis");
5009 return -1;
5010 }
5011 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005012 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013 } else if (ch == '#') {
5014 /* Error: can't include a comment character, inside parens
5015 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005016 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005017 return -1;
5018 } else if (nested_depth == 0 &&
5019 (ch == '!' || ch == ':' || ch == '}')) {
5020 /* First, test for the special case of "!=". Since '=' is
5021 not an allowed conversion character, nothing is lost in
5022 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005023 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024 /* This isn't a conversion character, just continue. */
5025 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005026 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027 /* Normal way out of this loop. */
5028 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005029 } else if (ch == ']' || ch == '}' || ch == ')') {
5030 if (!nested_depth) {
5031 ast_error(c, n, "f-string: unmatched '%c'", ch);
5032 return -1;
5033 }
5034 nested_depth--;
5035 int opening = parenstack[nested_depth];
5036 if (!((opening == '(' && ch == ')') ||
5037 (opening == '[' && ch == ']') ||
5038 (opening == '{' && ch == '}')))
5039 {
5040 ast_error(c, n,
5041 "f-string: closing parenthesis '%c' "
5042 "does not match opening parenthesis '%c'",
5043 ch, opening);
5044 return -1;
5045 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005046 } else {
5047 /* Just consume this char and loop around. */
5048 }
5049 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005050 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005051 /* If we leave this loop in a string or with mismatched parens, we
5052 don't care. We'll get a syntax error when compiling the
5053 expression. But, we can produce a better error message, so
5054 let's just do that.*/
5055 if (quote_char) {
5056 ast_error(c, n, "f-string: unterminated string");
5057 return -1;
5058 }
5059 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005060 int opening = parenstack[nested_depth - 1];
5061 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005062 return -1;
5063 }
5064
Eric V. Smith451d0e32016-09-09 21:56:20 -04005065 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005066 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005067
5068 /* Compile the expression as soon as possible, so we show errors
5069 related to the expression before errors related to the
5070 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005071 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005072 if (!simple_expression)
5073 return -1;
5074
5075 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005076 if (**str == '!') {
5077 *str += 1;
5078 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005079 goto unexpected_end_of_string;
5080
Eric V. Smith451d0e32016-09-09 21:56:20 -04005081 conversion = **str;
5082 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005083
5084 /* Validate the conversion. */
5085 if (!(conversion == 's' || conversion == 'r'
5086 || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005087 ast_error(c, n,
5088 "f-string: invalid conversion character: "
5089 "expected 's', 'r', or 'a'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005090 return -1;
5091 }
5092 }
5093
5094 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005095 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005096 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005097 if (**str == ':') {
5098 *str += 1;
5099 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005100 goto unexpected_end_of_string;
5101
5102 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005103 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104 if (!format_spec)
5105 return -1;
5106 }
5107
Eric V. Smith451d0e32016-09-09 21:56:20 -04005108 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005109 goto unexpected_end_of_string;
5110
5111 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005112 assert(*str < end);
5113 assert(**str == '}');
5114 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005115
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 /* And now create the FormattedValue node that represents this
5117 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005118 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005120 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005121 c->c_arena);
5122 if (!*expression)
5123 return -1;
5124
5125 return 0;
5126
5127unexpected_end_of_string:
5128 ast_error(c, n, "f-string: expecting '}'");
5129 return -1;
5130}
5131
5132/* Return -1 on error.
5133
5134 Return 0 if we have a literal (possible zero length) and an
5135 expression (zero length if at the end of the string.
5136
5137 Return 1 if we have a literal, but no expression, and we want the
5138 caller to call us again. This is used to deal with doubled
5139 braces.
5140
5141 When called multiple times on the string 'a{{b{0}c', this function
5142 will return:
5143
5144 1. the literal 'a{' with no expression, and a return value
5145 of 1. Despite the fact that there's no expression, the return
5146 value of 1 means we're not finished yet.
5147
5148 2. the literal 'b' and the expression '0', with a return value of
5149 0. The fact that there's an expression means we're not finished.
5150
5151 3. literal 'c' with no expression and a return value of 0. The
5152 combination of the return value of 0 with no expression means
5153 we're finished.
5154*/
5155static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005156fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5157 int recurse_lvl, PyObject **literal,
5158 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005159 struct compiling *c, const node *n)
5160{
5161 int result;
5162
5163 assert(*literal == NULL && *expression == NULL);
5164
5165 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005166 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005167 if (result < 0)
5168 goto error;
5169
5170 assert(result == 0 || result == 1);
5171
5172 if (result == 1)
5173 /* We have a literal, but don't look at the expression. */
5174 return 1;
5175
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005177 /* We're at the end of the string or the end of a nested
5178 f-string: no expression. The top-level error case where we
5179 expect to be at the end of the string but we're at a '}' is
5180 handled later. */
5181 return 0;
5182
5183 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005184 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185
Eric V. Smith451d0e32016-09-09 21:56:20 -04005186 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005187 goto error;
5188
5189 return 0;
5190
5191error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005192 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005193 return -1;
5194}
5195
5196#define EXPRLIST_N_CACHED 64
5197
5198typedef struct {
5199 /* Incrementally build an array of expr_ty, so be used in an
5200 asdl_seq. Cache some small but reasonably sized number of
5201 expr_ty's, and then after that start dynamically allocating,
5202 doubling the number allocated each time. Note that the f-string
5203 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005204 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005205 fast as you add exressions in an f-string. */
5206
5207 Py_ssize_t allocated; /* Number we've allocated. */
5208 Py_ssize_t size; /* Number we've used. */
5209 expr_ty *p; /* Pointer to the memory we're actually
5210 using. Will point to 'data' until we
5211 start dynamically allocating. */
5212 expr_ty data[EXPRLIST_N_CACHED];
5213} ExprList;
5214
5215#ifdef NDEBUG
5216#define ExprList_check_invariants(l)
5217#else
5218static void
5219ExprList_check_invariants(ExprList *l)
5220{
5221 /* Check our invariants. Make sure this object is "live", and
5222 hasn't been deallocated. */
5223 assert(l->size >= 0);
5224 assert(l->p != NULL);
5225 if (l->size <= EXPRLIST_N_CACHED)
5226 assert(l->data == l->p);
5227}
5228#endif
5229
5230static void
5231ExprList_Init(ExprList *l)
5232{
5233 l->allocated = EXPRLIST_N_CACHED;
5234 l->size = 0;
5235
5236 /* Until we start allocating dynamically, p points to data. */
5237 l->p = l->data;
5238
5239 ExprList_check_invariants(l);
5240}
5241
5242static int
5243ExprList_Append(ExprList *l, expr_ty exp)
5244{
5245 ExprList_check_invariants(l);
5246 if (l->size >= l->allocated) {
5247 /* We need to alloc (or realloc) the memory. */
5248 Py_ssize_t new_size = l->allocated * 2;
5249
5250 /* See if we've ever allocated anything dynamically. */
5251 if (l->p == l->data) {
5252 Py_ssize_t i;
5253 /* We're still using the cached data. Switch to
5254 alloc-ing. */
5255 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5256 if (!l->p)
5257 return -1;
5258 /* Copy the cached data into the new buffer. */
5259 for (i = 0; i < l->size; i++)
5260 l->p[i] = l->data[i];
5261 } else {
5262 /* Just realloc. */
5263 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5264 if (!tmp) {
5265 PyMem_RawFree(l->p);
5266 l->p = NULL;
5267 return -1;
5268 }
5269 l->p = tmp;
5270 }
5271
5272 l->allocated = new_size;
5273 assert(l->allocated == 2 * l->size);
5274 }
5275
5276 l->p[l->size++] = exp;
5277
5278 ExprList_check_invariants(l);
5279 return 0;
5280}
5281
5282static void
5283ExprList_Dealloc(ExprList *l)
5284{
5285 ExprList_check_invariants(l);
5286
5287 /* If there's been an error, or we've never dynamically allocated,
5288 do nothing. */
5289 if (!l->p || l->p == l->data) {
5290 /* Do nothing. */
5291 } else {
5292 /* We have dynamically allocated. Free the memory. */
5293 PyMem_RawFree(l->p);
5294 }
5295 l->p = NULL;
5296 l->size = -1;
5297}
5298
5299static asdl_seq *
5300ExprList_Finish(ExprList *l, PyArena *arena)
5301{
5302 asdl_seq *seq;
5303
5304 ExprList_check_invariants(l);
5305
5306 /* Allocate the asdl_seq and copy the expressions in to it. */
5307 seq = _Py_asdl_seq_new(l->size, arena);
5308 if (seq) {
5309 Py_ssize_t i;
5310 for (i = 0; i < l->size; i++)
5311 asdl_seq_SET(seq, i, l->p[i]);
5312 }
5313 ExprList_Dealloc(l);
5314 return seq;
5315}
5316
5317/* The FstringParser is designed to add a mix of strings and
5318 f-strings, and concat them together as needed. Ultimately, it
5319 generates an expr_ty. */
5320typedef struct {
5321 PyObject *last_str;
5322 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005323 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005324} FstringParser;
5325
5326#ifdef NDEBUG
5327#define FstringParser_check_invariants(state)
5328#else
5329static void
5330FstringParser_check_invariants(FstringParser *state)
5331{
5332 if (state->last_str)
5333 assert(PyUnicode_CheckExact(state->last_str));
5334 ExprList_check_invariants(&state->expr_list);
5335}
5336#endif
5337
5338static void
5339FstringParser_Init(FstringParser *state)
5340{
5341 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005342 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005343 ExprList_Init(&state->expr_list);
5344 FstringParser_check_invariants(state);
5345}
5346
5347static void
5348FstringParser_Dealloc(FstringParser *state)
5349{
5350 FstringParser_check_invariants(state);
5351
5352 Py_XDECREF(state->last_str);
5353 ExprList_Dealloc(&state->expr_list);
5354}
5355
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005356/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005357static expr_ty
5358make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5359{
5360 PyObject *s = *str;
5361 *str = NULL;
5362 assert(PyUnicode_CheckExact(s));
5363 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5364 Py_DECREF(s);
5365 return NULL;
5366 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005367 return Constant(s, LINENO(n), n->n_col_offset,
5368 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005369}
5370
5371/* Add a non-f-string (that is, a regular literal string). str is
5372 decref'd. */
5373static int
5374FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5375{
5376 FstringParser_check_invariants(state);
5377
5378 assert(PyUnicode_CheckExact(str));
5379
5380 if (PyUnicode_GET_LENGTH(str) == 0) {
5381 Py_DECREF(str);
5382 return 0;
5383 }
5384
5385 if (!state->last_str) {
5386 /* We didn't have a string before, so just remember this one. */
5387 state->last_str = str;
5388 } else {
5389 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005390 PyUnicode_AppendAndDel(&state->last_str, str);
5391 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005392 return -1;
5393 }
5394 FstringParser_check_invariants(state);
5395 return 0;
5396}
5397
Eric V. Smith451d0e32016-09-09 21:56:20 -04005398/* Parse an f-string. The f-string is in *str to end, with no
5399 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005400static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005401FstringParser_ConcatFstring(FstringParser *state, const char **str,
5402 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005403 struct compiling *c, const node *n)
5404{
5405 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005406 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005407
5408 /* Parse the f-string. */
5409 while (1) {
5410 PyObject *literal = NULL;
5411 expr_ty expression = NULL;
5412
5413 /* If there's a zero length literal in front of the
5414 expression, literal will be NULL. If we're at the end of
5415 the f-string, expression will be NULL (unless result == 1,
5416 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005417 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005418 &literal, &expression,
5419 c, n);
5420 if (result < 0)
5421 return -1;
5422
5423 /* Add the literal, if any. */
5424 if (!literal) {
5425 /* Do nothing. Just leave last_str alone (and possibly
5426 NULL). */
5427 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005428 /* Note that the literal can be zero length, if the
5429 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005430 state->last_str = literal;
5431 literal = NULL;
5432 } else {
5433 /* We have a literal, concatenate it. */
5434 assert(PyUnicode_GET_LENGTH(literal) != 0);
5435 if (FstringParser_ConcatAndDel(state, literal) < 0)
5436 return -1;
5437 literal = NULL;
5438 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005439
5440 /* We've dealt with the literal now. It can't be leaked on further
5441 errors. */
5442 assert(literal == NULL);
5443
5444 /* See if we should just loop around to get the next literal
5445 and expression, while ignoring the expression this
5446 time. This is used for un-doubling braces, as an
5447 optimization. */
5448 if (result == 1)
5449 continue;
5450
5451 if (!expression)
5452 /* We're done with this f-string. */
5453 break;
5454
5455 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005456 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005457 if (!state->last_str) {
5458 /* Do nothing. No previous literal. */
5459 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005460 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005461 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5462 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5463 return -1;
5464 }
5465
5466 if (ExprList_Append(&state->expr_list, expression) < 0)
5467 return -1;
5468 }
5469
Eric V. Smith235a6f02015-09-19 14:51:32 -04005470 /* If recurse_lvl is zero, then we must be at the end of the
5471 string. Otherwise, we must be at a right brace. */
5472
Eric V. Smith451d0e32016-09-09 21:56:20 -04005473 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005474 ast_error(c, n, "f-string: unexpected end of string");
5475 return -1;
5476 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005477 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005478 ast_error(c, n, "f-string: expecting '}'");
5479 return -1;
5480 }
5481
5482 FstringParser_check_invariants(state);
5483 return 0;
5484}
5485
5486/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005487 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005488static expr_ty
5489FstringParser_Finish(FstringParser *state, struct compiling *c,
5490 const node *n)
5491{
5492 asdl_seq *seq;
5493
5494 FstringParser_check_invariants(state);
5495
5496 /* If we're just a constant string with no expressions, return
5497 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005498 if (!state->fmode) {
5499 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005500 if (!state->last_str) {
5501 /* Create a zero length string. */
5502 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5503 if (!state->last_str)
5504 goto error;
5505 }
5506 return make_str_node_and_del(&state->last_str, c, n);
5507 }
5508
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005509 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005510 last node in our expression list. */
5511 if (state->last_str) {
5512 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5513 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5514 goto error;
5515 }
5516 /* This has already been freed. */
5517 assert(state->last_str == NULL);
5518
5519 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5520 if (!seq)
5521 goto error;
5522
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005523 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5524 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005525
5526error:
5527 FstringParser_Dealloc(state);
5528 return NULL;
5529}
5530
Eric V. Smith451d0e32016-09-09 21:56:20 -04005531/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5532 at end, parse it into an expr_ty. Return NULL on error. Adjust
5533 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005534static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005535fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005536 struct compiling *c, const node *n)
5537{
5538 FstringParser state;
5539
5540 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005541 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005542 c, n) < 0) {
5543 FstringParser_Dealloc(&state);
5544 return NULL;
5545 }
5546
5547 return FstringParser_Finish(&state, c, n);
5548}
5549
5550/* n is a Python string literal, including the bracketing quote
5551 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005552 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005553 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005554 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5555 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005556*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005557static int
5558parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5559 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005560{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005561 size_t len;
5562 const char *s = STR(n);
5563 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005564 int fmode = 0;
5565 *bytesmode = 0;
5566 *rawmode = 0;
5567 *result = NULL;
5568 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005569 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005570 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005571 if (quote == 'b' || quote == 'B') {
5572 quote = *++s;
5573 *bytesmode = 1;
5574 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005575 else if (quote == 'u' || quote == 'U') {
5576 quote = *++s;
5577 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005578 else if (quote == 'r' || quote == 'R') {
5579 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005580 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005581 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005582 else if (quote == 'f' || quote == 'F') {
5583 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005584 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005585 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005586 else {
5587 break;
5588 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005589 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005590 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005591 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005592 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005593 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005594 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005595 if (quote != '\'' && quote != '\"') {
5596 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005597 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005598 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005599 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005600 s++;
5601 len = strlen(s);
5602 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005603 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005604 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005605 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005606 }
5607 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005608 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005609 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005610 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005611 }
5612 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005613 /* A triple quoted string. We've already skipped one quote at
5614 the start and one at the end of the string. Now skip the
5615 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005616 s += 2;
5617 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005618 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005619 if (s[--len] != quote || s[--len] != quote) {
5620 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005621 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005622 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005623 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005624
Eric V. Smith451d0e32016-09-09 21:56:20 -04005625 if (fmode) {
5626 /* Just return the bytes. The caller will parse the resulting
5627 string. */
5628 *fstr = s;
5629 *fstrlen = len;
5630 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005631 }
5632
Eric V. Smith451d0e32016-09-09 21:56:20 -04005633 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005634 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005635 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005636 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005637 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005638 const char *ch;
5639 for (ch = s; *ch; ch++) {
5640 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005641 ast_error(c, n,
5642 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005643 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005644 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005645 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005646 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005647 if (*rawmode)
5648 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005649 else
Eric V. Smith56466482016-10-31 14:46:26 -04005650 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005651 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005652 if (*rawmode)
5653 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005654 else
Eric V. Smith56466482016-10-31 14:46:26 -04005655 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005656 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005657 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005658}
5659
Eric V. Smith235a6f02015-09-19 14:51:32 -04005660/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5661 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005662 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005663 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005664 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005665 node if there's just an f-string (with no leading or trailing
5666 literals), or a JoinedStr node if there are multiple f-strings or
5667 any literals involved. */
5668static expr_ty
5669parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005670{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005671 int bytesmode = 0;
5672 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005673 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005674
5675 FstringParser state;
5676 FstringParser_Init(&state);
5677
5678 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005679 int this_bytesmode;
5680 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005681 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005682 const char *fstr;
5683 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005684
5685 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005686 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5687 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005688 goto error;
5689
5690 /* Check that we're not mixing bytes with unicode. */
5691 if (i != 0 && bytesmode != this_bytesmode) {
5692 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005693 /* s is NULL if the current string part is an f-string. */
5694 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005695 goto error;
5696 }
5697 bytesmode = this_bytesmode;
5698
Eric V. Smith451d0e32016-09-09 21:56:20 -04005699 if (fstr != NULL) {
5700 int result;
5701 assert(s == NULL && !bytesmode);
5702 /* This is an f-string. Parse and concatenate it. */
5703 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5704 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005705 if (result < 0)
5706 goto error;
5707 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005708 /* A string or byte string. */
5709 assert(s != NULL && fstr == NULL);
5710
Eric V. Smith451d0e32016-09-09 21:56:20 -04005711 assert(bytesmode ? PyBytes_CheckExact(s) :
5712 PyUnicode_CheckExact(s));
5713
Eric V. Smith451d0e32016-09-09 21:56:20 -04005714 if (bytesmode) {
5715 /* For bytes, concat as we go. */
5716 if (i == 0) {
5717 /* First time, just remember this value. */
5718 bytes_str = s;
5719 } else {
5720 PyBytes_ConcatAndDel(&bytes_str, s);
5721 if (!bytes_str)
5722 goto error;
5723 }
5724 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005725 /* This is a regular string. Concatenate it. */
5726 if (FstringParser_ConcatAndDel(&state, s) < 0)
5727 goto error;
5728 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005729 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005730 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005731 if (bytesmode) {
5732 /* Just return the bytes object and we're done. */
5733 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5734 goto error;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005735 return Constant(bytes_str, LINENO(n), n->n_col_offset,
5736 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005737 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005738
Eric V. Smith235a6f02015-09-19 14:51:32 -04005739 /* We're not a bytes string, bytes_str should never have been set. */
5740 assert(bytes_str == NULL);
5741
5742 return FstringParser_Finish(&state, c, n);
5743
5744error:
5745 Py_XDECREF(bytes_str);
5746 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005747 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005748}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005749
5750PyObject *
5751_PyAST_GetDocString(asdl_seq *body)
5752{
5753 if (!asdl_seq_LEN(body)) {
5754 return NULL;
5755 }
5756 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5757 if (st->kind != Expr_kind) {
5758 return NULL;
5759 }
5760 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005761 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5762 return e->v.Constant.value;
5763 }
5764 return NULL;
5765}