blob: c422e9651ced462c6c0ba9857ea8757f7d63d7d4 [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
702new_type_comment(const char *s)
703{
704 return PyUnicode_DecodeUTF8(s, strlen(s), NULL);
705}
706#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n))
707
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708static int
709num_stmts(const node *n)
710{
711 int i, l;
712 node *ch;
713
714 switch (TYPE(n)) {
715 case single_input:
716 if (TYPE(CHILD(n, 0)) == NEWLINE)
717 return 0;
718 else
719 return num_stmts(CHILD(n, 0));
720 case file_input:
721 l = 0;
722 for (i = 0; i < NCH(n); i++) {
723 ch = CHILD(n, i);
724 if (TYPE(ch) == stmt)
725 l += num_stmts(ch);
726 }
727 return l;
728 case stmt:
729 return num_stmts(CHILD(n, 0));
730 case compound_stmt:
731 return 1;
732 case simple_stmt:
733 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
734 case suite:
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800735 case func_body_suite:
736 /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
737 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 if (NCH(n) == 1)
739 return num_stmts(CHILD(n, 0));
740 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800741 i = 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742 l = 0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800743 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT)
744 i += 2;
745 for (; i < (NCH(n) - 1); i++)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 l += num_stmts(CHILD(n, i));
747 return l;
748 }
749 default: {
750 char buf[128];
751
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000752 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 TYPE(n), NCH(n));
754 Py_FatalError(buf);
755 }
756 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700757 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758}
759
760/* Transform the CST rooted at node * to the appropriate AST
761*/
762
763mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200764PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
765 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000767 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 asdl_seq *stmts = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800769 asdl_seq *type_ignores = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 stmt_ty s;
771 node *ch;
772 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500773 mod_ty res = NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800774 asdl_seq *argtypes = NULL;
775 expr_ty ret, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400777 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200778 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400779 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800780 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800781
782 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Jeremy Hyltona8293132006-02-28 17:58:27 +0000785 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 switch (TYPE(n)) {
787 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200788 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500790 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 for (i = 0; i < NCH(n) - 1; i++) {
792 ch = CHILD(n, i);
793 if (TYPE(ch) == NEWLINE)
794 continue;
795 REQ(ch, stmt);
796 num = num_stmts(ch);
797 if (num == 1) {
798 s = ast_for_stmt(&c, ch);
799 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500800 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000801 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 }
803 else {
804 ch = CHILD(ch, 0);
805 REQ(ch, simple_stmt);
806 for (j = 0; j < num; j++) {
807 s = ast_for_stmt(&c, CHILD(ch, j * 2));
808 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000810 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 }
812 }
813 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800814
815 /* Type ignores are stored under the ENDMARKER in file_input. */
816 ch = CHILD(n, NCH(n) - 1);
817 REQ(ch, ENDMARKER);
818 num = NCH(ch);
819 type_ignores = _Py_asdl_seq_new(num, arena);
820 if (!type_ignores)
821 goto out;
822
823 for (i = 0; i < num; i++) {
824 type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), arena);
825 if (!ti)
826 goto out;
827 asdl_seq_SET(type_ignores, i, ti);
828 }
829
830 res = Module(stmts, type_ignores, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500831 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 case eval_input: {
833 expr_ty testlist_ast;
834
Nick Coghlan650f0d02007-04-15 12:05:43 +0000835 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000836 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 goto out;
839 res = Expression(testlist_ast, arena);
840 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 }
842 case single_input:
843 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200844 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500846 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000847 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000848 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000849 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000850 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500851 goto out;
852 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 }
854 else {
855 n = CHILD(n, 0);
856 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200857 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500859 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000861 s = ast_for_stmt(&c, n);
862 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500863 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 asdl_seq_SET(stmts, 0, s);
865 }
866 else {
867 /* Only a simple_stmt can contain multiple statements. */
868 REQ(n, simple_stmt);
869 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 if (TYPE(CHILD(n, i)) == NEWLINE)
871 break;
872 s = ast_for_stmt(&c, CHILD(n, i));
873 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500874 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 asdl_seq_SET(stmts, i / 2, s);
876 }
877 }
878
Benjamin Peterson55e00432012-01-16 17:22:31 -0500879 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500881 break;
Guido van Rossumdcfcd142019-01-31 03:40:27 -0800882 case func_type_input:
883 n = CHILD(n, 0);
884 REQ(n, func_type);
885
886 if (TYPE(CHILD(n, 1)) == typelist) {
887 ch = CHILD(n, 1);
888 /* this is overly permissive -- we don't pay any attention to
889 * stars on the args -- just parse them into an ordered list */
890 num = 0;
891 for (i = 0; i < NCH(ch); i++) {
892 if (TYPE(CHILD(ch, i)) == test) {
893 num++;
894 }
895 }
896
897 argtypes = _Py_asdl_seq_new(num, arena);
898 if (!argtypes)
899 goto out;
900
901 j = 0;
902 for (i = 0; i < NCH(ch); i++) {
903 if (TYPE(CHILD(ch, i)) == test) {
904 arg = ast_for_expr(&c, CHILD(ch, i));
905 if (!arg)
906 goto out;
907 asdl_seq_SET(argtypes, j++, arg);
908 }
909 }
910 }
911 else {
912 argtypes = _Py_asdl_seq_new(0, arena);
913 if (!argtypes)
914 goto out;
915 }
916
917 ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1));
918 if (!ret)
919 goto out;
920 res = FunctionType(argtypes, ret, arena);
921 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000923 PyErr_Format(PyExc_SystemError,
924 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500925 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500927 out:
928 if (c.c_normalize) {
929 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500930 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500931 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932}
933
Victor Stinner14e461d2013-08-26 22:28:21 +0200934mod_ty
935PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
936 PyArena *arena)
937{
938 mod_ty mod;
939 PyObject *filename;
940 filename = PyUnicode_DecodeFSDefault(filename_str);
941 if (filename == NULL)
942 return NULL;
943 mod = PyAST_FromNodeObject(n, flags, filename, arena);
944 Py_DECREF(filename);
945 return mod;
946
947}
948
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
950*/
951
952static operator_ty
953get_operator(const node *n)
954{
955 switch (TYPE(n)) {
956 case VBAR:
957 return BitOr;
958 case CIRCUMFLEX:
959 return BitXor;
960 case AMPER:
961 return BitAnd;
962 case LEFTSHIFT:
963 return LShift;
964 case RIGHTSHIFT:
965 return RShift;
966 case PLUS:
967 return Add;
968 case MINUS:
969 return Sub;
970 case STAR:
971 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400972 case AT:
973 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 case SLASH:
975 return Div;
976 case DOUBLESLASH:
977 return FloorDiv;
978 case PERCENT:
979 return Mod;
980 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000981 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 }
983}
984
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200985static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000986 "None",
987 "True",
988 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200989 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000990 NULL,
991};
992
993static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400994forbidden_name(struct compiling *c, identifier name, const node *n,
995 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000996{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000997 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200998 const char * const *p = FORBIDDEN;
999 if (!full_checks) {
1000 /* In most cases, the parser will protect True, False, and None
1001 from being assign to. */
1002 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +00001003 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001004 for (; *p; p++) {
1005 if (_PyUnicode_EqualToASCIIString(name, *p)) {
1006 ast_error(c, n, "cannot assign to %U", name);
1007 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +00001008 }
1009 }
1010 return 0;
1011}
1012
Serhiy Storchakab619b092018-11-27 09:40:29 +02001013static expr_ty
1014copy_location(expr_ty e, const node *n)
1015{
1016 if (e) {
1017 e->lineno = LINENO(n);
1018 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001019 e->end_lineno = n->n_end_lineno;
1020 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +02001021 }
1022 return e;
1023}
1024
Jeremy Hyltona8293132006-02-28 17:58:27 +00001025/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
1027 Only sets context for expr kinds that "can appear in assignment context"
1028 (according to ../Parser/Python.asdl). For other expr kinds, it sets
1029 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030*/
1031
1032static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001033set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034{
1035 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001036 /* If a particular expression type can't be used for assign / delete,
1037 set expr_name to its name and an error message will be generated.
1038 */
1039 const char* expr_name = NULL;
1040
1041 /* The ast defines augmented store and load contexts, but the
1042 implementation here doesn't actually use them. The code may be
1043 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001044 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001045 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001046 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001047 */
1048 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049
1050 switch (e->kind) {
1051 case Attribute_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001052 if (ctx == NamedStore) {
1053 expr_name = "attribute";
1054 break;
1055 }
1056
Thomas Wouters89f507f2006-12-13 04:49:30 +00001057 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001058 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001059 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001060 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 case Subscript_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001062 if (ctx == NamedStore) {
1063 expr_name = "subscript";
1064 break;
1065 }
1066
Thomas Wouters89f507f2006-12-13 04:49:30 +00001067 e->v.Subscript.ctx = ctx;
1068 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001069 case Starred_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001070 if (ctx == NamedStore) {
1071 expr_name = "starred";
1072 break;
1073 }
1074
Guido van Rossum0368b722007-05-11 16:50:42 +00001075 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001076 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001077 return 0;
1078 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001080 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001081 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001082 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001083 }
1084 e->v.Name.ctx = ctx;
1085 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 case List_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001087 if (ctx == NamedStore) {
1088 expr_name = "list";
1089 break;
1090 }
1091
Thomas Wouters89f507f2006-12-13 04:49:30 +00001092 e->v.List.ctx = ctx;
1093 s = e->v.List.elts;
1094 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 case Tuple_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001096 if (ctx == NamedStore) {
1097 expr_name = "tuple";
1098 break;
1099 }
1100
Berker Peksag094c9c92016-05-18 08:44:29 +03001101 e->v.Tuple.ctx = ctx;
1102 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001103 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001104 case Lambda_kind:
1105 expr_name = "lambda";
1106 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001108 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001109 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001110 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001112 case UnaryOp_kind:
1113 expr_name = "operator";
1114 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001116 expr_name = "generator expression";
1117 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001118 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001119 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001120 expr_name = "yield expression";
1121 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001122 case Await_kind:
1123 expr_name = "await expression";
1124 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001125 case ListComp_kind:
1126 expr_name = "list comprehension";
1127 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001128 case SetComp_kind:
1129 expr_name = "set comprehension";
1130 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001131 case DictComp_kind:
1132 expr_name = "dict comprehension";
1133 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001134 case Dict_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001135 expr_name = "dict display";
1136 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001137 case Set_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001138 expr_name = "set display";
1139 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001140 case JoinedStr_kind:
1141 case FormattedValue_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001142 expr_name = "f-string expression";
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001143 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001144 case Constant_kind: {
1145 PyObject *value = e->v.Constant.value;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001146 if (value == Py_None || value == Py_False || value == Py_True
1147 || value == Py_Ellipsis)
1148 {
1149 return ast_error(c, n, "cannot %s %R",
1150 ctx == Store ? "assign to" : "delete",
1151 value);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001152 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001153 expr_name = "literal";
Benjamin Peterson442f2092012-12-06 17:41:04 -05001154 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001155 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001156 case Compare_kind:
1157 expr_name = "comparison";
1158 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001159 case IfExp_kind:
1160 expr_name = "conditional expression";
1161 break;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001162 case NamedExpr_kind:
1163 expr_name = "named expression";
1164 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001165 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 PyErr_Format(PyExc_SystemError,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001167 "unexpected expression in %sassignment %d (line %d)",
1168 ctx == NamedStore ? "named ": "",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001169 e->kind, e->lineno);
1170 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001172 /* Check for error string set by switch */
1173 if (expr_name) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001174 if (ctx == NamedStore) {
1175 return ast_error(c, n, "cannot use named assignment with %s",
1176 expr_name);
1177 }
1178 else {
1179 return ast_error(c, n, "cannot %s %s",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001180 ctx == Store ? "assign to" : "delete",
1181 expr_name);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001182 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001183 }
1184
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 */
1188 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001189 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190
Thomas Wouters89f507f2006-12-13 04:49:30 +00001191 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001192 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001193 return 0;
1194 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 }
1196 return 1;
1197}
1198
1199static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001200ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201{
1202 REQ(n, augassign);
1203 n = CHILD(n, 0);
1204 switch (STR(n)[0]) {
1205 case '+':
1206 return Add;
1207 case '-':
1208 return Sub;
1209 case '/':
1210 if (STR(n)[1] == '/')
1211 return FloorDiv;
1212 else
1213 return Div;
1214 case '%':
1215 return Mod;
1216 case '<':
1217 return LShift;
1218 case '>':
1219 return RShift;
1220 case '&':
1221 return BitAnd;
1222 case '^':
1223 return BitXor;
1224 case '|':
1225 return BitOr;
1226 case '*':
1227 if (STR(n)[1] == '*')
1228 return Pow;
1229 else
1230 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001231 case '@':
1232 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001234 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001235 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 }
1237}
1238
1239static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001240ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001242 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 |'is' 'not'
1244 */
1245 REQ(n, comp_op);
1246 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001247 n = CHILD(n, 0);
1248 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 case LESS:
1250 return Lt;
1251 case GREATER:
1252 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001253 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 return Eq;
1255 case LESSEQUAL:
1256 return LtE;
1257 case GREATEREQUAL:
1258 return GtE;
1259 case NOTEQUAL:
1260 return NotEq;
1261 case NAME:
1262 if (strcmp(STR(n), "in") == 0)
1263 return In;
1264 if (strcmp(STR(n), "is") == 0)
1265 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001266 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001268 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001271 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 }
1273 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001274 /* handle "not in" and "is not" */
1275 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 case NAME:
1277 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1278 return NotIn;
1279 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1280 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001281 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001283 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001285 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001286 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 }
Neal Norwitz79792652005-11-14 04:25:03 +00001288 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001290 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291}
1292
1293static asdl_seq *
1294seq_for_testlist(struct compiling *c, const node *n)
1295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001297 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1298 */
Armin Rigo31441302005-10-21 12:57:31 +00001299 asdl_seq *seq;
1300 expr_ty expression;
1301 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001302 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001304 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 if (!seq)
1306 return NULL;
1307
1308 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001310 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311
Benjamin Peterson4905e802009-09-27 02:43:28 +00001312 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001313 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315
1316 assert(i / 2 < seq->size);
1317 asdl_seq_SET(seq, i / 2, expression);
1318 }
1319 return seq;
1320}
1321
Neal Norwitzc1505362006-12-28 06:47:50 +00001322static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001323ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001324{
1325 identifier name;
1326 expr_ty annotation = NULL;
1327 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001328 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001329
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001330 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001331 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001332 name = NEW_IDENTIFIER(ch);
1333 if (!name)
1334 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001335 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001336 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001337
1338 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1339 annotation = ast_for_expr(c, CHILD(n, 2));
1340 if (!annotation)
1341 return NULL;
1342 }
1343
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001344 ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001345 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001346 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001347 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001348 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349}
1350
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351/* returns -1 if failed to handle keyword only arguments
1352 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001353 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001354 ^^^
1355 start pointing here
1356 */
1357static int
1358handle_keywordonly_args(struct compiling *c, const node *n, int start,
1359 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1360{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001361 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001363 expr_ty expression, annotation;
1364 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 int i = start;
1366 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001367
1368 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001369 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001370 return -1;
1371 }
1372 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 while (i < NCH(n)) {
1374 ch = CHILD(n, i);
1375 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001376 case vfpdef:
1377 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001379 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001380 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001381 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 asdl_seq_SET(kwdefaults, j, expression);
1383 i += 2; /* '=' and test */
1384 }
1385 else { /* setting NULL if no default value exists */
1386 asdl_seq_SET(kwdefaults, j, NULL);
1387 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001388 if (NCH(ch) == 3) {
1389 /* ch is NAME ':' test */
1390 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001391 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001392 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001393 }
1394 else {
1395 annotation = NULL;
1396 }
1397 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001398 argname = NEW_IDENTIFIER(ch);
1399 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001400 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001401 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001402 goto error;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001403 arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001404 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001405 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001406 if (!arg)
1407 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001408 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001409 i += 1; /* the name */
1410 if (TYPE(CHILD(n, i)) == COMMA)
1411 i += 1; /* the comma, if present */
1412 break;
1413 case TYPE_COMMENT:
1414 /* arg will be equal to the last argument processed */
1415 arg->type_comment = NEW_TYPE_COMMENT(ch);
1416 if (!arg->type_comment)
1417 goto error;
1418 i += 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 break;
1420 case DOUBLESTAR:
1421 return i;
1422 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001423 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 goto error;
1425 }
1426 }
1427 return i;
1428 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001430}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431
Jeremy Hyltona8293132006-02-28 17:58:27 +00001432/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433
1434static arguments_ty
1435ast_for_arguments(struct compiling *c, const node *n)
1436{
Neal Norwitzc1505362006-12-28 06:47:50 +00001437 /* This function handles both typedargslist (function definition)
1438 and varargslist (lambda definition).
1439
1440 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001441 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1442 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1443 | '**' tfpdef [',']]]
1444 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1445 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001446 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001447 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1448 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1449 | '**' vfpdef [',']]]
1450 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1451 | '**' vfpdef [',']
1452 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001453 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001454
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001456 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1457 int nposdefaults = 0, found_default = 0;
1458 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001459 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001460 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 node *ch;
1462
1463 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001464 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001465 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001466 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001468 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469
Jeremy Hyltone921e022008-07-17 16:37:17 +00001470 /* First count the number of positional args & defaults. The
1471 variable i is the loop index for this for loop and the next.
1472 The next loop picks up where the first leaves off.
1473 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001475 ch = CHILD(n, i);
1476 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001477 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001478 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001479 if (i < NCH(n) && /* skip argument following star */
1480 (TYPE(CHILD(n, i)) == tfpdef ||
1481 TYPE(CHILD(n, i)) == vfpdef)) {
1482 i++;
1483 }
1484 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001485 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001486 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001487 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001488 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001491 defaults for keyword only args */
1492 for ( ; i < NCH(n); ++i) {
1493 ch = CHILD(n, i);
1494 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001495 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001497 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001498 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001499 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001500 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001501 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001502 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001503 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001505 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001506 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001507 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001509 since we set NULL as default for keyword only argument w/o default
1510 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001511 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001512 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001513 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001514 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001515
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001516 /* tfpdef: NAME [':' test]
1517 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 */
1519 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001520 j = 0; /* index for defaults */
1521 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001523 ch = CHILD(n, i);
1524 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001525 case tfpdef:
1526 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1528 anything other than EQUAL or a comma? */
1529 /* XXX Should NCH(n) check be made a separate check? */
1530 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001531 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1532 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001533 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001534 assert(posdefaults != NULL);
1535 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001537 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001539 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001540 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001541 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001542 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001543 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001544 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001545 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001546 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001547 asdl_seq_SET(posargs, k++, arg);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001548 i += 1; /* the name */
1549 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1550 i += 1; /* the comma, if present */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 break;
1552 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001553 if (i+1 >= NCH(n) ||
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001554 (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA
1555 || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001556 ast_error(c, CHILD(n, i),
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001557 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001558 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001559 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001560 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001561 if (TYPE(ch) == COMMA) {
1562 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001563 i += 2; /* now follows keyword only arguments */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001564
1565 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1566 ast_error(c, CHILD(n, i),
1567 "bare * has associated type comment");
1568 return NULL;
1569 }
1570
Guido van Rossum4f72a782006-10-27 23:31:49 +00001571 res = handle_keywordonly_args(c, n, i,
1572 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001573 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001574 i = res; /* res has new position to process */
1575 }
1576 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001577 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001578 if (!vararg)
1579 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001580
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001581 i += 2; /* the star and the name */
1582 if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA)
1583 i += 1; /* the comma, if present */
1584
1585 if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) {
1586 vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i));
1587 if (!vararg->type_comment)
1588 return NULL;
1589 i += 1;
1590 }
1591
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001592 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1593 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001594 int res = 0;
1595 res = handle_keywordonly_args(c, n, i,
1596 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001597 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001598 i = res; /* res has new position to process */
1599 }
1600 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601 break;
1602 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001603 ch = CHILD(n, i+1); /* tfpdef */
1604 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001605 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001606 if (!kwarg)
1607 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001608 i += 2; /* the double star and the name */
1609 if (TYPE(CHILD(n, i)) == COMMA)
1610 i += 1; /* the comma, if present */
1611 break;
1612 case TYPE_COMMENT:
1613 assert(i);
1614
1615 if (kwarg)
1616 arg = kwarg;
1617
1618 /* arg will be equal to the last argument processed */
1619 arg->type_comment = NEW_TYPE_COMMENT(ch);
1620 if (!arg->type_comment)
1621 return NULL;
1622 i += 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 break;
1624 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001625 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 "unexpected node in varargslist: %d @ %d",
1627 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001628 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001631 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632}
1633
1634static expr_ty
1635ast_for_dotted_name(struct compiling *c, const node *n)
1636{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001637 expr_ty e;
1638 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001639 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001641 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642
1643 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001644
1645 lineno = LINENO(n);
1646 col_offset = n->n_col_offset;
1647
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001648 ch = CHILD(n, 0);
1649 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001651 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001652 e = Name(id, Load, lineno, col_offset,
1653 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656
1657 for (i = 2; i < NCH(n); i+=2) {
1658 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001659 if (!id)
1660 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001661 e = Attribute(e, id, Load, lineno, col_offset,
1662 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001663 if (!e)
1664 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 }
1666
1667 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668}
1669
1670static expr_ty
1671ast_for_decorator(struct compiling *c, const node *n)
1672{
1673 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1674 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001675 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001678 REQ(CHILD(n, 0), AT);
1679 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1682 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001683 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001686 d = name_expr;
1687 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 }
1689 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001690 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001691 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001692 if (!d)
1693 return NULL;
1694 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 }
1696 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001697 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001698 if (!d)
1699 return NULL;
1700 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 }
1702
1703 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704}
1705
1706static asdl_seq*
1707ast_for_decorators(struct compiling *c, const node *n)
1708{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001709 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001710 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001714 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 if (!decorator_seq)
1716 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001719 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001720 if (!d)
1721 return NULL;
1722 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 }
1724 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725}
1726
1727static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001728ast_for_funcdef_impl(struct compiling *c, const node *n0,
1729 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001731 /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */
guoci90fc8982018-09-11 17:45:45 -04001732 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001733 identifier name;
1734 arguments_ty args;
1735 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001736 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001737 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001738 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001739 node *tc;
1740 string type_comment = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741
1742 REQ(n, funcdef);
1743
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 name = NEW_IDENTIFIER(CHILD(n, name_i));
1745 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001746 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001747 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001748 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1750 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001751 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001752 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1753 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1754 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001755 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001756 name_i += 2;
1757 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001758 if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) {
1759 type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3));
1760 if (!type_comment)
1761 return NULL;
1762 name_i += 1;
1763 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001764 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001766 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001767 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001769 if (NCH(CHILD(n, name_i + 3)) > 1) {
1770 /* Check if the suite has a type comment in it. */
1771 tc = CHILD(CHILD(n, name_i + 3), 1);
1772
1773 if (TYPE(tc) == TYPE_COMMENT) {
1774 if (type_comment != NULL) {
1775 ast_error(c, n, "Cannot have two type comments on def");
1776 return NULL;
1777 }
1778 type_comment = NEW_TYPE_COMMENT(tc);
1779 if (!type_comment)
1780 return NULL;
1781 }
1782 }
1783
Yury Selivanov75445082015-05-11 22:57:16 -04001784 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001785 return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001786 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001787 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08001788 return FunctionDef(name, args, body, decorator_seq, returns, type_comment,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001789 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001790}
1791
1792static stmt_ty
1793ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1794{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001795 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001796 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001797 REQ(CHILD(n, 0), NAME);
1798 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001799 REQ(CHILD(n, 1), funcdef);
1800
guoci90fc8982018-09-11 17:45:45 -04001801 return ast_for_funcdef_impl(c, n, decorator_seq,
1802 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001803}
1804
1805static stmt_ty
1806ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1807{
1808 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1809 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001810 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001811}
1812
1813
1814static stmt_ty
1815ast_for_async_stmt(struct compiling *c, const node *n)
1816{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001817 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001818 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001819 REQ(CHILD(n, 0), NAME);
1820 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001821
1822 switch (TYPE(CHILD(n, 1))) {
1823 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001824 return ast_for_funcdef_impl(c, n, NULL,
1825 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001826 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001827 return ast_for_with_stmt(c, n,
1828 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001829
1830 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001831 return ast_for_for_stmt(c, n,
1832 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001833
1834 default:
1835 PyErr_Format(PyExc_SystemError,
1836 "invalid async stament: %s",
1837 STR(CHILD(n, 1)));
1838 return NULL;
1839 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840}
1841
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001842static stmt_ty
1843ast_for_decorated(struct compiling *c, const node *n)
1844{
Yury Selivanov75445082015-05-11 22:57:16 -04001845 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001846 stmt_ty thing = NULL;
1847 asdl_seq *decorator_seq = NULL;
1848
1849 REQ(n, decorated);
1850
1851 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1852 if (!decorator_seq)
1853 return NULL;
1854
1855 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001856 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001858
1859 if (TYPE(CHILD(n, 1)) == funcdef) {
1860 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1861 } else if (TYPE(CHILD(n, 1)) == classdef) {
1862 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001863 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1864 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001865 }
1866 return thing;
1867}
1868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001870ast_for_namedexpr(struct compiling *c, const node *n)
1871{
1872 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1873 ['else' ':' suite]
1874 namedexpr_test: test [':=' test]
1875 argument: ( test [comp_for] |
1876 test ':=' test |
1877 test '=' test |
1878 '**' test |
1879 '*' test )
1880 */
1881 expr_ty target, value;
1882
1883 target = ast_for_expr(c, CHILD(n, 0));
1884 if (!target)
1885 return NULL;
1886
1887 value = ast_for_expr(c, CHILD(n, 2));
1888 if (!value)
1889 return NULL;
1890
1891 if (!set_context(c, target, NamedStore, n))
1892 return NULL;
1893
1894 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1895 n->n_end_col_offset, c->c_arena);
1896}
1897
1898static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899ast_for_lambdef(struct compiling *c, const node *n)
1900{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001901 /* lambdef: 'lambda' [varargslist] ':' test
1902 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903 arguments_ty args;
1904 expr_ty expression;
1905
1906 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001907 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 if (!args)
1909 return NULL;
1910 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001911 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 }
1914 else {
1915 args = ast_for_arguments(c, CHILD(n, 1));
1916 if (!args)
1917 return NULL;
1918 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001919 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 }
1922
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001923 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1924 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925}
1926
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001927static expr_ty
1928ast_for_ifexpr(struct compiling *c, const node *n)
1929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001931 expr_ty expression, body, orelse;
1932
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001933 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001934 body = ast_for_expr(c, CHILD(n, 0));
1935 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001936 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001937 expression = ast_for_expr(c, CHILD(n, 2));
1938 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001939 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001940 orelse = ast_for_expr(c, CHILD(n, 4));
1941 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001942 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001943 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001944 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001945 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001946}
1947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001949 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950
Nick Coghlan650f0d02007-04-15 12:05:43 +00001951 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952*/
1953
1954static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001955count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001957 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958
Guido van Rossumd8faa362007-04-27 19:54:29 +00001959 count_comp_for:
1960 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001961 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001962 if (NCH(n) == 2) {
1963 REQ(CHILD(n, 0), NAME);
1964 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1965 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001966 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001967 else if (NCH(n) == 1) {
1968 n = CHILD(n, 0);
1969 }
1970 else {
1971 goto error;
1972 }
1973 if (NCH(n) == (5)) {
1974 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001975 }
1976 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001977 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001978 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001979 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001980 REQ(n, comp_iter);
1981 n = CHILD(n, 0);
1982 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001983 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001984 else if (TYPE(n) == comp_if) {
1985 if (NCH(n) == 3) {
1986 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001987 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001988 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001989 else
1990 return n_fors;
1991 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001992
Jelle Zijlstraac317702017-10-05 20:24:46 -07001993 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001994 /* Should never be reached */
1995 PyErr_SetString(PyExc_SystemError,
1996 "logic error in count_comp_fors");
1997 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998}
1999
Nick Coghlan650f0d02007-04-15 12:05:43 +00002000/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001
Nick Coghlan650f0d02007-04-15 12:05:43 +00002002 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003*/
2004
2005static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002006count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002008 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009
Guido van Rossumd8faa362007-04-27 19:54:29 +00002010 while (1) {
2011 REQ(n, comp_iter);
2012 if (TYPE(CHILD(n, 0)) == comp_for)
2013 return n_ifs;
2014 n = CHILD(n, 0);
2015 REQ(n, comp_if);
2016 n_ifs++;
2017 if (NCH(n) == 2)
2018 return n_ifs;
2019 n = CHILD(n, 2);
2020 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021}
2022
Guido van Rossum992d4a32007-07-11 13:09:30 +00002023static asdl_seq *
2024ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002027 asdl_seq *comps;
2028
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002029 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 if (n_fors == -1)
2031 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002032
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002033 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002034 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002036
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002038 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002040 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002041 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002042 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002043 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044
Guido van Rossum992d4a32007-07-11 13:09:30 +00002045 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046
Jelle Zijlstraac317702017-10-05 20:24:46 -07002047 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002048 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002049 REQ(CHILD(n, 0), NAME);
2050 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
2051 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002052 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07002053 else {
2054 sync_n = CHILD(n, 0);
2055 }
2056 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002057
Jelle Zijlstraac317702017-10-05 20:24:46 -07002058 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002059 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002060 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07002062 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002063 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065
Thomas Wouters89f507f2006-12-13 04:49:30 +00002066 /* Check the # of children rather than the length of t, since
2067 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00002068 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002069 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002070 comp = comprehension(first, expression, NULL,
2071 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002073 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
2074 for_ch->n_end_lineno, for_ch->n_end_col_offset,
2075 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07002076 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002077 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002079
Jelle Zijlstraac317702017-10-05 20:24:46 -07002080 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 int j, n_ifs;
2082 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083
Jelle Zijlstraac317702017-10-05 20:24:46 -07002084 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002085 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002086 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002088
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002089 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002090 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002094 REQ(n, comp_iter);
2095 n = CHILD(n, 0);
2096 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097
Guido van Rossum992d4a32007-07-11 13:09:30 +00002098 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002100 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00002101 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002102 if (NCH(n) == 3)
2103 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002105 /* on exit, must guarantee that n is a comp_for */
2106 if (TYPE(n) == comp_iter)
2107 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002108 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002110 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00002112 return comps;
2113}
2114
2115static expr_ty
2116ast_for_itercomp(struct compiling *c, const node *n, int type)
2117{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002118 /* testlist_comp: (test|star_expr)
2119 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002120 expr_ty elt;
2121 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002122 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123
Guido van Rossum992d4a32007-07-11 13:09:30 +00002124 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002126 ch = CHILD(n, 0);
2127 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002128 if (!elt)
2129 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002130 if (elt->kind == Starred_kind) {
2131 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
2132 return NULL;
2133 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134
Guido van Rossum992d4a32007-07-11 13:09:30 +00002135 comps = ast_for_comprehension(c, CHILD(n, 1));
2136 if (!comps)
2137 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002138
2139 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002140 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2141 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002142 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002143 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2144 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002145 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002146 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2147 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002148 else
2149 /* Should never happen */
2150 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151}
2152
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002153/* Fills in the key, value pair corresponding to the dict element. In case
2154 * of an unpacking, key is NULL. *i is advanced by the number of ast
2155 * elements. Iff successful, nonzero is returned.
2156 */
2157static int
2158ast_for_dictelement(struct compiling *c, const node *n, int *i,
2159 expr_ty *key, expr_ty *value)
2160{
2161 expr_ty expression;
2162 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2163 assert(NCH(n) - *i >= 2);
2164
2165 expression = ast_for_expr(c, CHILD(n, *i + 1));
2166 if (!expression)
2167 return 0;
2168 *key = NULL;
2169 *value = expression;
2170
2171 *i += 2;
2172 }
2173 else {
2174 assert(NCH(n) - *i >= 3);
2175
2176 expression = ast_for_expr(c, CHILD(n, *i));
2177 if (!expression)
2178 return 0;
2179 *key = expression;
2180
2181 REQ(CHILD(n, *i + 1), COLON);
2182
2183 expression = ast_for_expr(c, CHILD(n, *i + 2));
2184 if (!expression)
2185 return 0;
2186 *value = expression;
2187
2188 *i += 3;
2189 }
2190 return 1;
2191}
2192
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002194ast_for_dictcomp(struct compiling *c, const node *n)
2195{
2196 expr_ty key, value;
2197 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002198 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002201 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002202 assert(key);
2203 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002206 if (!comps)
2207 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002209 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2210 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002211}
2212
2213static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214ast_for_dictdisplay(struct compiling *c, const node *n)
2215{
2216 int i;
2217 int j;
2218 int size;
2219 asdl_seq *keys, *values;
2220
2221 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2222 keys = _Py_asdl_seq_new(size, c->c_arena);
2223 if (!keys)
2224 return NULL;
2225
2226 values = _Py_asdl_seq_new(size, c->c_arena);
2227 if (!values)
2228 return NULL;
2229
2230 j = 0;
2231 for (i = 0; i < NCH(n); i++) {
2232 expr_ty key, value;
2233
2234 if (!ast_for_dictelement(c, n, &i, &key, &value))
2235 return NULL;
2236 asdl_seq_SET(keys, j, key);
2237 asdl_seq_SET(values, j, value);
2238
2239 j++;
2240 }
2241 keys->size = j;
2242 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002243 return Dict(keys, values, LINENO(n), n->n_col_offset,
2244 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002245}
2246
2247static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002248ast_for_genexp(struct compiling *c, const node *n)
2249{
2250 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002251 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002252}
2253
2254static expr_ty
2255ast_for_listcomp(struct compiling *c, const node *n)
2256{
2257 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002258 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002259}
2260
2261static expr_ty
2262ast_for_setcomp(struct compiling *c, const node *n)
2263{
2264 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002265 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002266}
2267
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002268static expr_ty
2269ast_for_setdisplay(struct compiling *c, const node *n)
2270{
2271 int i;
2272 int size;
2273 asdl_seq *elts;
2274
2275 assert(TYPE(n) == (dictorsetmaker));
2276 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2277 elts = _Py_asdl_seq_new(size, c->c_arena);
2278 if (!elts)
2279 return NULL;
2280 for (i = 0; i < NCH(n); i += 2) {
2281 expr_ty expression;
2282 expression = ast_for_expr(c, CHILD(n, i));
2283 if (!expression)
2284 return NULL;
2285 asdl_seq_SET(elts, i / 2, expression);
2286 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002287 return Set(elts, LINENO(n), n->n_col_offset,
2288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002289}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002290
2291static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292ast_for_atom(struct compiling *c, const node *n)
2293{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002294 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2295 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002296 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 */
2298 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002301 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002302 PyObject *name;
2303 const char *s = STR(ch);
2304 size_t len = strlen(s);
2305 if (len >= 4 && len <= 5) {
2306 if (!strcmp(s, "None"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002307 return Constant(Py_None, LINENO(n), n->n_col_offset,
2308 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002309 if (!strcmp(s, "True"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002310 return Constant(Py_True, LINENO(n), n->n_col_offset,
2311 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002312 if (!strcmp(s, "False"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002313 return Constant(Py_False, LINENO(n), n->n_col_offset,
2314 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002315 }
2316 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002317 if (!name)
2318 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002319 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002320 return Name(name, Load, LINENO(n), n->n_col_offset,
2321 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002322 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002324 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002325 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002326 const char *errtype = NULL;
2327 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2328 errtype = "unicode error";
2329 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2330 errtype = "value error";
2331 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002332 PyObject *type, *value, *tback, *errstr;
2333 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002334 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002335 if (errstr) {
2336 ast_error(c, n, "(%s) %U", errtype, errstr);
2337 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002338 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002339 else {
2340 PyErr_Clear();
2341 ast_error(c, n, "(%s) unknown error", errtype);
2342 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002343 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002344 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002345 Py_XDECREF(tback);
2346 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002347 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002348 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002349 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 }
2351 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002352 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002353 if (!pynum)
2354 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002355
Victor Stinner43d81952013-07-17 00:57:58 +02002356 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2357 Py_DECREF(pynum);
2358 return NULL;
2359 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002360 return Constant(pynum, LINENO(n), n->n_col_offset,
2361 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002362 }
Georg Brandldde00282007-03-18 19:01:53 +00002363 case ELLIPSIS: /* Ellipsis */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002364 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset,
2365 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002367 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368
Thomas Wouters89f507f2006-12-13 04:49:30 +00002369 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002370 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2371 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372
Thomas Wouters89f507f2006-12-13 04:49:30 +00002373 if (TYPE(ch) == yield_expr)
2374 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002377 if (NCH(ch) == 1) {
2378 return ast_for_testlist(c, ch);
2379 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002380
Serhiy Storchakab619b092018-11-27 09:40:29 +02002381 if (TYPE(CHILD(ch, 1)) == comp_for) {
2382 return copy_location(ast_for_genexp(c, ch), n);
2383 }
2384 else {
2385 return copy_location(ast_for_testlist(c, ch), n);
2386 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002388 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389
Thomas Wouters89f507f2006-12-13 04:49:30 +00002390 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002391 return List(NULL, Load, LINENO(n), n->n_col_offset,
2392 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393
Nick Coghlan650f0d02007-04-15 12:05:43 +00002394 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002395 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2396 asdl_seq *elts = seq_for_testlist(c, ch);
2397 if (!elts)
2398 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002399
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002400 return List(elts, Load, LINENO(n), n->n_col_offset,
2401 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002402 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002403 else {
2404 return copy_location(ast_for_listcomp(c, ch), n);
2405 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002407 /* dictorsetmaker: ( ((test ':' test | '**' test)
2408 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2409 * ((test | '*' test)
2410 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002411 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002412 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002413 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002414 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002415 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2416 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002417 }
2418 else {
2419 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2420 if (NCH(ch) == 1 ||
2421 (NCH(ch) > 1 &&
2422 TYPE(CHILD(ch, 1)) == COMMA)) {
2423 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002424 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002425 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002426 else if (NCH(ch) > 1 &&
2427 TYPE(CHILD(ch, 1)) == comp_for) {
2428 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002429 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002430 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002431 else if (NCH(ch) > 3 - is_dict &&
2432 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2433 /* It's a dictionary comprehension. */
2434 if (is_dict) {
2435 ast_error(c, n, "dict unpacking cannot be used in "
Guido van Rossumdcfcd142019-01-31 03:40:27 -08002436 "dict comprehension");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002437 return NULL;
2438 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002439 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002440 }
2441 else {
2442 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002443 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002444 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002445 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002446 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002449 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2450 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 }
2452}
2453
2454static slice_ty
2455ast_for_slice(struct compiling *c, const node *n)
2456{
2457 node *ch;
2458 expr_ty lower = NULL, upper = NULL, step = NULL;
2459
2460 REQ(n, subscript);
2461
2462 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002463 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 sliceop: ':' [test]
2465 */
2466 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 if (NCH(n) == 1 && TYPE(ch) == test) {
2468 /* 'step' variable hold no significance in terms of being used over
2469 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 if (!step)
2472 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473
Thomas Wouters89f507f2006-12-13 04:49:30 +00002474 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 }
2476
2477 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002478 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 if (!lower)
2480 return NULL;
2481 }
2482
2483 /* If there's an upper bound it's in the second or third position. */
2484 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002485 if (NCH(n) > 1) {
2486 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002487
Thomas Wouters89f507f2006-12-13 04:49:30 +00002488 if (TYPE(n2) == test) {
2489 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 if (!upper)
2491 return NULL;
2492 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002493 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002495 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496
Thomas Wouters89f507f2006-12-13 04:49:30 +00002497 if (TYPE(n2) == test) {
2498 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 if (!upper)
2500 return NULL;
2501 }
2502 }
2503
2504 ch = CHILD(n, NCH(n) - 1);
2505 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002506 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002507 ch = CHILD(ch, 1);
2508 if (TYPE(ch) == test) {
2509 step = ast_for_expr(c, ch);
2510 if (!step)
2511 return NULL;
2512 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 }
2514 }
2515
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002516 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517}
2518
2519static expr_ty
2520ast_for_binop(struct compiling *c, const node *n)
2521{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002522 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002524 BinOp(BinOp(A, op, B), op, C).
2525 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526
Guido van Rossumd8faa362007-04-27 19:54:29 +00002527 int i, nops;
2528 expr_ty expr1, expr2, result;
2529 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530
Guido van Rossumd8faa362007-04-27 19:54:29 +00002531 expr1 = ast_for_expr(c, CHILD(n, 0));
2532 if (!expr1)
2533 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534
Guido van Rossumd8faa362007-04-27 19:54:29 +00002535 expr2 = ast_for_expr(c, CHILD(n, 2));
2536 if (!expr2)
2537 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538
Guido van Rossumd8faa362007-04-27 19:54:29 +00002539 newoperator = get_operator(CHILD(n, 1));
2540 if (!newoperator)
2541 return NULL;
2542
2543 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002544 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002545 c->c_arena);
2546 if (!result)
2547 return NULL;
2548
2549 nops = (NCH(n) - 1) / 2;
2550 for (i = 1; i < nops; i++) {
2551 expr_ty tmp_result, tmp;
2552 const node* next_oper = CHILD(n, i * 2 + 1);
2553
2554 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002555 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 return NULL;
2557
Guido van Rossumd8faa362007-04-27 19:54:29 +00002558 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2559 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 return NULL;
2561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002563 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002564 CHILD(n, i * 2 + 2)->n_end_lineno,
2565 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002566 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002568 return NULL;
2569 result = tmp_result;
2570 }
2571 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572}
2573
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002574static expr_ty
2575ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002578 subscriptlist: subscript (',' subscript)* [',']
2579 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2580 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002581 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002582 REQ(n, trailer);
2583 if (TYPE(CHILD(n, 0)) == LPAR) {
2584 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002585 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2586 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002587 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002588 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002589 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002590 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002591 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2592 if (!attr_id)
2593 return NULL;
2594 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002595 LINENO(n), n->n_col_offset,
2596 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002597 }
2598 else {
2599 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002600 REQ(CHILD(n, 2), RSQB);
2601 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002602 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002603 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2604 if (!slc)
2605 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002606 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002607 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002608 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002609 }
2610 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002612 by treating the sequence as a tuple literal if there are
2613 no slice features.
2614 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002615 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002616 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002617 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002618 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002619 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002620 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002621 if (!slices)
2622 return NULL;
2623 for (j = 0; j < NCH(n); j += 2) {
2624 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002625 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002626 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002627 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002628 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002629 asdl_seq_SET(slices, j / 2, slc);
2630 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002631 if (!simple) {
2632 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002633 Load, LINENO(n), n->n_col_offset,
2634 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002635 }
2636 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002637 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002638 if (!elts)
2639 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002640 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2641 slc = (slice_ty)asdl_seq_GET(slices, j);
2642 assert(slc->kind == Index_kind && slc->v.Index.value);
2643 asdl_seq_SET(elts, j, slc->v.Index.value);
2644 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002645 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2646 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002647 if (!e)
2648 return NULL;
2649 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002650 Load, LINENO(n), n->n_col_offset,
2651 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002652 }
2653 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002654}
2655
2656static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002657ast_for_factor(struct compiling *c, const node *n)
2658{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002659 expr_ty expression;
2660
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002661 expression = ast_for_expr(c, CHILD(n, 1));
2662 if (!expression)
2663 return NULL;
2664
2665 switch (TYPE(CHILD(n, 0))) {
2666 case PLUS:
2667 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002668 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002669 c->c_arena);
2670 case MINUS:
2671 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002672 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002673 c->c_arena);
2674 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002675 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2676 n->n_end_lineno, n->n_end_col_offset,
2677 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002678 }
2679 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2680 TYPE(CHILD(n, 0)));
2681 return NULL;
2682}
2683
2684static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002685ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002686{
Yury Selivanov75445082015-05-11 22:57:16 -04002687 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002688 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002689
2690 REQ(n, atom_expr);
2691 nch = NCH(n);
2692
Jelle Zijlstraac317702017-10-05 20:24:46 -07002693 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002694 start = 1;
2695 assert(nch > 1);
2696 }
2697
2698 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002699 if (!e)
2700 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002701 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002702 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002703 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002704 return Await(e, LINENO(n), n->n_col_offset,
2705 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002706 }
2707
2708 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002709 node *ch = CHILD(n, i);
2710 if (TYPE(ch) != trailer)
2711 break;
2712 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002713 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002714 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002715 tmp->lineno = e->lineno;
2716 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002717 e = tmp;
2718 }
Yury Selivanov75445082015-05-11 22:57:16 -04002719
2720 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002721 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002722 return Await(e, LINENO(n), n->n_col_offset,
2723 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002724 }
2725 else {
2726 return e;
2727 }
2728}
2729
2730static expr_ty
2731ast_for_power(struct compiling *c, const node *n)
2732{
2733 /* power: atom trailer* ('**' factor)*
2734 */
2735 expr_ty e;
2736 REQ(n, power);
2737 e = ast_for_atom_expr(c, CHILD(n, 0));
2738 if (!e)
2739 return NULL;
2740 if (NCH(n) == 1)
2741 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002742 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2743 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002744 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002745 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002746 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2747 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002748 }
2749 return e;
2750}
2751
Guido van Rossum0368b722007-05-11 16:50:42 +00002752static expr_ty
2753ast_for_starred(struct compiling *c, const node *n)
2754{
2755 expr_ty tmp;
2756 REQ(n, star_expr);
2757
2758 tmp = ast_for_expr(c, CHILD(n, 1));
2759 if (!tmp)
2760 return NULL;
2761
2762 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002763 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2764 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002765}
2766
2767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768/* Do not name a variable 'expr'! Will cause a compile error.
2769*/
2770
2771static expr_ty
2772ast_for_expr(struct compiling *c, const node *n)
2773{
2774 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002775 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002776 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002777 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 and_test: not_test ('and' not_test)*
2780 not_test: 'not' not_test | comparison
2781 comparison: expr (comp_op expr)*
2782 expr: xor_expr ('|' xor_expr)*
2783 xor_expr: and_expr ('^' and_expr)*
2784 and_expr: shift_expr ('&' shift_expr)*
2785 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2786 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002787 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002789 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002790 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002791 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 */
2793
2794 asdl_seq *seq;
2795 int i;
2796
2797 loop:
2798 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002799 case namedexpr_test:
2800 if (NCH(n) == 3)
2801 return ast_for_namedexpr(c, n);
2802 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002804 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002805 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002806 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002808 else if (NCH(n) > 1)
2809 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002810 /* Fallthrough */
2811 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 case and_test:
2813 if (NCH(n) == 1) {
2814 n = CHILD(n, 0);
2815 goto loop;
2816 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002817 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 if (!seq)
2819 return NULL;
2820 for (i = 0; i < NCH(n); i += 2) {
2821 expr_ty e = ast_for_expr(c, CHILD(n, i));
2822 if (!e)
2823 return NULL;
2824 asdl_seq_SET(seq, i / 2, e);
2825 }
2826 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002827 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002828 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002829 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002830 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002831 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2832 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 case not_test:
2834 if (NCH(n) == 1) {
2835 n = CHILD(n, 0);
2836 goto loop;
2837 }
2838 else {
2839 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2840 if (!expression)
2841 return NULL;
2842
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002843 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002844 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002845 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846 }
2847 case comparison:
2848 if (NCH(n) == 1) {
2849 n = CHILD(n, 0);
2850 goto loop;
2851 }
2852 else {
2853 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002854 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002855 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002856 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 if (!ops)
2858 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002859 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 return NULL;
2862 }
2863 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002864 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002866 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002867 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002869 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870
2871 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002872 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002874 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002876 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 asdl_seq_SET(cmps, i / 2, expression);
2878 }
2879 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002880 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002882 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002884 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2885 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 }
2887 break;
2888
Guido van Rossum0368b722007-05-11 16:50:42 +00002889 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 /* The next five cases all handle BinOps. The main body of code
2892 is the same in each case, but the switch turned inside out to
2893 reuse the code for each type of operator.
2894 */
2895 case expr:
2896 case xor_expr:
2897 case and_expr:
2898 case shift_expr:
2899 case arith_expr:
2900 case term:
2901 if (NCH(n) == 1) {
2902 n = CHILD(n, 0);
2903 goto loop;
2904 }
2905 return ast_for_binop(c, n);
2906 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002907 node *an = NULL;
2908 node *en = NULL;
2909 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002910 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002911 if (NCH(n) > 1)
2912 an = CHILD(n, 1); /* yield_arg */
2913 if (an) {
2914 en = CHILD(an, NCH(an) - 1);
2915 if (NCH(an) == 2) {
2916 is_from = 1;
2917 exp = ast_for_expr(c, en);
2918 }
2919 else
2920 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002921 if (!exp)
2922 return NULL;
2923 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002924 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002925 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2926 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2927 return Yield(exp, LINENO(n), n->n_col_offset,
2928 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002929 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002930 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 if (NCH(n) == 1) {
2932 n = CHILD(n, 0);
2933 goto loop;
2934 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002935 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002936 case power:
2937 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002939 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 return NULL;
2941 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002942 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 return NULL;
2944}
2945
2946static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002947ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002948 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949{
2950 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002951 arglist: argument (',' argument)* [',']
2952 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 */
2954
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002955 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002956 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002957 asdl_seq *args;
2958 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959
2960 REQ(n, arglist);
2961
2962 nargs = 0;
2963 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002965 node *ch = CHILD(n, i);
2966 if (TYPE(ch) == argument) {
2967 if (NCH(ch) == 1)
2968 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002969 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2970 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002971 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002972 ast_error(c, ch, "invalid syntax");
2973 return NULL;
2974 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002975 if (NCH(n) > 1) {
2976 ast_error(c, ch, "Generator expression must be parenthesized");
2977 return NULL;
2978 }
2979 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002980 else if (TYPE(CHILD(ch, 0)) == STAR)
2981 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002982 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2983 nargs++;
2984 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002986 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002987 nkeywords++;
2988 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002991 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002993 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002994 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002996 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002997
2998 nargs = 0; /* positional arguments + iterable argument unpackings */
2999 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
3000 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003002 node *ch = CHILD(n, i);
3003 if (TYPE(ch) == argument) {
3004 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003005 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003006 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003007 /* a positional argument */
3008 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003009 if (ndoublestars) {
3010 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003011 "positional argument follows "
3012 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003013 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003014 else {
3015 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003016 "positional argument follows "
3017 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003018 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003019 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00003020 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003021 e = ast_for_expr(c, chch);
3022 if (!e)
3023 return NULL;
3024 asdl_seq_SET(args, nargs++, e);
3025 }
3026 else if (TYPE(chch) == STAR) {
3027 /* an iterable argument unpacking */
3028 expr_ty starred;
3029 if (ndoublestars) {
3030 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003031 "iterable argument unpacking follows "
3032 "keyword argument unpacking");
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003033 return NULL;
3034 }
3035 e = ast_for_expr(c, CHILD(ch, 1));
3036 if (!e)
3037 return NULL;
3038 starred = Starred(e, Load, LINENO(chch),
3039 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003040 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04003041 c->c_arena);
3042 if (!starred)
3043 return NULL;
3044 asdl_seq_SET(args, nargs++, starred);
3045
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003046 }
3047 else if (TYPE(chch) == DOUBLESTAR) {
3048 /* a keyword argument unpacking */
3049 keyword_ty kw;
3050 i++;
3051 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003053 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003054 kw = keyword(NULL, e, c->c_arena);
3055 asdl_seq_SET(keywords, nkeywords++, kw);
3056 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003058 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003059 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02003060 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003062 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003063 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003065 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
3066 /* treat colon equal as positional argument */
3067 if (nkeywords) {
3068 if (ndoublestars) {
3069 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003070 "positional argument follows "
3071 "keyword argument unpacking");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003072 }
3073 else {
3074 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003075 "positional argument follows "
3076 "keyword argument");
Emily Morehouse8f59ee02019-01-24 16:49:56 -07003077 }
3078 return NULL;
3079 }
3080 e = ast_for_namedexpr(c, ch);
3081 if (!e)
3082 return NULL;
3083 asdl_seq_SET(args, nargs++, e);
3084 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003085 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003086 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003087 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003088 identifier key, tmp;
3089 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003091 // To remain LL(1), the grammar accepts any test (basically, any
3092 // expression) in the keyword slot of a call site. So, we need
3093 // to manually enforce that the keyword is a NAME here.
3094 static const int name_tree[] = {
3095 test,
3096 or_test,
3097 and_test,
3098 not_test,
3099 comparison,
3100 expr,
3101 xor_expr,
3102 and_expr,
3103 shift_expr,
3104 arith_expr,
3105 term,
3106 factor,
3107 power,
3108 atom_expr,
3109 atom,
3110 0,
3111 };
3112 node *expr_node = chch;
3113 for (int i = 0; name_tree[i]; i++) {
3114 if (TYPE(expr_node) != name_tree[i])
3115 break;
3116 if (NCH(expr_node) != 1)
3117 break;
3118 expr_node = CHILD(expr_node, 0);
3119 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003120 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003121 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02003122 "expression cannot contain assignment, "
3123 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003124 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003125 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003126 key = new_identifier(STR(expr_node), c);
3127 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05003128 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07003130 if (forbidden_name(c, key, chch, 1)) {
3131 return NULL;
3132 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003133 for (k = 0; k < nkeywords; k++) {
3134 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003135 if (tmp && !PyUnicode_Compare(tmp, key)) {
3136 ast_error(c, chch,
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003137 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003138 return NULL;
3139 }
3140 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003141 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003143 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003144 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003146 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003147 asdl_seq_SET(keywords, nkeywords++, kw);
3148 }
3149 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 }
3151
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003152 return Call(func, args, keywords, func->lineno, func->col_offset,
3153 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154}
3155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003157ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003159 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003160 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003162 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003163 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003164 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003165 }
3166 else {
3167 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003168 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003169 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003171 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 else {
3173 asdl_seq *tmp = seq_for_testlist(c, n);
3174 if (!tmp)
3175 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003176 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3177 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003179}
3180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181static stmt_ty
3182ast_for_expr_stmt(struct compiling *c, const node *n)
3183{
3184 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003185 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003186 [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
3187 annassign: ':' test ['=' (yield_expr|testlist)]
3188 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
3189 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
3190 '<<=' | '>>=' | '**=' | '//=')
Martin Panter69332c12016-08-04 13:07:31 +00003191 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003193 int num = NCH(n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003195 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003196 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 if (!e)
3198 return NULL;
3199
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003200 return Expr(e, LINENO(n), n->n_col_offset,
3201 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 }
3203 else if (TYPE(CHILD(n, 1)) == augassign) {
3204 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003205 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003206 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207
Thomas Wouters89f507f2006-12-13 04:49:30 +00003208 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 if (!expr1)
3210 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003211 if(!set_context(c, expr1, Store, ch))
3212 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003213 /* set_context checks that most expressions are not the left side.
3214 Augmented assignments can only have a name, a subscript, or an
3215 attribute on the left, though, so we have to explicitly check for
3216 those. */
3217 switch (expr1->kind) {
3218 case Name_kind:
3219 case Attribute_kind:
3220 case Subscript_kind:
3221 break;
3222 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003223 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003224 return NULL;
3225 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226
Thomas Wouters89f507f2006-12-13 04:49:30 +00003227 ch = CHILD(n, 2);
3228 if (TYPE(ch) == testlist)
3229 expr2 = ast_for_testlist(c, ch);
3230 else
3231 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003232 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 return NULL;
3234
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003235 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003236 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 return NULL;
3238
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003239 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3240 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003242 else if (TYPE(CHILD(n, 1)) == annassign) {
3243 expr_ty expr1, expr2, expr3;
3244 node *ch = CHILD(n, 0);
3245 node *deep, *ann = CHILD(n, 1);
3246 int simple = 1;
3247
3248 /* we keep track of parens to qualify (x) as expression not name */
3249 deep = ch;
3250 while (NCH(deep) == 1) {
3251 deep = CHILD(deep, 0);
3252 }
3253 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3254 simple = 0;
3255 }
3256 expr1 = ast_for_testlist(c, ch);
3257 if (!expr1) {
3258 return NULL;
3259 }
3260 switch (expr1->kind) {
3261 case Name_kind:
3262 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3263 return NULL;
3264 }
3265 expr1->v.Name.ctx = Store;
3266 break;
3267 case Attribute_kind:
3268 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3269 return NULL;
3270 }
3271 expr1->v.Attribute.ctx = Store;
3272 break;
3273 case Subscript_kind:
3274 expr1->v.Subscript.ctx = Store;
3275 break;
3276 case List_kind:
3277 ast_error(c, ch,
3278 "only single target (not list) can be annotated");
3279 return NULL;
3280 case Tuple_kind:
3281 ast_error(c, ch,
3282 "only single target (not tuple) can be annotated");
3283 return NULL;
3284 default:
3285 ast_error(c, ch,
3286 "illegal target for annotation");
3287 return NULL;
3288 }
3289
3290 if (expr1->kind != Name_kind) {
3291 simple = 0;
3292 }
3293 ch = CHILD(ann, 1);
3294 expr2 = ast_for_expr(c, ch);
3295 if (!expr2) {
3296 return NULL;
3297 }
3298 if (NCH(ann) == 2) {
3299 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003300 LINENO(n), n->n_col_offset,
3301 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003302 }
3303 else {
3304 ch = CHILD(ann, 3);
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003305 if (TYPE(ch) == testlist) {
3306 expr3 = ast_for_testlist(c, ch);
3307 }
3308 else {
3309 expr3 = ast_for_expr(c, ch);
3310 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003311 if (!expr3) {
3312 return NULL;
3313 }
3314 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003315 LINENO(n), n->n_col_offset,
3316 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003317 }
3318 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003320 int i, nch_minus_type, has_type_comment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 asdl_seq *targets;
3322 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323 expr_ty expression;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003324 string type_comment;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325
Thomas Wouters89f507f2006-12-13 04:49:30 +00003326 /* a normal assignment */
3327 REQ(CHILD(n, 1), EQUAL);
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003328
3329 has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT;
3330 nch_minus_type = num - has_type_comment;
3331
3332 targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 if (!targets)
3334 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003335 for (i = 0; i < nch_minus_type - 2; i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003336 expr_ty e;
3337 node *ch = CHILD(n, i);
3338 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003339 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 return NULL;
3341 }
3342 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003344 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003346 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003347 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003348 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349
Thomas Wouters89f507f2006-12-13 04:49:30 +00003350 asdl_seq_SET(targets, i / 2, e);
3351 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003352 value = CHILD(n, nch_minus_type - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003353 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003354 expression = ast_for_testlist(c, value);
3355 else
3356 expression = ast_for_expr(c, value);
3357 if (!expression)
3358 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003359 if (has_type_comment) {
3360 type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type));
3361 if (!type_comment)
3362 return NULL;
3363 }
3364 else
3365 type_comment = NULL;
3366 return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003367 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369}
3370
Benjamin Peterson78565b22009-06-28 19:19:51 +00003371
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003373ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374{
3375 asdl_seq *seq;
3376 int i;
3377 expr_ty e;
3378
3379 REQ(n, exprlist);
3380
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003381 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003383 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003385 e = ast_for_expr(c, CHILD(n, i));
3386 if (!e)
3387 return NULL;
3388 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003389 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003390 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391 }
3392 return seq;
3393}
3394
3395static stmt_ty
3396ast_for_del_stmt(struct compiling *c, const node *n)
3397{
3398 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 /* del_stmt: 'del' exprlist */
3401 REQ(n, del_stmt);
3402
3403 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3404 if (!expr_list)
3405 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003406 return Delete(expr_list, LINENO(n), n->n_col_offset,
3407 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408}
3409
3410static stmt_ty
3411ast_for_flow_stmt(struct compiling *c, const node *n)
3412{
3413 /*
3414 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3415 | yield_stmt
3416 break_stmt: 'break'
3417 continue_stmt: 'continue'
3418 return_stmt: 'return' [testlist]
3419 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003420 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 raise_stmt: 'raise' [test [',' test [',' test]]]
3422 */
3423 node *ch;
3424
3425 REQ(n, flow_stmt);
3426 ch = CHILD(n, 0);
3427 switch (TYPE(ch)) {
3428 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003429 return Break(LINENO(n), n->n_col_offset,
3430 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003431 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003432 return Continue(LINENO(n), n->n_col_offset,
3433 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003435 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3436 if (!exp)
3437 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003438 return Expr(exp, LINENO(n), n->n_col_offset,
3439 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 }
3441 case return_stmt:
3442 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003443 return Return(NULL, 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 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003446 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447 if (!expression)
3448 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003449 return Return(expression, LINENO(n), n->n_col_offset,
3450 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451 }
3452 case raise_stmt:
3453 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003454 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3455 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003456 else if (NCH(ch) >= 2) {
3457 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3459 if (!expression)
3460 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003461 if (NCH(ch) == 4) {
3462 cause = ast_for_expr(c, CHILD(ch, 3));
3463 if (!cause)
3464 return NULL;
3465 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003466 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3467 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468 }
Stefan Krahf432a322017-08-21 13:09:59 +02003469 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003471 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472 "unexpected flow_stmt: %d", TYPE(ch));
3473 return NULL;
3474 }
3475}
3476
3477static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003478alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479{
3480 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003481 import_as_name: NAME ['as' NAME]
3482 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483 dotted_name: NAME ('.' NAME)*
3484 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003485 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003486
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 loop:
3488 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003489 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003490 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003491 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003492 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003493 if (!name)
3494 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003495 if (NCH(n) == 3) {
3496 node *str_node = CHILD(n, 2);
3497 str = NEW_IDENTIFIER(str_node);
3498 if (!str)
3499 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003500 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003501 return NULL;
3502 }
3503 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003504 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003505 return NULL;
3506 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003507 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 case dotted_as_name:
3510 if (NCH(n) == 1) {
3511 n = CHILD(n, 0);
3512 goto loop;
3513 }
3514 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003515 node *asname_node = CHILD(n, 2);
3516 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003517 if (!a)
3518 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003520 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003521 if (!a->asname)
3522 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003523 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003524 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 return a;
3526 }
3527 break;
3528 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003529 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003530 node *name_node = CHILD(n, 0);
3531 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003532 if (!name)
3533 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003534 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003535 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003536 return alias(name, NULL, c->c_arena);
3537 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 else {
3539 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003540 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003541 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003544
3545 len = 0;
3546 for (i = 0; i < NCH(n); i += 2)
3547 /* length of string plus one for the dot */
3548 len += strlen(STR(CHILD(n, i))) + 1;
3549 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003550 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 if (!str)
3552 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003553 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554 if (!s)
3555 return NULL;
3556 for (i = 0; i < NCH(n); i += 2) {
3557 char *sch = STR(CHILD(n, i));
3558 strcpy(s, STR(CHILD(n, i)));
3559 s += strlen(sch);
3560 *s++ = '.';
3561 }
3562 --s;
3563 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3565 PyBytes_GET_SIZE(str),
3566 NULL);
3567 Py_DECREF(str);
3568 if (!uni)
3569 return NULL;
3570 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003571 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003572 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3573 Py_DECREF(str);
3574 return NULL;
3575 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003576 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 }
3578 break;
3579 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003580 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003581 if (!str)
3582 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003583 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3584 Py_DECREF(str);
3585 return NULL;
3586 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003587 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003589 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 "unexpected import name: %d", TYPE(n));
3591 return NULL;
3592 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003593
3594 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003595 return NULL;
3596}
3597
3598static stmt_ty
3599ast_for_import_stmt(struct compiling *c, const node *n)
3600{
3601 /*
3602 import_stmt: import_name | import_from
3603 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003604 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3605 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003607 int lineno;
3608 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 int i;
3610 asdl_seq *aliases;
3611
3612 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003613 lineno = LINENO(n);
3614 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003616 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003618 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003619 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003620 if (!aliases)
3621 return NULL;
3622 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003623 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003624 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003626 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003628 // Even though n is modified above, the end position is not changed
3629 return Import(aliases, lineno, col_offset,
3630 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003632 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003634 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003635 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003636 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003637 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003639 /* Count the number of dots (for relative imports) and check for the
3640 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003641 for (idx = 1; idx < NCH(n); idx++) {
3642 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003643 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3644 if (!mod)
3645 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003646 idx++;
3647 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003648 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003650 ndots += 3;
3651 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003652 } else if (TYPE(CHILD(n, idx)) != DOT) {
3653 break;
3654 }
3655 ndots++;
3656 }
3657 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003658 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003659 case STAR:
3660 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003661 n = CHILD(n, idx);
3662 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003663 break;
3664 case LPAR:
3665 /* from ... import (x, y, z) */
3666 n = CHILD(n, idx + 1);
3667 n_children = NCH(n);
3668 break;
3669 case import_as_names:
3670 /* from ... import x, y, z */
3671 n = CHILD(n, idx);
3672 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003673 if (n_children % 2 == 0) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003674 ast_error(c, n,
3675 "trailing comma not allowed without"
3676 " surrounding parentheses");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677 return NULL;
3678 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003679 break;
3680 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003681 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003682 return NULL;
3683 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003685 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003686 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688
3689 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003690 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003691 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003692 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003694 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003696 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003697 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003698 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003699 if (!import_alias)
3700 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003701 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003702 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003704 if (mod != NULL)
3705 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003706 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003707 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003708 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 }
Neal Norwitz79792652005-11-14 04:25:03 +00003710 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 "unknown import statement: starts with command '%s'",
3712 STR(CHILD(n, 0)));
3713 return NULL;
3714}
3715
3716static stmt_ty
3717ast_for_global_stmt(struct compiling *c, const node *n)
3718{
3719 /* global_stmt: 'global' NAME (',' NAME)* */
3720 identifier name;
3721 asdl_seq *s;
3722 int i;
3723
3724 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003725 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003727 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003729 name = NEW_IDENTIFIER(CHILD(n, i));
3730 if (!name)
3731 return NULL;
3732 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003734 return Global(s, LINENO(n), n->n_col_offset,
3735 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736}
3737
3738static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003739ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3740{
3741 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3742 identifier name;
3743 asdl_seq *s;
3744 int i;
3745
3746 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003747 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003748 if (!s)
3749 return NULL;
3750 for (i = 1; i < NCH(n); i += 2) {
3751 name = NEW_IDENTIFIER(CHILD(n, i));
3752 if (!name)
3753 return NULL;
3754 asdl_seq_SET(s, i / 2, name);
3755 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003756 return Nonlocal(s, LINENO(n), n->n_col_offset,
3757 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003758}
3759
3760static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761ast_for_assert_stmt(struct compiling *c, const node *n)
3762{
3763 /* assert_stmt: 'assert' test [',' test] */
3764 REQ(n, assert_stmt);
3765 if (NCH(n) == 2) {
3766 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3767 if (!expression)
3768 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003769 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3770 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 }
3772 else if (NCH(n) == 4) {
3773 expr_ty expr1, expr2;
3774
3775 expr1 = ast_for_expr(c, CHILD(n, 1));
3776 if (!expr1)
3777 return NULL;
3778 expr2 = ast_for_expr(c, CHILD(n, 3));
3779 if (!expr2)
3780 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003782 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3783 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 }
Neal Norwitz79792652005-11-14 04:25:03 +00003785 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 "improper number of parts to 'assert' statement: %d",
3787 NCH(n));
3788 return NULL;
3789}
3790
3791static asdl_seq *
3792ast_for_suite(struct compiling *c, const node *n)
3793{
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003794 /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003795 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 stmt_ty s;
3797 int i, total, num, end, pos = 0;
3798 node *ch;
3799
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003800 if (TYPE(n) != func_body_suite) {
3801 REQ(n, suite);
3802 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803
3804 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003805 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003809 n = CHILD(n, 0);
3810 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003812 */
3813 end = NCH(n) - 1;
3814 if (TYPE(CHILD(n, end - 1)) == SEMI)
3815 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003817 for (i = 0; i < end; i += 2) {
3818 ch = CHILD(n, i);
3819 s = ast_for_stmt(c, ch);
3820 if (!s)
3821 return NULL;
3822 asdl_seq_SET(seq, pos++, s);
3823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 }
3825 else {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08003826 i = 2;
3827 if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) {
3828 i += 2;
3829 REQ(CHILD(n, 2), NEWLINE);
3830 }
3831
3832 for (; i < (NCH(n) - 1); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003833 ch = CHILD(n, i);
3834 REQ(ch, stmt);
3835 num = num_stmts(ch);
3836 if (num == 1) {
3837 /* small_stmt or compound_stmt with only one child */
3838 s = ast_for_stmt(c, ch);
3839 if (!s)
3840 return NULL;
3841 asdl_seq_SET(seq, pos++, s);
3842 }
3843 else {
3844 int j;
3845 ch = CHILD(ch, 0);
3846 REQ(ch, simple_stmt);
3847 for (j = 0; j < NCH(ch); j += 2) {
3848 /* statement terminates with a semi-colon ';' */
3849 if (NCH(CHILD(ch, j)) == 0) {
3850 assert((j + 1) == NCH(ch));
3851 break;
3852 }
3853 s = ast_for_stmt(c, CHILD(ch, j));
3854 if (!s)
3855 return NULL;
3856 asdl_seq_SET(seq, pos++, s);
3857 }
3858 }
3859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 }
3861 assert(pos == seq->size);
3862 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863}
3864
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003865static void
3866get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3867{
3868 int tot = asdl_seq_LEN(s);
3869 // Suite should not be empty, but it is safe to just ignore it
3870 // if it will ever occur.
3871 if (!tot) {
3872 return;
3873 }
3874 stmt_ty last = asdl_seq_GET(s, tot - 1);
3875 *end_lineno = last->end_lineno;
3876 *end_col_offset = last->end_col_offset;
3877}
3878
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003879static stmt_ty
3880ast_for_if_stmt(struct compiling *c, const node *n)
3881{
3882 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3883 ['else' ':' suite]
3884 */
3885 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003886 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887
3888 REQ(n, if_stmt);
3889
3890 if (NCH(n) == 4) {
3891 expr_ty expression;
3892 asdl_seq *suite_seq;
3893
3894 expression = ast_for_expr(c, CHILD(n, 1));
3895 if (!expression)
3896 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003898 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003900 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901
Guido van Rossumd8faa362007-04-27 19:54:29 +00003902 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003903 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003904 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003905
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 s = STR(CHILD(n, 4));
3907 /* s[2], the third character in the string, will be
3908 's' for el_s_e, or
3909 'i' for el_i_f
3910 */
3911 if (s[2] == 's') {
3912 expr_ty expression;
3913 asdl_seq *seq1, *seq2;
3914
3915 expression = ast_for_expr(c, CHILD(n, 1));
3916 if (!expression)
3917 return NULL;
3918 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003919 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920 return NULL;
3921 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003922 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003924 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925
Guido van Rossumd8faa362007-04-27 19:54:29 +00003926 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003927 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 }
3929 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003930 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003931 expr_ty expression;
3932 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003933 asdl_seq *orelse = NULL;
3934 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935 /* must reference the child n_elif+1 since 'else' token is third,
3936 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003937 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3938 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3939 has_else = 1;
3940 n_elif -= 3;
3941 }
3942 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943
Thomas Wouters89f507f2006-12-13 04:49:30 +00003944 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003945 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003947 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003948 if (!orelse)
3949 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003951 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003953 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3954 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003956 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3957 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003959 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 asdl_seq_SET(orelse, 0,
3962 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003963 LINENO(CHILD(n, NCH(n) - 6)),
3964 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003965 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003966 /* the just-created orelse handled the last elif */
3967 n_elif--;
3968 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969
Thomas Wouters89f507f2006-12-13 04:49:30 +00003970 for (i = 0; i < n_elif; i++) {
3971 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003972 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003973 if (!newobj)
3974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003976 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003979 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003982 if (orelse != NULL) {
3983 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3984 } else {
3985 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3986 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003987 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003989 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003990 CHILD(n, off)->n_col_offset,
3991 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003992 orelse = newobj;
3993 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003994 expression = ast_for_expr(c, CHILD(n, 1));
3995 if (!expression)
3996 return NULL;
3997 suite_seq = ast_for_suite(c, CHILD(n, 3));
3998 if (!suite_seq)
3999 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004000 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004001 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004002 LINENO(n), n->n_col_offset,
4003 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004005
4006 PyErr_Format(PyExc_SystemError,
4007 "unexpected token in 'if' statement: %s", s);
4008 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004009}
4010
4011static stmt_ty
4012ast_for_while_stmt(struct compiling *c, const node *n)
4013{
4014 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
4015 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004016 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017
4018 if (NCH(n) == 4) {
4019 expr_ty expression;
4020 asdl_seq *suite_seq;
4021
4022 expression = ast_for_expr(c, CHILD(n, 1));
4023 if (!expression)
4024 return NULL;
4025 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004026 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004028 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4029 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
4030 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 }
4032 else if (NCH(n) == 7) {
4033 expr_ty expression;
4034 asdl_seq *seq1, *seq2;
4035
4036 expression = ast_for_expr(c, CHILD(n, 1));
4037 if (!expression)
4038 return NULL;
4039 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004040 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 return NULL;
4042 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004043 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004044 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004045 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004047 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
4048 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004050
4051 PyErr_Format(PyExc_SystemError,
4052 "wrong number of tokens for 'while' statement: %d",
4053 NCH(n));
4054 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004055}
4056
4057static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004058ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059{
guoci90fc8982018-09-11 17:45:45 -04004060 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00004061 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004063 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004064 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004065 int end_lineno, end_col_offset;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004066 int has_type_comment;
4067 string type_comment;
4068 /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 REQ(n, for_stmt);
4070
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004071 has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT;
4072
4073 if (NCH(n) == 9 + has_type_comment) {
4074 seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075 if (!seq)
4076 return NULL;
4077 }
4078
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004079 node_target = CHILD(n, 1);
4080 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004081 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004083 /* Check the # of children rather than the length of _target, since
4084 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004085 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004086 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00004087 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004088 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004089 target = Tuple(_target, Store, first->lineno, first->col_offset,
4090 node_target->n_end_lineno, node_target->n_end_col_offset,
4091 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004092
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00004093 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004094 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004095 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004096 suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004097 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004098 return NULL;
4099
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004100 if (seq != NULL) {
4101 get_last_end_pos(seq, &end_lineno, &end_col_offset);
4102 } else {
4103 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
4104 }
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004105
4106 if (has_type_comment) {
4107 type_comment = NEW_TYPE_COMMENT(CHILD(n, 5));
4108 if (!type_comment)
4109 return NULL;
4110 }
4111 else
4112 type_comment = NULL;
4113
Yury Selivanov75445082015-05-11 22:57:16 -04004114 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004115 return AsyncFor(target, expression, suite_seq, seq, type_comment,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07004116 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004117 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004118 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004119 return For(target, expression, suite_seq, seq, type_comment,
Yury Selivanov75445082015-05-11 22:57:16 -04004120 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004121 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122}
4123
4124static excepthandler_ty
4125ast_for_except_clause(struct compiling *c, const node *exc, node *body)
4126{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004127 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004128 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004129 REQ(exc, except_clause);
4130 REQ(body, suite);
4131
4132 if (NCH(exc) == 1) {
4133 asdl_seq *suite_seq = ast_for_suite(c, body);
4134 if (!suite_seq)
4135 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004136 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137
Neal Norwitzad74aa82008-03-31 05:14:30 +00004138 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004139 exc->n_col_offset,
4140 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141 }
4142 else if (NCH(exc) == 2) {
4143 expr_ty expression;
4144 asdl_seq *suite_seq;
4145
4146 expression = ast_for_expr(c, CHILD(exc, 1));
4147 if (!expression)
4148 return NULL;
4149 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004150 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004152 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004153
Neal Norwitzad74aa82008-03-31 05:14:30 +00004154 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004155 exc->n_col_offset,
4156 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157 }
4158 else if (NCH(exc) == 4) {
4159 asdl_seq *suite_seq;
4160 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00004161 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004162 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004163 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004164 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004165 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004166 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004167 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004168 return NULL;
4169 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00004170 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004171 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004172 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173
Neal Norwitzad74aa82008-03-31 05:14:30 +00004174 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004175 exc->n_col_offset,
4176 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004177 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004178
4179 PyErr_Format(PyExc_SystemError,
4180 "wrong number of children for 'except' clause: %d",
4181 NCH(exc));
4182 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004183}
4184
4185static stmt_ty
4186ast_for_try_stmt(struct compiling *c, const node *n)
4187{
Neal Norwitzf599f422005-12-17 21:33:47 +00004188 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004189 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004190 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004191 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004192
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004193 REQ(n, try_stmt);
4194
Neal Norwitzf599f422005-12-17 21:33:47 +00004195 body = ast_for_suite(c, CHILD(n, 2));
4196 if (body == NULL)
4197 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004198
Neal Norwitzf599f422005-12-17 21:33:47 +00004199 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4200 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4201 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4202 /* we can assume it's an "else",
4203 because nch >= 9 for try-else-finally and
4204 it would otherwise have a type of except_clause */
4205 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4206 if (orelse == NULL)
4207 return NULL;
4208 n_except--;
4209 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004210
Neal Norwitzf599f422005-12-17 21:33:47 +00004211 finally = ast_for_suite(c, CHILD(n, nch - 1));
4212 if (finally == NULL)
4213 return NULL;
4214 n_except--;
4215 }
4216 else {
4217 /* we can assume it's an "else",
4218 otherwise it would have a type of except_clause */
4219 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4220 if (orelse == NULL)
4221 return NULL;
4222 n_except--;
4223 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004224 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004225 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004226 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004227 return NULL;
4228 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229
Neal Norwitzf599f422005-12-17 21:33:47 +00004230 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004231 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004232 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004233 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004234 if (handlers == NULL)
4235 return NULL;
4236
4237 for (i = 0; i < n_except; i++) {
4238 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4239 CHILD(n, 5 + i * 3));
4240 if (!e)
4241 return NULL;
4242 asdl_seq_SET(handlers, i, e);
4243 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004244 }
4245
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004246 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004247 if (finally != NULL) {
4248 // finally is always last
4249 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4250 } else if (orelse != NULL) {
4251 // otherwise else is last
4252 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4253 } else {
4254 // inline the get_last_end_pos logic due to layout mismatch
4255 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4256 end_lineno = last_handler->end_lineno;
4257 end_col_offset = last_handler->end_col_offset;
4258 }
4259 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4260 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004261}
4262
Georg Brandl0c315622009-05-25 21:10:36 +00004263/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004264static withitem_ty
4265ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004266{
4267 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004268
Georg Brandl0c315622009-05-25 21:10:36 +00004269 REQ(n, with_item);
4270 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004271 if (!context_expr)
4272 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004273 if (NCH(n) == 3) {
4274 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004275
4276 if (!optional_vars) {
4277 return NULL;
4278 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004279 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004280 return NULL;
4281 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004282 }
4283
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004284 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004285}
4286
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004287/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */
Georg Brandl0c315622009-05-25 21:10:36 +00004288static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004289ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004290{
guoci90fc8982018-09-11 17:45:45 -04004291 const node * const n = is_async ? CHILD(n0, 1) : n0;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004292 int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004293 asdl_seq *items, *body;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004294 string type_comment;
Georg Brandl0c315622009-05-25 21:10:36 +00004295
4296 REQ(n, with_stmt);
4297
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004298 has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT;
4299 nch_minus_type = NCH(n) - has_type_comment;
4300
4301 n_items = (nch_minus_type - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004302 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004303 if (!items)
4304 return NULL;
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004305 for (i = 1; i < nch_minus_type - 2; i += 2) {
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004306 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4307 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004308 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004309 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004310 }
4311
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004312 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4313 if (!body)
4314 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004315 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004316
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004317 if (has_type_comment) {
4318 type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2));
4319 if (!type_comment)
4320 return NULL;
4321 }
4322 else
4323 type_comment = NULL;
4324
Yury Selivanov75445082015-05-11 22:57:16 -04004325 if (is_async)
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004326 return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004327 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004328 else
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004329 return With(items, body, type_comment, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004330 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004331}
4332
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004333static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004334ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004335{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004336 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004337 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004338 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004339 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004340 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004341
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004342 REQ(n, classdef);
4343
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004344 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004345 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004346 if (!s)
4347 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004348 get_last_end_pos(s, &end_lineno, &end_col_offset);
4349
Benjamin Peterson30760062008-11-25 04:02:28 +00004350 classname = NEW_IDENTIFIER(CHILD(n, 1));
4351 if (!classname)
4352 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004353 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004354 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004355 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004356 LINENO(n), n->n_col_offset,
4357 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004358 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004359
4360 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004361 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004362 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004363 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004364 get_last_end_pos(s, &end_lineno, &end_col_offset);
4365
Benjamin Peterson30760062008-11-25 04:02:28 +00004366 classname = NEW_IDENTIFIER(CHILD(n, 1));
4367 if (!classname)
4368 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004369 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004370 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004371 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004372 LINENO(n), n->n_col_offset,
4373 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004374 }
4375
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004376 /* class NAME '(' arglist ')' ':' suite */
4377 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004378 {
4379 PyObject *dummy_name;
4380 expr_ty dummy;
4381 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4382 if (!dummy_name)
4383 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004384 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4385 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4386 c->c_arena);
4387 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004388 if (!call)
4389 return NULL;
4390 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004391 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004392 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004393 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004394 get_last_end_pos(s, &end_lineno, &end_col_offset);
4395
Benjamin Peterson30760062008-11-25 04:02:28 +00004396 classname = NEW_IDENTIFIER(CHILD(n, 1));
4397 if (!classname)
4398 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004399 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004400 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004401
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004402 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004403 decorator_seq, LINENO(n), n->n_col_offset,
4404 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004405}
4406
4407static stmt_ty
4408ast_for_stmt(struct compiling *c, const node *n)
4409{
4410 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004411 assert(NCH(n) == 1);
4412 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004413 }
4414 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004415 assert(num_stmts(n) == 1);
4416 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004417 }
4418 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004419 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004420 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4421 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004422 */
4423 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004424 case expr_stmt:
4425 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004426 case del_stmt:
4427 return ast_for_del_stmt(c, n);
4428 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004429 return Pass(LINENO(n), n->n_col_offset,
4430 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004431 case flow_stmt:
4432 return ast_for_flow_stmt(c, n);
4433 case import_stmt:
4434 return ast_for_import_stmt(c, n);
4435 case global_stmt:
4436 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004437 case nonlocal_stmt:
4438 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004439 case assert_stmt:
4440 return ast_for_assert_stmt(c, n);
4441 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004442 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004443 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4444 TYPE(n), NCH(n));
4445 return NULL;
4446 }
4447 }
4448 else {
4449 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004450 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004451 */
4452 node *ch = CHILD(n, 0);
4453 REQ(n, compound_stmt);
4454 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004455 case if_stmt:
4456 return ast_for_if_stmt(c, ch);
4457 case while_stmt:
4458 return ast_for_while_stmt(c, ch);
4459 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004460 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461 case try_stmt:
4462 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004463 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004464 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004465 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004466 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004467 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004468 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 case decorated:
4470 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004471 case async_stmt:
4472 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004473 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004474 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004475 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004476 TYPE(n), NCH(n));
4477 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004478 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004479 }
4480}
4481
4482static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004483parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004484{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004485 const char *end;
4486 long x;
4487 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004488 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004489 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004490
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004491 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004492 errno = 0;
4493 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004494 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004495 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004496 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004497 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004498 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004499 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004500 }
4501 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004502 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004503 if (*end == '\0') {
4504 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004505 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004506 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004507 }
4508 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004509 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004510 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004511 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4512 if (compl.imag == -1.0 && PyErr_Occurred())
4513 return NULL;
4514 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004515 }
4516 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004517 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004518 dx = PyOS_string_to_double(s, NULL, NULL);
4519 if (dx == -1.0 && PyErr_Occurred())
4520 return NULL;
4521 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004523}
4524
4525static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004526parsenumber(struct compiling *c, const char *s)
4527{
4528 char *dup, *end;
4529 PyObject *res = NULL;
4530
4531 assert(s != NULL);
4532
4533 if (strchr(s, '_') == NULL) {
4534 return parsenumber_raw(c, s);
4535 }
4536 /* Create a duplicate without underscores. */
4537 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004538 if (dup == NULL) {
4539 return PyErr_NoMemory();
4540 }
Brett Cannona721aba2016-09-09 14:57:09 -07004541 end = dup;
4542 for (; *s; s++) {
4543 if (*s != '_') {
4544 *end++ = *s;
4545 }
4546 }
4547 *end = '\0';
4548 res = parsenumber_raw(c, dup);
4549 PyMem_Free(dup);
4550 return res;
4551}
4552
4553static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004554decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004555{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004556 const char *s, *t;
4557 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004558 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4559 while (s < end && (*s & 0x80)) s++;
4560 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004561 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004562}
4563
Eric V. Smith56466482016-10-31 14:46:26 -04004564static int
4565warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004566 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004567{
4568 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4569 first_invalid_escape_char);
4570 if (msg == NULL) {
4571 return -1;
4572 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004573 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004574 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004575 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004576 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004577 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004578 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004579 to get a more accurate error report */
4580 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004581 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004582 }
4583 Py_DECREF(msg);
4584 return -1;
4585 }
4586 Py_DECREF(msg);
4587 return 0;
4588}
4589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004590static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004591decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4592 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004593{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004594 PyObject *v, *u;
4595 char *buf;
4596 char *p;
4597 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004598
Benjamin Peterson202803a2016-02-25 22:34:45 -08004599 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004600 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004601 return NULL;
4602 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4603 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4604 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4605 if (u == NULL)
4606 return NULL;
4607 p = buf = PyBytes_AsString(u);
4608 end = s + len;
4609 while (s < end) {
4610 if (*s == '\\') {
4611 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004612 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004613 strcpy(p, "u005c");
4614 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004615 if (s >= end)
4616 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004617 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004618 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004619 if (*s & 0x80) { /* XXX inefficient */
4620 PyObject *w;
4621 int kind;
4622 void *data;
4623 Py_ssize_t len, i;
4624 w = decode_utf8(c, &s, end);
4625 if (w == NULL) {
4626 Py_DECREF(u);
4627 return NULL;
4628 }
4629 kind = PyUnicode_KIND(w);
4630 data = PyUnicode_DATA(w);
4631 len = PyUnicode_GET_LENGTH(w);
4632 for (i = 0; i < len; i++) {
4633 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4634 sprintf(p, "\\U%08x", chr);
4635 p += 10;
4636 }
4637 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004638 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004639 Py_DECREF(w);
4640 } else {
4641 *p++ = *s++;
4642 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004643 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004644 len = p - buf;
4645 s = buf;
4646
Eric V. Smith56466482016-10-31 14:46:26 -04004647 const char *first_invalid_escape;
4648 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4649
4650 if (v != NULL && first_invalid_escape != NULL) {
4651 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4652 /* We have not decref u before because first_invalid_escape points
4653 inside u. */
4654 Py_XDECREF(u);
4655 Py_DECREF(v);
4656 return NULL;
4657 }
4658 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004659 Py_XDECREF(u);
4660 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004661}
4662
Eric V. Smith56466482016-10-31 14:46:26 -04004663static PyObject *
4664decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4665 size_t len)
4666{
4667 const char *first_invalid_escape;
4668 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4669 &first_invalid_escape);
4670 if (result == NULL)
4671 return NULL;
4672
4673 if (first_invalid_escape != NULL) {
4674 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4675 Py_DECREF(result);
4676 return NULL;
4677 }
4678 }
4679 return result;
4680}
4681
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004682/* Shift locations for the given node and all its children by adding `lineno`
4683 and `col_offset` to existing locations. */
4684static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4685{
4686 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004687 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004688 for (int i = 0; i < NCH(n); ++i) {
4689 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4690 /* Shifting column offsets unnecessary if there's been newlines. */
4691 col_offset = 0;
4692 }
4693 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4694 }
4695 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004696 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004697}
4698
4699/* Fix locations for the given node and its children.
4700
4701 `parent` is the enclosing node.
4702 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004703 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004704*/
4705static void
4706fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4707{
4708 char *substr = NULL;
4709 char *start;
4710 int lines = LINENO(parent) - 1;
4711 int cols = parent->n_col_offset;
4712 /* Find the full fstring to fix location information in `n`. */
4713 while (parent && parent->n_type != STRING)
4714 parent = parent->n_child;
4715 if (parent && parent->n_str) {
4716 substr = strstr(parent->n_str, expr_str);
4717 if (substr) {
4718 start = substr;
4719 while (start > parent->n_str) {
4720 if (start[0] == '\n')
4721 break;
4722 start--;
4723 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004724 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004725 /* adjust the start based on the number of newlines encountered
4726 before the f-string expression */
4727 for (char* p = parent->n_str; p < substr; p++) {
4728 if (*p == '\n') {
4729 lines++;
4730 }
4731 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004732 }
4733 }
4734 fstring_shift_node_locations(n, lines, cols);
4735}
4736
Eric V. Smith451d0e32016-09-09 21:56:20 -04004737/* Compile this expression in to an expr_ty. Add parens around the
4738 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004739static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004740fstring_compile_expr(const char *expr_start, const char *expr_end,
4741 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004742
Eric V. Smith235a6f02015-09-19 14:51:32 -04004743{
4744 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004745 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004746 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004747 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004748 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004749 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004750
Eric V. Smith1d44c412015-09-23 07:49:00 -04004751 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004752 assert(*(expr_start-1) == '{');
4753 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004754
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004755 /* If the substring is all whitespace, it's an error. We need to catch this
4756 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4757 because turning the expression '' in to '()' would go from being invalid
4758 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004759 for (s = expr_start; s != expr_end; s++) {
4760 char c = *s;
4761 /* The Python parser ignores only the following whitespace
4762 characters (\r already is converted to \n). */
4763 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004764 break;
4765 }
4766 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004767 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004768 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004769 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004770 }
4771
Eric V. Smith451d0e32016-09-09 21:56:20 -04004772 len = expr_end - expr_start;
4773 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4774 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004775 if (str == NULL) {
4776 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004777 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004778 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004779
Eric V. Smith451d0e32016-09-09 21:56:20 -04004780 str[0] = '(';
4781 memcpy(str+1, expr_start, len);
4782 str[len+1] = ')';
4783 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004784
4785 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004786 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4787 Py_eval_input, 0);
4788 if (!mod_n) {
4789 PyMem_RawFree(str);
4790 return NULL;
4791 }
4792 /* Reuse str to find the correct column offset. */
4793 str[0] = '{';
4794 str[len+1] = '}';
4795 fstring_fix_node_location(n, mod_n, str);
4796 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004797 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004798 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004799 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004800 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004801 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004802}
4803
4804/* Return -1 on error.
4805
4806 Return 0 if we reached the end of the literal.
4807
4808 Return 1 if we haven't reached the end of the literal, but we want
4809 the caller to process the literal up to this point. Used for
4810 doubled braces.
4811*/
4812static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004813fstring_find_literal(const char **str, const char *end, int raw,
4814 PyObject **literal, int recurse_lvl,
4815 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004816{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004817 /* Get any literal string. It ends when we hit an un-doubled left
4818 brace (which isn't part of a unicode name escape such as
4819 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004820
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004821 const char *s = *str;
4822 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004823 int result = 0;
4824
Eric V. Smith235a6f02015-09-19 14:51:32 -04004825 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004826 while (s < end) {
4827 char ch = *s++;
4828 if (!raw && ch == '\\' && s < end) {
4829 ch = *s++;
4830 if (ch == 'N') {
4831 if (s < end && *s++ == '{') {
4832 while (s < end && *s++ != '}') {
4833 }
4834 continue;
4835 }
4836 break;
4837 }
4838 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4839 return -1;
4840 }
4841 }
4842 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004843 /* Check for doubled braces, but only at the top level. If
4844 we checked at every level, then f'{0:{3}}' would fail
4845 with the two closing braces. */
4846 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004847 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004848 /* We're going to tell the caller that the literal ends
4849 here, but that they should continue scanning. But also
4850 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004851 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004852 result = 1;
4853 goto done;
4854 }
4855
4856 /* Where a single '{' is the start of a new expression, a
4857 single '}' is not allowed. */
4858 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004859 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004860 ast_error(c, n, "f-string: single '}' is not allowed");
4861 return -1;
4862 }
4863 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004864 /* We're either at a '{', which means we're starting another
4865 expression; or a '}', which means we're at the end of this
4866 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004867 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004868 break;
4869 }
4870 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004871 *str = s;
4872 assert(s <= end);
4873 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004874done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004875 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004876 if (raw)
4877 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004878 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004879 NULL, NULL);
4880 else
Eric V. Smith56466482016-10-31 14:46:26 -04004881 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004882 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004883 if (!*literal)
4884 return -1;
4885 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004886 return result;
4887}
4888
4889/* Forward declaration because parsing is recursive. */
4890static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004891fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004892 struct compiling *c, const node *n);
4893
Eric V. Smith451d0e32016-09-09 21:56:20 -04004894/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004895 expression (so it must be a '{'). Returns the FormattedValue node,
4896 which includes the expression, conversion character, and
4897 format_spec expression.
4898
4899 Note that I don't do a perfect job here: I don't make sure that a
4900 closing brace doesn't match an opening paren, for example. It
4901 doesn't need to error on all invalid expressions, just correctly
4902 find the end of all valid ones. Any errors inside the expression
4903 will be caught when we parse it later. */
4904static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004905fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906 expr_ty *expression, struct compiling *c, const node *n)
4907{
4908 /* Return -1 on error, else 0. */
4909
Eric V. Smith451d0e32016-09-09 21:56:20 -04004910 const char *expr_start;
4911 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004912 expr_ty simple_expression;
4913 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004914 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004915
4916 /* 0 if we're not in a string, else the quote char we're trying to
4917 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004918 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004919
4920 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4921 int string_type = 0;
4922
4923 /* Keep track of nesting level for braces/parens/brackets in
4924 expressions. */
4925 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004926 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927
4928 /* Can only nest one level deep. */
4929 if (recurse_lvl >= 2) {
4930 ast_error(c, n, "f-string: expressions nested too deeply");
4931 return -1;
4932 }
4933
4934 /* The first char must be a left brace, or we wouldn't have gotten
4935 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004936 assert(**str == '{');
4937 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004938
Eric V. Smith451d0e32016-09-09 21:56:20 -04004939 expr_start = *str;
4940 for (; *str < end; (*str)++) {
4941 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004942
4943 /* Loop invariants. */
4944 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004945 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004946 if (quote_char)
4947 assert(string_type == 1 || string_type == 3);
4948 else
4949 assert(string_type == 0);
4950
Eric V. Smith451d0e32016-09-09 21:56:20 -04004951 ch = **str;
4952 /* Nowhere inside an expression is a backslash allowed. */
4953 if (ch == '\\') {
4954 /* Error: can't include a backslash character, inside
4955 parens or strings or not. */
Guido van Rossumdcfcd142019-01-31 03:40:27 -08004956 ast_error(c, n,
4957 "f-string expression part "
4958 "cannot include a backslash");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004959 return -1;
4960 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004961 if (quote_char) {
4962 /* We're inside a string. See if we're at the end. */
4963 /* This code needs to implement the same non-error logic
4964 as tok_get from tokenizer.c, at the letter_quote
4965 label. To actually share that code would be a
4966 nightmare. But, it's unlikely to change and is small,
4967 so duplicate it here. Note we don't need to catch all
4968 of the errors, since they'll be caught when parsing the
4969 expression. We just need to match the non-error
4970 cases. Thus we can ignore \n in single-quoted strings,
4971 for example. Or non-terminated strings. */
4972 if (ch == quote_char) {
4973 /* Does this match the string_type (single or triple
4974 quoted)? */
4975 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004976 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004977 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004978 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979 string_type = 0;
4980 quote_char = 0;
4981 continue;
4982 }
4983 } else {
4984 /* We're at the end of a normal string. */
4985 quote_char = 0;
4986 string_type = 0;
4987 continue;
4988 }
4989 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004990 } else if (ch == '\'' || ch == '"') {
4991 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004992 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004994 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 } else {
4996 /* Start of a normal string. */
4997 string_type = 1;
4998 }
4999 /* Start looking for the end of the string. */
5000 quote_char = ch;
5001 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005002 if (nested_depth >= MAXLEVEL) {
5003 ast_error(c, n, "f-string: too many nested parenthesis");
5004 return -1;
5005 }
5006 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005007 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005008 } else if (ch == '#') {
5009 /* Error: can't include a comment character, inside parens
5010 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04005011 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005012 return -1;
5013 } else if (nested_depth == 0 &&
5014 (ch == '!' || ch == ':' || ch == '}')) {
5015 /* First, test for the special case of "!=". Since '=' is
5016 not an allowed conversion character, nothing is lost in
5017 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005018 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005019 /* This isn't a conversion character, just continue. */
5020 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005021 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005022 /* Normal way out of this loop. */
5023 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005024 } else if (ch == ']' || ch == '}' || ch == ')') {
5025 if (!nested_depth) {
5026 ast_error(c, n, "f-string: unmatched '%c'", ch);
5027 return -1;
5028 }
5029 nested_depth--;
5030 int opening = parenstack[nested_depth];
5031 if (!((opening == '(' && ch == ')') ||
5032 (opening == '[' && ch == ']') ||
5033 (opening == '{' && ch == '}')))
5034 {
5035 ast_error(c, n,
5036 "f-string: closing parenthesis '%c' "
5037 "does not match opening parenthesis '%c'",
5038 ch, opening);
5039 return -1;
5040 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005041 } else {
5042 /* Just consume this char and loop around. */
5043 }
5044 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005045 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005046 /* If we leave this loop in a string or with mismatched parens, we
5047 don't care. We'll get a syntax error when compiling the
5048 expression. But, we can produce a better error message, so
5049 let's just do that.*/
5050 if (quote_char) {
5051 ast_error(c, n, "f-string: unterminated string");
5052 return -1;
5053 }
5054 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02005055 int opening = parenstack[nested_depth - 1];
5056 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005057 return -1;
5058 }
5059
Eric V. Smith451d0e32016-09-09 21:56:20 -04005060 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005061 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04005062
5063 /* Compile the expression as soon as possible, so we show errors
5064 related to the expression before errors related to the
5065 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005066 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04005067 if (!simple_expression)
5068 return -1;
5069
5070 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005071 if (**str == '!') {
5072 *str += 1;
5073 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005074 goto unexpected_end_of_string;
5075
Eric V. Smith451d0e32016-09-09 21:56:20 -04005076 conversion = **str;
5077 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005078
5079 /* Validate the conversion. */
5080 if (!(conversion == 's' || conversion == 'r'
5081 || conversion == 'a')) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005082 ast_error(c, n,
5083 "f-string: invalid conversion character: "
5084 "expected 's', 'r', or 'a'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04005085 return -1;
5086 }
5087 }
5088
5089 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005091 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005092 if (**str == ':') {
5093 *str += 1;
5094 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005095 goto unexpected_end_of_string;
5096
5097 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005098 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005099 if (!format_spec)
5100 return -1;
5101 }
5102
Eric V. Smith451d0e32016-09-09 21:56:20 -04005103 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104 goto unexpected_end_of_string;
5105
5106 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 assert(*str < end);
5108 assert(**str == '}');
5109 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005110
Eric V. Smith451d0e32016-09-09 21:56:20 -04005111 /* And now create the FormattedValue node that represents this
5112 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07005113 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005114 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005115 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005116 c->c_arena);
5117 if (!*expression)
5118 return -1;
5119
5120 return 0;
5121
5122unexpected_end_of_string:
5123 ast_error(c, n, "f-string: expecting '}'");
5124 return -1;
5125}
5126
5127/* Return -1 on error.
5128
5129 Return 0 if we have a literal (possible zero length) and an
5130 expression (zero length if at the end of the string.
5131
5132 Return 1 if we have a literal, but no expression, and we want the
5133 caller to call us again. This is used to deal with doubled
5134 braces.
5135
5136 When called multiple times on the string 'a{{b{0}c', this function
5137 will return:
5138
5139 1. the literal 'a{' with no expression, and a return value
5140 of 1. Despite the fact that there's no expression, the return
5141 value of 1 means we're not finished yet.
5142
5143 2. the literal 'b' and the expression '0', with a return value of
5144 0. The fact that there's an expression means we're not finished.
5145
5146 3. literal 'c' with no expression and a return value of 0. The
5147 combination of the return value of 0 with no expression means
5148 we're finished.
5149*/
5150static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005151fstring_find_literal_and_expr(const char **str, const char *end, int raw,
5152 int recurse_lvl, PyObject **literal,
5153 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005154 struct compiling *c, const node *n)
5155{
5156 int result;
5157
5158 assert(*literal == NULL && *expression == NULL);
5159
5160 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005161 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005162 if (result < 0)
5163 goto error;
5164
5165 assert(result == 0 || result == 1);
5166
5167 if (result == 1)
5168 /* We have a literal, but don't look at the expression. */
5169 return 1;
5170
Eric V. Smith451d0e32016-09-09 21:56:20 -04005171 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04005172 /* We're at the end of the string or the end of a nested
5173 f-string: no expression. The top-level error case where we
5174 expect to be at the end of the string but we're at a '}' is
5175 handled later. */
5176 return 0;
5177
5178 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005179 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04005180
Eric V. Smith451d0e32016-09-09 21:56:20 -04005181 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005182 goto error;
5183
5184 return 0;
5185
5186error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005187 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005188 return -1;
5189}
5190
5191#define EXPRLIST_N_CACHED 64
5192
5193typedef struct {
5194 /* Incrementally build an array of expr_ty, so be used in an
5195 asdl_seq. Cache some small but reasonably sized number of
5196 expr_ty's, and then after that start dynamically allocating,
5197 doubling the number allocated each time. Note that the f-string
5198 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005199 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005200 fast as you add exressions in an f-string. */
5201
5202 Py_ssize_t allocated; /* Number we've allocated. */
5203 Py_ssize_t size; /* Number we've used. */
5204 expr_ty *p; /* Pointer to the memory we're actually
5205 using. Will point to 'data' until we
5206 start dynamically allocating. */
5207 expr_ty data[EXPRLIST_N_CACHED];
5208} ExprList;
5209
5210#ifdef NDEBUG
5211#define ExprList_check_invariants(l)
5212#else
5213static void
5214ExprList_check_invariants(ExprList *l)
5215{
5216 /* Check our invariants. Make sure this object is "live", and
5217 hasn't been deallocated. */
5218 assert(l->size >= 0);
5219 assert(l->p != NULL);
5220 if (l->size <= EXPRLIST_N_CACHED)
5221 assert(l->data == l->p);
5222}
5223#endif
5224
5225static void
5226ExprList_Init(ExprList *l)
5227{
5228 l->allocated = EXPRLIST_N_CACHED;
5229 l->size = 0;
5230
5231 /* Until we start allocating dynamically, p points to data. */
5232 l->p = l->data;
5233
5234 ExprList_check_invariants(l);
5235}
5236
5237static int
5238ExprList_Append(ExprList *l, expr_ty exp)
5239{
5240 ExprList_check_invariants(l);
5241 if (l->size >= l->allocated) {
5242 /* We need to alloc (or realloc) the memory. */
5243 Py_ssize_t new_size = l->allocated * 2;
5244
5245 /* See if we've ever allocated anything dynamically. */
5246 if (l->p == l->data) {
5247 Py_ssize_t i;
5248 /* We're still using the cached data. Switch to
5249 alloc-ing. */
5250 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5251 if (!l->p)
5252 return -1;
5253 /* Copy the cached data into the new buffer. */
5254 for (i = 0; i < l->size; i++)
5255 l->p[i] = l->data[i];
5256 } else {
5257 /* Just realloc. */
5258 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5259 if (!tmp) {
5260 PyMem_RawFree(l->p);
5261 l->p = NULL;
5262 return -1;
5263 }
5264 l->p = tmp;
5265 }
5266
5267 l->allocated = new_size;
5268 assert(l->allocated == 2 * l->size);
5269 }
5270
5271 l->p[l->size++] = exp;
5272
5273 ExprList_check_invariants(l);
5274 return 0;
5275}
5276
5277static void
5278ExprList_Dealloc(ExprList *l)
5279{
5280 ExprList_check_invariants(l);
5281
5282 /* If there's been an error, or we've never dynamically allocated,
5283 do nothing. */
5284 if (!l->p || l->p == l->data) {
5285 /* Do nothing. */
5286 } else {
5287 /* We have dynamically allocated. Free the memory. */
5288 PyMem_RawFree(l->p);
5289 }
5290 l->p = NULL;
5291 l->size = -1;
5292}
5293
5294static asdl_seq *
5295ExprList_Finish(ExprList *l, PyArena *arena)
5296{
5297 asdl_seq *seq;
5298
5299 ExprList_check_invariants(l);
5300
5301 /* Allocate the asdl_seq and copy the expressions in to it. */
5302 seq = _Py_asdl_seq_new(l->size, arena);
5303 if (seq) {
5304 Py_ssize_t i;
5305 for (i = 0; i < l->size; i++)
5306 asdl_seq_SET(seq, i, l->p[i]);
5307 }
5308 ExprList_Dealloc(l);
5309 return seq;
5310}
5311
5312/* The FstringParser is designed to add a mix of strings and
5313 f-strings, and concat them together as needed. Ultimately, it
5314 generates an expr_ty. */
5315typedef struct {
5316 PyObject *last_str;
5317 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005318 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005319} FstringParser;
5320
5321#ifdef NDEBUG
5322#define FstringParser_check_invariants(state)
5323#else
5324static void
5325FstringParser_check_invariants(FstringParser *state)
5326{
5327 if (state->last_str)
5328 assert(PyUnicode_CheckExact(state->last_str));
5329 ExprList_check_invariants(&state->expr_list);
5330}
5331#endif
5332
5333static void
5334FstringParser_Init(FstringParser *state)
5335{
5336 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005337 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005338 ExprList_Init(&state->expr_list);
5339 FstringParser_check_invariants(state);
5340}
5341
5342static void
5343FstringParser_Dealloc(FstringParser *state)
5344{
5345 FstringParser_check_invariants(state);
5346
5347 Py_XDECREF(state->last_str);
5348 ExprList_Dealloc(&state->expr_list);
5349}
5350
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005351/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005352static expr_ty
5353make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5354{
5355 PyObject *s = *str;
5356 *str = NULL;
5357 assert(PyUnicode_CheckExact(s));
5358 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5359 Py_DECREF(s);
5360 return NULL;
5361 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005362 return Constant(s, LINENO(n), n->n_col_offset,
5363 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005364}
5365
5366/* Add a non-f-string (that is, a regular literal string). str is
5367 decref'd. */
5368static int
5369FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5370{
5371 FstringParser_check_invariants(state);
5372
5373 assert(PyUnicode_CheckExact(str));
5374
5375 if (PyUnicode_GET_LENGTH(str) == 0) {
5376 Py_DECREF(str);
5377 return 0;
5378 }
5379
5380 if (!state->last_str) {
5381 /* We didn't have a string before, so just remember this one. */
5382 state->last_str = str;
5383 } else {
5384 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005385 PyUnicode_AppendAndDel(&state->last_str, str);
5386 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005387 return -1;
5388 }
5389 FstringParser_check_invariants(state);
5390 return 0;
5391}
5392
Eric V. Smith451d0e32016-09-09 21:56:20 -04005393/* Parse an f-string. The f-string is in *str to end, with no
5394 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005395static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005396FstringParser_ConcatFstring(FstringParser *state, const char **str,
5397 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005398 struct compiling *c, const node *n)
5399{
5400 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005401 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005402
5403 /* Parse the f-string. */
5404 while (1) {
5405 PyObject *literal = NULL;
5406 expr_ty expression = NULL;
5407
5408 /* If there's a zero length literal in front of the
5409 expression, literal will be NULL. If we're at the end of
5410 the f-string, expression will be NULL (unless result == 1,
5411 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005412 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005413 &literal, &expression,
5414 c, n);
5415 if (result < 0)
5416 return -1;
5417
5418 /* Add the literal, if any. */
5419 if (!literal) {
5420 /* Do nothing. Just leave last_str alone (and possibly
5421 NULL). */
5422 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005423 /* Note that the literal can be zero length, if the
5424 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005425 state->last_str = literal;
5426 literal = NULL;
5427 } else {
5428 /* We have a literal, concatenate it. */
5429 assert(PyUnicode_GET_LENGTH(literal) != 0);
5430 if (FstringParser_ConcatAndDel(state, literal) < 0)
5431 return -1;
5432 literal = NULL;
5433 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005434
5435 /* We've dealt with the literal now. It can't be leaked on further
5436 errors. */
5437 assert(literal == NULL);
5438
5439 /* See if we should just loop around to get the next literal
5440 and expression, while ignoring the expression this
5441 time. This is used for un-doubling braces, as an
5442 optimization. */
5443 if (result == 1)
5444 continue;
5445
5446 if (!expression)
5447 /* We're done with this f-string. */
5448 break;
5449
5450 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005451 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005452 if (!state->last_str) {
5453 /* Do nothing. No previous literal. */
5454 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005455 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005456 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5457 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5458 return -1;
5459 }
5460
5461 if (ExprList_Append(&state->expr_list, expression) < 0)
5462 return -1;
5463 }
5464
Eric V. Smith235a6f02015-09-19 14:51:32 -04005465 /* If recurse_lvl is zero, then we must be at the end of the
5466 string. Otherwise, we must be at a right brace. */
5467
Eric V. Smith451d0e32016-09-09 21:56:20 -04005468 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005469 ast_error(c, n, "f-string: unexpected end of string");
5470 return -1;
5471 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005472 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005473 ast_error(c, n, "f-string: expecting '}'");
5474 return -1;
5475 }
5476
5477 FstringParser_check_invariants(state);
5478 return 0;
5479}
5480
5481/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005482 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005483static expr_ty
5484FstringParser_Finish(FstringParser *state, struct compiling *c,
5485 const node *n)
5486{
5487 asdl_seq *seq;
5488
5489 FstringParser_check_invariants(state);
5490
5491 /* If we're just a constant string with no expressions, return
5492 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005493 if (!state->fmode) {
5494 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005495 if (!state->last_str) {
5496 /* Create a zero length string. */
5497 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5498 if (!state->last_str)
5499 goto error;
5500 }
5501 return make_str_node_and_del(&state->last_str, c, n);
5502 }
5503
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005504 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005505 last node in our expression list. */
5506 if (state->last_str) {
5507 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5508 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5509 goto error;
5510 }
5511 /* This has already been freed. */
5512 assert(state->last_str == NULL);
5513
5514 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5515 if (!seq)
5516 goto error;
5517
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005518 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5519 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005520
5521error:
5522 FstringParser_Dealloc(state);
5523 return NULL;
5524}
5525
Eric V. Smith451d0e32016-09-09 21:56:20 -04005526/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5527 at end, parse it into an expr_ty. Return NULL on error. Adjust
5528 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005529static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005530fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005531 struct compiling *c, const node *n)
5532{
5533 FstringParser state;
5534
5535 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005536 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005537 c, n) < 0) {
5538 FstringParser_Dealloc(&state);
5539 return NULL;
5540 }
5541
5542 return FstringParser_Finish(&state, c, n);
5543}
5544
5545/* n is a Python string literal, including the bracketing quote
5546 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005547 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005548 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005549 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5550 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005551*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005552static int
5553parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5554 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005555{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005556 size_t len;
5557 const char *s = STR(n);
5558 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005559 int fmode = 0;
5560 *bytesmode = 0;
5561 *rawmode = 0;
5562 *result = NULL;
5563 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005564 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005565 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005566 if (quote == 'b' || quote == 'B') {
5567 quote = *++s;
5568 *bytesmode = 1;
5569 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005570 else if (quote == 'u' || quote == 'U') {
5571 quote = *++s;
5572 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005573 else if (quote == 'r' || quote == 'R') {
5574 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005575 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005576 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005577 else if (quote == 'f' || quote == 'F') {
5578 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005579 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005580 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005581 else {
5582 break;
5583 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005584 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005585 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005586 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005587 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005588 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005589 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005590 if (quote != '\'' && quote != '\"') {
5591 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005592 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005593 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005594 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005595 s++;
5596 len = strlen(s);
5597 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005598 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005599 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005600 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005601 }
5602 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005603 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005604 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005605 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005606 }
5607 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005608 /* A triple quoted string. We've already skipped one quote at
5609 the start and one at the end of the string. Now skip the
5610 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005611 s += 2;
5612 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005613 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005614 if (s[--len] != quote || s[--len] != quote) {
5615 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005616 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005617 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005618 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005619
Eric V. Smith451d0e32016-09-09 21:56:20 -04005620 if (fmode) {
5621 /* Just return the bytes. The caller will parse the resulting
5622 string. */
5623 *fstr = s;
5624 *fstrlen = len;
5625 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005626 }
5627
Eric V. Smith451d0e32016-09-09 21:56:20 -04005628 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005629 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005630 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005631 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005632 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005633 const char *ch;
5634 for (ch = s; *ch; ch++) {
5635 if (Py_CHARMASK(*ch) >= 0x80) {
Guido van Rossumdcfcd142019-01-31 03:40:27 -08005636 ast_error(c, n,
5637 "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005638 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005639 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005640 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005641 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005642 if (*rawmode)
5643 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005644 else
Eric V. Smith56466482016-10-31 14:46:26 -04005645 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005646 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005647 if (*rawmode)
5648 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005649 else
Eric V. Smith56466482016-10-31 14:46:26 -04005650 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005651 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005652 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005653}
5654
Eric V. Smith235a6f02015-09-19 14:51:32 -04005655/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5656 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005657 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005658 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005659 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005660 node if there's just an f-string (with no leading or trailing
5661 literals), or a JoinedStr node if there are multiple f-strings or
5662 any literals involved. */
5663static expr_ty
5664parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005665{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005666 int bytesmode = 0;
5667 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005668 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005669
5670 FstringParser state;
5671 FstringParser_Init(&state);
5672
5673 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005674 int this_bytesmode;
5675 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005676 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005677 const char *fstr;
5678 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005679
5680 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005681 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5682 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005683 goto error;
5684
5685 /* Check that we're not mixing bytes with unicode. */
5686 if (i != 0 && bytesmode != this_bytesmode) {
5687 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005688 /* s is NULL if the current string part is an f-string. */
5689 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005690 goto error;
5691 }
5692 bytesmode = this_bytesmode;
5693
Eric V. Smith451d0e32016-09-09 21:56:20 -04005694 if (fstr != NULL) {
5695 int result;
5696 assert(s == NULL && !bytesmode);
5697 /* This is an f-string. Parse and concatenate it. */
5698 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5699 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005700 if (result < 0)
5701 goto error;
5702 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005703 /* A string or byte string. */
5704 assert(s != NULL && fstr == NULL);
5705
Eric V. Smith451d0e32016-09-09 21:56:20 -04005706 assert(bytesmode ? PyBytes_CheckExact(s) :
5707 PyUnicode_CheckExact(s));
5708
Eric V. Smith451d0e32016-09-09 21:56:20 -04005709 if (bytesmode) {
5710 /* For bytes, concat as we go. */
5711 if (i == 0) {
5712 /* First time, just remember this value. */
5713 bytes_str = s;
5714 } else {
5715 PyBytes_ConcatAndDel(&bytes_str, s);
5716 if (!bytes_str)
5717 goto error;
5718 }
5719 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005720 /* This is a regular string. Concatenate it. */
5721 if (FstringParser_ConcatAndDel(&state, s) < 0)
5722 goto error;
5723 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005724 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005725 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005726 if (bytesmode) {
5727 /* Just return the bytes object and we're done. */
5728 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5729 goto error;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005730 return Constant(bytes_str, LINENO(n), n->n_col_offset,
5731 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005732 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005733
Eric V. Smith235a6f02015-09-19 14:51:32 -04005734 /* We're not a bytes string, bytes_str should never have been set. */
5735 assert(bytes_str == NULL);
5736
5737 return FstringParser_Finish(&state, c, n);
5738
5739error:
5740 Py_XDECREF(bytes_str);
5741 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005742 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005743}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005744
5745PyObject *
5746_PyAST_GetDocString(asdl_seq *body)
5747{
5748 if (!asdl_seq_LEN(body)) {
5749 return NULL;
5750 }
5751 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5752 if (st->kind != Expr_kind) {
5753 return NULL;
5754 }
5755 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005756 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5757 return e->v.Constant.value;
5758 }
5759 return NULL;
5760}