blob: 4fa98b17f880db1c9ea2e4861a18bd7f4c82319b [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);
Ivan Levkivskyi181835d2019-02-10 15:39:49 +00003874 // There must be no empty suites.
3875 assert(tot > 0);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003876 stmt_ty last = asdl_seq_GET(s, tot - 1);
3877 *end_lineno = last->end_lineno;
3878 *end_col_offset = last->end_col_offset;
3879}
3880
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881static stmt_ty
3882ast_for_if_stmt(struct compiling *c, const node *n)
3883{
3884 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3885 ['else' ':' suite]
3886 */
3887 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003888 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889
3890 REQ(n, if_stmt);
3891
3892 if (NCH(n) == 4) {
3893 expr_ty expression;
3894 asdl_seq *suite_seq;
3895
3896 expression = ast_for_expr(c, CHILD(n, 1));
3897 if (!expression)
3898 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003900 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003902 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903
Guido van Rossumd8faa362007-04-27 19:54:29 +00003904 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003905 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003907
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908 s = STR(CHILD(n, 4));
3909 /* s[2], the third character in the string, will be
3910 's' for el_s_e, or
3911 'i' for el_i_f
3912 */
3913 if (s[2] == 's') {
3914 expr_ty expression;
3915 asdl_seq *seq1, *seq2;
3916
3917 expression = ast_for_expr(c, CHILD(n, 1));
3918 if (!expression)
3919 return NULL;
3920 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003921 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 return NULL;
3923 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003924 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003926 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927
Guido van Rossumd8faa362007-04-27 19:54:29 +00003928 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003929 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 }
3931 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003932 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003933 expr_ty expression;
3934 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003935 asdl_seq *orelse = NULL;
3936 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937 /* must reference the child n_elif+1 since 'else' token is third,
3938 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003939 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3940 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3941 has_else = 1;
3942 n_elif -= 3;
3943 }
3944 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945
Thomas Wouters89f507f2006-12-13 04:49:30 +00003946 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003947 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003948
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003949 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003950 if (!orelse)
3951 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003953 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003955 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3956 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003958 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3959 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003961 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 asdl_seq_SET(orelse, 0,
3964 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003965 LINENO(CHILD(n, NCH(n) - 6)),
3966 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003967 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003968 /* the just-created orelse handled the last elif */
3969 n_elif--;
3970 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971
Thomas Wouters89f507f2006-12-13 04:49:30 +00003972 for (i = 0; i < n_elif; i++) {
3973 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003974 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003975 if (!newobj)
3976 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003978 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003981 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003984 if (orelse != NULL) {
3985 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3986 } else {
3987 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3988 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003989 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003991 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003992 CHILD(n, off)->n_col_offset,
3993 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003994 orelse = newobj;
3995 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003996 expression = ast_for_expr(c, CHILD(n, 1));
3997 if (!expression)
3998 return NULL;
3999 suite_seq = ast_for_suite(c, CHILD(n, 3));
4000 if (!suite_seq)
4001 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004002 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004003 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004004 LINENO(n), n->n_col_offset,
4005 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004007
4008 PyErr_Format(PyExc_SystemError,
4009 "unexpected token in 'if' statement: %s", s);
4010 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011}
4012
4013static stmt_ty
4014ast_for_while_stmt(struct compiling *c, const node *n)
4015{
4016 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4017 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004018 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019
4020 if (NCH(n) == 4) {
4021 expr_ty expression;
4022 asdl_seq *suite_seq;
4023
4024 expression = ast_for_expr(c, CHILD(n, 1));
4025 if (!expression)
4026 return NULL;
4027 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004028 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004030 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4031 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4032 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033 }
4034 else if (NCH(n) == 7) {
4035 expr_ty expression;
4036 asdl_seq *seq1, *seq2;
4037
4038 expression = ast_for_expr(c, CHILD(n, 1));
4039 if (!expression)
4040 return NULL;
4041 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004042 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043 return NULL;
4044 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004045 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004047 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004049 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4050 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004052
4053 PyErr_Format(PyExc_SystemError,
4054 "wrong number of tokens for 'while' statement: %d",
4055 NCH(n));
4056 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057}
4058
4059static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004060ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061{
guoci90fc8982018-09-11 17:45:45 -04004062 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004063 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004065 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004066 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004067 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004068 int has_type_comment;
4069 string type_comment;
4070 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071 REQ(n, for_stmt);
4072
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004073 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4074
4075 if (NCH(n) == 9 + has_type_comment) {
4076 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004077 if (!seq)
4078 return NULL;
4079 }
4080
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004081 node_target = CHILD(n, 1);
4082 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004083 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004084 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004085 /* Check the # of children rather than the length of _target, since
4086 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004087 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004088 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004089 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004090 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004091 target = Tuple(_target, Store, first->lineno, first->col_offset,
4092 node_target->n_end_lineno, node_target->n_end_col_offset,
4093 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004094
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004095 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004096 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004097 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004098 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004099 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004100 return NULL;
4101
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004102 if (seq != NULL) {
4103 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4104 } else {
4105 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4106 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004107
4108 if (has_type_comment) {
4109 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4110 if (!type_comment)
4111 return NULL;
4112 }
4113 else
4114 type_comment = NULL;
4115
Yury Selivanov75445082015-05-11 22:57:16 -04004116 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004117 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004118 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004119 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004120 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004121 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004122 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004123 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004124}
4125
4126static excepthandler_ty
4127ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4128{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004129 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004130 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 REQ(exc, except_clause);
4132 REQ(body, suite);
4133
4134 if (NCH(exc) == 1) {
4135 asdl_seq *suite_seq = ast_for_suite(c, body);
4136 if (!suite_seq)
4137 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004138 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139
Neal Norwitzad74aa82008-03-31 05:14:30 +00004140 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004141 exc->n_col_offset,
4142 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004143 }
4144 else if (NCH(exc) == 2) {
4145 expr_ty expression;
4146 asdl_seq *suite_seq;
4147
4148 expression = ast_for_expr(c, CHILD(exc, 1));
4149 if (!expression)
4150 return NULL;
4151 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004152 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004154 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155
Neal Norwitzad74aa82008-03-31 05:14:30 +00004156 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004157 exc->n_col_offset,
4158 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159 }
4160 else if (NCH(exc) == 4) {
4161 asdl_seq *suite_seq;
4162 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004163 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004164 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004165 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004166 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004168 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004169 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004170 return NULL;
4171 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004172 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004174 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175
Neal Norwitzad74aa82008-03-31 05:14:30 +00004176 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004177 exc->n_col_offset,
4178 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004179 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004180
4181 PyErr_Format(PyExc_SystemError,
4182 "wrong number of children for 'except' clause: %d",
4183 NCH(exc));
4184 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004185}
4186
4187static stmt_ty
4188ast_for_try_stmt(struct compiling *c, const node *n)
4189{
Neal Norwitzf599f422005-12-17 21:33:47 +00004190 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004191 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004192 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004193 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195 REQ(n, try_stmt);
4196
Neal Norwitzf599f422005-12-17 21:33:47 +00004197 body = ast_for_suite(c, CHILD(n, 2));
4198 if (body == NULL)
4199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004200
Neal Norwitzf599f422005-12-17 21:33:47 +00004201 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4202 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4203 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4204 /* we can assume it's an "else",
4205 because nch >= 9 for try-else-finally and
4206 it would otherwise have a type of except_clause */
4207 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4208 if (orelse == NULL)
4209 return NULL;
4210 n_except--;
4211 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004212
Neal Norwitzf599f422005-12-17 21:33:47 +00004213 finally = ast_for_suite(c, CHILD(n, nch - 1));
4214 if (finally == NULL)
4215 return NULL;
4216 n_except--;
4217 }
4218 else {
4219 /* we can assume it's an "else",
4220 otherwise it would have a type of except_clause */
4221 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4222 if (orelse == NULL)
4223 return NULL;
4224 n_except--;
4225 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004226 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004227 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004228 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004229 return NULL;
4230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231
Neal Norwitzf599f422005-12-17 21:33:47 +00004232 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004233 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004234 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004235 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004236 if (handlers == NULL)
4237 return NULL;
4238
4239 for (i = 0; i < n_except; i++) {
4240 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4241 CHILD(n, 5 + i * 3));
4242 if (!e)
4243 return NULL;
4244 asdl_seq_SET(handlers, i, e);
4245 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004246 }
4247
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004248 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004249 if (finally != NULL) {
4250 // finally is always last
4251 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4252 } else if (orelse != NULL) {
4253 // otherwise else is last
4254 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4255 } else {
4256 // inline the get_last_end_pos logic due to layout mismatch
4257 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4258 end_lineno = last_handler->end_lineno;
4259 end_col_offset = last_handler->end_col_offset;
4260 }
4261 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4262 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004263}
4264
Georg Brandl0c315622009-05-25 21:10:36 +00004265/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004266static withitem_ty
4267ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004268{
4269 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004270
Georg Brandl0c315622009-05-25 21:10:36 +00004271 REQ(n, with_item);
4272 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004273 if (!context_expr)
4274 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004275 if (NCH(n) == 3) {
4276 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004277
4278 if (!optional_vars) {
4279 return NULL;
4280 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004281 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004282 return NULL;
4283 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004284 }
4285
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004286 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004287}
4288
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004289/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004290static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004291ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004292{
guoci90fc8982018-09-11 17:45:45 -04004293 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004294 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004295 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004296 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004297
4298 REQ(n, with_stmt);
4299
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004300 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4301 nch_minus_type = NCH(n) - has_type_comment;
4302
4303 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004304 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004305 if (!items)
4306 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004307 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004308 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4309 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004310 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004311 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004312 }
4313
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004314 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4315 if (!body)
4316 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004317 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004318
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004319 if (has_type_comment) {
4320 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4321 if (!type_comment)
4322 return NULL;
4323 }
4324 else
4325 type_comment = NULL;
4326
Yury Selivanov75445082015-05-11 22:57:16 -04004327 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004328 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004329 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004330 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004331 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004332 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004333}
4334
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004335static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004336ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004337{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004338 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004339 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004340 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004341 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004342 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004343
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004344 REQ(n, classdef);
4345
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004346 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004347 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004348 if (!s)
4349 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004350 get_last_end_pos(s, &end_lineno, &end_col_offset);
4351
Benjamin Peterson30760062008-11-25 04:02:28 +00004352 classname = NEW_IDENTIFIER(CHILD(n, 1));
4353 if (!classname)
4354 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004355 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004356 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004357 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004358 LINENO(n), n->n_col_offset,
4359 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004360 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004361
4362 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004363 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004364 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004365 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004366 get_last_end_pos(s, &end_lineno, &end_col_offset);
4367
Benjamin Peterson30760062008-11-25 04:02:28 +00004368 classname = NEW_IDENTIFIER(CHILD(n, 1));
4369 if (!classname)
4370 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004371 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004372 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004373 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004374 LINENO(n), n->n_col_offset,
4375 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004376 }
4377
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004378 /* class NAME '(' arglist ')' ':' suite */
4379 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004380 {
4381 PyObject *dummy_name;
4382 expr_ty dummy;
4383 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4384 if (!dummy_name)
4385 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004386 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4387 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4388 c->c_arena);
4389 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004390 if (!call)
4391 return NULL;
4392 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004393 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004394 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004395 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004396 get_last_end_pos(s, &end_lineno, &end_col_offset);
4397
Benjamin Peterson30760062008-11-25 04:02:28 +00004398 classname = NEW_IDENTIFIER(CHILD(n, 1));
4399 if (!classname)
4400 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004401 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004402 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004403
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004404 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004405 decorator_seq, LINENO(n), n->n_col_offset,
4406 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004407}
4408
4409static stmt_ty
4410ast_for_stmt(struct compiling *c, const node *n)
4411{
4412 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004413 assert(NCH(n) == 1);
4414 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004415 }
4416 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004417 assert(num_stmts(n) == 1);
4418 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004419 }
4420 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004421 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004422 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4423 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004424 */
4425 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004426 case expr_stmt:
4427 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004428 case del_stmt:
4429 return ast_for_del_stmt(c, n);
4430 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004431 return Pass(LINENO(n), n->n_col_offset,
4432 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004433 case flow_stmt:
4434 return ast_for_flow_stmt(c, n);
4435 case import_stmt:
4436 return ast_for_import_stmt(c, n);
4437 case global_stmt:
4438 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004439 case nonlocal_stmt:
4440 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004441 case assert_stmt:
4442 return ast_for_assert_stmt(c, n);
4443 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004444 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004445 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4446 TYPE(n), NCH(n));
4447 return NULL;
4448 }
4449 }
4450 else {
4451 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004452 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004453 */
4454 node *ch = CHILD(n, 0);
4455 REQ(n, compound_stmt);
4456 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004457 case if_stmt:
4458 return ast_for_if_stmt(c, ch);
4459 case while_stmt:
4460 return ast_for_while_stmt(c, ch);
4461 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004462 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004463 case try_stmt:
4464 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004465 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004466 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004467 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004468 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004469 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004470 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 case decorated:
4472 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004473 case async_stmt:
4474 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004475 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004476 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004477 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004478 TYPE(n), NCH(n));
4479 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004480 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004481 }
4482}
4483
4484static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004485parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004486{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004487 const char *end;
4488 long x;
4489 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004490 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004491 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004492
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004493 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004494 errno = 0;
4495 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004496 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004497 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004498 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004499 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004500 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004501 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004502 }
4503 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004504 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004505 if (*end == '\0') {
4506 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004507 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004508 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004509 }
4510 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004511 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004512 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004513 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4514 if (compl.imag == -1.0 && PyErr_Occurred())
4515 return NULL;
4516 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004517 }
4518 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004519 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004520 dx = PyOS_string_to_double(s, NULL, NULL);
4521 if (dx == -1.0 && PyErr_Occurred())
4522 return NULL;
4523 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004525}
4526
4527static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004528parsenumber(struct compiling *c, const char *s)
4529{
4530 char *dup, *end;
4531 PyObject *res = NULL;
4532
4533 assert(s != NULL);
4534
4535 if (strchr(s, '_') == NULL) {
4536 return parsenumber_raw(c, s);
4537 }
4538 /* Create a duplicate without underscores. */
4539 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004540 if (dup == NULL) {
4541 return PyErr_NoMemory();
4542 }
Brett Cannona721aba2016-09-09 14:57:09 -07004543 end = dup;
4544 for (; *s; s++) {
4545 if (*s != '_') {
4546 *end++ = *s;
4547 }
4548 }
4549 *end = '\0';
4550 res = parsenumber_raw(c, dup);
4551 PyMem_Free(dup);
4552 return res;
4553}
4554
4555static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004556decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004557{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004558 const char *s, *t;
4559 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004560 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4561 while (s < end && (*s & 0x80)) s++;
4562 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004563 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004564}
4565
Eric V. Smith56466482016-10-31 14:46:26 -04004566static int
4567warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004568 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004569{
4570 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4571 first_invalid_escape_char);
4572 if (msg == NULL) {
4573 return -1;
4574 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004575 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004576 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004577 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004578 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004579 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004580 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004581 to get a more accurate error report */
4582 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004583 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004584 }
4585 Py_DECREF(msg);
4586 return -1;
4587 }
4588 Py_DECREF(msg);
4589 return 0;
4590}
4591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004592static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004593decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4594 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004595{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004596 PyObject *v, *u;
4597 char *buf;
4598 char *p;
4599 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004600
Benjamin Peterson202803a2016-02-25 22:34:45 -08004601 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004602 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004603 return NULL;
4604 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4605 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4606 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4607 if (u == NULL)
4608 return NULL;
4609 p = buf = PyBytes_AsString(u);
4610 end = s + len;
4611 while (s < end) {
4612 if (*s == '\\') {
4613 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004614 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004615 strcpy(p, "u005c");
4616 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004617 if (s >= end)
4618 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004619 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004620 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004621 if (*s & 0x80) { /* XXX inefficient */
4622 PyObject *w;
4623 int kind;
4624 void *data;
4625 Py_ssize_t len, i;
4626 w = decode_utf8(c, &s, end);
4627 if (w == NULL) {
4628 Py_DECREF(u);
4629 return NULL;
4630 }
4631 kind = PyUnicode_KIND(w);
4632 data = PyUnicode_DATA(w);
4633 len = PyUnicode_GET_LENGTH(w);
4634 for (i = 0; i < len; i++) {
4635 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4636 sprintf(p, "\\U%08x", chr);
4637 p += 10;
4638 }
4639 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004640 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004641 Py_DECREF(w);
4642 } else {
4643 *p++ = *s++;
4644 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004645 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004646 len = p - buf;
4647 s = buf;
4648
Eric V. Smith56466482016-10-31 14:46:26 -04004649 const char *first_invalid_escape;
4650 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4651
4652 if (v != NULL && first_invalid_escape != NULL) {
4653 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4654 /* We have not decref u before because first_invalid_escape points
4655 inside u. */
4656 Py_XDECREF(u);
4657 Py_DECREF(v);
4658 return NULL;
4659 }
4660 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004661 Py_XDECREF(u);
4662 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004663}
4664
Eric V. Smith56466482016-10-31 14:46:26 -04004665static PyObject *
4666decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4667 size_t len)
4668{
4669 const char *first_invalid_escape;
4670 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4671 &first_invalid_escape);
4672 if (result == NULL)
4673 return NULL;
4674
4675 if (first_invalid_escape != NULL) {
4676 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4677 Py_DECREF(result);
4678 return NULL;
4679 }
4680 }
4681 return result;
4682}
4683
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004684/* Shift locations for the given node and all its children by adding `lineno`
4685 and `col_offset` to existing locations. */
4686static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4687{
4688 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004689 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004690 for (int i = 0; i < NCH(n); ++i) {
4691 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4692 /* Shifting column offsets unnecessary if there's been newlines. */
4693 col_offset = 0;
4694 }
4695 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4696 }
4697 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004698 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004699}
4700
4701/* Fix locations for the given node and its children.
4702
4703 `parent` is the enclosing node.
4704 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004705 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004706*/
4707static void
4708fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4709{
4710 char *substr = NULL;
4711 char *start;
4712 int lines = LINENO(parent) - 1;
4713 int cols = parent->n_col_offset;
4714 /* Find the full fstring to fix location information in `n`. */
4715 while (parent && parent->n_type != STRING)
4716 parent = parent->n_child;
4717 if (parent && parent->n_str) {
4718 substr = strstr(parent->n_str, expr_str);
4719 if (substr) {
4720 start = substr;
4721 while (start > parent->n_str) {
4722 if (start[0] == '\n')
4723 break;
4724 start--;
4725 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004726 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004727 /* adjust the start based on the number of newlines encountered
4728 before the f-string expression */
4729 for (char* p = parent->n_str; p < substr; p++) {
4730 if (*p == '\n') {
4731 lines++;
4732 }
4733 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004734 }
4735 }
4736 fstring_shift_node_locations(n, lines, cols);
4737}
4738
Eric V. Smith451d0e32016-09-09 21:56:20 -04004739/* Compile this expression in to an expr_ty. Add parens around the
4740 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004741static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004742fstring_compile_expr(const char *expr_start, const char *expr_end,
4743 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004744
Eric V. Smith235a6f02015-09-19 14:51:32 -04004745{
4746 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004747 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004748 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004749 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004750 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004751 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004752
Eric V. Smith1d44c412015-09-23 07:49:00 -04004753 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004754 assert(*(expr_start-1) == '{');
4755 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004756
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004757 /* If the substring is all whitespace, it's an error. We need to catch this
4758 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4759 because turning the expression '' in to '()' would go from being invalid
4760 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004761 for (s = expr_start; s != expr_end; s++) {
4762 char c = *s;
4763 /* The Python parser ignores only the following whitespace
4764 characters (\r already is converted to \n). */
4765 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004766 break;
4767 }
4768 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004769 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004770 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004771 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004772 }
4773
Eric V. Smith451d0e32016-09-09 21:56:20 -04004774 len = expr_end - expr_start;
4775 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4776 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004777 if (str == NULL) {
4778 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004779 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004780 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004781
Eric V. Smith451d0e32016-09-09 21:56:20 -04004782 str[0] = '(';
4783 memcpy(str+1, expr_start, len);
4784 str[len+1] = ')';
4785 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004786
4787 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004788 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4789 Py_eval_input, 0);
4790 if (!mod_n) {
4791 PyMem_RawFree(str);
4792 return NULL;
4793 }
4794 /* Reuse str to find the correct column offset. */
4795 str[0] = '{';
4796 str[len+1] = '}';
4797 fstring_fix_node_location(n, mod_n, str);
4798 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004799 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004800 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004801 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004802 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004803 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004804}
4805
4806/* Return -1 on error.
4807
4808 Return 0 if we reached the end of the literal.
4809
4810 Return 1 if we haven't reached the end of the literal, but we want
4811 the caller to process the literal up to this point. Used for
4812 doubled braces.
4813*/
4814static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004815fstring_find_literal(const char **str, const char *end, int raw,
4816 PyObject **literal, int recurse_lvl,
4817 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004818{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004819 /* Get any literal string. It ends when we hit an un-doubled left
4820 brace (which isn't part of a unicode name escape such as
4821 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004822
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004823 const char *s = *str;
4824 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004825 int result = 0;
4826
Eric V. Smith235a6f02015-09-19 14:51:32 -04004827 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004828 while (s < end) {
4829 char ch = *s++;
4830 if (!raw && ch == '\\' && s < end) {
4831 ch = *s++;
4832 if (ch == 'N') {
4833 if (s < end && *s++ == '{') {
4834 while (s < end && *s++ != '}') {
4835 }
4836 continue;
4837 }
4838 break;
4839 }
4840 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4841 return -1;
4842 }
4843 }
4844 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004845 /* Check for doubled braces, but only at the top level. If
4846 we checked at every level, then f'{0:{3}}' would fail
4847 with the two closing braces. */
4848 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004849 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004850 /* We're going to tell the caller that the literal ends
4851 here, but that they should continue scanning. But also
4852 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004853 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004854 result = 1;
4855 goto done;
4856 }
4857
4858 /* Where a single '{' is the start of a new expression, a
4859 single '}' is not allowed. */
4860 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004861 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004862 ast_error(c, n, "f-string: single '}' is not allowed");
4863 return -1;
4864 }
4865 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004866 /* We're either at a '{', which means we're starting another
4867 expression; or a '}', which means we're at the end of this
4868 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004869 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004870 break;
4871 }
4872 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004873 *str = s;
4874 assert(s <= end);
4875 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004876done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004877 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004878 if (raw)
4879 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004880 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004881 NULL, NULL);
4882 else
Eric V. Smith56466482016-10-31 14:46:26 -04004883 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004884 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004885 if (!*literal)
4886 return -1;
4887 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888 return result;
4889}
4890
4891/* Forward declaration because parsing is recursive. */
4892static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004893fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004894 struct compiling *c, const node *n);
4895
Eric V. Smith451d0e32016-09-09 21:56:20 -04004896/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004897 expression (so it must be a '{'). Returns the FormattedValue node,
4898 which includes the expression, conversion character, and
4899 format_spec expression.
4900
4901 Note that I don't do a perfect job here: I don't make sure that a
4902 closing brace doesn't match an opening paren, for example. It
4903 doesn't need to error on all invalid expressions, just correctly
4904 find the end of all valid ones. Any errors inside the expression
4905 will be caught when we parse it later. */
4906static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004907fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004908 expr_ty *expression, struct compiling *c, const node *n)
4909{
4910 /* Return -1 on error, else 0. */
4911
Eric V. Smith451d0e32016-09-09 21:56:20 -04004912 const char *expr_start;
4913 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004914 expr_ty simple_expression;
4915 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004916 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917
4918 /* 0 if we're not in a string, else the quote char we're trying to
4919 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004920 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921
4922 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4923 int string_type = 0;
4924
4925 /* Keep track of nesting level for braces/parens/brackets in
4926 expressions. */
4927 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004928 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929
4930 /* Can only nest one level deep. */
4931 if (recurse_lvl >= 2) {
4932 ast_error(c, n, "f-string: expressions nested too deeply");
4933 return -1;
4934 }
4935
4936 /* The first char must be a left brace, or we wouldn't have gotten
4937 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004938 assert(**str == '{');
4939 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004940
Eric V. Smith451d0e32016-09-09 21:56:20 -04004941 expr_start = *str;
4942 for (; *str < end; (*str)++) {
4943 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944
4945 /* Loop invariants. */
4946 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004947 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004948 if (quote_char)
4949 assert(string_type == 1 || string_type == 3);
4950 else
4951 assert(string_type == 0);
4952
Eric V. Smith451d0e32016-09-09 21:56:20 -04004953 ch = **str;
4954 /* Nowhere inside an expression is a backslash allowed. */
4955 if (ch == '\\') {
4956 /* Error: can't include a backslash character, inside
4957 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004958 ast_error(c, n,
4959 "f-string expression part "
4960 "cannot include a backslash");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004961 return -1;
4962 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963 if (quote_char) {
4964 /* We're inside a string. See if we're at the end. */
4965 /* This code needs to implement the same non-error logic
4966 as tok_get from tokenizer.c, at the letter_quote
4967 label. To actually share that code would be a
4968 nightmare. But, it's unlikely to change and is small,
4969 so duplicate it here. Note we don't need to catch all
4970 of the errors, since they'll be caught when parsing the
4971 expression. We just need to match the non-error
4972 cases. Thus we can ignore \n in single-quoted strings,
4973 for example. Or non-terminated strings. */
4974 if (ch == quote_char) {
4975 /* Does this match the string_type (single or triple
4976 quoted)? */
4977 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004978 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004980 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004981 string_type = 0;
4982 quote_char = 0;
4983 continue;
4984 }
4985 } else {
4986 /* We're at the end of a normal string. */
4987 quote_char = 0;
4988 string_type = 0;
4989 continue;
4990 }
4991 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004992 } else if (ch == '\'' || ch == '"') {
4993 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004994 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004996 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004997 } else {
4998 /* Start of a normal string. */
4999 string_type = 1;
5000 }
5001 /* Start looking for the end of the string. */
5002 quote_char = ch;
5003 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005004 if (nested_depth >= MAXLEVEL) {
5005 ast_error(c, n, "f-string: too many nested parenthesis");
5006 return -1;
5007 }
5008 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005009 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005010 } else if (ch == '#') {
5011 /* Error: can't include a comment character, inside parens
5012 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005013 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005014 return -1;
5015 } else if (nested_depth == 0 &&
5016 (ch == '!' || ch == ':' || ch == '}')) {
5017 /* First, test for the special case of "!=". Since '=' is
5018 not an allowed conversion character, nothing is lost in
5019 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005020 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005021 /* This isn't a conversion character, just continue. */
5022 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005023 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024 /* Normal way out of this loop. */
5025 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005026 } else if (ch == ']' || ch == '}' || ch == ')') {
5027 if (!nested_depth) {
5028 ast_error(c, n, "f-string: unmatched '%c'", ch);
5029 return -1;
5030 }
5031 nested_depth--;
5032 int opening = parenstack[nested_depth];
5033 if (!((opening == '(' && ch == ')') ||
5034 (opening == '[' && ch == ']') ||
5035 (opening == '{' && ch == '}')))
5036 {
5037 ast_error(c, n,
5038 "f-string: closing parenthesis '%c' "
5039 "does not match opening parenthesis '%c'",
5040 ch, opening);
5041 return -1;
5042 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005043 } else {
5044 /* Just consume this char and loop around. */
5045 }
5046 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005047 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005048 /* If we leave this loop in a string or with mismatched parens, we
5049 don't care. We'll get a syntax error when compiling the
5050 expression. But, we can produce a better error message, so
5051 let's just do that.*/
5052 if (quote_char) {
5053 ast_error(c, n, "f-string: unterminated string");
5054 return -1;
5055 }
5056 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005057 int opening = parenstack[nested_depth - 1];
5058 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005059 return -1;
5060 }
5061
Eric V. Smith451d0e32016-09-09 21:56:20 -04005062 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005063 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005064
5065 /* Compile the expression as soon as possible, so we show errors
5066 related to the expression before errors related to the
5067 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005068 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005069 if (!simple_expression)
5070 return -1;
5071
5072 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005073 if (**str == '!') {
5074 *str += 1;
5075 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005076 goto unexpected_end_of_string;
5077
Eric V. Smith451d0e32016-09-09 21:56:20 -04005078 conversion = **str;
5079 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005080
5081 /* Validate the conversion. */
5082 if (!(conversion == 's' || conversion == 'r'
5083 || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005084 ast_error(c, n,
5085 "f-string: invalid conversion character: "
5086 "expected 's', 'r', or 'a'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005087 return -1;
5088 }
5089 }
5090
5091 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005092 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005093 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005094 if (**str == ':') {
5095 *str += 1;
5096 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005097 goto unexpected_end_of_string;
5098
5099 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005100 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005101 if (!format_spec)
5102 return -1;
5103 }
5104
Eric V. Smith451d0e32016-09-09 21:56:20 -04005105 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005106 goto unexpected_end_of_string;
5107
5108 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005109 assert(*str < end);
5110 assert(**str == '}');
5111 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005112
Eric V. Smith451d0e32016-09-09 21:56:20 -04005113 /* And now create the FormattedValue node that represents this
5114 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005115 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005116 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005117 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118 c->c_arena);
5119 if (!*expression)
5120 return -1;
5121
5122 return 0;
5123
5124unexpected_end_of_string:
5125 ast_error(c, n, "f-string: expecting '}'");
5126 return -1;
5127}
5128
5129/* Return -1 on error.
5130
5131 Return 0 if we have a literal (possible zero length) and an
5132 expression (zero length if at the end of the string.
5133
5134 Return 1 if we have a literal, but no expression, and we want the
5135 caller to call us again. This is used to deal with doubled
5136 braces.
5137
5138 When called multiple times on the string 'a{{b{0}c', this function
5139 will return:
5140
5141 1. the literal 'a{' with no expression, and a return value
5142 of 1. Despite the fact that there's no expression, the return
5143 value of 1 means we're not finished yet.
5144
5145 2. the literal 'b' and the expression '0', with a return value of
5146 0. The fact that there's an expression means we're not finished.
5147
5148 3. literal 'c' with no expression and a return value of 0. The
5149 combination of the return value of 0 with no expression means
5150 we're finished.
5151*/
5152static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005153fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5154 int recurse_lvl, PyObject **literal,
5155 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005156 struct compiling *c, const node *n)
5157{
5158 int result;
5159
5160 assert(*literal == NULL && *expression == NULL);
5161
5162 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005163 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005164 if (result < 0)
5165 goto error;
5166
5167 assert(result == 0 || result == 1);
5168
5169 if (result == 1)
5170 /* We have a literal, but don't look at the expression. */
5171 return 1;
5172
Eric V. Smith451d0e32016-09-09 21:56:20 -04005173 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005174 /* We're at the end of the string or the end of a nested
5175 f-string: no expression. The top-level error case where we
5176 expect to be at the end of the string but we're at a '}' is
5177 handled later. */
5178 return 0;
5179
5180 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005181 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005182
Eric V. Smith451d0e32016-09-09 21:56:20 -04005183 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005184 goto error;
5185
5186 return 0;
5187
5188error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005189 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005190 return -1;
5191}
5192
5193#define EXPRLIST_N_CACHED 64
5194
5195typedef struct {
5196 /* Incrementally build an array of expr_ty, so be used in an
5197 asdl_seq. Cache some small but reasonably sized number of
5198 expr_ty's, and then after that start dynamically allocating,
5199 doubling the number allocated each time. Note that the f-string
5200 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005201 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005202 fast as you add exressions in an f-string. */
5203
5204 Py_ssize_t allocated; /* Number we've allocated. */
5205 Py_ssize_t size; /* Number we've used. */
5206 expr_ty *p; /* Pointer to the memory we're actually
5207 using. Will point to 'data' until we
5208 start dynamically allocating. */
5209 expr_ty data[EXPRLIST_N_CACHED];
5210} ExprList;
5211
5212#ifdef NDEBUG
5213#define ExprList_check_invariants(l)
5214#else
5215static void
5216ExprList_check_invariants(ExprList *l)
5217{
5218 /* Check our invariants. Make sure this object is "live", and
5219 hasn't been deallocated. */
5220 assert(l->size >= 0);
5221 assert(l->p != NULL);
5222 if (l->size <= EXPRLIST_N_CACHED)
5223 assert(l->data == l->p);
5224}
5225#endif
5226
5227static void
5228ExprList_Init(ExprList *l)
5229{
5230 l->allocated = EXPRLIST_N_CACHED;
5231 l->size = 0;
5232
5233 /* Until we start allocating dynamically, p points to data. */
5234 l->p = l->data;
5235
5236 ExprList_check_invariants(l);
5237}
5238
5239static int
5240ExprList_Append(ExprList *l, expr_ty exp)
5241{
5242 ExprList_check_invariants(l);
5243 if (l->size >= l->allocated) {
5244 /* We need to alloc (or realloc) the memory. */
5245 Py_ssize_t new_size = l->allocated * 2;
5246
5247 /* See if we've ever allocated anything dynamically. */
5248 if (l->p == l->data) {
5249 Py_ssize_t i;
5250 /* We're still using the cached data. Switch to
5251 alloc-ing. */
5252 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5253 if (!l->p)
5254 return -1;
5255 /* Copy the cached data into the new buffer. */
5256 for (i = 0; i < l->size; i++)
5257 l->p[i] = l->data[i];
5258 } else {
5259 /* Just realloc. */
5260 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5261 if (!tmp) {
5262 PyMem_RawFree(l->p);
5263 l->p = NULL;
5264 return -1;
5265 }
5266 l->p = tmp;
5267 }
5268
5269 l->allocated = new_size;
5270 assert(l->allocated == 2 * l->size);
5271 }
5272
5273 l->p[l->size++] = exp;
5274
5275 ExprList_check_invariants(l);
5276 return 0;
5277}
5278
5279static void
5280ExprList_Dealloc(ExprList *l)
5281{
5282 ExprList_check_invariants(l);
5283
5284 /* If there's been an error, or we've never dynamically allocated,
5285 do nothing. */
5286 if (!l->p || l->p == l->data) {
5287 /* Do nothing. */
5288 } else {
5289 /* We have dynamically allocated. Free the memory. */
5290 PyMem_RawFree(l->p);
5291 }
5292 l->p = NULL;
5293 l->size = -1;
5294}
5295
5296static asdl_seq *
5297ExprList_Finish(ExprList *l, PyArena *arena)
5298{
5299 asdl_seq *seq;
5300
5301 ExprList_check_invariants(l);
5302
5303 /* Allocate the asdl_seq and copy the expressions in to it. */
5304 seq = _Py_asdl_seq_new(l->size, arena);
5305 if (seq) {
5306 Py_ssize_t i;
5307 for (i = 0; i < l->size; i++)
5308 asdl_seq_SET(seq, i, l->p[i]);
5309 }
5310 ExprList_Dealloc(l);
5311 return seq;
5312}
5313
5314/* The FstringParser is designed to add a mix of strings and
5315 f-strings, and concat them together as needed. Ultimately, it
5316 generates an expr_ty. */
5317typedef struct {
5318 PyObject *last_str;
5319 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005320 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005321} FstringParser;
5322
5323#ifdef NDEBUG
5324#define FstringParser_check_invariants(state)
5325#else
5326static void
5327FstringParser_check_invariants(FstringParser *state)
5328{
5329 if (state->last_str)
5330 assert(PyUnicode_CheckExact(state->last_str));
5331 ExprList_check_invariants(&state->expr_list);
5332}
5333#endif
5334
5335static void
5336FstringParser_Init(FstringParser *state)
5337{
5338 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005339 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005340 ExprList_Init(&state->expr_list);
5341 FstringParser_check_invariants(state);
5342}
5343
5344static void
5345FstringParser_Dealloc(FstringParser *state)
5346{
5347 FstringParser_check_invariants(state);
5348
5349 Py_XDECREF(state->last_str);
5350 ExprList_Dealloc(&state->expr_list);
5351}
5352
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005353/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005354static expr_ty
5355make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5356{
5357 PyObject *s = *str;
5358 *str = NULL;
5359 assert(PyUnicode_CheckExact(s));
5360 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5361 Py_DECREF(s);
5362 return NULL;
5363 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005364 return Constant(s, LINENO(n), n->n_col_offset,
5365 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005366}
5367
5368/* Add a non-f-string (that is, a regular literal string). str is
5369 decref'd. */
5370static int
5371FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5372{
5373 FstringParser_check_invariants(state);
5374
5375 assert(PyUnicode_CheckExact(str));
5376
5377 if (PyUnicode_GET_LENGTH(str) == 0) {
5378 Py_DECREF(str);
5379 return 0;
5380 }
5381
5382 if (!state->last_str) {
5383 /* We didn't have a string before, so just remember this one. */
5384 state->last_str = str;
5385 } else {
5386 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005387 PyUnicode_AppendAndDel(&state->last_str, str);
5388 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005389 return -1;
5390 }
5391 FstringParser_check_invariants(state);
5392 return 0;
5393}
5394
Eric V. Smith451d0e32016-09-09 21:56:20 -04005395/* Parse an f-string. The f-string is in *str to end, with no
5396 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005397static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005398FstringParser_ConcatFstring(FstringParser *state, const char **str,
5399 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005400 struct compiling *c, const node *n)
5401{
5402 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005403 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005404
5405 /* Parse the f-string. */
5406 while (1) {
5407 PyObject *literal = NULL;
5408 expr_ty expression = NULL;
5409
5410 /* If there's a zero length literal in front of the
5411 expression, literal will be NULL. If we're at the end of
5412 the f-string, expression will be NULL (unless result == 1,
5413 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005414 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005415 &literal, &expression,
5416 c, n);
5417 if (result < 0)
5418 return -1;
5419
5420 /* Add the literal, if any. */
5421 if (!literal) {
5422 /* Do nothing. Just leave last_str alone (and possibly
5423 NULL). */
5424 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005425 /* Note that the literal can be zero length, if the
5426 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005427 state->last_str = literal;
5428 literal = NULL;
5429 } else {
5430 /* We have a literal, concatenate it. */
5431 assert(PyUnicode_GET_LENGTH(literal) != 0);
5432 if (FstringParser_ConcatAndDel(state, literal) < 0)
5433 return -1;
5434 literal = NULL;
5435 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005436
5437 /* We've dealt with the literal now. It can't be leaked on further
5438 errors. */
5439 assert(literal == NULL);
5440
5441 /* See if we should just loop around to get the next literal
5442 and expression, while ignoring the expression this
5443 time. This is used for un-doubling braces, as an
5444 optimization. */
5445 if (result == 1)
5446 continue;
5447
5448 if (!expression)
5449 /* We're done with this f-string. */
5450 break;
5451
5452 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005453 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005454 if (!state->last_str) {
5455 /* Do nothing. No previous literal. */
5456 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005457 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005458 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5459 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5460 return -1;
5461 }
5462
5463 if (ExprList_Append(&state->expr_list, expression) < 0)
5464 return -1;
5465 }
5466
Eric V. Smith235a6f02015-09-19 14:51:32 -04005467 /* If recurse_lvl is zero, then we must be at the end of the
5468 string. Otherwise, we must be at a right brace. */
5469
Eric V. Smith451d0e32016-09-09 21:56:20 -04005470 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005471 ast_error(c, n, "f-string: unexpected end of string");
5472 return -1;
5473 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005474 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005475 ast_error(c, n, "f-string: expecting '}'");
5476 return -1;
5477 }
5478
5479 FstringParser_check_invariants(state);
5480 return 0;
5481}
5482
5483/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005484 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005485static expr_ty
5486FstringParser_Finish(FstringParser *state, struct compiling *c,
5487 const node *n)
5488{
5489 asdl_seq *seq;
5490
5491 FstringParser_check_invariants(state);
5492
5493 /* If we're just a constant string with no expressions, return
5494 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005495 if (!state->fmode) {
5496 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005497 if (!state->last_str) {
5498 /* Create a zero length string. */
5499 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5500 if (!state->last_str)
5501 goto error;
5502 }
5503 return make_str_node_and_del(&state->last_str, c, n);
5504 }
5505
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005506 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005507 last node in our expression list. */
5508 if (state->last_str) {
5509 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5510 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5511 goto error;
5512 }
5513 /* This has already been freed. */
5514 assert(state->last_str == NULL);
5515
5516 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5517 if (!seq)
5518 goto error;
5519
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005520 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5521 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005522
5523error:
5524 FstringParser_Dealloc(state);
5525 return NULL;
5526}
5527
Eric V. Smith451d0e32016-09-09 21:56:20 -04005528/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5529 at end, parse it into an expr_ty. Return NULL on error. Adjust
5530 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005531static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005532fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005533 struct compiling *c, const node *n)
5534{
5535 FstringParser state;
5536
5537 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005538 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005539 c, n) < 0) {
5540 FstringParser_Dealloc(&state);
5541 return NULL;
5542 }
5543
5544 return FstringParser_Finish(&state, c, n);
5545}
5546
5547/* n is a Python string literal, including the bracketing quote
5548 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005549 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005550 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005551 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5552 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005553*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005554static int
5555parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5556 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005557{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005558 size_t len;
5559 const char *s = STR(n);
5560 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005561 int fmode = 0;
5562 *bytesmode = 0;
5563 *rawmode = 0;
5564 *result = NULL;
5565 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005566 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005567 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005568 if (quote == 'b' || quote == 'B') {
5569 quote = *++s;
5570 *bytesmode = 1;
5571 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005572 else if (quote == 'u' || quote == 'U') {
5573 quote = *++s;
5574 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005575 else if (quote == 'r' || quote == 'R') {
5576 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005577 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005578 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005579 else if (quote == 'f' || quote == 'F') {
5580 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005581 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005582 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005583 else {
5584 break;
5585 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005586 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005587 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005588 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005589 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005590 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005591 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005592 if (quote != '\'' && quote != '\"') {
5593 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005594 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005595 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005596 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005597 s++;
5598 len = strlen(s);
5599 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005600 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005601 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005602 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005603 }
5604 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005605 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005606 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005607 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005608 }
5609 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005610 /* A triple quoted string. We've already skipped one quote at
5611 the start and one at the end of the string. Now skip the
5612 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005613 s += 2;
5614 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005615 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005616 if (s[--len] != quote || s[--len] != quote) {
5617 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005618 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005619 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005620 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005621
Eric V. Smith451d0e32016-09-09 21:56:20 -04005622 if (fmode) {
5623 /* Just return the bytes. The caller will parse the resulting
5624 string. */
5625 *fstr = s;
5626 *fstrlen = len;
5627 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005628 }
5629
Eric V. Smith451d0e32016-09-09 21:56:20 -04005630 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005631 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005632 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005633 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005634 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005635 const char *ch;
5636 for (ch = s; *ch; ch++) {
5637 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005638 ast_error(c, n,
5639 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005640 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005641 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005642 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005643 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005644 if (*rawmode)
5645 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005646 else
Eric V. Smith56466482016-10-31 14:46:26 -04005647 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005648 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005649 if (*rawmode)
5650 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005651 else
Eric V. Smith56466482016-10-31 14:46:26 -04005652 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005653 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005654 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005655}
5656
Eric V. Smith235a6f02015-09-19 14:51:32 -04005657/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5658 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005659 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005660 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005661 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005662 node if there's just an f-string (with no leading or trailing
5663 literals), or a JoinedStr node if there are multiple f-strings or
5664 any literals involved. */
5665static expr_ty
5666parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005667{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005668 int bytesmode = 0;
5669 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005670 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005671
5672 FstringParser state;
5673 FstringParser_Init(&state);
5674
5675 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005676 int this_bytesmode;
5677 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005678 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005679 const char *fstr;
5680 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005681
5682 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005683 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5684 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005685 goto error;
5686
5687 /* Check that we're not mixing bytes with unicode. */
5688 if (i != 0 && bytesmode != this_bytesmode) {
5689 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005690 /* s is NULL if the current string part is an f-string. */
5691 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005692 goto error;
5693 }
5694 bytesmode = this_bytesmode;
5695
Eric V. Smith451d0e32016-09-09 21:56:20 -04005696 if (fstr != NULL) {
5697 int result;
5698 assert(s == NULL && !bytesmode);
5699 /* This is an f-string. Parse and concatenate it. */
5700 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5701 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005702 if (result < 0)
5703 goto error;
5704 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005705 /* A string or byte string. */
5706 assert(s != NULL && fstr == NULL);
5707
Eric V. Smith451d0e32016-09-09 21:56:20 -04005708 assert(bytesmode ? PyBytes_CheckExact(s) :
5709 PyUnicode_CheckExact(s));
5710
Eric V. Smith451d0e32016-09-09 21:56:20 -04005711 if (bytesmode) {
5712 /* For bytes, concat as we go. */
5713 if (i == 0) {
5714 /* First time, just remember this value. */
5715 bytes_str = s;
5716 } else {
5717 PyBytes_ConcatAndDel(&bytes_str, s);
5718 if (!bytes_str)
5719 goto error;
5720 }
5721 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005722 /* This is a regular string. Concatenate it. */
5723 if (FstringParser_ConcatAndDel(&state, s) < 0)
5724 goto error;
5725 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005726 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005727 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005728 if (bytesmode) {
5729 /* Just return the bytes object and we're done. */
5730 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5731 goto error;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005732 return Constant(bytes_str, LINENO(n), n->n_col_offset,
5733 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005734 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005735
Eric V. Smith235a6f02015-09-19 14:51:32 -04005736 /* We're not a bytes string, bytes_str should never have been set. */
5737 assert(bytes_str == NULL);
5738
5739 return FstringParser_Finish(&state, c, n);
5740
5741error:
5742 Py_XDECREF(bytes_str);
5743 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005745}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005746
5747PyObject *
5748_PyAST_GetDocString(asdl_seq *body)
5749{
5750 if (!asdl_seq_LEN(body)) {
5751 return NULL;
5752 }
5753 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5754 if (st->kind != Expr_kind) {
5755 return NULL;
5756 }
5757 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005758 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5759 return e->v.Constant.value;
5760 }
5761 return NULL;
5762}