blob: 69dfe3c3c435cecce2e0b00b659b9c1e735ded11 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
Serhiy Storchakaddbce132017-11-15 17:39:37 +020014#include <stdbool.h>
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Serhiy Storchaka58159ef2019-01-12 09:46:50 +020016#define MAXLEVEL 200 /* Max parentheses level */
17
Benjamin Peterson832bfe22011-08-09 16:15:04 -050018static int validate_stmts(asdl_seq *);
19static int validate_exprs(asdl_seq *, expr_context_ty, int);
20static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
21static int validate_stmt(stmt_ty);
22static int validate_expr(expr_ty, expr_context_ty);
23
24static int
25validate_comprehension(asdl_seq *gens)
26{
Victor Stinner4d73ae72018-11-22 14:45:16 +010027 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050028 if (!asdl_seq_LEN(gens)) {
29 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
30 return 0;
31 }
32 for (i = 0; i < asdl_seq_LEN(gens); i++) {
33 comprehension_ty comp = asdl_seq_GET(gens, i);
34 if (!validate_expr(comp->target, Store) ||
35 !validate_expr(comp->iter, Load) ||
36 !validate_exprs(comp->ifs, Load, 0))
37 return 0;
38 }
39 return 1;
40}
41
42static int
43validate_slice(slice_ty slice)
44{
45 switch (slice->kind) {
46 case Slice_kind:
47 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
48 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
49 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
50 case ExtSlice_kind: {
Victor Stinner4d73ae72018-11-22 14:45:16 +010051 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050052 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
53 return 0;
54 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
55 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
56 return 0;
57 return 1;
58 }
59 case Index_kind:
60 return validate_expr(slice->v.Index.value, Load);
61 default:
62 PyErr_SetString(PyExc_SystemError, "unknown slice node");
63 return 0;
64 }
65}
66
67static int
68validate_keywords(asdl_seq *keywords)
69{
Victor Stinner4d73ae72018-11-22 14:45:16 +010070 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050071 for (i = 0; i < asdl_seq_LEN(keywords); i++)
72 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
73 return 0;
74 return 1;
75}
76
77static int
78validate_args(asdl_seq *args)
79{
Victor Stinner4d73ae72018-11-22 14:45:16 +010080 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050081 for (i = 0; i < asdl_seq_LEN(args); i++) {
82 arg_ty arg = asdl_seq_GET(args, i);
83 if (arg->annotation && !validate_expr(arg->annotation, Load))
84 return 0;
85 }
86 return 1;
87}
88
89static const char *
90expr_context_name(expr_context_ty ctx)
91{
92 switch (ctx) {
93 case Load:
94 return "Load";
95 case Store:
96 return "Store";
97 case Del:
98 return "Del";
99 case AugLoad:
100 return "AugLoad";
101 case AugStore:
102 return "AugStore";
103 case Param:
104 return "Param";
105 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700106 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500107 }
108}
109
110static int
111validate_arguments(arguments_ty args)
112{
113 if (!validate_args(args->args))
114 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700115 if (args->vararg && args->vararg->annotation
116 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500117 return 0;
118 }
119 if (!validate_args(args->kwonlyargs))
120 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100121 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700122 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500123 return 0;
124 }
125 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
126 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
127 return 0;
128 }
129 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
130 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
131 "kw_defaults on arguments");
132 return 0;
133 }
134 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
135}
136
137static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100138validate_constant(PyObject *value)
139{
140 if (value == Py_None || value == Py_Ellipsis)
141 return 1;
142
143 if (PyLong_CheckExact(value)
144 || PyFloat_CheckExact(value)
145 || PyComplex_CheckExact(value)
146 || PyBool_Check(value)
147 || PyUnicode_CheckExact(value)
148 || PyBytes_CheckExact(value))
149 return 1;
150
151 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
152 PyObject *it;
153
154 it = PyObject_GetIter(value);
155 if (it == NULL)
156 return 0;
157
158 while (1) {
159 PyObject *item = PyIter_Next(it);
160 if (item == NULL) {
161 if (PyErr_Occurred()) {
162 Py_DECREF(it);
163 return 0;
164 }
165 break;
166 }
167
168 if (!validate_constant(item)) {
169 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 return 0;
172 }
Victor Stinner726f6902016-01-27 00:11:47 +0100173 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100174 }
175
176 Py_DECREF(it);
177 return 1;
178 }
179
180 return 0;
181}
182
183static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500184validate_expr(expr_ty exp, expr_context_ty ctx)
185{
186 int check_ctx = 1;
187 expr_context_ty actual_ctx;
188
189 /* First check expression context. */
190 switch (exp->kind) {
191 case Attribute_kind:
192 actual_ctx = exp->v.Attribute.ctx;
193 break;
194 case Subscript_kind:
195 actual_ctx = exp->v.Subscript.ctx;
196 break;
197 case Starred_kind:
198 actual_ctx = exp->v.Starred.ctx;
199 break;
200 case Name_kind:
201 actual_ctx = exp->v.Name.ctx;
202 break;
203 case List_kind:
204 actual_ctx = exp->v.List.ctx;
205 break;
206 case Tuple_kind:
207 actual_ctx = exp->v.Tuple.ctx;
208 break;
209 default:
210 if (ctx != Load) {
211 PyErr_Format(PyExc_ValueError, "expression which can't be "
212 "assigned to in %s context", expr_context_name(ctx));
213 return 0;
214 }
215 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100216 /* set actual_ctx to prevent gcc warning */
217 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500218 }
219 if (check_ctx && actual_ctx != ctx) {
220 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
221 expr_context_name(ctx), expr_context_name(actual_ctx));
222 return 0;
223 }
224
225 /* Now validate expression. */
226 switch (exp->kind) {
227 case BoolOp_kind:
228 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
229 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
230 return 0;
231 }
232 return validate_exprs(exp->v.BoolOp.values, Load, 0);
233 case BinOp_kind:
234 return validate_expr(exp->v.BinOp.left, Load) &&
235 validate_expr(exp->v.BinOp.right, Load);
236 case UnaryOp_kind:
237 return validate_expr(exp->v.UnaryOp.operand, Load);
238 case Lambda_kind:
239 return validate_arguments(exp->v.Lambda.args) &&
240 validate_expr(exp->v.Lambda.body, Load);
241 case IfExp_kind:
242 return validate_expr(exp->v.IfExp.test, Load) &&
243 validate_expr(exp->v.IfExp.body, Load) &&
244 validate_expr(exp->v.IfExp.orelse, Load);
245 case Dict_kind:
246 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
247 PyErr_SetString(PyExc_ValueError,
248 "Dict doesn't have the same number of keys as values");
249 return 0;
250 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400251 /* null_ok=1 for keys expressions to allow dict unpacking to work in
252 dict literals, i.e. ``{**{a:b}}`` */
253 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
254 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500255 case Set_kind:
256 return validate_exprs(exp->v.Set.elts, Load, 0);
257#define COMP(NAME) \
258 case NAME ## _kind: \
259 return validate_comprehension(exp->v.NAME.generators) && \
260 validate_expr(exp->v.NAME.elt, Load);
261 COMP(ListComp)
262 COMP(SetComp)
263 COMP(GeneratorExp)
264#undef COMP
265 case DictComp_kind:
266 return validate_comprehension(exp->v.DictComp.generators) &&
267 validate_expr(exp->v.DictComp.key, Load) &&
268 validate_expr(exp->v.DictComp.value, Load);
269 case Yield_kind:
270 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500271 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000272 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400273 case Await_kind:
274 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500275 case Compare_kind:
276 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
277 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
278 return 0;
279 }
280 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
281 asdl_seq_LEN(exp->v.Compare.ops)) {
282 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
283 "of comparators and operands");
284 return 0;
285 }
286 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
287 validate_expr(exp->v.Compare.left, Load);
288 case Call_kind:
289 return validate_expr(exp->v.Call.func, Load) &&
290 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400291 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100292 case Constant_kind:
293 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100294 PyErr_Format(PyExc_TypeError,
295 "got an invalid type in Constant: %s",
296 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100297 return 0;
298 }
299 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400300 case JoinedStr_kind:
301 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
302 case FormattedValue_kind:
303 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
304 return 0;
305 if (exp->v.FormattedValue.format_spec)
306 return validate_expr(exp->v.FormattedValue.format_spec, Load);
307 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500308 case Attribute_kind:
309 return validate_expr(exp->v.Attribute.value, Load);
310 case Subscript_kind:
311 return validate_slice(exp->v.Subscript.slice) &&
312 validate_expr(exp->v.Subscript.value, Load);
313 case Starred_kind:
314 return validate_expr(exp->v.Starred.value, ctx);
315 case List_kind:
316 return validate_exprs(exp->v.List.elts, ctx, 0);
317 case Tuple_kind:
318 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300319 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500320 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500321 return 1;
322 default:
323 PyErr_SetString(PyExc_SystemError, "unexpected expression");
324 return 0;
325 }
326}
327
328static int
329validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
330{
331 if (asdl_seq_LEN(seq))
332 return 1;
333 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
334 return 0;
335}
336
337static int
338validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
339{
340 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
341 validate_exprs(targets, ctx, 0);
342}
343
344static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300345validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348}
349
350static int
351validate_stmt(stmt_ty stmt)
352{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100353 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500354 switch (stmt->kind) {
355 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300356 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500357 validate_arguments(stmt->v.FunctionDef.args) &&
358 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
359 (!stmt->v.FunctionDef.returns ||
360 validate_expr(stmt->v.FunctionDef.returns, Load));
361 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300362 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500363 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
364 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400365 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500366 case Return_kind:
367 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
368 case Delete_kind:
369 return validate_assignlist(stmt->v.Delete.targets, Del);
370 case Assign_kind:
371 return validate_assignlist(stmt->v.Assign.targets, Store) &&
372 validate_expr(stmt->v.Assign.value, Load);
373 case AugAssign_kind:
374 return validate_expr(stmt->v.AugAssign.target, Store) &&
375 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700376 case AnnAssign_kind:
377 if (stmt->v.AnnAssign.target->kind != Name_kind &&
378 stmt->v.AnnAssign.simple) {
379 PyErr_SetString(PyExc_TypeError,
380 "AnnAssign with simple non-Name target");
381 return 0;
382 }
383 return validate_expr(stmt->v.AnnAssign.target, Store) &&
384 (!stmt->v.AnnAssign.value ||
385 validate_expr(stmt->v.AnnAssign.value, Load)) &&
386 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500387 case For_kind:
388 return validate_expr(stmt->v.For.target, Store) &&
389 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300390 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500391 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400392 case AsyncFor_kind:
393 return validate_expr(stmt->v.AsyncFor.target, Store) &&
394 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300395 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400396 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500397 case While_kind:
398 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300399 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500400 validate_stmts(stmt->v.While.orelse);
401 case If_kind:
402 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300403 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500404 validate_stmts(stmt->v.If.orelse);
405 case With_kind:
406 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
407 return 0;
408 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
409 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
410 if (!validate_expr(item->context_expr, Load) ||
411 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
412 return 0;
413 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300414 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400415 case AsyncWith_kind:
416 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
417 return 0;
418 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
419 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
420 if (!validate_expr(item->context_expr, Load) ||
421 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
422 return 0;
423 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300424 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500425 case Raise_kind:
426 if (stmt->v.Raise.exc) {
427 return validate_expr(stmt->v.Raise.exc, Load) &&
428 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
429 }
430 if (stmt->v.Raise.cause) {
431 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
432 return 0;
433 }
434 return 1;
435 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300436 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500437 return 0;
438 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
439 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
440 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
441 return 0;
442 }
443 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
444 asdl_seq_LEN(stmt->v.Try.orelse)) {
445 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
446 return 0;
447 }
448 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
449 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
450 if ((handler->v.ExceptHandler.type &&
451 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300452 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500453 return 0;
454 }
455 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
456 validate_stmts(stmt->v.Try.finalbody)) &&
457 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
458 validate_stmts(stmt->v.Try.orelse));
459 case Assert_kind:
460 return validate_expr(stmt->v.Assert.test, Load) &&
461 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
462 case Import_kind:
463 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
464 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300465 if (stmt->v.ImportFrom.level < 0) {
466 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500467 return 0;
468 }
469 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
470 case Global_kind:
471 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
472 case Nonlocal_kind:
473 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
474 case Expr_kind:
475 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400476 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300477 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400478 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
479 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
480 (!stmt->v.AsyncFunctionDef.returns ||
481 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500482 case Pass_kind:
483 case Break_kind:
484 case Continue_kind:
485 return 1;
486 default:
487 PyErr_SetString(PyExc_SystemError, "unexpected statement");
488 return 0;
489 }
490}
491
492static int
493validate_stmts(asdl_seq *seq)
494{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100495 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500496 for (i = 0; i < asdl_seq_LEN(seq); i++) {
497 stmt_ty stmt = asdl_seq_GET(seq, i);
498 if (stmt) {
499 if (!validate_stmt(stmt))
500 return 0;
501 }
502 else {
503 PyErr_SetString(PyExc_ValueError,
504 "None disallowed in statement list");
505 return 0;
506 }
507 }
508 return 1;
509}
510
511static int
512validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
513{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100514 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500515 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
516 expr_ty expr = asdl_seq_GET(exprs, i);
517 if (expr) {
518 if (!validate_expr(expr, ctx))
519 return 0;
520 }
521 else if (!null_ok) {
522 PyErr_SetString(PyExc_ValueError,
523 "None disallowed in expression list");
524 return 0;
525 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100526
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500527 }
528 return 1;
529}
530
531int
532PyAST_Validate(mod_ty mod)
533{
534 int res = 0;
535
536 switch (mod->kind) {
537 case Module_kind:
538 res = validate_stmts(mod->v.Module.body);
539 break;
540 case Interactive_kind:
541 res = validate_stmts(mod->v.Interactive.body);
542 break;
543 case Expression_kind:
544 res = validate_expr(mod->v.Expression.body, Load);
545 break;
546 case Suite_kind:
547 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
548 break;
549 default:
550 PyErr_SetString(PyExc_SystemError, "impossible module node");
551 res = 0;
552 break;
553 }
554 return res;
555}
556
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500557/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500558#include "grammar.h"
559#include "parsetok.h"
560#include "graminit.h"
561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562/* Data structure used internally */
563struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400564 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200565 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500566 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567};
568
569static asdl_seq *seq_for_testlist(struct compiling *, const node *);
570static expr_ty ast_for_expr(struct compiling *, const node *);
571static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300572static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000573static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
574 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000575static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000576static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577
guoci90fc8982018-09-11 17:45:45 -0400578static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
579static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200582static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
583 const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000585static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400586static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587
Nick Coghlan650f0d02007-04-15 12:05:43 +0000588#define COMP_GENEXP 0
589#define COMP_LISTCOMP 1
590#define COMP_SETCOMP 2
591
Benjamin Peterson55e00432012-01-16 17:22:31 -0500592static int
593init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000594{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500595 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
596 if (!m)
597 return 0;
598 c->c_normalize = PyObject_GetAttrString(m, "normalize");
599 Py_DECREF(m);
600 if (!c->c_normalize)
601 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500602 return 1;
603}
604
605static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400606new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500607{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400608 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500609 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000610 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500611 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500612 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000613 /* Check whether there are non-ASCII characters in the
614 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500615 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200616 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300617 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500618 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500619 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200620 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500621 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300622 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
623 if (form == NULL) {
624 Py_DECREF(id);
625 return NULL;
626 }
627 PyObject *args[2] = {form, id};
628 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500629 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200630 if (!id2)
631 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300632 if (!PyUnicode_Check(id2)) {
633 PyErr_Format(PyExc_TypeError,
634 "unicodedata.normalize() must return a string, not "
635 "%.200s",
636 Py_TYPE(id2)->tp_name);
637 Py_DECREF(id2);
638 return NULL;
639 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200640 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000641 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000642 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200643 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
644 Py_DECREF(id);
645 return NULL;
646 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000647 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648}
649
Benjamin Peterson55e00432012-01-16 17:22:31 -0500650#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200653ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400655 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200656 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200658 va_start(va, errmsg);
659 errstr = PyUnicode_FromFormatV(errmsg, va);
660 va_end(va);
661 if (!errstr) {
662 return 0;
663 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200664 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000666 Py_INCREF(Py_None);
667 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 }
Ammar Askar025eb982018-09-24 17:12:49 -0400669 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200670 if (!tmp) {
671 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400672 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000673 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000674 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 Py_DECREF(errstr);
676 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400677 if (value) {
678 PyErr_SetObject(PyExc_SyntaxError, value);
679 Py_DECREF(value);
680 }
681 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682}
683
684/* num_stmts() returns number of contained statements.
685
686 Use this routine to determine how big a sequence is needed for
687 the statements in a parse tree. Its raison d'etre is this bit of
688 grammar:
689
690 stmt: simple_stmt | compound_stmt
691 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
692
693 A simple_stmt can contain multiple small_stmt elements joined
694 by semicolons. If the arg is a simple_stmt, the number of
695 small_stmt elements is returned.
696*/
697
698static int
699num_stmts(const node *n)
700{
701 int i, l;
702 node *ch;
703
704 switch (TYPE(n)) {
705 case single_input:
706 if (TYPE(CHILD(n, 0)) == NEWLINE)
707 return 0;
708 else
709 return num_stmts(CHILD(n, 0));
710 case file_input:
711 l = 0;
712 for (i = 0; i < NCH(n); i++) {
713 ch = CHILD(n, i);
714 if (TYPE(ch) == stmt)
715 l += num_stmts(ch);
716 }
717 return l;
718 case stmt:
719 return num_stmts(CHILD(n, 0));
720 case compound_stmt:
721 return 1;
722 case simple_stmt:
723 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
724 case suite:
725 if (NCH(n) == 1)
726 return num_stmts(CHILD(n, 0));
727 else {
728 l = 0;
729 for (i = 2; i < (NCH(n) - 1); i++)
730 l += num_stmts(CHILD(n, i));
731 return l;
732 }
733 default: {
734 char buf[128];
735
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000736 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 TYPE(n), NCH(n));
738 Py_FatalError(buf);
739 }
740 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700741 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742}
743
744/* Transform the CST rooted at node * to the appropriate AST
745*/
746
747mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200748PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
749 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000751 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 asdl_seq *stmts = NULL;
753 stmt_ty s;
754 node *ch;
755 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500756 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400758 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200759 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400760 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800761 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800762
763 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765
Jeremy Hyltona8293132006-02-28 17:58:27 +0000766 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 switch (TYPE(n)) {
768 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200769 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500771 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 for (i = 0; i < NCH(n) - 1; i++) {
773 ch = CHILD(n, i);
774 if (TYPE(ch) == NEWLINE)
775 continue;
776 REQ(ch, stmt);
777 num = num_stmts(ch);
778 if (num == 1) {
779 s = ast_for_stmt(&c, ch);
780 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500781 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000782 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 }
784 else {
785 ch = CHILD(ch, 0);
786 REQ(ch, simple_stmt);
787 for (j = 0; j < num; j++) {
788 s = ast_for_stmt(&c, CHILD(ch, j * 2));
789 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500790 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000791 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 }
793 }
794 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300795 res = Module(stmts, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500796 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 case eval_input: {
798 expr_ty testlist_ast;
799
Nick Coghlan650f0d02007-04-15 12:05:43 +0000800 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000801 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500803 goto out;
804 res = Expression(testlist_ast, arena);
805 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
807 case single_input:
808 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200809 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500811 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000812 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
813 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000814 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500815 goto out;
816 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 }
818 else {
819 n = CHILD(n, 0);
820 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200821 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500823 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000825 s = ast_for_stmt(&c, n);
826 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500827 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 asdl_seq_SET(stmts, 0, s);
829 }
830 else {
831 /* Only a simple_stmt can contain multiple statements. */
832 REQ(n, simple_stmt);
833 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 if (TYPE(CHILD(n, i)) == NEWLINE)
835 break;
836 s = ast_for_stmt(&c, CHILD(n, i));
837 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 asdl_seq_SET(stmts, i / 2, s);
840 }
841 }
842
Benjamin Peterson55e00432012-01-16 17:22:31 -0500843 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500845 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000847 PyErr_Format(PyExc_SystemError,
848 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500849 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500851 out:
852 if (c.c_normalize) {
853 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500854 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500855 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856}
857
Victor Stinner14e461d2013-08-26 22:28:21 +0200858mod_ty
859PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
860 PyArena *arena)
861{
862 mod_ty mod;
863 PyObject *filename;
864 filename = PyUnicode_DecodeFSDefault(filename_str);
865 if (filename == NULL)
866 return NULL;
867 mod = PyAST_FromNodeObject(n, flags, filename, arena);
868 Py_DECREF(filename);
869 return mod;
870
871}
872
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
874*/
875
876static operator_ty
877get_operator(const node *n)
878{
879 switch (TYPE(n)) {
880 case VBAR:
881 return BitOr;
882 case CIRCUMFLEX:
883 return BitXor;
884 case AMPER:
885 return BitAnd;
886 case LEFTSHIFT:
887 return LShift;
888 case RIGHTSHIFT:
889 return RShift;
890 case PLUS:
891 return Add;
892 case MINUS:
893 return Sub;
894 case STAR:
895 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400896 case AT:
897 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898 case SLASH:
899 return Div;
900 case DOUBLESLASH:
901 return FloorDiv;
902 case PERCENT:
903 return Mod;
904 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 }
907}
908
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200909static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000910 "None",
911 "True",
912 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200913 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000914 NULL,
915};
916
917static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400918forbidden_name(struct compiling *c, identifier name, const node *n,
919 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000920{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000921 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200922 const char * const *p = FORBIDDEN;
923 if (!full_checks) {
924 /* In most cases, the parser will protect True, False, and None
925 from being assign to. */
926 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000927 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200928 for (; *p; p++) {
929 if (_PyUnicode_EqualToASCIIString(name, *p)) {
930 ast_error(c, n, "cannot assign to %U", name);
931 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000932 }
933 }
934 return 0;
935}
936
Serhiy Storchakab619b092018-11-27 09:40:29 +0200937static expr_ty
938copy_location(expr_ty e, const node *n)
939{
940 if (e) {
941 e->lineno = LINENO(n);
942 e->col_offset = n->n_col_offset;
943 }
944 return e;
945}
946
Jeremy Hyltona8293132006-02-28 17:58:27 +0000947/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948
949 Only sets context for expr kinds that "can appear in assignment context"
950 (according to ../Parser/Python.asdl). For other expr kinds, it sets
951 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952*/
953
954static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000955set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956{
957 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000958 /* If a particular expression type can't be used for assign / delete,
959 set expr_name to its name and an error message will be generated.
960 */
961 const char* expr_name = NULL;
962
963 /* The ast defines augmented store and load contexts, but the
964 implementation here doesn't actually use them. The code may be
965 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000966 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000967 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000968 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000969 */
970 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971
972 switch (e->kind) {
973 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000974 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400975 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000976 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000977 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000979 e->v.Subscript.ctx = ctx;
980 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000981 case Starred_kind:
982 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000983 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000984 return 0;
985 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000987 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500988 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000989 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000990 }
991 e->v.Name.ctx = ctx;
992 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000994 e->v.List.ctx = ctx;
995 s = e->v.List.elts;
996 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +0300998 e->v.Tuple.ctx = ctx;
999 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001000 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001001 case Lambda_kind:
1002 expr_name = "lambda";
1003 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001005 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001006 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001007 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001009 case UnaryOp_kind:
1010 expr_name = "operator";
1011 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001013 expr_name = "generator expression";
1014 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001016 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001017 expr_name = "yield expression";
1018 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001019 case Await_kind:
1020 expr_name = "await expression";
1021 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001022 case ListComp_kind:
1023 expr_name = "list comprehension";
1024 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001025 case SetComp_kind:
1026 expr_name = "set comprehension";
1027 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001028 case DictComp_kind:
1029 expr_name = "dict comprehension";
1030 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001031 case Dict_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001032 expr_name = "dict display";
1033 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001034 case Set_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001035 expr_name = "set display";
1036 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001037 case JoinedStr_kind:
1038 case FormattedValue_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001039 expr_name = "f-string expression";
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001040 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001041 case Constant_kind: {
1042 PyObject *value = e->v.Constant.value;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001043 if (value == Py_None || value == Py_False || value == Py_True
1044 || value == Py_Ellipsis)
1045 {
1046 return ast_error(c, n, "cannot %s %R",
1047 ctx == Store ? "assign to" : "delete",
1048 value);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001049 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001050 expr_name = "literal";
Benjamin Peterson442f2092012-12-06 17:41:04 -05001051 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001052 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001053 case Compare_kind:
1054 expr_name = "comparison";
1055 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056 case IfExp_kind:
1057 expr_name = "conditional expression";
1058 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001059 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 PyErr_Format(PyExc_SystemError,
1061 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001062 e->kind, e->lineno);
1063 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001065 /* Check for error string set by switch */
1066 if (expr_name) {
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001067 return ast_error(c, n, "cannot %s %s",
1068 ctx == Store ? "assign to" : "delete",
1069 expr_name);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001070 }
1071
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 */
1075 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001076 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077
Thomas Wouters89f507f2006-12-13 04:49:30 +00001078 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001079 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001080 return 0;
1081 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082 }
1083 return 1;
1084}
1085
1086static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001087ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088{
1089 REQ(n, augassign);
1090 n = CHILD(n, 0);
1091 switch (STR(n)[0]) {
1092 case '+':
1093 return Add;
1094 case '-':
1095 return Sub;
1096 case '/':
1097 if (STR(n)[1] == '/')
1098 return FloorDiv;
1099 else
1100 return Div;
1101 case '%':
1102 return Mod;
1103 case '<':
1104 return LShift;
1105 case '>':
1106 return RShift;
1107 case '&':
1108 return BitAnd;
1109 case '^':
1110 return BitXor;
1111 case '|':
1112 return BitOr;
1113 case '*':
1114 if (STR(n)[1] == '*')
1115 return Pow;
1116 else
1117 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001118 case '@':
1119 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001121 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001122 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 }
1124}
1125
1126static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001127ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001129 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 |'is' 'not'
1131 */
1132 REQ(n, comp_op);
1133 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001134 n = CHILD(n, 0);
1135 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 case LESS:
1137 return Lt;
1138 case GREATER:
1139 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001140 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 return Eq;
1142 case LESSEQUAL:
1143 return LtE;
1144 case GREATEREQUAL:
1145 return GtE;
1146 case NOTEQUAL:
1147 return NotEq;
1148 case NAME:
1149 if (strcmp(STR(n), "in") == 0)
1150 return In;
1151 if (strcmp(STR(n), "is") == 0)
1152 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001153 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001155 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001157 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001158 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 }
1160 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001161 /* handle "not in" and "is not" */
1162 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 case NAME:
1164 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1165 return NotIn;
1166 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1167 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001168 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001170 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001172 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001173 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 }
Neal Norwitz79792652005-11-14 04:25:03 +00001175 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001177 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178}
1179
1180static asdl_seq *
1181seq_for_testlist(struct compiling *c, const node *n)
1182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001184 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1185 */
Armin Rigo31441302005-10-21 12:57:31 +00001186 asdl_seq *seq;
1187 expr_ty expression;
1188 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001189 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001191 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 if (!seq)
1193 return NULL;
1194
1195 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001197 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198
Benjamin Peterson4905e802009-09-27 02:43:28 +00001199 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202
1203 assert(i / 2 < seq->size);
1204 asdl_seq_SET(seq, i / 2, expression);
1205 }
1206 return seq;
1207}
1208
Neal Norwitzc1505362006-12-28 06:47:50 +00001209static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001210ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001211{
1212 identifier name;
1213 expr_ty annotation = NULL;
1214 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001215 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001216
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001217 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001218 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001219 name = NEW_IDENTIFIER(ch);
1220 if (!name)
1221 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001222 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001223 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001224
1225 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1226 annotation = ast_for_expr(c, CHILD(n, 2));
1227 if (!annotation)
1228 return NULL;
1229 }
1230
Victor Stinnerc106c682015-11-06 17:01:48 +01001231 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001232 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001233 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001234 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235}
1236
Guido van Rossum4f72a782006-10-27 23:31:49 +00001237/* returns -1 if failed to handle keyword only arguments
1238 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001239 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001240 ^^^
1241 start pointing here
1242 */
1243static int
1244handle_keywordonly_args(struct compiling *c, const node *n, int start,
1245 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1246{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001247 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001248 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001249 expr_ty expression, annotation;
1250 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001251 int i = start;
1252 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001253
1254 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001255 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001256 return -1;
1257 }
1258 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001259 while (i < NCH(n)) {
1260 ch = CHILD(n, i);
1261 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001262 case vfpdef:
1263 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001264 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001265 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001266 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001267 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001268 asdl_seq_SET(kwdefaults, j, expression);
1269 i += 2; /* '=' and test */
1270 }
1271 else { /* setting NULL if no default value exists */
1272 asdl_seq_SET(kwdefaults, j, NULL);
1273 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001274 if (NCH(ch) == 3) {
1275 /* ch is NAME ':' test */
1276 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001277 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001278 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001279 }
1280 else {
1281 annotation = NULL;
1282 }
1283 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001284 argname = NEW_IDENTIFIER(ch);
1285 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001287 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001288 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001289 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1290 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001291 if (!arg)
1292 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001294 i += 2; /* the name and the comma */
1295 break;
1296 case DOUBLESTAR:
1297 return i;
1298 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001299 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001300 goto error;
1301 }
1302 }
1303 return i;
1304 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307
Jeremy Hyltona8293132006-02-28 17:58:27 +00001308/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309
1310static arguments_ty
1311ast_for_arguments(struct compiling *c, const node *n)
1312{
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 /* This function handles both typedargslist (function definition)
1314 and varargslist (lambda definition).
1315
1316 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001317 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1318 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1319 | '**' tfpdef [',']]]
1320 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1321 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001322 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001323 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1324 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1325 | '**' vfpdef [',']]]
1326 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1327 | '**' vfpdef [',']
1328 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001329 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001330
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1333 int nposdefaults = 0, found_default = 0;
1334 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001335 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 node *ch;
1338
1339 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001340 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001341 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001342 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345
Jeremy Hyltone921e022008-07-17 16:37:17 +00001346 /* First count the number of positional args & defaults. The
1347 variable i is the loop index for this for loop and the next.
1348 The next loop picks up where the first leaves off.
1349 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351 ch = CHILD(n, i);
1352 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001353 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001354 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001355 if (i < NCH(n) && /* skip argument following star */
1356 (TYPE(CHILD(n, i)) == tfpdef ||
1357 TYPE(CHILD(n, i)) == vfpdef)) {
1358 i++;
1359 }
1360 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001362 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001363 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 defaults for keyword only args */
1368 for ( ; i < NCH(n); ++i) {
1369 ch = CHILD(n, i);
1370 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001371 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001373 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001375 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001377 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001379 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001381 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001383 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 since we set NULL as default for keyword only argument w/o default
1386 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001387 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001388 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001390 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001392 /* tfpdef: NAME [':' test]
1393 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 */
1395 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001396 j = 0; /* index for defaults */
1397 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001399 ch = CHILD(n, i);
1400 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001401 case tfpdef:
1402 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1404 anything other than EQUAL or a comma? */
1405 /* XXX Should NCH(n) check be made a separate check? */
1406 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001407 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1408 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001409 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001410 assert(posdefaults != NULL);
1411 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001416 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001418 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001420 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001421 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001422 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001423 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 i += 2; /* the name and the comma */
1425 break;
1426 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001427 if (i+1 >= NCH(n) ||
1428 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001429 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001430 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001431 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001432 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001433 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001434 if (TYPE(ch) == COMMA) {
1435 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001436 i += 2; /* now follows keyword only arguments */
1437 res = handle_keywordonly_args(c, n, i,
1438 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001439 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001440 i = res; /* res has new position to process */
1441 }
1442 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001443 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001444 if (!vararg)
1445 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001446
Guido van Rossum4f72a782006-10-27 23:31:49 +00001447 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001448 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1449 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001450 int res = 0;
1451 res = handle_keywordonly_args(c, n, i,
1452 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001453 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001454 i = res; /* res has new position to process */
1455 }
1456 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 break;
1458 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001459 ch = CHILD(n, i+1); /* tfpdef */
1460 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001461 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001462 if (!kwarg)
1463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 i += 3;
1465 break;
1466 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001467 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 "unexpected node in varargslist: %d @ %d",
1469 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001470 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001471 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001473 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474}
1475
1476static expr_ty
1477ast_for_dotted_name(struct compiling *c, const node *n)
1478{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001479 expr_ty e;
1480 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001481 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 int i;
1483
1484 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001485
1486 lineno = LINENO(n);
1487 col_offset = n->n_col_offset;
1488
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 id = NEW_IDENTIFIER(CHILD(n, 0));
1490 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001491 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001492 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001494 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495
1496 for (i = 2; i < NCH(n); i+=2) {
1497 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001498 if (!id)
1499 return NULL;
1500 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1501 if (!e)
1502 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 }
1504
1505 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506}
1507
1508static expr_ty
1509ast_for_decorator(struct compiling *c, const node *n)
1510{
1511 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1512 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001513 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001516 REQ(CHILD(n, 0), AT);
1517 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1520 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001521 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001524 d = name_expr;
1525 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526 }
1527 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001528 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001530 if (!d)
1531 return NULL;
1532 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 }
1534 else {
Serhiy Storchakab619b092018-11-27 09:40:29 +02001535 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001536 if (!d)
1537 return NULL;
1538 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 }
1540
1541 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542}
1543
1544static asdl_seq*
1545ast_for_decorators(struct compiling *c, const node *n)
1546{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001547 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001548 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001552 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553 if (!decorator_seq)
1554 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001557 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001558 if (!d)
1559 return NULL;
1560 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 }
1562 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563}
1564
1565static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001566ast_for_funcdef_impl(struct compiling *c, const node *n0,
1567 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001569 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
guoci90fc8982018-09-11 17:45:45 -04001570 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001571 identifier name;
1572 arguments_ty args;
1573 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001574 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001575 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576
1577 REQ(n, funcdef);
1578
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 name = NEW_IDENTIFIER(CHILD(n, name_i));
1580 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001581 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001582 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1585 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001586 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001587 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1588 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1589 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001590 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001591 name_i += 2;
1592 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001593 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001595 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596
Yury Selivanov75445082015-05-11 22:57:16 -04001597 if (is_async)
1598 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07001599 LINENO(n0), n0->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001600 else
1601 return FunctionDef(name, args, body, decorator_seq, returns,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001602 LINENO(n), n->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001603}
1604
1605static stmt_ty
1606ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1607{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001608 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001609 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001610 REQ(CHILD(n, 0), NAME);
1611 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001612 REQ(CHILD(n, 1), funcdef);
1613
guoci90fc8982018-09-11 17:45:45 -04001614 return ast_for_funcdef_impl(c, n, decorator_seq,
1615 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001616}
1617
1618static stmt_ty
1619ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1620{
1621 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1622 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001623 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001624}
1625
1626
1627static stmt_ty
1628ast_for_async_stmt(struct compiling *c, const node *n)
1629{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001630 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001631 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001632 REQ(CHILD(n, 0), NAME);
1633 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001634
1635 switch (TYPE(CHILD(n, 1))) {
1636 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001637 return ast_for_funcdef_impl(c, n, NULL,
1638 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001639 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001640 return ast_for_with_stmt(c, n,
1641 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001642
1643 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001644 return ast_for_for_stmt(c, n,
1645 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001646
1647 default:
1648 PyErr_Format(PyExc_SystemError,
1649 "invalid async stament: %s",
1650 STR(CHILD(n, 1)));
1651 return NULL;
1652 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653}
1654
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001655static stmt_ty
1656ast_for_decorated(struct compiling *c, const node *n)
1657{
Yury Selivanov75445082015-05-11 22:57:16 -04001658 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001659 stmt_ty thing = NULL;
1660 asdl_seq *decorator_seq = NULL;
1661
1662 REQ(n, decorated);
1663
1664 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1665 if (!decorator_seq)
1666 return NULL;
1667
1668 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001669 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001671
1672 if (TYPE(CHILD(n, 1)) == funcdef) {
1673 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1674 } else if (TYPE(CHILD(n, 1)) == classdef) {
1675 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001676 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1677 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001678 }
1679 return thing;
1680}
1681
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682static expr_ty
1683ast_for_lambdef(struct compiling *c, const node *n)
1684{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001685 /* lambdef: 'lambda' [varargslist] ':' test
1686 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 arguments_ty args;
1688 expr_ty expression;
1689
1690 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001691 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 if (!args)
1693 return NULL;
1694 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001695 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 }
1698 else {
1699 args = ast_for_arguments(c, CHILD(n, 1));
1700 if (!args)
1701 return NULL;
1702 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001703 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 }
1706
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001707 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708}
1709
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001710static expr_ty
1711ast_for_ifexpr(struct compiling *c, const node *n)
1712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001714 expr_ty expression, body, orelse;
1715
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001716 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001717 body = ast_for_expr(c, CHILD(n, 0));
1718 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001719 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001720 expression = ast_for_expr(c, CHILD(n, 2));
1721 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001722 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001723 orelse = ast_for_expr(c, CHILD(n, 4));
1724 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001725 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001726 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1727 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001728}
1729
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001731 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732
Nick Coghlan650f0d02007-04-15 12:05:43 +00001733 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734*/
1735
1736static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001737count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001739 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740
Guido van Rossumd8faa362007-04-27 19:54:29 +00001741 count_comp_for:
1742 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001743 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001744 if (NCH(n) == 2) {
1745 REQ(CHILD(n, 0), NAME);
1746 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1747 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001748 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001749 else if (NCH(n) == 1) {
1750 n = CHILD(n, 0);
1751 }
1752 else {
1753 goto error;
1754 }
1755 if (NCH(n) == (5)) {
1756 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001757 }
1758 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001759 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001760 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001761 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001762 REQ(n, comp_iter);
1763 n = CHILD(n, 0);
1764 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001765 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001766 else if (TYPE(n) == comp_if) {
1767 if (NCH(n) == 3) {
1768 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001769 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001770 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001771 else
1772 return n_fors;
1773 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001774
Jelle Zijlstraac317702017-10-05 20:24:46 -07001775 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001776 /* Should never be reached */
1777 PyErr_SetString(PyExc_SystemError,
1778 "logic error in count_comp_fors");
1779 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780}
1781
Nick Coghlan650f0d02007-04-15 12:05:43 +00001782/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783
Nick Coghlan650f0d02007-04-15 12:05:43 +00001784 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785*/
1786
1787static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001788count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791
Guido van Rossumd8faa362007-04-27 19:54:29 +00001792 while (1) {
1793 REQ(n, comp_iter);
1794 if (TYPE(CHILD(n, 0)) == comp_for)
1795 return n_ifs;
1796 n = CHILD(n, 0);
1797 REQ(n, comp_if);
1798 n_ifs++;
1799 if (NCH(n) == 2)
1800 return n_ifs;
1801 n = CHILD(n, 2);
1802 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803}
1804
Guido van Rossum992d4a32007-07-11 13:09:30 +00001805static asdl_seq *
1806ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001809 asdl_seq *comps;
1810
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001811 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 if (n_fors == -1)
1813 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001814
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001815 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001816 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001820 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001822 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001823 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001824 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001825 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826
Guido van Rossum992d4a32007-07-11 13:09:30 +00001827 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828
Jelle Zijlstraac317702017-10-05 20:24:46 -07001829 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001830 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001831 REQ(CHILD(n, 0), NAME);
1832 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1833 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001834 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001835 else {
1836 sync_n = CHILD(n, 0);
1837 }
1838 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001839
Jelle Zijlstraac317702017-10-05 20:24:46 -07001840 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001841 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001842 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001844 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001847
Thomas Wouters89f507f2006-12-13 04:49:30 +00001848 /* Check the # of children rather than the length of t, since
1849 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001850 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001851 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001852 comp = comprehension(first, expression, NULL,
1853 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001855 comp = comprehension(Tuple(t, Store, first->lineno,
1856 first->col_offset, c->c_arena),
1857 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001858 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860
Jelle Zijlstraac317702017-10-05 20:24:46 -07001861 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 int j, n_ifs;
1863 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864
Jelle Zijlstraac317702017-10-05 20:24:46 -07001865 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001866 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001867 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001870 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001873
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001875 REQ(n, comp_iter);
1876 n = CHILD(n, 0);
1877 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878
Guido van Rossum992d4a32007-07-11 13:09:30 +00001879 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001880 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001881 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001882 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001883 if (NCH(n) == 3)
1884 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001886 /* on exit, must guarantee that n is a comp_for */
1887 if (TYPE(n) == comp_iter)
1888 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001889 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001891 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001893 return comps;
1894}
1895
1896static expr_ty
1897ast_for_itercomp(struct compiling *c, const node *n, int type)
1898{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001899 /* testlist_comp: (test|star_expr)
1900 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 expr_ty elt;
1902 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001903 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904
Guido van Rossum992d4a32007-07-11 13:09:30 +00001905 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001907 ch = CHILD(n, 0);
1908 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001909 if (!elt)
1910 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001911 if (elt->kind == Starred_kind) {
1912 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1913 return NULL;
1914 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915
Guido van Rossum992d4a32007-07-11 13:09:30 +00001916 comps = ast_for_comprehension(c, CHILD(n, 1));
1917 if (!comps)
1918 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001919
1920 if (type == COMP_GENEXP)
1921 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1922 else if (type == COMP_LISTCOMP)
1923 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1924 else if (type == COMP_SETCOMP)
1925 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1926 else
1927 /* Should never happen */
1928 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929}
1930
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001931/* Fills in the key, value pair corresponding to the dict element. In case
1932 * of an unpacking, key is NULL. *i is advanced by the number of ast
1933 * elements. Iff successful, nonzero is returned.
1934 */
1935static int
1936ast_for_dictelement(struct compiling *c, const node *n, int *i,
1937 expr_ty *key, expr_ty *value)
1938{
1939 expr_ty expression;
1940 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1941 assert(NCH(n) - *i >= 2);
1942
1943 expression = ast_for_expr(c, CHILD(n, *i + 1));
1944 if (!expression)
1945 return 0;
1946 *key = NULL;
1947 *value = expression;
1948
1949 *i += 2;
1950 }
1951 else {
1952 assert(NCH(n) - *i >= 3);
1953
1954 expression = ast_for_expr(c, CHILD(n, *i));
1955 if (!expression)
1956 return 0;
1957 *key = expression;
1958
1959 REQ(CHILD(n, *i + 1), COLON);
1960
1961 expression = ast_for_expr(c, CHILD(n, *i + 2));
1962 if (!expression)
1963 return 0;
1964 *value = expression;
1965
1966 *i += 3;
1967 }
1968 return 1;
1969}
1970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001972ast_for_dictcomp(struct compiling *c, const node *n)
1973{
1974 expr_ty key, value;
1975 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001976 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001978 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001979 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001980 assert(key);
1981 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001983 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001984 if (!comps)
1985 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986
Guido van Rossum992d4a32007-07-11 13:09:30 +00001987 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1988}
1989
1990static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001991ast_for_dictdisplay(struct compiling *c, const node *n)
1992{
1993 int i;
1994 int j;
1995 int size;
1996 asdl_seq *keys, *values;
1997
1998 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1999 keys = _Py_asdl_seq_new(size, c->c_arena);
2000 if (!keys)
2001 return NULL;
2002
2003 values = _Py_asdl_seq_new(size, c->c_arena);
2004 if (!values)
2005 return NULL;
2006
2007 j = 0;
2008 for (i = 0; i < NCH(n); i++) {
2009 expr_ty key, value;
2010
2011 if (!ast_for_dictelement(c, n, &i, &key, &value))
2012 return NULL;
2013 asdl_seq_SET(keys, j, key);
2014 asdl_seq_SET(values, j, value);
2015
2016 j++;
2017 }
2018 keys->size = j;
2019 values->size = j;
2020 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2021}
2022
2023static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002024ast_for_genexp(struct compiling *c, const node *n)
2025{
2026 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002027 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002028}
2029
2030static expr_ty
2031ast_for_listcomp(struct compiling *c, const node *n)
2032{
2033 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002034 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002035}
2036
2037static expr_ty
2038ast_for_setcomp(struct compiling *c, const node *n)
2039{
2040 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002041 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002042}
2043
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002044static expr_ty
2045ast_for_setdisplay(struct compiling *c, const node *n)
2046{
2047 int i;
2048 int size;
2049 asdl_seq *elts;
2050
2051 assert(TYPE(n) == (dictorsetmaker));
2052 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2053 elts = _Py_asdl_seq_new(size, c->c_arena);
2054 if (!elts)
2055 return NULL;
2056 for (i = 0; i < NCH(n); i += 2) {
2057 expr_ty expression;
2058 expression = ast_for_expr(c, CHILD(n, i));
2059 if (!expression)
2060 return NULL;
2061 asdl_seq_SET(elts, i / 2, expression);
2062 }
2063 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2064}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002065
2066static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067ast_for_atom(struct compiling *c, const node *n)
2068{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002069 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2070 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002071 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 */
2073 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002076 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002077 PyObject *name;
2078 const char *s = STR(ch);
2079 size_t len = strlen(s);
2080 if (len >= 4 && len <= 5) {
2081 if (!strcmp(s, "None"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002082 return Constant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002083 if (!strcmp(s, "True"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002084 return Constant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002085 if (!strcmp(s, "False"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002086 return Constant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002087 }
2088 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002089 if (!name)
2090 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002091 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002092 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2093 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002095 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002096 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002097 const char *errtype = NULL;
2098 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2099 errtype = "unicode error";
2100 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2101 errtype = "value error";
2102 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002103 PyObject *type, *value, *tback, *errstr;
2104 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002105 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002106 if (errstr) {
2107 ast_error(c, n, "(%s) %U", errtype, errstr);
2108 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002109 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002110 else {
2111 PyErr_Clear();
2112 ast_error(c, n, "(%s) unknown error", errtype);
2113 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002114 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002115 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002116 Py_XDECREF(tback);
2117 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002118 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002119 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002120 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 }
2122 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002123 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002124 if (!pynum)
2125 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002126
Victor Stinner43d81952013-07-17 00:57:58 +02002127 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2128 Py_DECREF(pynum);
2129 return NULL;
2130 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002131 return Constant(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 }
Georg Brandldde00282007-03-18 19:01:53 +00002133 case ELLIPSIS: /* Ellipsis */
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002134 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002136 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137
Thomas Wouters89f507f2006-12-13 04:49:30 +00002138 if (TYPE(ch) == RPAR)
2139 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140
Thomas Wouters89f507f2006-12-13 04:49:30 +00002141 if (TYPE(ch) == yield_expr)
2142 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002145 if (NCH(ch) == 1) {
2146 return ast_for_testlist(c, ch);
2147 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002148
Serhiy Storchakab619b092018-11-27 09:40:29 +02002149 if (TYPE(CHILD(ch, 1)) == comp_for) {
2150 return copy_location(ast_for_genexp(c, ch), n);
2151 }
2152 else {
2153 return copy_location(ast_for_testlist(c, ch), n);
2154 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002156 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157
Thomas Wouters89f507f2006-12-13 04:49:30 +00002158 if (TYPE(ch) == RSQB)
2159 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160
Nick Coghlan650f0d02007-04-15 12:05:43 +00002161 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002162 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2163 asdl_seq *elts = seq_for_testlist(c, ch);
2164 if (!elts)
2165 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002166
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2168 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002169 else {
2170 return copy_location(ast_for_listcomp(c, ch), n);
2171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002173 /* dictorsetmaker: ( ((test ':' test | '**' test)
2174 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2175 * ((test | '*' test)
2176 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002177 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002178 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002179 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002180 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002181 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002182 }
2183 else {
2184 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2185 if (NCH(ch) == 1 ||
2186 (NCH(ch) > 1 &&
2187 TYPE(CHILD(ch, 1)) == COMMA)) {
2188 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002189 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002190 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191 else if (NCH(ch) > 1 &&
2192 TYPE(CHILD(ch, 1)) == comp_for) {
2193 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002194 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002195 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002196 else if (NCH(ch) > 3 - is_dict &&
2197 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2198 /* It's a dictionary comprehension. */
2199 if (is_dict) {
2200 ast_error(c, n, "dict unpacking cannot be used in "
2201 "dict comprehension");
2202 return NULL;
2203 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002204 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205 }
2206 else {
2207 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002208 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002209 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002210 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002211 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002214 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2215 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 }
2217}
2218
2219static slice_ty
2220ast_for_slice(struct compiling *c, const node *n)
2221{
2222 node *ch;
2223 expr_ty lower = NULL, upper = NULL, step = NULL;
2224
2225 REQ(n, subscript);
2226
2227 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002228 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 sliceop: ':' [test]
2230 */
2231 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 if (NCH(n) == 1 && TYPE(ch) == test) {
2233 /* 'step' variable hold no significance in terms of being used over
2234 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 if (!step)
2237 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238
Thomas Wouters89f507f2006-12-13 04:49:30 +00002239 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 }
2241
2242 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002243 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 if (!lower)
2245 return NULL;
2246 }
2247
2248 /* If there's an upper bound it's in the second or third position. */
2249 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002250 if (NCH(n) > 1) {
2251 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252
Thomas Wouters89f507f2006-12-13 04:49:30 +00002253 if (TYPE(n2) == test) {
2254 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 if (!upper)
2256 return NULL;
2257 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002258 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002260 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261
Thomas Wouters89f507f2006-12-13 04:49:30 +00002262 if (TYPE(n2) == test) {
2263 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 if (!upper)
2265 return NULL;
2266 }
2267 }
2268
2269 ch = CHILD(n, NCH(n) - 1);
2270 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002271 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002272 ch = CHILD(ch, 1);
2273 if (TYPE(ch) == test) {
2274 step = ast_for_expr(c, ch);
2275 if (!step)
2276 return NULL;
2277 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 }
2279 }
2280
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002281 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282}
2283
2284static expr_ty
2285ast_for_binop(struct compiling *c, const node *n)
2286{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002287 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002289 BinOp(BinOp(A, op, B), op, C).
2290 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291
Guido van Rossumd8faa362007-04-27 19:54:29 +00002292 int i, nops;
2293 expr_ty expr1, expr2, result;
2294 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295
Guido van Rossumd8faa362007-04-27 19:54:29 +00002296 expr1 = ast_for_expr(c, CHILD(n, 0));
2297 if (!expr1)
2298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299
Guido van Rossumd8faa362007-04-27 19:54:29 +00002300 expr2 = ast_for_expr(c, CHILD(n, 2));
2301 if (!expr2)
2302 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303
Guido van Rossumd8faa362007-04-27 19:54:29 +00002304 newoperator = get_operator(CHILD(n, 1));
2305 if (!newoperator)
2306 return NULL;
2307
2308 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2309 c->c_arena);
2310 if (!result)
2311 return NULL;
2312
2313 nops = (NCH(n) - 1) / 2;
2314 for (i = 1; i < nops; i++) {
2315 expr_ty tmp_result, tmp;
2316 const node* next_oper = CHILD(n, i * 2 + 1);
2317
2318 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002319 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 return NULL;
2321
Guido van Rossumd8faa362007-04-27 19:54:29 +00002322 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2323 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 return NULL;
2325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002327 LINENO(next_oper), next_oper->n_col_offset,
2328 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002330 return NULL;
2331 result = tmp_result;
2332 }
2333 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334}
2335
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002336static expr_ty
2337ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002340 subscriptlist: subscript (',' subscript)* [',']
2341 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2342 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002343 REQ(n, trailer);
2344 if (TYPE(CHILD(n, 0)) == LPAR) {
2345 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002346 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002347 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002348 else
Serhiy Storchakab619b092018-11-27 09:40:29 +02002349 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002350 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002351 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002352 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2353 if (!attr_id)
2354 return NULL;
2355 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002356 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002357 }
2358 else {
2359 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002360 REQ(CHILD(n, 2), RSQB);
2361 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002362 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002363 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2364 if (!slc)
2365 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002366 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2367 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002368 }
2369 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002371 by treating the sequence as a tuple literal if there are
2372 no slice features.
2373 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002374 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002375 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002376 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002377 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002378 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002379 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002380 if (!slices)
2381 return NULL;
2382 for (j = 0; j < NCH(n); j += 2) {
2383 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002384 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002385 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002386 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002387 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002388 asdl_seq_SET(slices, j / 2, slc);
2389 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002390 if (!simple) {
2391 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002392 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002393 }
2394 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002395 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002396 if (!elts)
2397 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002398 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2399 slc = (slice_ty)asdl_seq_GET(slices, j);
2400 assert(slc->kind == Index_kind && slc->v.Index.value);
2401 asdl_seq_SET(elts, j, slc->v.Index.value);
2402 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002403 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002404 if (!e)
2405 return NULL;
2406 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002407 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002408 }
2409 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002410}
2411
2412static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002413ast_for_factor(struct compiling *c, const node *n)
2414{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002415 expr_ty expression;
2416
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002417 expression = ast_for_expr(c, CHILD(n, 1));
2418 if (!expression)
2419 return NULL;
2420
2421 switch (TYPE(CHILD(n, 0))) {
2422 case PLUS:
2423 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2424 c->c_arena);
2425 case MINUS:
2426 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2427 c->c_arena);
2428 case TILDE:
2429 return UnaryOp(Invert, expression, LINENO(n),
2430 n->n_col_offset, c->c_arena);
2431 }
2432 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2433 TYPE(CHILD(n, 0)));
2434 return NULL;
2435}
2436
2437static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002438ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002439{
Yury Selivanov75445082015-05-11 22:57:16 -04002440 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002441 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002442
2443 REQ(n, atom_expr);
2444 nch = NCH(n);
2445
Jelle Zijlstraac317702017-10-05 20:24:46 -07002446 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002447 start = 1;
2448 assert(nch > 1);
2449 }
2450
2451 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002452 if (!e)
2453 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002454 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002455 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002456 if (start && nch == 2) {
2457 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2458 }
2459
2460 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002461 node *ch = CHILD(n, i);
2462 if (TYPE(ch) != trailer)
2463 break;
2464 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002465 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002466 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002467 tmp->lineno = e->lineno;
2468 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002469 e = tmp;
2470 }
Yury Selivanov75445082015-05-11 22:57:16 -04002471
2472 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002473 /* there was an 'await' */
Yury Selivanov75445082015-05-11 22:57:16 -04002474 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2475 }
2476 else {
2477 return e;
2478 }
2479}
2480
2481static expr_ty
2482ast_for_power(struct compiling *c, const node *n)
2483{
2484 /* power: atom trailer* ('**' factor)*
2485 */
2486 expr_ty e;
2487 REQ(n, power);
2488 e = ast_for_atom_expr(c, CHILD(n, 0));
2489 if (!e)
2490 return NULL;
2491 if (NCH(n) == 1)
2492 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002493 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2494 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002495 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002496 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002497 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002498 }
2499 return e;
2500}
2501
Guido van Rossum0368b722007-05-11 16:50:42 +00002502static expr_ty
2503ast_for_starred(struct compiling *c, const node *n)
2504{
2505 expr_ty tmp;
2506 REQ(n, star_expr);
2507
2508 tmp = ast_for_expr(c, CHILD(n, 1));
2509 if (!tmp)
2510 return NULL;
2511
2512 /* The Load context is changed later. */
2513 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2514}
2515
2516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517/* Do not name a variable 'expr'! Will cause a compile error.
2518*/
2519
2520static expr_ty
2521ast_for_expr(struct compiling *c, const node *n)
2522{
2523 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002524 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002525 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 and_test: not_test ('and' not_test)*
2528 not_test: 'not' not_test | comparison
2529 comparison: expr (comp_op expr)*
2530 expr: xor_expr ('|' xor_expr)*
2531 xor_expr: and_expr ('^' and_expr)*
2532 and_expr: shift_expr ('&' shift_expr)*
2533 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2534 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002535 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002537 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002538 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002539 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 */
2541
2542 asdl_seq *seq;
2543 int i;
2544
2545 loop:
2546 switch (TYPE(n)) {
2547 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002548 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002549 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002550 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002552 else if (NCH(n) > 1)
2553 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002554 /* Fallthrough */
2555 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 case and_test:
2557 if (NCH(n) == 1) {
2558 n = CHILD(n, 0);
2559 goto loop;
2560 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002561 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 if (!seq)
2563 return NULL;
2564 for (i = 0; i < NCH(n); i += 2) {
2565 expr_ty e = ast_for_expr(c, CHILD(n, i));
2566 if (!e)
2567 return NULL;
2568 asdl_seq_SET(seq, i / 2, e);
2569 }
2570 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002571 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2572 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002573 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002574 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 case not_test:
2576 if (NCH(n) == 1) {
2577 n = CHILD(n, 0);
2578 goto loop;
2579 }
2580 else {
2581 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2582 if (!expression)
2583 return NULL;
2584
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002585 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2586 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 }
2588 case comparison:
2589 if (NCH(n) == 1) {
2590 n = CHILD(n, 0);
2591 goto loop;
2592 }
2593 else {
2594 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002595 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002596 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002597 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 if (!ops)
2599 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002600 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 return NULL;
2603 }
2604 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002605 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002607 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002608 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002610 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611
2612 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002613 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002615 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002617 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618 asdl_seq_SET(cmps, i / 2, expression);
2619 }
2620 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002621 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002623 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002625 return Compare(expression, ops, cmps, LINENO(n),
2626 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 }
2628 break;
2629
Guido van Rossum0368b722007-05-11 16:50:42 +00002630 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 /* The next five cases all handle BinOps. The main body of code
2633 is the same in each case, but the switch turned inside out to
2634 reuse the code for each type of operator.
2635 */
2636 case expr:
2637 case xor_expr:
2638 case and_expr:
2639 case shift_expr:
2640 case arith_expr:
2641 case term:
2642 if (NCH(n) == 1) {
2643 n = CHILD(n, 0);
2644 goto loop;
2645 }
2646 return ast_for_binop(c, n);
2647 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002648 node *an = NULL;
2649 node *en = NULL;
2650 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002651 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002652 if (NCH(n) > 1)
2653 an = CHILD(n, 1); /* yield_arg */
2654 if (an) {
2655 en = CHILD(an, NCH(an) - 1);
2656 if (NCH(an) == 2) {
2657 is_from = 1;
2658 exp = ast_for_expr(c, en);
2659 }
2660 else
2661 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002662 if (!exp)
2663 return NULL;
2664 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002665 if (is_from)
2666 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2667 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002668 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002669 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 if (NCH(n) == 1) {
2671 n = CHILD(n, 0);
2672 goto loop;
2673 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002674 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002675 case power:
2676 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002678 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 return NULL;
2680 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002681 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 return NULL;
2683}
2684
2685static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002686ast_for_call(struct compiling *c, const node *n, expr_ty func,
2687 const node *maybegenbeg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688{
2689 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002690 arglist: argument (',' argument)* [',']
2691 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 */
2693
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002694 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002695 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002696 asdl_seq *args;
2697 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698
2699 REQ(n, arglist);
2700
2701 nargs = 0;
2702 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002704 node *ch = CHILD(n, i);
2705 if (TYPE(ch) == argument) {
2706 if (NCH(ch) == 1)
2707 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002708 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2709 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002710 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002711 ast_error(c, ch, "invalid syntax");
2712 return NULL;
2713 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002714 if (NCH(n) > 1) {
2715 ast_error(c, ch, "Generator expression must be parenthesized");
2716 return NULL;
2717 }
2718 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002719 else if (TYPE(CHILD(ch, 0)) == STAR)
2720 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002722 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002723 nkeywords++;
2724 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002727 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002729 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002730 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002732 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002733
2734 nargs = 0; /* positional arguments + iterable argument unpackings */
2735 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2736 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002738 node *ch = CHILD(n, i);
2739 if (TYPE(ch) == argument) {
2740 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002741 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002742 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002743 /* a positional argument */
2744 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002745 if (ndoublestars) {
2746 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002747 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002748 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002749 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002750 else {
2751 ast_error(c, chch,
2752 "positional argument follows "
2753 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002754 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002755 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002756 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002757 e = ast_for_expr(c, chch);
2758 if (!e)
2759 return NULL;
2760 asdl_seq_SET(args, nargs++, e);
2761 }
2762 else if (TYPE(chch) == STAR) {
2763 /* an iterable argument unpacking */
2764 expr_ty starred;
2765 if (ndoublestars) {
2766 ast_error(c, chch,
2767 "iterable argument unpacking follows "
2768 "keyword argument unpacking");
2769 return NULL;
2770 }
2771 e = ast_for_expr(c, CHILD(ch, 1));
2772 if (!e)
2773 return NULL;
2774 starred = Starred(e, Load, LINENO(chch),
2775 chch->n_col_offset,
2776 c->c_arena);
2777 if (!starred)
2778 return NULL;
2779 asdl_seq_SET(args, nargs++, starred);
2780
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002781 }
2782 else if (TYPE(chch) == DOUBLESTAR) {
2783 /* a keyword argument unpacking */
2784 keyword_ty kw;
2785 i++;
2786 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002788 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002789 kw = keyword(NULL, e, c->c_arena);
2790 asdl_seq_SET(keywords, nkeywords++, kw);
2791 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002793 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002794 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002795 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002797 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002798 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002800 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002801 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002802 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002803 identifier key, tmp;
2804 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002806 // To remain LL(1), the grammar accepts any test (basically, any
2807 // expression) in the keyword slot of a call site. So, we need
2808 // to manually enforce that the keyword is a NAME here.
2809 static const int name_tree[] = {
2810 test,
2811 or_test,
2812 and_test,
2813 not_test,
2814 comparison,
2815 expr,
2816 xor_expr,
2817 and_expr,
2818 shift_expr,
2819 arith_expr,
2820 term,
2821 factor,
2822 power,
2823 atom_expr,
2824 atom,
2825 0,
2826 };
2827 node *expr_node = chch;
2828 for (int i = 0; name_tree[i]; i++) {
2829 if (TYPE(expr_node) != name_tree[i])
2830 break;
2831 if (NCH(expr_node) != 1)
2832 break;
2833 expr_node = CHILD(expr_node, 0);
2834 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002835 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002836 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002837 "expression cannot contain assignment, "
2838 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002839 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002840 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002841 key = new_identifier(STR(expr_node), c);
2842 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002843 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002845 if (forbidden_name(c, key, chch, 1)) {
2846 return NULL;
2847 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002848 for (k = 0; k < nkeywords; k++) {
2849 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002850 if (tmp && !PyUnicode_Compare(tmp, key)) {
2851 ast_error(c, chch,
2852 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002853 return NULL;
2854 }
2855 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002856 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002858 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002861 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002862 asdl_seq_SET(keywords, nkeywords++, kw);
2863 }
2864 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 }
2866
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002867 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868}
2869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002871ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002873 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002874 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002876 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002877 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002878 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002879 }
2880 else {
2881 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002882 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002883 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002885 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 else {
2887 asdl_seq *tmp = seq_for_testlist(c, n);
2888 if (!tmp)
2889 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002890 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002892}
2893
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894static stmt_ty
2895ast_for_expr_stmt(struct compiling *c, const node *n)
2896{
2897 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002898 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2899 ('=' (yield_expr|testlist_star_expr))*)
2900 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002901 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002902 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002903 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002904 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 */
2906
2907 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002908 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 if (!e)
2910 return NULL;
2911
Thomas Wouters89f507f2006-12-13 04:49:30 +00002912 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 }
2914 else if (TYPE(CHILD(n, 1)) == augassign) {
2915 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002916 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002917 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 if (!expr1)
2921 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002922 if(!set_context(c, expr1, Store, ch))
2923 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002924 /* set_context checks that most expressions are not the left side.
2925 Augmented assignments can only have a name, a subscript, or an
2926 attribute on the left, though, so we have to explicitly check for
2927 those. */
2928 switch (expr1->kind) {
2929 case Name_kind:
2930 case Attribute_kind:
2931 case Subscript_kind:
2932 break;
2933 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002934 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002935 return NULL;
2936 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937
Thomas Wouters89f507f2006-12-13 04:49:30 +00002938 ch = CHILD(n, 2);
2939 if (TYPE(ch) == testlist)
2940 expr2 = ast_for_testlist(c, ch);
2941 else
2942 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002943 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 return NULL;
2945
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002946 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002947 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 return NULL;
2949
Thomas Wouters89f507f2006-12-13 04:49:30 +00002950 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002952 else if (TYPE(CHILD(n, 1)) == annassign) {
2953 expr_ty expr1, expr2, expr3;
2954 node *ch = CHILD(n, 0);
2955 node *deep, *ann = CHILD(n, 1);
2956 int simple = 1;
2957
2958 /* we keep track of parens to qualify (x) as expression not name */
2959 deep = ch;
2960 while (NCH(deep) == 1) {
2961 deep = CHILD(deep, 0);
2962 }
2963 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2964 simple = 0;
2965 }
2966 expr1 = ast_for_testlist(c, ch);
2967 if (!expr1) {
2968 return NULL;
2969 }
2970 switch (expr1->kind) {
2971 case Name_kind:
2972 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2973 return NULL;
2974 }
2975 expr1->v.Name.ctx = Store;
2976 break;
2977 case Attribute_kind:
2978 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2979 return NULL;
2980 }
2981 expr1->v.Attribute.ctx = Store;
2982 break;
2983 case Subscript_kind:
2984 expr1->v.Subscript.ctx = Store;
2985 break;
2986 case List_kind:
2987 ast_error(c, ch,
2988 "only single target (not list) can be annotated");
2989 return NULL;
2990 case Tuple_kind:
2991 ast_error(c, ch,
2992 "only single target (not tuple) can be annotated");
2993 return NULL;
2994 default:
2995 ast_error(c, ch,
2996 "illegal target for annotation");
2997 return NULL;
2998 }
2999
3000 if (expr1->kind != Name_kind) {
3001 simple = 0;
3002 }
3003 ch = CHILD(ann, 1);
3004 expr2 = ast_for_expr(c, ch);
3005 if (!expr2) {
3006 return NULL;
3007 }
3008 if (NCH(ann) == 2) {
3009 return AnnAssign(expr1, expr2, NULL, simple,
3010 LINENO(n), n->n_col_offset, c->c_arena);
3011 }
3012 else {
3013 ch = CHILD(ann, 3);
3014 expr3 = ast_for_expr(c, ch);
3015 if (!expr3) {
3016 return NULL;
3017 }
3018 return AnnAssign(expr1, expr2, expr3, simple,
3019 LINENO(n), n->n_col_offset, c->c_arena);
3020 }
3021 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003023 int i;
3024 asdl_seq *targets;
3025 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 expr_ty expression;
3027
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 /* a normal assignment */
3029 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003030 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003031 if (!targets)
3032 return NULL;
3033 for (i = 0; i < NCH(n) - 2; i += 2) {
3034 expr_ty e;
3035 node *ch = CHILD(n, i);
3036 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003037 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003038 return NULL;
3039 }
3040 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003044 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003045 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003046 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047
Thomas Wouters89f507f2006-12-13 04:49:30 +00003048 asdl_seq_SET(targets, i / 2, e);
3049 }
3050 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003051 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003052 expression = ast_for_testlist(c, value);
3053 else
3054 expression = ast_for_expr(c, value);
3055 if (!expression)
3056 return NULL;
3057 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059}
3060
Benjamin Peterson78565b22009-06-28 19:19:51 +00003061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003063ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064{
3065 asdl_seq *seq;
3066 int i;
3067 expr_ty e;
3068
3069 REQ(n, exprlist);
3070
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003071 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003075 e = ast_for_expr(c, CHILD(n, i));
3076 if (!e)
3077 return NULL;
3078 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003079 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003080 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081 }
3082 return seq;
3083}
3084
3085static stmt_ty
3086ast_for_del_stmt(struct compiling *c, const node *n)
3087{
3088 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090 /* del_stmt: 'del' exprlist */
3091 REQ(n, del_stmt);
3092
3093 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3094 if (!expr_list)
3095 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003096 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097}
3098
3099static stmt_ty
3100ast_for_flow_stmt(struct compiling *c, const node *n)
3101{
3102 /*
3103 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3104 | yield_stmt
3105 break_stmt: 'break'
3106 continue_stmt: 'continue'
3107 return_stmt: 'return' [testlist]
3108 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003109 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 raise_stmt: 'raise' [test [',' test [',' test]]]
3111 */
3112 node *ch;
3113
3114 REQ(n, flow_stmt);
3115 ch = CHILD(n, 0);
3116 switch (TYPE(ch)) {
3117 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003118 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003120 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003122 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3123 if (!exp)
3124 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003125 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 }
3127 case return_stmt:
3128 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003129 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003131 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 if (!expression)
3133 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003134 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 }
3136 case raise_stmt:
3137 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003138 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3139 else if (NCH(ch) >= 2) {
3140 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3142 if (!expression)
3143 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003144 if (NCH(ch) == 4) {
3145 cause = ast_for_expr(c, CHILD(ch, 3));
3146 if (!cause)
3147 return NULL;
3148 }
3149 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 }
Stefan Krahf432a322017-08-21 13:09:59 +02003151 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003153 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 "unexpected flow_stmt: %d", TYPE(ch));
3155 return NULL;
3156 }
3157}
3158
3159static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003160alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161{
3162 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003163 import_as_name: NAME ['as' NAME]
3164 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 dotted_name: NAME ('.' NAME)*
3166 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003167 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003168
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 loop:
3170 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003171 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003173 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003174 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003175 if (!name)
3176 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003177 if (NCH(n) == 3) {
3178 node *str_node = CHILD(n, 2);
3179 str = NEW_IDENTIFIER(str_node);
3180 if (!str)
3181 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003182 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003183 return NULL;
3184 }
3185 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003186 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003187 return NULL;
3188 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003189 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003190 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 case dotted_as_name:
3192 if (NCH(n) == 1) {
3193 n = CHILD(n, 0);
3194 goto loop;
3195 }
3196 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003197 node *asname_node = CHILD(n, 2);
3198 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003199 if (!a)
3200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003202 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003203 if (!a->asname)
3204 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003205 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 return a;
3208 }
3209 break;
3210 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003211 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003212 node *name_node = CHILD(n, 0);
3213 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003214 if (!name)
3215 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003216 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003217 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003218 return alias(name, NULL, c->c_arena);
3219 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 else {
3221 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003222 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003223 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226
3227 len = 0;
3228 for (i = 0; i < NCH(n); i += 2)
3229 /* length of string plus one for the dot */
3230 len += strlen(STR(CHILD(n, i))) + 1;
3231 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003232 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 if (!str)
3234 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003235 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 if (!s)
3237 return NULL;
3238 for (i = 0; i < NCH(n); i += 2) {
3239 char *sch = STR(CHILD(n, i));
3240 strcpy(s, STR(CHILD(n, i)));
3241 s += strlen(sch);
3242 *s++ = '.';
3243 }
3244 --s;
3245 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3247 PyBytes_GET_SIZE(str),
3248 NULL);
3249 Py_DECREF(str);
3250 if (!uni)
3251 return NULL;
3252 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003253 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003254 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3255 Py_DECREF(str);
3256 return NULL;
3257 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003258 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 }
3260 break;
3261 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003262 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003263 if (!str)
3264 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003265 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3266 Py_DECREF(str);
3267 return NULL;
3268 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003269 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003271 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 "unexpected import name: %d", TYPE(n));
3273 return NULL;
3274 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003275
3276 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 return NULL;
3278}
3279
3280static stmt_ty
3281ast_for_import_stmt(struct compiling *c, const node *n)
3282{
3283 /*
3284 import_stmt: import_name | import_from
3285 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003286 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3287 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003289 int lineno;
3290 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291 int i;
3292 asdl_seq *aliases;
3293
3294 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003295 lineno = LINENO(n);
3296 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003298 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003300 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003301 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003302 if (!aliases)
3303 return NULL;
3304 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003305 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003306 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003312 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 int idx, ndots = 0;
3315 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003316 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003318 /* Count the number of dots (for relative imports) and check for the
3319 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003320 for (idx = 1; idx < NCH(n); idx++) {
3321 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003322 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3323 if (!mod)
3324 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 idx++;
3326 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003327 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003329 ndots += 3;
3330 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003331 } else if (TYPE(CHILD(n, idx)) != DOT) {
3332 break;
3333 }
3334 ndots++;
3335 }
3336 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003337 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003338 case STAR:
3339 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 n = CHILD(n, idx);
3341 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003342 break;
3343 case LPAR:
3344 /* from ... import (x, y, z) */
3345 n = CHILD(n, idx + 1);
3346 n_children = NCH(n);
3347 break;
3348 case import_as_names:
3349 /* from ... import x, y, z */
3350 n = CHILD(n, idx);
3351 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003352 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003353 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 " surrounding parentheses");
3355 return NULL;
3356 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003357 break;
3358 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003359 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003360 return NULL;
3361 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003363 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003364 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366
3367 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003368 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003369 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003370 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003372 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003374 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003375 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003376 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003377 if (!import_alias)
3378 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003379 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003380 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003382 if (mod != NULL)
3383 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003384 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003385 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 }
Neal Norwitz79792652005-11-14 04:25:03 +00003387 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 "unknown import statement: starts with command '%s'",
3389 STR(CHILD(n, 0)));
3390 return NULL;
3391}
3392
3393static stmt_ty
3394ast_for_global_stmt(struct compiling *c, const node *n)
3395{
3396 /* global_stmt: 'global' NAME (',' NAME)* */
3397 identifier name;
3398 asdl_seq *s;
3399 int i;
3400
3401 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003402 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003404 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003406 name = NEW_IDENTIFIER(CHILD(n, i));
3407 if (!name)
3408 return NULL;
3409 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003411 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412}
3413
3414static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003415ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3416{
3417 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3418 identifier name;
3419 asdl_seq *s;
3420 int i;
3421
3422 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003423 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003424 if (!s)
3425 return NULL;
3426 for (i = 1; i < NCH(n); i += 2) {
3427 name = NEW_IDENTIFIER(CHILD(n, i));
3428 if (!name)
3429 return NULL;
3430 asdl_seq_SET(s, i / 2, name);
3431 }
3432 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3433}
3434
3435static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436ast_for_assert_stmt(struct compiling *c, const node *n)
3437{
3438 /* assert_stmt: 'assert' test [',' test] */
3439 REQ(n, assert_stmt);
3440 if (NCH(n) == 2) {
3441 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3442 if (!expression)
3443 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003444 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445 }
3446 else if (NCH(n) == 4) {
3447 expr_ty expr1, expr2;
3448
3449 expr1 = ast_for_expr(c, CHILD(n, 1));
3450 if (!expr1)
3451 return NULL;
3452 expr2 = ast_for_expr(c, CHILD(n, 3));
3453 if (!expr2)
3454 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455
Thomas Wouters89f507f2006-12-13 04:49:30 +00003456 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 }
Neal Norwitz79792652005-11-14 04:25:03 +00003458 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 "improper number of parts to 'assert' statement: %d",
3460 NCH(n));
3461 return NULL;
3462}
3463
3464static asdl_seq *
3465ast_for_suite(struct compiling *c, const node *n)
3466{
3467 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003468 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 stmt_ty s;
3470 int i, total, num, end, pos = 0;
3471 node *ch;
3472
3473 REQ(n, suite);
3474
3475 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003476 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 n = CHILD(n, 0);
3481 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003483 */
3484 end = NCH(n) - 1;
3485 if (TYPE(CHILD(n, end - 1)) == SEMI)
3486 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003488 for (i = 0; i < end; i += 2) {
3489 ch = CHILD(n, i);
3490 s = ast_for_stmt(c, ch);
3491 if (!s)
3492 return NULL;
3493 asdl_seq_SET(seq, pos++, s);
3494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 }
3496 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003497 for (i = 2; i < (NCH(n) - 1); i++) {
3498 ch = CHILD(n, i);
3499 REQ(ch, stmt);
3500 num = num_stmts(ch);
3501 if (num == 1) {
3502 /* small_stmt or compound_stmt with only one child */
3503 s = ast_for_stmt(c, ch);
3504 if (!s)
3505 return NULL;
3506 asdl_seq_SET(seq, pos++, s);
3507 }
3508 else {
3509 int j;
3510 ch = CHILD(ch, 0);
3511 REQ(ch, simple_stmt);
3512 for (j = 0; j < NCH(ch); j += 2) {
3513 /* statement terminates with a semi-colon ';' */
3514 if (NCH(CHILD(ch, j)) == 0) {
3515 assert((j + 1) == NCH(ch));
3516 break;
3517 }
3518 s = ast_for_stmt(c, CHILD(ch, j));
3519 if (!s)
3520 return NULL;
3521 asdl_seq_SET(seq, pos++, s);
3522 }
3523 }
3524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 }
3526 assert(pos == seq->size);
3527 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528}
3529
3530static stmt_ty
3531ast_for_if_stmt(struct compiling *c, const node *n)
3532{
3533 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3534 ['else' ':' suite]
3535 */
3536 char *s;
3537
3538 REQ(n, if_stmt);
3539
3540 if (NCH(n) == 4) {
3541 expr_ty expression;
3542 asdl_seq *suite_seq;
3543
3544 expression = ast_for_expr(c, CHILD(n, 1));
3545 if (!expression)
3546 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003548 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550
Guido van Rossumd8faa362007-04-27 19:54:29 +00003551 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3552 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 s = STR(CHILD(n, 4));
3556 /* s[2], the third character in the string, will be
3557 's' for el_s_e, or
3558 'i' for el_i_f
3559 */
3560 if (s[2] == 's') {
3561 expr_ty expression;
3562 asdl_seq *seq1, *seq2;
3563
3564 expression = ast_for_expr(c, CHILD(n, 1));
3565 if (!expression)
3566 return NULL;
3567 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003568 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003569 return NULL;
3570 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003571 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572 return NULL;
3573
Guido van Rossumd8faa362007-04-27 19:54:29 +00003574 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3575 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 }
3577 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003578 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003579 expr_ty expression;
3580 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003581 asdl_seq *orelse = NULL;
3582 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583 /* must reference the child n_elif+1 since 'else' token is third,
3584 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003585 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3586 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3587 has_else = 1;
3588 n_elif -= 3;
3589 }
3590 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591
Thomas Wouters89f507f2006-12-13 04:49:30 +00003592 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003593 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003595 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003596 if (!orelse)
3597 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003599 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003601 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3602 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003604 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3605 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 asdl_seq_SET(orelse, 0,
3609 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003610 LINENO(CHILD(n, NCH(n) - 6)),
3611 CHILD(n, NCH(n) - 6)->n_col_offset,
3612 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003613 /* the just-created orelse handled the last elif */
3614 n_elif--;
3615 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616
Thomas Wouters89f507f2006-12-13 04:49:30 +00003617 for (i = 0; i < n_elif; i++) {
3618 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003619 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003620 if (!newobj)
3621 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003623 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003626 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628
Thomas Wouters89f507f2006-12-13 04:49:30 +00003629 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003631 LINENO(CHILD(n, off)),
3632 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003633 orelse = newobj;
3634 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003635 expression = ast_for_expr(c, CHILD(n, 1));
3636 if (!expression)
3637 return NULL;
3638 suite_seq = ast_for_suite(c, CHILD(n, 3));
3639 if (!suite_seq)
3640 return NULL;
3641 return If(expression, suite_seq, orelse,
3642 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003644
3645 PyErr_Format(PyExc_SystemError,
3646 "unexpected token in 'if' statement: %s", s);
3647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648}
3649
3650static stmt_ty
3651ast_for_while_stmt(struct compiling *c, const node *n)
3652{
3653 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3654 REQ(n, while_stmt);
3655
3656 if (NCH(n) == 4) {
3657 expr_ty expression;
3658 asdl_seq *suite_seq;
3659
3660 expression = ast_for_expr(c, CHILD(n, 1));
3661 if (!expression)
3662 return NULL;
3663 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003664 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003666 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 }
3668 else if (NCH(n) == 7) {
3669 expr_ty expression;
3670 asdl_seq *seq1, *seq2;
3671
3672 expression = ast_for_expr(c, CHILD(n, 1));
3673 if (!expression)
3674 return NULL;
3675 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003676 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677 return NULL;
3678 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003679 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003680 return NULL;
3681
Thomas Wouters89f507f2006-12-13 04:49:30 +00003682 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003684
3685 PyErr_Format(PyExc_SystemError,
3686 "wrong number of tokens for 'while' statement: %d",
3687 NCH(n));
3688 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689}
3690
3691static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003692ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693{
guoci90fc8982018-09-11 17:45:45 -04003694 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003695 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003697 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003698 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3700 REQ(n, for_stmt);
3701
3702 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003703 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 if (!seq)
3705 return NULL;
3706 }
3707
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003708 node_target = CHILD(n, 1);
3709 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003710 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003712 /* Check the # of children rather than the length of _target, since
3713 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003714 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003715 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003716 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003718 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003720 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003721 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 return NULL;
3723 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003724 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 return NULL;
3726
Yury Selivanov75445082015-05-11 22:57:16 -04003727 if (is_async)
3728 return AsyncFor(target, expression, suite_seq, seq,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003729 LINENO(n0), n0->n_col_offset,
Yury Selivanov75445082015-05-11 22:57:16 -04003730 c->c_arena);
3731 else
3732 return For(target, expression, suite_seq, seq,
3733 LINENO(n), n->n_col_offset,
3734 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735}
3736
3737static excepthandler_ty
3738ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3739{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003740 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 REQ(exc, except_clause);
3742 REQ(body, suite);
3743
3744 if (NCH(exc) == 1) {
3745 asdl_seq *suite_seq = ast_for_suite(c, body);
3746 if (!suite_seq)
3747 return NULL;
3748
Neal Norwitzad74aa82008-03-31 05:14:30 +00003749 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003750 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 }
3752 else if (NCH(exc) == 2) {
3753 expr_ty expression;
3754 asdl_seq *suite_seq;
3755
3756 expression = ast_for_expr(c, CHILD(exc, 1));
3757 if (!expression)
3758 return NULL;
3759 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003760 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761 return NULL;
3762
Neal Norwitzad74aa82008-03-31 05:14:30 +00003763 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003764 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 }
3766 else if (NCH(exc) == 4) {
3767 asdl_seq *suite_seq;
3768 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003769 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003770 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003772 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003773 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003775 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 return NULL;
3777 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003778 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779 return NULL;
3780
Neal Norwitzad74aa82008-03-31 05:14:30 +00003781 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003782 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003784
3785 PyErr_Format(PyExc_SystemError,
3786 "wrong number of children for 'except' clause: %d",
3787 NCH(exc));
3788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789}
3790
3791static stmt_ty
3792ast_for_try_stmt(struct compiling *c, const node *n)
3793{
Neal Norwitzf599f422005-12-17 21:33:47 +00003794 const int nch = NCH(n);
3795 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003796 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003797
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 REQ(n, try_stmt);
3799
Neal Norwitzf599f422005-12-17 21:33:47 +00003800 body = ast_for_suite(c, CHILD(n, 2));
3801 if (body == NULL)
3802 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803
Neal Norwitzf599f422005-12-17 21:33:47 +00003804 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3805 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3806 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3807 /* we can assume it's an "else",
3808 because nch >= 9 for try-else-finally and
3809 it would otherwise have a type of except_clause */
3810 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3811 if (orelse == NULL)
3812 return NULL;
3813 n_except--;
3814 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815
Neal Norwitzf599f422005-12-17 21:33:47 +00003816 finally = ast_for_suite(c, CHILD(n, nch - 1));
3817 if (finally == NULL)
3818 return NULL;
3819 n_except--;
3820 }
3821 else {
3822 /* we can assume it's an "else",
3823 otherwise it would have a type of except_clause */
3824 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3825 if (orelse == NULL)
3826 return NULL;
3827 n_except--;
3828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003830 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003831 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832 return NULL;
3833 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834
Neal Norwitzf599f422005-12-17 21:33:47 +00003835 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003836 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003837 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003838 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003839 if (handlers == NULL)
3840 return NULL;
3841
3842 for (i = 0; i < n_except; i++) {
3843 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3844 CHILD(n, 5 + i * 3));
3845 if (!e)
3846 return NULL;
3847 asdl_seq_SET(handlers, i, e);
3848 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003849 }
3850
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003851 assert(finally != NULL || asdl_seq_LEN(handlers));
3852 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853}
3854
Georg Brandl0c315622009-05-25 21:10:36 +00003855/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003856static withitem_ty
3857ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003858{
3859 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003860
Georg Brandl0c315622009-05-25 21:10:36 +00003861 REQ(n, with_item);
3862 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003863 if (!context_expr)
3864 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003865 if (NCH(n) == 3) {
3866 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003867
3868 if (!optional_vars) {
3869 return NULL;
3870 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003871 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003872 return NULL;
3873 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003874 }
3875
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003876 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003877}
3878
Georg Brandl0c315622009-05-25 21:10:36 +00003879/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3880static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003881ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003882{
guoci90fc8982018-09-11 17:45:45 -04003883 const node * const n = is_async ? CHILD(n0, 1) : n0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003884 int i, n_items;
3885 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003886
3887 REQ(n, with_stmt);
3888
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003889 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003890 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003891 if (!items)
3892 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003893 for (i = 1; i < NCH(n) - 2; i += 2) {
3894 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3895 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003896 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003897 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003898 }
3899
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003900 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3901 if (!body)
3902 return NULL;
3903
Yury Selivanov75445082015-05-11 22:57:16 -04003904 if (is_async)
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003905 return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04003906 else
3907 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003908}
3909
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003911ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003912{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003913 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003914 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003915 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003916 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003917
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 REQ(n, classdef);
3919
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003920 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003921 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 if (!s)
3923 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003924 classname = NEW_IDENTIFIER(CHILD(n, 1));
3925 if (!classname)
3926 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003927 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003928 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003929 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003930 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003932
3933 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003934 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003935 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003936 return NULL;
3937 classname = NEW_IDENTIFIER(CHILD(n, 1));
3938 if (!classname)
3939 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003940 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003941 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003942 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003943 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 }
3945
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003946 /* class NAME '(' arglist ')' ':' suite */
3947 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003948 {
3949 PyObject *dummy_name;
3950 expr_ty dummy;
3951 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3952 if (!dummy_name)
3953 return NULL;
3954 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
Serhiy Storchakab619b092018-11-27 09:40:29 +02003955 call = ast_for_call(c, CHILD(n, 3), dummy, NULL);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003956 if (!call)
3957 return NULL;
3958 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003959 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003960 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003962 classname = NEW_IDENTIFIER(CHILD(n, 1));
3963 if (!classname)
3964 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003965 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003966 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003967
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003968 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003969 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970}
3971
3972static stmt_ty
3973ast_for_stmt(struct compiling *c, const node *n)
3974{
3975 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003976 assert(NCH(n) == 1);
3977 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978 }
3979 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003980 assert(num_stmts(n) == 1);
3981 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 }
3983 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003984 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003985 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3986 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003987 */
3988 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989 case expr_stmt:
3990 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 case del_stmt:
3992 return ast_for_del_stmt(c, n);
3993 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003994 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 case flow_stmt:
3996 return ast_for_flow_stmt(c, n);
3997 case import_stmt:
3998 return ast_for_import_stmt(c, n);
3999 case global_stmt:
4000 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004001 case nonlocal_stmt:
4002 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003 case assert_stmt:
4004 return ast_for_assert_stmt(c, n);
4005 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004006 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4008 TYPE(n), NCH(n));
4009 return NULL;
4010 }
4011 }
4012 else {
4013 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004014 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004015 */
4016 node *ch = CHILD(n, 0);
4017 REQ(n, compound_stmt);
4018 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 case if_stmt:
4020 return ast_for_if_stmt(c, ch);
4021 case while_stmt:
4022 return ast_for_while_stmt(c, ch);
4023 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004024 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 case try_stmt:
4026 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004027 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004028 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004030 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004032 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 case decorated:
4034 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004035 case async_stmt:
4036 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004038 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004039 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 TYPE(n), NCH(n));
4041 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004042 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043 }
4044}
4045
4046static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004047parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004049 const char *end;
4050 long x;
4051 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004052 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004053 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004055 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004056 errno = 0;
4057 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004058 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004059 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004060 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004061 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004062 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004063 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004064 }
4065 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004066 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004067 if (*end == '\0') {
4068 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004069 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004070 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004071 }
4072 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004073 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004074 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004075 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4076 if (compl.imag == -1.0 && PyErr_Occurred())
4077 return NULL;
4078 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004079 }
4080 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004081 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004082 dx = PyOS_string_to_double(s, NULL, NULL);
4083 if (dx == -1.0 && PyErr_Occurred())
4084 return NULL;
4085 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004086 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004087}
4088
4089static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004090parsenumber(struct compiling *c, const char *s)
4091{
4092 char *dup, *end;
4093 PyObject *res = NULL;
4094
4095 assert(s != NULL);
4096
4097 if (strchr(s, '_') == NULL) {
4098 return parsenumber_raw(c, s);
4099 }
4100 /* Create a duplicate without underscores. */
4101 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004102 if (dup == NULL) {
4103 return PyErr_NoMemory();
4104 }
Brett Cannona721aba2016-09-09 14:57:09 -07004105 end = dup;
4106 for (; *s; s++) {
4107 if (*s != '_') {
4108 *end++ = *s;
4109 }
4110 }
4111 *end = '\0';
4112 res = parsenumber_raw(c, dup);
4113 PyMem_Free(dup);
4114 return res;
4115}
4116
4117static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004118decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004119{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004120 const char *s, *t;
4121 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004122 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4123 while (s < end && (*s & 0x80)) s++;
4124 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004125 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126}
4127
Eric V. Smith56466482016-10-31 14:46:26 -04004128static int
4129warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004130 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004131{
4132 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4133 first_invalid_escape_char);
4134 if (msg == NULL) {
4135 return -1;
4136 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004137 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004138 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004139 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004140 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004141 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004142 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004143 to get a more accurate error report */
4144 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004145 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004146 }
4147 Py_DECREF(msg);
4148 return -1;
4149 }
4150 Py_DECREF(msg);
4151 return 0;
4152}
4153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004154static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004155decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4156 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004158 PyObject *v, *u;
4159 char *buf;
4160 char *p;
4161 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004162
Benjamin Peterson202803a2016-02-25 22:34:45 -08004163 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004164 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004165 return NULL;
4166 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4167 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4168 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4169 if (u == NULL)
4170 return NULL;
4171 p = buf = PyBytes_AsString(u);
4172 end = s + len;
4173 while (s < end) {
4174 if (*s == '\\') {
4175 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004176 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004177 strcpy(p, "u005c");
4178 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004179 if (s >= end)
4180 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004181 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004182 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004183 if (*s & 0x80) { /* XXX inefficient */
4184 PyObject *w;
4185 int kind;
4186 void *data;
4187 Py_ssize_t len, i;
4188 w = decode_utf8(c, &s, end);
4189 if (w == NULL) {
4190 Py_DECREF(u);
4191 return NULL;
4192 }
4193 kind = PyUnicode_KIND(w);
4194 data = PyUnicode_DATA(w);
4195 len = PyUnicode_GET_LENGTH(w);
4196 for (i = 0; i < len; i++) {
4197 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4198 sprintf(p, "\\U%08x", chr);
4199 p += 10;
4200 }
4201 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004202 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004203 Py_DECREF(w);
4204 } else {
4205 *p++ = *s++;
4206 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004207 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004208 len = p - buf;
4209 s = buf;
4210
Eric V. Smith56466482016-10-31 14:46:26 -04004211 const char *first_invalid_escape;
4212 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4213
4214 if (v != NULL && first_invalid_escape != NULL) {
4215 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4216 /* We have not decref u before because first_invalid_escape points
4217 inside u. */
4218 Py_XDECREF(u);
4219 Py_DECREF(v);
4220 return NULL;
4221 }
4222 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004223 Py_XDECREF(u);
4224 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004225}
4226
Eric V. Smith56466482016-10-31 14:46:26 -04004227static PyObject *
4228decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4229 size_t len)
4230{
4231 const char *first_invalid_escape;
4232 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4233 &first_invalid_escape);
4234 if (result == NULL)
4235 return NULL;
4236
4237 if (first_invalid_escape != NULL) {
4238 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4239 Py_DECREF(result);
4240 return NULL;
4241 }
4242 }
4243 return result;
4244}
4245
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004246/* Shift locations for the given node and all its children by adding `lineno`
4247 and `col_offset` to existing locations. */
4248static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4249{
4250 n->n_col_offset = n->n_col_offset + col_offset;
4251 for (int i = 0; i < NCH(n); ++i) {
4252 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4253 /* Shifting column offsets unnecessary if there's been newlines. */
4254 col_offset = 0;
4255 }
4256 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4257 }
4258 n->n_lineno = n->n_lineno + lineno;
4259}
4260
4261/* Fix locations for the given node and its children.
4262
4263 `parent` is the enclosing node.
4264 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004265 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004266*/
4267static void
4268fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4269{
4270 char *substr = NULL;
4271 char *start;
4272 int lines = LINENO(parent) - 1;
4273 int cols = parent->n_col_offset;
4274 /* Find the full fstring to fix location information in `n`. */
4275 while (parent && parent->n_type != STRING)
4276 parent = parent->n_child;
4277 if (parent && parent->n_str) {
4278 substr = strstr(parent->n_str, expr_str);
4279 if (substr) {
4280 start = substr;
4281 while (start > parent->n_str) {
4282 if (start[0] == '\n')
4283 break;
4284 start--;
4285 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004286 cols += (int)(substr - start);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004287 /* Fix lineno in mulitline strings. */
4288 while ((substr = strchr(substr + 1, '\n')))
4289 lines--;
4290 }
4291 }
4292 fstring_shift_node_locations(n, lines, cols);
4293}
4294
Eric V. Smith451d0e32016-09-09 21:56:20 -04004295/* Compile this expression in to an expr_ty. Add parens around the
4296 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004297static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004298fstring_compile_expr(const char *expr_start, const char *expr_end,
4299 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004300
Eric V. Smith235a6f02015-09-19 14:51:32 -04004301{
4302 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004303 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004304 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004305 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004306 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004307 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004308
Eric V. Smith1d44c412015-09-23 07:49:00 -04004309 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004310 assert(*(expr_start-1) == '{');
4311 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004312
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004313 /* If the substring is all whitespace, it's an error. We need to catch this
4314 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4315 because turning the expression '' in to '()' would go from being invalid
4316 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004317 for (s = expr_start; s != expr_end; s++) {
4318 char c = *s;
4319 /* The Python parser ignores only the following whitespace
4320 characters (\r already is converted to \n). */
4321 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004322 break;
4323 }
4324 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004325 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004326 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004327 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004328 }
4329
Eric V. Smith451d0e32016-09-09 21:56:20 -04004330 len = expr_end - expr_start;
4331 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4332 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004333 if (str == NULL) {
4334 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004335 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004336 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004337
Eric V. Smith451d0e32016-09-09 21:56:20 -04004338 str[0] = '(';
4339 memcpy(str+1, expr_start, len);
4340 str[len+1] = ')';
4341 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004342
4343 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004344 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4345 Py_eval_input, 0);
4346 if (!mod_n) {
4347 PyMem_RawFree(str);
4348 return NULL;
4349 }
4350 /* Reuse str to find the correct column offset. */
4351 str[0] = '{';
4352 str[len+1] = '}';
4353 fstring_fix_node_location(n, mod_n, str);
4354 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004355 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004356 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004357 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004358 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004359 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004360}
4361
4362/* Return -1 on error.
4363
4364 Return 0 if we reached the end of the literal.
4365
4366 Return 1 if we haven't reached the end of the literal, but we want
4367 the caller to process the literal up to this point. Used for
4368 doubled braces.
4369*/
4370static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004371fstring_find_literal(const char **str, const char *end, int raw,
4372 PyObject **literal, int recurse_lvl,
4373 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004374{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004375 /* Get any literal string. It ends when we hit an un-doubled left
4376 brace (which isn't part of a unicode name escape such as
4377 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004378
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004379 const char *s = *str;
4380 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004381 int result = 0;
4382
Eric V. Smith235a6f02015-09-19 14:51:32 -04004383 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004384 while (s < end) {
4385 char ch = *s++;
4386 if (!raw && ch == '\\' && s < end) {
4387 ch = *s++;
4388 if (ch == 'N') {
4389 if (s < end && *s++ == '{') {
4390 while (s < end && *s++ != '}') {
4391 }
4392 continue;
4393 }
4394 break;
4395 }
4396 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4397 return -1;
4398 }
4399 }
4400 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004401 /* Check for doubled braces, but only at the top level. If
4402 we checked at every level, then f'{0:{3}}' would fail
4403 with the two closing braces. */
4404 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004405 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004406 /* We're going to tell the caller that the literal ends
4407 here, but that they should continue scanning. But also
4408 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004409 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004410 result = 1;
4411 goto done;
4412 }
4413
4414 /* Where a single '{' is the start of a new expression, a
4415 single '}' is not allowed. */
4416 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004417 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004418 ast_error(c, n, "f-string: single '}' is not allowed");
4419 return -1;
4420 }
4421 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004422 /* We're either at a '{', which means we're starting another
4423 expression; or a '}', which means we're at the end of this
4424 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004425 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004426 break;
4427 }
4428 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004429 *str = s;
4430 assert(s <= end);
4431 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004432done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004433 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004434 if (raw)
4435 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004436 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004437 NULL, NULL);
4438 else
Eric V. Smith56466482016-10-31 14:46:26 -04004439 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004440 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004441 if (!*literal)
4442 return -1;
4443 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004444 return result;
4445}
4446
4447/* Forward declaration because parsing is recursive. */
4448static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004449fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004450 struct compiling *c, const node *n);
4451
Eric V. Smith451d0e32016-09-09 21:56:20 -04004452/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004453 expression (so it must be a '{'). Returns the FormattedValue node,
4454 which includes the expression, conversion character, and
4455 format_spec expression.
4456
4457 Note that I don't do a perfect job here: I don't make sure that a
4458 closing brace doesn't match an opening paren, for example. It
4459 doesn't need to error on all invalid expressions, just correctly
4460 find the end of all valid ones. Any errors inside the expression
4461 will be caught when we parse it later. */
4462static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004463fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004464 expr_ty *expression, struct compiling *c, const node *n)
4465{
4466 /* Return -1 on error, else 0. */
4467
Eric V. Smith451d0e32016-09-09 21:56:20 -04004468 const char *expr_start;
4469 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004470 expr_ty simple_expression;
4471 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004472 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004473
4474 /* 0 if we're not in a string, else the quote char we're trying to
4475 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004476 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004477
4478 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4479 int string_type = 0;
4480
4481 /* Keep track of nesting level for braces/parens/brackets in
4482 expressions. */
4483 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004484 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004485
4486 /* Can only nest one level deep. */
4487 if (recurse_lvl >= 2) {
4488 ast_error(c, n, "f-string: expressions nested too deeply");
4489 return -1;
4490 }
4491
4492 /* The first char must be a left brace, or we wouldn't have gotten
4493 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004494 assert(**str == '{');
4495 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004496
Eric V. Smith451d0e32016-09-09 21:56:20 -04004497 expr_start = *str;
4498 for (; *str < end; (*str)++) {
4499 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004500
4501 /* Loop invariants. */
4502 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004503 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004504 if (quote_char)
4505 assert(string_type == 1 || string_type == 3);
4506 else
4507 assert(string_type == 0);
4508
Eric V. Smith451d0e32016-09-09 21:56:20 -04004509 ch = **str;
4510 /* Nowhere inside an expression is a backslash allowed. */
4511 if (ch == '\\') {
4512 /* Error: can't include a backslash character, inside
4513 parens or strings or not. */
4514 ast_error(c, n, "f-string expression part "
4515 "cannot include a backslash");
4516 return -1;
4517 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004518 if (quote_char) {
4519 /* We're inside a string. See if we're at the end. */
4520 /* This code needs to implement the same non-error logic
4521 as tok_get from tokenizer.c, at the letter_quote
4522 label. To actually share that code would be a
4523 nightmare. But, it's unlikely to change and is small,
4524 so duplicate it here. Note we don't need to catch all
4525 of the errors, since they'll be caught when parsing the
4526 expression. We just need to match the non-error
4527 cases. Thus we can ignore \n in single-quoted strings,
4528 for example. Or non-terminated strings. */
4529 if (ch == quote_char) {
4530 /* Does this match the string_type (single or triple
4531 quoted)? */
4532 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004533 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004534 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004535 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004536 string_type = 0;
4537 quote_char = 0;
4538 continue;
4539 }
4540 } else {
4541 /* We're at the end of a normal string. */
4542 quote_char = 0;
4543 string_type = 0;
4544 continue;
4545 }
4546 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004547 } else if (ch == '\'' || ch == '"') {
4548 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004549 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004550 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004551 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004552 } else {
4553 /* Start of a normal string. */
4554 string_type = 1;
4555 }
4556 /* Start looking for the end of the string. */
4557 quote_char = ch;
4558 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004559 if (nested_depth >= MAXLEVEL) {
4560 ast_error(c, n, "f-string: too many nested parenthesis");
4561 return -1;
4562 }
4563 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004564 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004565 } else if (ch == '#') {
4566 /* Error: can't include a comment character, inside parens
4567 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004568 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004569 return -1;
4570 } else if (nested_depth == 0 &&
4571 (ch == '!' || ch == ':' || ch == '}')) {
4572 /* First, test for the special case of "!=". Since '=' is
4573 not an allowed conversion character, nothing is lost in
4574 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004575 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004576 /* This isn't a conversion character, just continue. */
4577 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004578 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004579 /* Normal way out of this loop. */
4580 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004581 } else if (ch == ']' || ch == '}' || ch == ')') {
4582 if (!nested_depth) {
4583 ast_error(c, n, "f-string: unmatched '%c'", ch);
4584 return -1;
4585 }
4586 nested_depth--;
4587 int opening = parenstack[nested_depth];
4588 if (!((opening == '(' && ch == ')') ||
4589 (opening == '[' && ch == ']') ||
4590 (opening == '{' && ch == '}')))
4591 {
4592 ast_error(c, n,
4593 "f-string: closing parenthesis '%c' "
4594 "does not match opening parenthesis '%c'",
4595 ch, opening);
4596 return -1;
4597 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004598 } else {
4599 /* Just consume this char and loop around. */
4600 }
4601 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004602 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004603 /* If we leave this loop in a string or with mismatched parens, we
4604 don't care. We'll get a syntax error when compiling the
4605 expression. But, we can produce a better error message, so
4606 let's just do that.*/
4607 if (quote_char) {
4608 ast_error(c, n, "f-string: unterminated string");
4609 return -1;
4610 }
4611 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004612 int opening = parenstack[nested_depth - 1];
4613 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004614 return -1;
4615 }
4616
Eric V. Smith451d0e32016-09-09 21:56:20 -04004617 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004618 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004619
4620 /* Compile the expression as soon as possible, so we show errors
4621 related to the expression before errors related to the
4622 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004623 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004624 if (!simple_expression)
4625 return -1;
4626
4627 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004628 if (**str == '!') {
4629 *str += 1;
4630 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004631 goto unexpected_end_of_string;
4632
Eric V. Smith451d0e32016-09-09 21:56:20 -04004633 conversion = **str;
4634 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004635
4636 /* Validate the conversion. */
4637 if (!(conversion == 's' || conversion == 'r'
4638 || conversion == 'a')) {
4639 ast_error(c, n, "f-string: invalid conversion character: "
4640 "expected 's', 'r', or 'a'");
4641 return -1;
4642 }
4643 }
4644
4645 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004646 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004647 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004648 if (**str == ':') {
4649 *str += 1;
4650 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004651 goto unexpected_end_of_string;
4652
4653 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004654 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004655 if (!format_spec)
4656 return -1;
4657 }
4658
Eric V. Smith451d0e32016-09-09 21:56:20 -04004659 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004660 goto unexpected_end_of_string;
4661
4662 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004663 assert(*str < end);
4664 assert(**str == '}');
4665 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004666
Eric V. Smith451d0e32016-09-09 21:56:20 -04004667 /* And now create the FormattedValue node that represents this
4668 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004669 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004670 format_spec, LINENO(n), n->n_col_offset,
4671 c->c_arena);
4672 if (!*expression)
4673 return -1;
4674
4675 return 0;
4676
4677unexpected_end_of_string:
4678 ast_error(c, n, "f-string: expecting '}'");
4679 return -1;
4680}
4681
4682/* Return -1 on error.
4683
4684 Return 0 if we have a literal (possible zero length) and an
4685 expression (zero length if at the end of the string.
4686
4687 Return 1 if we have a literal, but no expression, and we want the
4688 caller to call us again. This is used to deal with doubled
4689 braces.
4690
4691 When called multiple times on the string 'a{{b{0}c', this function
4692 will return:
4693
4694 1. the literal 'a{' with no expression, and a return value
4695 of 1. Despite the fact that there's no expression, the return
4696 value of 1 means we're not finished yet.
4697
4698 2. the literal 'b' and the expression '0', with a return value of
4699 0. The fact that there's an expression means we're not finished.
4700
4701 3. literal 'c' with no expression and a return value of 0. The
4702 combination of the return value of 0 with no expression means
4703 we're finished.
4704*/
4705static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004706fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4707 int recurse_lvl, PyObject **literal,
4708 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004709 struct compiling *c, const node *n)
4710{
4711 int result;
4712
4713 assert(*literal == NULL && *expression == NULL);
4714
4715 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004716 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004717 if (result < 0)
4718 goto error;
4719
4720 assert(result == 0 || result == 1);
4721
4722 if (result == 1)
4723 /* We have a literal, but don't look at the expression. */
4724 return 1;
4725
Eric V. Smith451d0e32016-09-09 21:56:20 -04004726 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004727 /* We're at the end of the string or the end of a nested
4728 f-string: no expression. The top-level error case where we
4729 expect to be at the end of the string but we're at a '}' is
4730 handled later. */
4731 return 0;
4732
4733 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004734 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004735
Eric V. Smith451d0e32016-09-09 21:56:20 -04004736 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004737 goto error;
4738
4739 return 0;
4740
4741error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004742 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004743 return -1;
4744}
4745
4746#define EXPRLIST_N_CACHED 64
4747
4748typedef struct {
4749 /* Incrementally build an array of expr_ty, so be used in an
4750 asdl_seq. Cache some small but reasonably sized number of
4751 expr_ty's, and then after that start dynamically allocating,
4752 doubling the number allocated each time. Note that the f-string
4753 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004754 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04004755 fast as you add exressions in an f-string. */
4756
4757 Py_ssize_t allocated; /* Number we've allocated. */
4758 Py_ssize_t size; /* Number we've used. */
4759 expr_ty *p; /* Pointer to the memory we're actually
4760 using. Will point to 'data' until we
4761 start dynamically allocating. */
4762 expr_ty data[EXPRLIST_N_CACHED];
4763} ExprList;
4764
4765#ifdef NDEBUG
4766#define ExprList_check_invariants(l)
4767#else
4768static void
4769ExprList_check_invariants(ExprList *l)
4770{
4771 /* Check our invariants. Make sure this object is "live", and
4772 hasn't been deallocated. */
4773 assert(l->size >= 0);
4774 assert(l->p != NULL);
4775 if (l->size <= EXPRLIST_N_CACHED)
4776 assert(l->data == l->p);
4777}
4778#endif
4779
4780static void
4781ExprList_Init(ExprList *l)
4782{
4783 l->allocated = EXPRLIST_N_CACHED;
4784 l->size = 0;
4785
4786 /* Until we start allocating dynamically, p points to data. */
4787 l->p = l->data;
4788
4789 ExprList_check_invariants(l);
4790}
4791
4792static int
4793ExprList_Append(ExprList *l, expr_ty exp)
4794{
4795 ExprList_check_invariants(l);
4796 if (l->size >= l->allocated) {
4797 /* We need to alloc (or realloc) the memory. */
4798 Py_ssize_t new_size = l->allocated * 2;
4799
4800 /* See if we've ever allocated anything dynamically. */
4801 if (l->p == l->data) {
4802 Py_ssize_t i;
4803 /* We're still using the cached data. Switch to
4804 alloc-ing. */
4805 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4806 if (!l->p)
4807 return -1;
4808 /* Copy the cached data into the new buffer. */
4809 for (i = 0; i < l->size; i++)
4810 l->p[i] = l->data[i];
4811 } else {
4812 /* Just realloc. */
4813 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4814 if (!tmp) {
4815 PyMem_RawFree(l->p);
4816 l->p = NULL;
4817 return -1;
4818 }
4819 l->p = tmp;
4820 }
4821
4822 l->allocated = new_size;
4823 assert(l->allocated == 2 * l->size);
4824 }
4825
4826 l->p[l->size++] = exp;
4827
4828 ExprList_check_invariants(l);
4829 return 0;
4830}
4831
4832static void
4833ExprList_Dealloc(ExprList *l)
4834{
4835 ExprList_check_invariants(l);
4836
4837 /* If there's been an error, or we've never dynamically allocated,
4838 do nothing. */
4839 if (!l->p || l->p == l->data) {
4840 /* Do nothing. */
4841 } else {
4842 /* We have dynamically allocated. Free the memory. */
4843 PyMem_RawFree(l->p);
4844 }
4845 l->p = NULL;
4846 l->size = -1;
4847}
4848
4849static asdl_seq *
4850ExprList_Finish(ExprList *l, PyArena *arena)
4851{
4852 asdl_seq *seq;
4853
4854 ExprList_check_invariants(l);
4855
4856 /* Allocate the asdl_seq and copy the expressions in to it. */
4857 seq = _Py_asdl_seq_new(l->size, arena);
4858 if (seq) {
4859 Py_ssize_t i;
4860 for (i = 0; i < l->size; i++)
4861 asdl_seq_SET(seq, i, l->p[i]);
4862 }
4863 ExprList_Dealloc(l);
4864 return seq;
4865}
4866
4867/* The FstringParser is designed to add a mix of strings and
4868 f-strings, and concat them together as needed. Ultimately, it
4869 generates an expr_ty. */
4870typedef struct {
4871 PyObject *last_str;
4872 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004873 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004874} FstringParser;
4875
4876#ifdef NDEBUG
4877#define FstringParser_check_invariants(state)
4878#else
4879static void
4880FstringParser_check_invariants(FstringParser *state)
4881{
4882 if (state->last_str)
4883 assert(PyUnicode_CheckExact(state->last_str));
4884 ExprList_check_invariants(&state->expr_list);
4885}
4886#endif
4887
4888static void
4889FstringParser_Init(FstringParser *state)
4890{
4891 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004892 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004893 ExprList_Init(&state->expr_list);
4894 FstringParser_check_invariants(state);
4895}
4896
4897static void
4898FstringParser_Dealloc(FstringParser *state)
4899{
4900 FstringParser_check_invariants(state);
4901
4902 Py_XDECREF(state->last_str);
4903 ExprList_Dealloc(&state->expr_list);
4904}
4905
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004906/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004907static expr_ty
4908make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4909{
4910 PyObject *s = *str;
4911 *str = NULL;
4912 assert(PyUnicode_CheckExact(s));
4913 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4914 Py_DECREF(s);
4915 return NULL;
4916 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004917 return Constant(s, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004918}
4919
4920/* Add a non-f-string (that is, a regular literal string). str is
4921 decref'd. */
4922static int
4923FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4924{
4925 FstringParser_check_invariants(state);
4926
4927 assert(PyUnicode_CheckExact(str));
4928
4929 if (PyUnicode_GET_LENGTH(str) == 0) {
4930 Py_DECREF(str);
4931 return 0;
4932 }
4933
4934 if (!state->last_str) {
4935 /* We didn't have a string before, so just remember this one. */
4936 state->last_str = str;
4937 } else {
4938 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004939 PyUnicode_AppendAndDel(&state->last_str, str);
4940 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004941 return -1;
4942 }
4943 FstringParser_check_invariants(state);
4944 return 0;
4945}
4946
Eric V. Smith451d0e32016-09-09 21:56:20 -04004947/* Parse an f-string. The f-string is in *str to end, with no
4948 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004949static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004950FstringParser_ConcatFstring(FstringParser *state, const char **str,
4951 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004952 struct compiling *c, const node *n)
4953{
4954 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004955 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004956
4957 /* Parse the f-string. */
4958 while (1) {
4959 PyObject *literal = NULL;
4960 expr_ty expression = NULL;
4961
4962 /* If there's a zero length literal in front of the
4963 expression, literal will be NULL. If we're at the end of
4964 the f-string, expression will be NULL (unless result == 1,
4965 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004966 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004967 &literal, &expression,
4968 c, n);
4969 if (result < 0)
4970 return -1;
4971
4972 /* Add the literal, if any. */
4973 if (!literal) {
4974 /* Do nothing. Just leave last_str alone (and possibly
4975 NULL). */
4976 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004977 /* Note that the literal can be zero length, if the
4978 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004979 state->last_str = literal;
4980 literal = NULL;
4981 } else {
4982 /* We have a literal, concatenate it. */
4983 assert(PyUnicode_GET_LENGTH(literal) != 0);
4984 if (FstringParser_ConcatAndDel(state, literal) < 0)
4985 return -1;
4986 literal = NULL;
4987 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004988
4989 /* We've dealt with the literal now. It can't be leaked on further
4990 errors. */
4991 assert(literal == NULL);
4992
4993 /* See if we should just loop around to get the next literal
4994 and expression, while ignoring the expression this
4995 time. This is used for un-doubling braces, as an
4996 optimization. */
4997 if (result == 1)
4998 continue;
4999
5000 if (!expression)
5001 /* We're done with this f-string. */
5002 break;
5003
5004 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005005 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005006 if (!state->last_str) {
5007 /* Do nothing. No previous literal. */
5008 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005009 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005010 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5011 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5012 return -1;
5013 }
5014
5015 if (ExprList_Append(&state->expr_list, expression) < 0)
5016 return -1;
5017 }
5018
Eric V. Smith235a6f02015-09-19 14:51:32 -04005019 /* If recurse_lvl is zero, then we must be at the end of the
5020 string. Otherwise, we must be at a right brace. */
5021
Eric V. Smith451d0e32016-09-09 21:56:20 -04005022 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005023 ast_error(c, n, "f-string: unexpected end of string");
5024 return -1;
5025 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005026 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005027 ast_error(c, n, "f-string: expecting '}'");
5028 return -1;
5029 }
5030
5031 FstringParser_check_invariants(state);
5032 return 0;
5033}
5034
5035/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005036 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005037static expr_ty
5038FstringParser_Finish(FstringParser *state, struct compiling *c,
5039 const node *n)
5040{
5041 asdl_seq *seq;
5042
5043 FstringParser_check_invariants(state);
5044
5045 /* If we're just a constant string with no expressions, return
5046 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005047 if (!state->fmode) {
5048 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005049 if (!state->last_str) {
5050 /* Create a zero length string. */
5051 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5052 if (!state->last_str)
5053 goto error;
5054 }
5055 return make_str_node_and_del(&state->last_str, c, n);
5056 }
5057
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005058 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005059 last node in our expression list. */
5060 if (state->last_str) {
5061 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5062 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5063 goto error;
5064 }
5065 /* This has already been freed. */
5066 assert(state->last_str == NULL);
5067
5068 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5069 if (!seq)
5070 goto error;
5071
Eric V. Smith235a6f02015-09-19 14:51:32 -04005072 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5073
5074error:
5075 FstringParser_Dealloc(state);
5076 return NULL;
5077}
5078
Eric V. Smith451d0e32016-09-09 21:56:20 -04005079/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5080 at end, parse it into an expr_ty. Return NULL on error. Adjust
5081 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005082static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005083fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005084 struct compiling *c, const node *n)
5085{
5086 FstringParser state;
5087
5088 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005089 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005090 c, n) < 0) {
5091 FstringParser_Dealloc(&state);
5092 return NULL;
5093 }
5094
5095 return FstringParser_Finish(&state, c, n);
5096}
5097
5098/* n is a Python string literal, including the bracketing quote
5099 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005100 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005101 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005102 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5103 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005105static int
5106parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5107 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005108{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005109 size_t len;
5110 const char *s = STR(n);
5111 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005112 int fmode = 0;
5113 *bytesmode = 0;
5114 *rawmode = 0;
5115 *result = NULL;
5116 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005117 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005118 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005119 if (quote == 'b' || quote == 'B') {
5120 quote = *++s;
5121 *bytesmode = 1;
5122 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005123 else if (quote == 'u' || quote == 'U') {
5124 quote = *++s;
5125 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005126 else if (quote == 'r' || quote == 'R') {
5127 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005128 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005129 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005130 else if (quote == 'f' || quote == 'F') {
5131 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005132 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005133 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005134 else {
5135 break;
5136 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005137 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005138 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005139 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005140 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005141 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005142 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005143 if (quote != '\'' && quote != '\"') {
5144 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005145 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005146 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005148 s++;
5149 len = strlen(s);
5150 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005151 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005152 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005153 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005154 }
5155 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005156 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005157 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005158 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005159 }
5160 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005161 /* A triple quoted string. We've already skipped one quote at
5162 the start and one at the end of the string. Now skip the
5163 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005164 s += 2;
5165 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005166 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005167 if (s[--len] != quote || s[--len] != quote) {
5168 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005169 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005170 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005171 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005172
Eric V. Smith451d0e32016-09-09 21:56:20 -04005173 if (fmode) {
5174 /* Just return the bytes. The caller will parse the resulting
5175 string. */
5176 *fstr = s;
5177 *fstrlen = len;
5178 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005179 }
5180
Eric V. Smith451d0e32016-09-09 21:56:20 -04005181 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005182 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005183 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005184 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005185 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005186 const char *ch;
5187 for (ch = s; *ch; ch++) {
5188 if (Py_CHARMASK(*ch) >= 0x80) {
5189 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005190 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005191 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005192 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005193 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005194 if (*rawmode)
5195 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005196 else
Eric V. Smith56466482016-10-31 14:46:26 -04005197 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005198 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005199 if (*rawmode)
5200 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005201 else
Eric V. Smith56466482016-10-31 14:46:26 -04005202 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005203 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005204 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005205}
5206
Eric V. Smith235a6f02015-09-19 14:51:32 -04005207/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5208 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005209 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005210 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005211 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212 node if there's just an f-string (with no leading or trailing
5213 literals), or a JoinedStr node if there are multiple f-strings or
5214 any literals involved. */
5215static expr_ty
5216parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005217{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005218 int bytesmode = 0;
5219 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005220 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005221
5222 FstringParser state;
5223 FstringParser_Init(&state);
5224
5225 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005226 int this_bytesmode;
5227 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005228 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005229 const char *fstr;
5230 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005231
5232 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005233 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5234 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005235 goto error;
5236
5237 /* Check that we're not mixing bytes with unicode. */
5238 if (i != 0 && bytesmode != this_bytesmode) {
5239 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005240 /* s is NULL if the current string part is an f-string. */
5241 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005242 goto error;
5243 }
5244 bytesmode = this_bytesmode;
5245
Eric V. Smith451d0e32016-09-09 21:56:20 -04005246 if (fstr != NULL) {
5247 int result;
5248 assert(s == NULL && !bytesmode);
5249 /* This is an f-string. Parse and concatenate it. */
5250 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5251 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005252 if (result < 0)
5253 goto error;
5254 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005255 /* A string or byte string. */
5256 assert(s != NULL && fstr == NULL);
5257
Eric V. Smith451d0e32016-09-09 21:56:20 -04005258 assert(bytesmode ? PyBytes_CheckExact(s) :
5259 PyUnicode_CheckExact(s));
5260
Eric V. Smith451d0e32016-09-09 21:56:20 -04005261 if (bytesmode) {
5262 /* For bytes, concat as we go. */
5263 if (i == 0) {
5264 /* First time, just remember this value. */
5265 bytes_str = s;
5266 } else {
5267 PyBytes_ConcatAndDel(&bytes_str, s);
5268 if (!bytes_str)
5269 goto error;
5270 }
5271 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005272 /* This is a regular string. Concatenate it. */
5273 if (FstringParser_ConcatAndDel(&state, s) < 0)
5274 goto error;
5275 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005276 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005277 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005278 if (bytesmode) {
5279 /* Just return the bytes object and we're done. */
5280 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5281 goto error;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005282 return Constant(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005283 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005284
Eric V. Smith235a6f02015-09-19 14:51:32 -04005285 /* We're not a bytes string, bytes_str should never have been set. */
5286 assert(bytes_str == NULL);
5287
5288 return FstringParser_Finish(&state, c, n);
5289
5290error:
5291 Py_XDECREF(bytes_str);
5292 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005293 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005294}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005295
5296PyObject *
5297_PyAST_GetDocString(asdl_seq *body)
5298{
5299 if (!asdl_seq_LEN(body)) {
5300 return NULL;
5301 }
5302 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5303 if (st->kind != Expr_kind) {
5304 return NULL;
5305 }
5306 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005307 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5308 return e->v.Constant.value;
5309 }
5310 return NULL;
5311}