blob: 33356da075669c193e08ef48cfe54e96243f2f8b [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>
14
Benjamin Peterson832bfe22011-08-09 16:15:04 -050015static int validate_stmts(asdl_seq *);
16static int validate_exprs(asdl_seq *, expr_context_ty, int);
17static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
18static int validate_stmt(stmt_ty);
19static int validate_expr(expr_ty, expr_context_ty);
20
21static int
22validate_comprehension(asdl_seq *gens)
23{
24 int i;
25 if (!asdl_seq_LEN(gens)) {
26 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
27 return 0;
28 }
29 for (i = 0; i < asdl_seq_LEN(gens); i++) {
30 comprehension_ty comp = asdl_seq_GET(gens, i);
31 if (!validate_expr(comp->target, Store) ||
32 !validate_expr(comp->iter, Load) ||
33 !validate_exprs(comp->ifs, Load, 0))
34 return 0;
35 }
36 return 1;
37}
38
39static int
40validate_slice(slice_ty slice)
41{
42 switch (slice->kind) {
43 case Slice_kind:
44 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
45 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
46 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
47 case ExtSlice_kind: {
48 int i;
49 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
50 return 0;
51 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
52 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
53 return 0;
54 return 1;
55 }
56 case Index_kind:
57 return validate_expr(slice->v.Index.value, Load);
58 default:
59 PyErr_SetString(PyExc_SystemError, "unknown slice node");
60 return 0;
61 }
62}
63
64static int
65validate_keywords(asdl_seq *keywords)
66{
67 int i;
68 for (i = 0; i < asdl_seq_LEN(keywords); i++)
69 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
70 return 0;
71 return 1;
72}
73
74static int
75validate_args(asdl_seq *args)
76{
77 int i;
78 for (i = 0; i < asdl_seq_LEN(args); i++) {
79 arg_ty arg = asdl_seq_GET(args, i);
80 if (arg->annotation && !validate_expr(arg->annotation, Load))
81 return 0;
82 }
83 return 1;
84}
85
86static const char *
87expr_context_name(expr_context_ty ctx)
88{
89 switch (ctx) {
90 case Load:
91 return "Load";
92 case Store:
93 return "Store";
94 case Del:
95 return "Del";
96 case AugLoad:
97 return "AugLoad";
98 case AugStore:
99 return "AugStore";
100 case Param:
101 return "Param";
102 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700103 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100135validate_constant(PyObject *value)
136{
137 if (value == Py_None || value == Py_Ellipsis)
138 return 1;
139
140 if (PyLong_CheckExact(value)
141 || PyFloat_CheckExact(value)
142 || PyComplex_CheckExact(value)
143 || PyBool_Check(value)
144 || PyUnicode_CheckExact(value)
145 || PyBytes_CheckExact(value))
146 return 1;
147
148 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
149 PyObject *it;
150
151 it = PyObject_GetIter(value);
152 if (it == NULL)
153 return 0;
154
155 while (1) {
156 PyObject *item = PyIter_Next(it);
157 if (item == NULL) {
158 if (PyErr_Occurred()) {
159 Py_DECREF(it);
160 return 0;
161 }
162 break;
163 }
164
165 if (!validate_constant(item)) {
166 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100167 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100168 return 0;
169 }
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 }
172
173 Py_DECREF(it);
174 return 1;
175 }
176
177 return 0;
178}
179
180static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500181validate_expr(expr_ty exp, expr_context_ty ctx)
182{
183 int check_ctx = 1;
184 expr_context_ty actual_ctx;
185
186 /* First check expression context. */
187 switch (exp->kind) {
188 case Attribute_kind:
189 actual_ctx = exp->v.Attribute.ctx;
190 break;
191 case Subscript_kind:
192 actual_ctx = exp->v.Subscript.ctx;
193 break;
194 case Starred_kind:
195 actual_ctx = exp->v.Starred.ctx;
196 break;
197 case Name_kind:
198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100291 PyErr_Format(PyExc_TypeError,
292 "got an invalid type in Constant: %s",
293 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 return 0;
295 }
296 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 case Num_kind: {
298 PyObject *n = exp->v.Num.n;
299 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
300 !PyComplex_CheckExact(n)) {
301 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
302 return 0;
303 }
304 return 1;
305 }
306 case Str_kind: {
307 PyObject *s = exp->v.Str.s;
308 if (!PyUnicode_CheckExact(s)) {
309 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
310 return 0;
311 }
312 return 1;
313 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314 case JoinedStr_kind:
315 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
316 case FormattedValue_kind:
317 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
318 return 0;
319 if (exp->v.FormattedValue.format_spec)
320 return validate_expr(exp->v.FormattedValue.format_spec, Load);
321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Bytes_kind: {
323 PyObject *b = exp->v.Bytes.s;
324 if (!PyBytes_CheckExact(b)) {
325 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
326 return 0;
327 }
328 return 1;
329 }
330 case Attribute_kind:
331 return validate_expr(exp->v.Attribute.value, Load);
332 case Subscript_kind:
333 return validate_slice(exp->v.Subscript.slice) &&
334 validate_expr(exp->v.Subscript.value, Load);
335 case Starred_kind:
336 return validate_expr(exp->v.Starred.value, ctx);
337 case List_kind:
338 return validate_exprs(exp->v.List.elts, ctx, 0);
339 case Tuple_kind:
340 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
341 /* These last cases don't have any checking. */
342 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500343 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 case Ellipsis_kind:
345 return 1;
346 default:
347 PyErr_SetString(PyExc_SystemError, "unexpected expression");
348 return 0;
349 }
350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
INADA Naokicb41b272017-02-23 00:31:59 +0900369validate_body(asdl_seq *body, const char *owner, int allowempty)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500370{
INADA Naokicb41b272017-02-23 00:31:59 +0900371 if (!allowempty && !validate_nonempty_seq(body, "body", owner)) {
372 return 0;
373 }
374 return validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500375}
376
377static int
378validate_stmt(stmt_ty stmt)
379{
380 int i;
381 switch (stmt->kind) {
382 case FunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900383 return validate_body(stmt->v.FunctionDef.body, "FunctionDef",
384 stmt->v.FunctionDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500385 validate_arguments(stmt->v.FunctionDef.args) &&
386 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
387 (!stmt->v.FunctionDef.returns ||
388 validate_expr(stmt->v.FunctionDef.returns, Load));
389 case ClassDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900390 return validate_body(stmt->v.ClassDef.body, "ClassDef",
391 stmt->v.ClassDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500392 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
393 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400394 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500395 case Return_kind:
396 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
397 case Delete_kind:
398 return validate_assignlist(stmt->v.Delete.targets, Del);
399 case Assign_kind:
400 return validate_assignlist(stmt->v.Assign.targets, Store) &&
401 validate_expr(stmt->v.Assign.value, Load);
402 case AugAssign_kind:
403 return validate_expr(stmt->v.AugAssign.target, Store) &&
404 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700405 case AnnAssign_kind:
406 if (stmt->v.AnnAssign.target->kind != Name_kind &&
407 stmt->v.AnnAssign.simple) {
408 PyErr_SetString(PyExc_TypeError,
409 "AnnAssign with simple non-Name target");
410 return 0;
411 }
412 return validate_expr(stmt->v.AnnAssign.target, Store) &&
413 (!stmt->v.AnnAssign.value ||
414 validate_expr(stmt->v.AnnAssign.value, Load)) &&
415 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500416 case For_kind:
417 return validate_expr(stmt->v.For.target, Store) &&
418 validate_expr(stmt->v.For.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900419 validate_body(stmt->v.For.body, "For", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500420 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400421 case AsyncFor_kind:
422 return validate_expr(stmt->v.AsyncFor.target, Store) &&
423 validate_expr(stmt->v.AsyncFor.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900424 validate_body(stmt->v.AsyncFor.body, "AsyncFor", 0) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400425 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500426 case While_kind:
427 return validate_expr(stmt->v.While.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900428 validate_body(stmt->v.While.body, "While", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500429 validate_stmts(stmt->v.While.orelse);
430 case If_kind:
431 return validate_expr(stmt->v.If.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900432 validate_body(stmt->v.If.body, "If", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500433 validate_stmts(stmt->v.If.orelse);
434 case With_kind:
435 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
436 return 0;
437 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
438 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
439 if (!validate_expr(item->context_expr, Load) ||
440 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
441 return 0;
442 }
INADA Naokicb41b272017-02-23 00:31:59 +0900443 return validate_body(stmt->v.With.body, "With", 0);
Yury Selivanov75445082015-05-11 22:57:16 -0400444 case AsyncWith_kind:
445 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
446 return 0;
447 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
448 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
449 if (!validate_expr(item->context_expr, Load) ||
450 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
451 return 0;
452 }
INADA Naokicb41b272017-02-23 00:31:59 +0900453 return validate_body(stmt->v.AsyncWith.body, "AsyncWith", 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500454 case Raise_kind:
455 if (stmt->v.Raise.exc) {
456 return validate_expr(stmt->v.Raise.exc, Load) &&
457 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
458 }
459 if (stmt->v.Raise.cause) {
460 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
461 return 0;
462 }
463 return 1;
464 case Try_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900465 if (!validate_body(stmt->v.Try.body, "Try", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500466 return 0;
467 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
468 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
469 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
470 return 0;
471 }
472 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
473 asdl_seq_LEN(stmt->v.Try.orelse)) {
474 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
475 return 0;
476 }
477 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
478 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
479 if ((handler->v.ExceptHandler.type &&
480 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
INADA Naokicb41b272017-02-23 00:31:59 +0900481 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500482 return 0;
483 }
484 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
485 validate_stmts(stmt->v.Try.finalbody)) &&
486 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
487 validate_stmts(stmt->v.Try.orelse));
488 case Assert_kind:
489 return validate_expr(stmt->v.Assert.test, Load) &&
490 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
491 case Import_kind:
492 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
493 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300494 if (stmt->v.ImportFrom.level < 0) {
495 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500496 return 0;
497 }
498 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
499 case Global_kind:
500 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
501 case Nonlocal_kind:
502 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
503 case Expr_kind:
504 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400505 case AsyncFunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900506 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef",
507 stmt->v.AsyncFunctionDef.docstring != NULL) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400508 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
509 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
510 (!stmt->v.AsyncFunctionDef.returns ||
511 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500512 case Pass_kind:
513 case Break_kind:
514 case Continue_kind:
515 return 1;
516 default:
517 PyErr_SetString(PyExc_SystemError, "unexpected statement");
518 return 0;
519 }
520}
521
522static int
523validate_stmts(asdl_seq *seq)
524{
525 int i;
526 for (i = 0; i < asdl_seq_LEN(seq); i++) {
527 stmt_ty stmt = asdl_seq_GET(seq, i);
528 if (stmt) {
529 if (!validate_stmt(stmt))
530 return 0;
531 }
532 else {
533 PyErr_SetString(PyExc_ValueError,
534 "None disallowed in statement list");
535 return 0;
536 }
537 }
538 return 1;
539}
540
541static int
542validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
543{
544 int i;
545 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
546 expr_ty expr = asdl_seq_GET(exprs, i);
547 if (expr) {
548 if (!validate_expr(expr, ctx))
549 return 0;
550 }
551 else if (!null_ok) {
552 PyErr_SetString(PyExc_ValueError,
553 "None disallowed in expression list");
554 return 0;
555 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100556
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500557 }
558 return 1;
559}
560
561int
562PyAST_Validate(mod_ty mod)
563{
564 int res = 0;
565
566 switch (mod->kind) {
567 case Module_kind:
568 res = validate_stmts(mod->v.Module.body);
569 break;
570 case Interactive_kind:
571 res = validate_stmts(mod->v.Interactive.body);
572 break;
573 case Expression_kind:
574 res = validate_expr(mod->v.Expression.body, Load);
575 break;
576 case Suite_kind:
577 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
578 break;
579 default:
580 PyErr_SetString(PyExc_SystemError, "impossible module node");
581 res = 0;
582 break;
583 }
584 return res;
585}
586
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500587/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500588#include "grammar.h"
589#include "parsetok.h"
590#include "graminit.h"
591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592/* Data structure used internally */
593struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400594 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200595 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597};
598
599static asdl_seq *seq_for_testlist(struct compiling *, const node *);
600static expr_ty ast_for_expr(struct compiling *, const node *);
601static stmt_ty ast_for_stmt(struct compiling *, const node *);
INADA Naokicb41b272017-02-23 00:31:59 +0900602static asdl_seq *ast_for_body(struct compiling *c, const node *n,
603 string *docstring);
604static string docstring_from_stmts(asdl_seq *stmts);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000605static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
606 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000607static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000608static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
Yury Selivanov75445082015-05-11 22:57:16 -0400610static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
611static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
612
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613/* Note different signature for ast_for_call */
614static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
615
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000616static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400617static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618
Nick Coghlan650f0d02007-04-15 12:05:43 +0000619#define COMP_GENEXP 0
620#define COMP_LISTCOMP 1
621#define COMP_SETCOMP 2
622
Benjamin Peterson55e00432012-01-16 17:22:31 -0500623static int
624init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000625{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500626 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
627 if (!m)
628 return 0;
629 c->c_normalize = PyObject_GetAttrString(m, "normalize");
630 Py_DECREF(m);
631 if (!c->c_normalize)
632 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500633 return 1;
634}
635
636static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400637new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500638{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400639 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500640 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000641 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500642 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500643 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 /* Check whether there are non-ASCII characters in the
645 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500646 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200647 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300648 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500649 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500650 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200651 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500652 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300653 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
654 if (form == NULL) {
655 Py_DECREF(id);
656 return NULL;
657 }
658 PyObject *args[2] = {form, id};
659 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500660 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200661 if (!id2)
662 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300663 if (!PyUnicode_Check(id2)) {
664 PyErr_Format(PyExc_TypeError,
665 "unicodedata.normalize() must return a string, not "
666 "%.200s",
667 Py_TYPE(id2)->tp_name);
668 Py_DECREF(id2);
669 return NULL;
670 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200671 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000672 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000673 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200674 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
675 Py_DECREF(id);
676 return NULL;
677 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000678 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679}
680
Benjamin Peterson55e00432012-01-16 17:22:31 -0500681#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400684ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400686 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687
Victor Stinner14e461d2013-08-26 22:28:21 +0200688 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000690 Py_INCREF(Py_None);
691 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200693 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400694 if (!tmp)
695 return 0;
696 errstr = PyUnicode_FromString(errmsg);
697 if (!errstr) {
698 Py_DECREF(tmp);
699 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000700 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 Py_DECREF(errstr);
703 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400704 if (value) {
705 PyErr_SetObject(PyExc_SyntaxError, value);
706 Py_DECREF(value);
707 }
708 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709}
710
711/* num_stmts() returns number of contained statements.
712
713 Use this routine to determine how big a sequence is needed for
714 the statements in a parse tree. Its raison d'etre is this bit of
715 grammar:
716
717 stmt: simple_stmt | compound_stmt
718 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
719
720 A simple_stmt can contain multiple small_stmt elements joined
721 by semicolons. If the arg is a simple_stmt, the number of
722 small_stmt elements is returned.
723*/
724
725static int
726num_stmts(const node *n)
727{
728 int i, l;
729 node *ch;
730
731 switch (TYPE(n)) {
732 case single_input:
733 if (TYPE(CHILD(n, 0)) == NEWLINE)
734 return 0;
735 else
736 return num_stmts(CHILD(n, 0));
737 case file_input:
738 l = 0;
739 for (i = 0; i < NCH(n); i++) {
740 ch = CHILD(n, i);
741 if (TYPE(ch) == stmt)
742 l += num_stmts(ch);
743 }
744 return l;
745 case stmt:
746 return num_stmts(CHILD(n, 0));
747 case compound_stmt:
748 return 1;
749 case simple_stmt:
750 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
751 case suite:
752 if (NCH(n) == 1)
753 return num_stmts(CHILD(n, 0));
754 else {
755 l = 0;
756 for (i = 2; i < (NCH(n) - 1); i++)
757 l += num_stmts(CHILD(n, i));
758 return l;
759 }
760 default: {
761 char buf[128];
762
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000763 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 TYPE(n), NCH(n));
765 Py_FatalError(buf);
766 }
767 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700768 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769}
770
771/* Transform the CST rooted at node * to the appropriate AST
772*/
773
774mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200775PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
776 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000778 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 asdl_seq *stmts = NULL;
780 stmt_ty s;
781 node *ch;
782 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500783 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400785 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200786 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400787 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800788 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800789
790 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Jeremy Hyltona8293132006-02-28 17:58:27 +0000793 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 switch (TYPE(n)) {
795 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200796 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500798 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 for (i = 0; i < NCH(n) - 1; i++) {
800 ch = CHILD(n, i);
801 if (TYPE(ch) == NEWLINE)
802 continue;
803 REQ(ch, stmt);
804 num = num_stmts(ch);
805 if (num == 1) {
806 s = ast_for_stmt(&c, ch);
807 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500808 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000809 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 else {
812 ch = CHILD(ch, 0);
813 REQ(ch, simple_stmt);
814 for (j = 0; j < num; j++) {
815 s = ast_for_stmt(&c, CHILD(ch, j * 2));
816 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000818 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 }
820 }
821 }
INADA Naokicb41b272017-02-23 00:31:59 +0900822 res = Module(stmts, docstring_from_stmts(stmts), arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500823 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 case eval_input: {
825 expr_ty testlist_ast;
826
Nick Coghlan650f0d02007-04-15 12:05:43 +0000827 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000828 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500830 goto out;
831 res = Expression(testlist_ast, arena);
832 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 }
834 case single_input:
835 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200836 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
840 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000841 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 goto out;
843 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 }
845 else {
846 n = CHILD(n, 0);
847 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200848 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500850 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000852 s = ast_for_stmt(&c, n);
853 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500854 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 asdl_seq_SET(stmts, 0, s);
856 }
857 else {
858 /* Only a simple_stmt can contain multiple statements. */
859 REQ(n, simple_stmt);
860 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 if (TYPE(CHILD(n, i)) == NEWLINE)
862 break;
863 s = ast_for_stmt(&c, CHILD(n, i));
864 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 asdl_seq_SET(stmts, i / 2, s);
867 }
868 }
869
Benjamin Peterson55e00432012-01-16 17:22:31 -0500870 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500872 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000874 PyErr_Format(PyExc_SystemError,
875 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500876 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500878 out:
879 if (c.c_normalize) {
880 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500881 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500882 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883}
884
Victor Stinner14e461d2013-08-26 22:28:21 +0200885mod_ty
886PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
887 PyArena *arena)
888{
889 mod_ty mod;
890 PyObject *filename;
891 filename = PyUnicode_DecodeFSDefault(filename_str);
892 if (filename == NULL)
893 return NULL;
894 mod = PyAST_FromNodeObject(n, flags, filename, arena);
895 Py_DECREF(filename);
896 return mod;
897
898}
899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
901*/
902
903static operator_ty
904get_operator(const node *n)
905{
906 switch (TYPE(n)) {
907 case VBAR:
908 return BitOr;
909 case CIRCUMFLEX:
910 return BitXor;
911 case AMPER:
912 return BitAnd;
913 case LEFTSHIFT:
914 return LShift;
915 case RIGHTSHIFT:
916 return RShift;
917 case PLUS:
918 return Add;
919 case MINUS:
920 return Sub;
921 case STAR:
922 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400923 case AT:
924 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925 case SLASH:
926 return Div;
927 case DOUBLESLASH:
928 return FloorDiv;
929 case PERCENT:
930 return Mod;
931 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 }
934}
935
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200936static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000937 "None",
938 "True",
939 "False",
940 NULL,
941};
942
943static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400944forbidden_name(struct compiling *c, identifier name, const node *n,
945 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000946{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000947 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200948 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400949 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000950 return 1;
951 }
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +0200952 if (_PyUnicode_EqualToASCIIString(name, "async") ||
953 _PyUnicode_EqualToASCIIString(name, "await"))
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400954 {
955 PyObject *message = PyUnicode_FromString(
956 "'async' and 'await' will become reserved keywords"
957 " in Python 3.7");
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500958 int ret;
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400959 if (message == NULL) {
960 return 1;
961 }
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500962 ret = PyErr_WarnExplicitObject(
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400963 PyExc_DeprecationWarning,
964 message,
965 c->c_filename,
966 LINENO(n),
967 NULL,
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500968 NULL);
969 Py_DECREF(message);
970 if (ret < 0) {
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400971 return 1;
972 }
973 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000974 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200975 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000976 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200977 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400978 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000979 return 1;
980 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000981 }
982 }
983 return 0;
984}
985
Jeremy Hyltona8293132006-02-28 17:58:27 +0000986/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987
988 Only sets context for expr kinds that "can appear in assignment context"
989 (according to ../Parser/Python.asdl). For other expr kinds, it sets
990 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991*/
992
993static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000994set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995{
996 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000997 /* If a particular expression type can't be used for assign / delete,
998 set expr_name to its name and an error message will be generated.
999 */
1000 const char* expr_name = NULL;
1001
1002 /* The ast defines augmented store and load contexts, but the
1003 implementation here doesn't actually use them. The code may be
1004 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001005 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001006 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001007 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001008 */
1009 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010
1011 switch (e->kind) {
1012 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001013 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001014 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001015 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001016 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001018 e->v.Subscript.ctx = ctx;
1019 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001020 case Starred_kind:
1021 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001022 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001023 return 0;
1024 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001026 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001027 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001028 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001029 }
1030 e->v.Name.ctx = ctx;
1031 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001033 e->v.List.ctx = ctx;
1034 s = e->v.List.elts;
1035 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001037 e->v.Tuple.ctx = ctx;
1038 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001039 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001040 case Lambda_kind:
1041 expr_name = "lambda";
1042 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001044 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001045 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001046 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001048 case UnaryOp_kind:
1049 expr_name = "operator";
1050 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001052 expr_name = "generator expression";
1053 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001054 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001055 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001056 expr_name = "yield expression";
1057 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001058 case Await_kind:
1059 expr_name = "await expression";
1060 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001061 case ListComp_kind:
1062 expr_name = "list comprehension";
1063 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001064 case SetComp_kind:
1065 expr_name = "set comprehension";
1066 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001067 case DictComp_kind:
1068 expr_name = "dict comprehension";
1069 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001070 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001071 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 case Num_kind:
1073 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001074 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001075 case JoinedStr_kind:
1076 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001077 expr_name = "literal";
1078 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001079 case NameConstant_kind:
1080 expr_name = "keyword";
1081 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001082 case Ellipsis_kind:
1083 expr_name = "Ellipsis";
1084 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001085 case Compare_kind:
1086 expr_name = "comparison";
1087 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001088 case IfExp_kind:
1089 expr_name = "conditional expression";
1090 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001091 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 PyErr_Format(PyExc_SystemError,
1093 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001094 e->kind, e->lineno);
1095 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001097 /* Check for error string set by switch */
1098 if (expr_name) {
1099 char buf[300];
1100 PyOS_snprintf(buf, sizeof(buf),
1101 "can't %s %s",
1102 ctx == Store ? "assign to" : "delete",
1103 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001104 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001105 }
1106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 */
1110 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001111 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112
Thomas Wouters89f507f2006-12-13 04:49:30 +00001113 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001114 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001115 return 0;
1116 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 }
1118 return 1;
1119}
1120
1121static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001122ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123{
1124 REQ(n, augassign);
1125 n = CHILD(n, 0);
1126 switch (STR(n)[0]) {
1127 case '+':
1128 return Add;
1129 case '-':
1130 return Sub;
1131 case '/':
1132 if (STR(n)[1] == '/')
1133 return FloorDiv;
1134 else
1135 return Div;
1136 case '%':
1137 return Mod;
1138 case '<':
1139 return LShift;
1140 case '>':
1141 return RShift;
1142 case '&':
1143 return BitAnd;
1144 case '^':
1145 return BitXor;
1146 case '|':
1147 return BitOr;
1148 case '*':
1149 if (STR(n)[1] == '*')
1150 return Pow;
1151 else
1152 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001153 case '@':
1154 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001156 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001157 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 }
1159}
1160
1161static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001162ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001164 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 |'is' 'not'
1166 */
1167 REQ(n, comp_op);
1168 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001169 n = CHILD(n, 0);
1170 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 case LESS:
1172 return Lt;
1173 case GREATER:
1174 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001175 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 return Eq;
1177 case LESSEQUAL:
1178 return LtE;
1179 case GREATEREQUAL:
1180 return GtE;
1181 case NOTEQUAL:
1182 return NotEq;
1183 case NAME:
1184 if (strcmp(STR(n), "in") == 0)
1185 return In;
1186 if (strcmp(STR(n), "is") == 0)
1187 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001188 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001190 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001192 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 }
1195 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001196 /* handle "not in" and "is not" */
1197 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 case NAME:
1199 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1200 return NotIn;
1201 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1202 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001203 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001205 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001207 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 }
Neal Norwitz79792652005-11-14 04:25:03 +00001210 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001212 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213}
1214
1215static asdl_seq *
1216seq_for_testlist(struct compiling *c, const node *n)
1217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001219 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1220 */
Armin Rigo31441302005-10-21 12:57:31 +00001221 asdl_seq *seq;
1222 expr_ty expression;
1223 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001224 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001226 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 if (!seq)
1228 return NULL;
1229
1230 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001232 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233
Benjamin Peterson4905e802009-09-27 02:43:28 +00001234 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001235 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237
1238 assert(i / 2 < seq->size);
1239 asdl_seq_SET(seq, i / 2, expression);
1240 }
1241 return seq;
1242}
1243
Neal Norwitzc1505362006-12-28 06:47:50 +00001244static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001245ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001246{
1247 identifier name;
1248 expr_ty annotation = NULL;
1249 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001250 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001251
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001252 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001253 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001254 name = NEW_IDENTIFIER(ch);
1255 if (!name)
1256 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001257 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001258 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001259
1260 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1261 annotation = ast_for_expr(c, CHILD(n, 2));
1262 if (!annotation)
1263 return NULL;
1264 }
1265
Victor Stinnerc106c682015-11-06 17:01:48 +01001266 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001267 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001268 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001269 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270}
1271
Guido van Rossum4f72a782006-10-27 23:31:49 +00001272/* returns -1 if failed to handle keyword only arguments
1273 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001274 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001275 ^^^
1276 start pointing here
1277 */
1278static int
1279handle_keywordonly_args(struct compiling *c, const node *n, int start,
1280 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1281{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001282 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001283 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001284 expr_ty expression, annotation;
1285 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 int i = start;
1287 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001288
1289 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001290 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001291 return -1;
1292 }
1293 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001294 while (i < NCH(n)) {
1295 ch = CHILD(n, i);
1296 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001297 case vfpdef:
1298 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001299 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001300 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001301 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001302 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001303 asdl_seq_SET(kwdefaults, j, expression);
1304 i += 2; /* '=' and test */
1305 }
1306 else { /* setting NULL if no default value exists */
1307 asdl_seq_SET(kwdefaults, j, NULL);
1308 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001309 if (NCH(ch) == 3) {
1310 /* ch is NAME ':' test */
1311 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001312 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001314 }
1315 else {
1316 annotation = NULL;
1317 }
1318 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001319 argname = NEW_IDENTIFIER(ch);
1320 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001321 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001322 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001323 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001324 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1325 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001326 if (!arg)
1327 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001328 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001329 i += 2; /* the name and the comma */
1330 break;
1331 case DOUBLESTAR:
1332 return i;
1333 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001334 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001335 goto error;
1336 }
1337 }
1338 return i;
1339 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001341}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342
Jeremy Hyltona8293132006-02-28 17:58:27 +00001343/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344
1345static arguments_ty
1346ast_for_arguments(struct compiling *c, const node *n)
1347{
Neal Norwitzc1505362006-12-28 06:47:50 +00001348 /* This function handles both typedargslist (function definition)
1349 and varargslist (lambda definition).
1350
1351 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001352 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1353 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1354 | '**' tfpdef [',']]]
1355 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1356 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001357 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001358 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1359 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1360 | '**' vfpdef [',']]]
1361 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1362 | '**' vfpdef [',']
1363 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001364 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001365
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001367 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1368 int nposdefaults = 0, found_default = 0;
1369 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001370 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001371 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 node *ch;
1373
1374 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001376 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001377 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001379 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380
Jeremy Hyltone921e022008-07-17 16:37:17 +00001381 /* First count the number of positional args & defaults. The
1382 variable i is the loop index for this for loop and the next.
1383 The next loop picks up where the first leaves off.
1384 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001386 ch = CHILD(n, i);
1387 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001388 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001389 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001390 if (i < NCH(n) && /* skip argument following star */
1391 (TYPE(CHILD(n, i)) == tfpdef ||
1392 TYPE(CHILD(n, i)) == vfpdef)) {
1393 i++;
1394 }
1395 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001396 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001397 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001398 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001399 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 defaults for keyword only args */
1403 for ( ; i < NCH(n); ++i) {
1404 ch = CHILD(n, i);
1405 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001406 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001408 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001409 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001410 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001412 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001414 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001416 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001418 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 since we set NULL as default for keyword only argument w/o default
1421 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001422 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001423 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001425 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001426
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001427 /* tfpdef: NAME [':' test]
1428 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 */
1430 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001431 j = 0; /* index for defaults */
1432 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001434 ch = CHILD(n, i);
1435 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001436 case tfpdef:
1437 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1439 anything other than EQUAL or a comma? */
1440 /* XXX Should NCH(n) check be made a separate check? */
1441 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001442 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1443 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001444 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 assert(posdefaults != NULL);
1446 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001450 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001451 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001452 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001453 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001454 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001455 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001456 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001457 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001458 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 i += 2; /* the name and the comma */
1460 break;
1461 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001462 if (i+1 >= NCH(n) ||
1463 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001464 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001465 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001466 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001467 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001468 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001469 if (TYPE(ch) == COMMA) {
1470 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001471 i += 2; /* now follows keyword only arguments */
1472 res = handle_keywordonly_args(c, n, i,
1473 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001474 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001475 i = res; /* res has new position to process */
1476 }
1477 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001478 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001479 if (!vararg)
1480 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001481
Guido van Rossum4f72a782006-10-27 23:31:49 +00001482 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001483 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1484 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001485 int res = 0;
1486 res = handle_keywordonly_args(c, n, i,
1487 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001488 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001489 i = res; /* res has new position to process */
1490 }
1491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492 break;
1493 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001494 ch = CHILD(n, i+1); /* tfpdef */
1495 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001496 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001497 if (!kwarg)
1498 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 i += 3;
1500 break;
1501 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001502 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 "unexpected node in varargslist: %d @ %d",
1504 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001505 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001506 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001508 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509}
1510
1511static expr_ty
1512ast_for_dotted_name(struct compiling *c, const node *n)
1513{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001514 expr_ty e;
1515 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001516 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 int i;
1518
1519 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001520
1521 lineno = LINENO(n);
1522 col_offset = n->n_col_offset;
1523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 id = NEW_IDENTIFIER(CHILD(n, 0));
1525 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001526 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001527 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001529 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530
1531 for (i = 2; i < NCH(n); i+=2) {
1532 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001533 if (!id)
1534 return NULL;
1535 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1536 if (!e)
1537 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 }
1539
1540 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541}
1542
1543static expr_ty
1544ast_for_decorator(struct compiling *c, const node *n)
1545{
1546 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1547 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001548 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001551 REQ(CHILD(n, 0), AT);
1552 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1555 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001556 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001559 d = name_expr;
1560 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 }
1562 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001563 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001564 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001565 if (!d)
1566 return NULL;
1567 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568 }
1569 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001570 d = ast_for_call(c, CHILD(n, 3), name_expr);
1571 if (!d)
1572 return NULL;
1573 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 }
1575
1576 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577}
1578
1579static asdl_seq*
1580ast_for_decorators(struct compiling *c, const node *n)
1581{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001582 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001583 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001587 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 if (!decorator_seq)
1589 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001592 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001593 if (!d)
1594 return NULL;
1595 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 }
1597 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
1600static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001601ast_for_funcdef_impl(struct compiling *c, const node *n,
1602 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001604 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001605 identifier name;
1606 arguments_ty args;
1607 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001608 expr_ty returns = NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09001609 string docstring;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001610 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611
1612 REQ(n, funcdef);
1613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614 name = NEW_IDENTIFIER(CHILD(n, name_i));
1615 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001616 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001617 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001618 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1620 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001621 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001622 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1623 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1624 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001625 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001626 name_i += 2;
1627 }
INADA Naokicb41b272017-02-23 00:31:59 +09001628 body = ast_for_body(c, CHILD(n, name_i + 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631
Yury Selivanov75445082015-05-11 22:57:16 -04001632 if (is_async)
1633 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001634 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001635 n->n_col_offset, c->c_arena);
1636 else
1637 return FunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001638 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001639 n->n_col_offset, c->c_arena);
1640}
1641
1642static stmt_ty
1643ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1644{
1645 /* async_funcdef: ASYNC funcdef */
1646 REQ(n, async_funcdef);
1647 REQ(CHILD(n, 0), ASYNC);
1648 REQ(CHILD(n, 1), funcdef);
1649
1650 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1651 1 /* is_async */);
1652}
1653
1654static stmt_ty
1655ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1656{
1657 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1658 return ast_for_funcdef_impl(c, n, decorator_seq,
1659 0 /* is_async */);
1660}
1661
1662
1663static stmt_ty
1664ast_for_async_stmt(struct compiling *c, const node *n)
1665{
1666 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1667 REQ(n, async_stmt);
1668 REQ(CHILD(n, 0), ASYNC);
1669
1670 switch (TYPE(CHILD(n, 1))) {
1671 case funcdef:
1672 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1673 1 /* is_async */);
1674 case with_stmt:
1675 return ast_for_with_stmt(c, CHILD(n, 1),
1676 1 /* is_async */);
1677
1678 case for_stmt:
1679 return ast_for_for_stmt(c, CHILD(n, 1),
1680 1 /* is_async */);
1681
1682 default:
1683 PyErr_Format(PyExc_SystemError,
1684 "invalid async stament: %s",
1685 STR(CHILD(n, 1)));
1686 return NULL;
1687 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688}
1689
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001690static stmt_ty
1691ast_for_decorated(struct compiling *c, const node *n)
1692{
Yury Selivanov75445082015-05-11 22:57:16 -04001693 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001694 stmt_ty thing = NULL;
1695 asdl_seq *decorator_seq = NULL;
1696
1697 REQ(n, decorated);
1698
1699 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1700 if (!decorator_seq)
1701 return NULL;
1702
1703 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001704 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001706
1707 if (TYPE(CHILD(n, 1)) == funcdef) {
1708 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1709 } else if (TYPE(CHILD(n, 1)) == classdef) {
1710 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001711 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1712 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001713 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001714 /* we count the decorators in when talking about the class' or
1715 * function's line number */
1716 if (thing) {
1717 thing->lineno = LINENO(n);
1718 thing->col_offset = n->n_col_offset;
1719 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001720 return thing;
1721}
1722
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723static expr_ty
1724ast_for_lambdef(struct compiling *c, const node *n)
1725{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001726 /* lambdef: 'lambda' [varargslist] ':' test
1727 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 arguments_ty args;
1729 expr_ty expression;
1730
1731 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001732 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 if (!args)
1734 return NULL;
1735 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001736 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 }
1739 else {
1740 args = ast_for_arguments(c, CHILD(n, 1));
1741 if (!args)
1742 return NULL;
1743 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001744 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 }
1747
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001748 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001751static expr_ty
1752ast_for_ifexpr(struct compiling *c, const node *n)
1753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001755 expr_ty expression, body, orelse;
1756
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001757 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001758 body = ast_for_expr(c, CHILD(n, 0));
1759 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001760 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001761 expression = ast_for_expr(c, CHILD(n, 2));
1762 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001763 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001764 orelse = ast_for_expr(c, CHILD(n, 4));
1765 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001766 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001767 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1768 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001769}
1770
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001772 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773
Nick Coghlan650f0d02007-04-15 12:05:43 +00001774 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775*/
1776
1777static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001778count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001781 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782
Guido van Rossumd8faa362007-04-27 19:54:29 +00001783 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001784 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001785 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001786 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001787 if (TYPE(CHILD(n, 0)) == ASYNC) {
1788 is_async = 1;
1789 }
1790 if (NCH(n) == (5 + is_async)) {
1791 n = CHILD(n, 4 + is_async);
1792 }
1793 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001794 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001795 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001796 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001797 REQ(n, comp_iter);
1798 n = CHILD(n, 0);
1799 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001800 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001801 else if (TYPE(n) == comp_if) {
1802 if (NCH(n) == 3) {
1803 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001804 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001805 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001806 else
1807 return n_fors;
1808 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001809
Guido van Rossumd8faa362007-04-27 19:54:29 +00001810 /* Should never be reached */
1811 PyErr_SetString(PyExc_SystemError,
1812 "logic error in count_comp_fors");
1813 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814}
1815
Nick Coghlan650f0d02007-04-15 12:05:43 +00001816/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817
Nick Coghlan650f0d02007-04-15 12:05:43 +00001818 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819*/
1820
1821static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001822count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001824 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825
Guido van Rossumd8faa362007-04-27 19:54:29 +00001826 while (1) {
1827 REQ(n, comp_iter);
1828 if (TYPE(CHILD(n, 0)) == comp_for)
1829 return n_ifs;
1830 n = CHILD(n, 0);
1831 REQ(n, comp_if);
1832 n_ifs++;
1833 if (NCH(n) == 2)
1834 return n_ifs;
1835 n = CHILD(n, 2);
1836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837}
1838
Guido van Rossum992d4a32007-07-11 13:09:30 +00001839static asdl_seq *
1840ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001843 asdl_seq *comps;
1844
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001845 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 if (n_fors == -1)
1847 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001848
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001849 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001850 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001854 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001856 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001857 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001858 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859
Guido van Rossum992d4a32007-07-11 13:09:30 +00001860 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001862 if (TYPE(CHILD(n, 0)) == ASYNC) {
1863 is_async = 1;
1864 }
1865
1866 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001867 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001868 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001870 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001873
Thomas Wouters89f507f2006-12-13 04:49:30 +00001874 /* Check the # of children rather than the length of t, since
1875 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001876 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001877 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001878 comp = comprehension(first, expression, NULL,
1879 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001881 comp = comprehension(Tuple(t, Store, first->lineno,
1882 first->col_offset, c->c_arena),
1883 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001884 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001887 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 int j, n_ifs;
1889 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001891 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001892 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001893 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001896 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001897 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 REQ(n, comp_iter);
1902 n = CHILD(n, 0);
1903 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904
Guido van Rossum992d4a32007-07-11 13:09:30 +00001905 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001906 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001907 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001908 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001909 if (NCH(n) == 3)
1910 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001912 /* on exit, must guarantee that n is a comp_for */
1913 if (TYPE(n) == comp_iter)
1914 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001915 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001917 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001919 return comps;
1920}
1921
1922static expr_ty
1923ast_for_itercomp(struct compiling *c, const node *n, int type)
1924{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001925 /* testlist_comp: (test|star_expr)
1926 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001927 expr_ty elt;
1928 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001929 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930
Guido van Rossum992d4a32007-07-11 13:09:30 +00001931 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001933 ch = CHILD(n, 0);
1934 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001935 if (!elt)
1936 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001937 if (elt->kind == Starred_kind) {
1938 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1939 return NULL;
1940 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941
Guido van Rossum992d4a32007-07-11 13:09:30 +00001942 comps = ast_for_comprehension(c, CHILD(n, 1));
1943 if (!comps)
1944 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001945
1946 if (type == COMP_GENEXP)
1947 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1948 else if (type == COMP_LISTCOMP)
1949 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1950 else if (type == COMP_SETCOMP)
1951 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1952 else
1953 /* Should never happen */
1954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955}
1956
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001957/* Fills in the key, value pair corresponding to the dict element. In case
1958 * of an unpacking, key is NULL. *i is advanced by the number of ast
1959 * elements. Iff successful, nonzero is returned.
1960 */
1961static int
1962ast_for_dictelement(struct compiling *c, const node *n, int *i,
1963 expr_ty *key, expr_ty *value)
1964{
1965 expr_ty expression;
1966 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1967 assert(NCH(n) - *i >= 2);
1968
1969 expression = ast_for_expr(c, CHILD(n, *i + 1));
1970 if (!expression)
1971 return 0;
1972 *key = NULL;
1973 *value = expression;
1974
1975 *i += 2;
1976 }
1977 else {
1978 assert(NCH(n) - *i >= 3);
1979
1980 expression = ast_for_expr(c, CHILD(n, *i));
1981 if (!expression)
1982 return 0;
1983 *key = expression;
1984
1985 REQ(CHILD(n, *i + 1), COLON);
1986
1987 expression = ast_for_expr(c, CHILD(n, *i + 2));
1988 if (!expression)
1989 return 0;
1990 *value = expression;
1991
1992 *i += 3;
1993 }
1994 return 1;
1995}
1996
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001998ast_for_dictcomp(struct compiling *c, const node *n)
1999{
2000 expr_ty key, value;
2001 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002002 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002004 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002005 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002006 assert(key);
2007 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002009 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002010 if (!comps)
2011 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012
Guido van Rossum992d4a32007-07-11 13:09:30 +00002013 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2014}
2015
2016static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002017ast_for_dictdisplay(struct compiling *c, const node *n)
2018{
2019 int i;
2020 int j;
2021 int size;
2022 asdl_seq *keys, *values;
2023
2024 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2025 keys = _Py_asdl_seq_new(size, c->c_arena);
2026 if (!keys)
2027 return NULL;
2028
2029 values = _Py_asdl_seq_new(size, c->c_arena);
2030 if (!values)
2031 return NULL;
2032
2033 j = 0;
2034 for (i = 0; i < NCH(n); i++) {
2035 expr_ty key, value;
2036
2037 if (!ast_for_dictelement(c, n, &i, &key, &value))
2038 return NULL;
2039 asdl_seq_SET(keys, j, key);
2040 asdl_seq_SET(values, j, value);
2041
2042 j++;
2043 }
2044 keys->size = j;
2045 values->size = j;
2046 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2047}
2048
2049static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002050ast_for_genexp(struct compiling *c, const node *n)
2051{
2052 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002053 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002054}
2055
2056static expr_ty
2057ast_for_listcomp(struct compiling *c, const node *n)
2058{
2059 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002060 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002061}
2062
2063static expr_ty
2064ast_for_setcomp(struct compiling *c, const node *n)
2065{
2066 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002067 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002068}
2069
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002070static expr_ty
2071ast_for_setdisplay(struct compiling *c, const node *n)
2072{
2073 int i;
2074 int size;
2075 asdl_seq *elts;
2076
2077 assert(TYPE(n) == (dictorsetmaker));
2078 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2079 elts = _Py_asdl_seq_new(size, c->c_arena);
2080 if (!elts)
2081 return NULL;
2082 for (i = 0; i < NCH(n); i += 2) {
2083 expr_ty expression;
2084 expression = ast_for_expr(c, CHILD(n, i));
2085 if (!expression)
2086 return NULL;
2087 asdl_seq_SET(elts, i / 2, expression);
2088 }
2089 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2090}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002091
2092static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093ast_for_atom(struct compiling *c, const node *n)
2094{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002095 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2096 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002097 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 */
2099 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002102 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002103 PyObject *name;
2104 const char *s = STR(ch);
2105 size_t len = strlen(s);
2106 if (len >= 4 && len <= 5) {
2107 if (!strcmp(s, "None"))
2108 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2109 if (!strcmp(s, "True"))
2110 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2111 if (!strcmp(s, "False"))
2112 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2113 }
2114 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002115 if (!name)
2116 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002117 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002118 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2119 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002121 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002122 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002123 const char *errtype = NULL;
2124 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2125 errtype = "unicode error";
2126 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2127 errtype = "value error";
2128 if (errtype) {
2129 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002130 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002131 PyObject *type, *value, *tback, *errstr;
2132 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002133 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002134 if (errstr)
2135 s = PyUnicode_AsUTF8(errstr);
2136 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002137 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002138 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002139 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002140 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002141 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002142 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002143 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002144 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002145 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002146 Py_XDECREF(tback);
2147 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002148 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002149 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002150 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 }
2152 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002153 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002154 if (!pynum)
2155 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002156
Victor Stinner43d81952013-07-17 00:57:58 +02002157 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2158 Py_DECREF(pynum);
2159 return NULL;
2160 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002161 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 }
Georg Brandldde00282007-03-18 19:01:53 +00002163 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002164 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002166 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167
Thomas Wouters89f507f2006-12-13 04:49:30 +00002168 if (TYPE(ch) == RPAR)
2169 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170
Thomas Wouters89f507f2006-12-13 04:49:30 +00002171 if (TYPE(ch) == yield_expr)
2172 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002175 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002176 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002177
Nick Coghlan650f0d02007-04-15 12:05:43 +00002178 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002180 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181
Thomas Wouters89f507f2006-12-13 04:49:30 +00002182 if (TYPE(ch) == RSQB)
2183 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184
Nick Coghlan650f0d02007-04-15 12:05:43 +00002185 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002186 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2187 asdl_seq *elts = seq_for_testlist(c, ch);
2188 if (!elts)
2189 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002190
Thomas Wouters89f507f2006-12-13 04:49:30 +00002191 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2192 }
2193 else
2194 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002196 /* dictorsetmaker: ( ((test ':' test | '**' test)
2197 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2198 * ((test | '*' test)
2199 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002200 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002201 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002202 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002203 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002204 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002205 }
2206 else {
2207 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2208 if (NCH(ch) == 1 ||
2209 (NCH(ch) > 1 &&
2210 TYPE(CHILD(ch, 1)) == COMMA)) {
2211 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002212 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002213 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214 else if (NCH(ch) > 1 &&
2215 TYPE(CHILD(ch, 1)) == comp_for) {
2216 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002217 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002218 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002219 else if (NCH(ch) > 3 - is_dict &&
2220 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2221 /* It's a dictionary comprehension. */
2222 if (is_dict) {
2223 ast_error(c, n, "dict unpacking cannot be used in "
2224 "dict comprehension");
2225 return NULL;
2226 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002227 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002228 }
2229 else {
2230 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002231 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002232 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002233 if (res) {
2234 res->lineno = LINENO(n);
2235 res->col_offset = n->n_col_offset;
2236 }
2237 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002238 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002241 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2242 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 }
2244}
2245
2246static slice_ty
2247ast_for_slice(struct compiling *c, const node *n)
2248{
2249 node *ch;
2250 expr_ty lower = NULL, upper = NULL, step = NULL;
2251
2252 REQ(n, subscript);
2253
2254 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002255 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 sliceop: ':' [test]
2257 */
2258 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 if (NCH(n) == 1 && TYPE(ch) == test) {
2260 /* 'step' variable hold no significance in terms of being used over
2261 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 if (!step)
2264 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265
Thomas Wouters89f507f2006-12-13 04:49:30 +00002266 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 }
2268
2269 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002270 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 if (!lower)
2272 return NULL;
2273 }
2274
2275 /* If there's an upper bound it's in the second or third position. */
2276 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002277 if (NCH(n) > 1) {
2278 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279
Thomas Wouters89f507f2006-12-13 04:49:30 +00002280 if (TYPE(n2) == test) {
2281 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 if (!upper)
2283 return NULL;
2284 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002287 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288
Thomas Wouters89f507f2006-12-13 04:49:30 +00002289 if (TYPE(n2) == test) {
2290 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 if (!upper)
2292 return NULL;
2293 }
2294 }
2295
2296 ch = CHILD(n, NCH(n) - 1);
2297 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002298 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002299 ch = CHILD(ch, 1);
2300 if (TYPE(ch) == test) {
2301 step = ast_for_expr(c, ch);
2302 if (!step)
2303 return NULL;
2304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 }
2306 }
2307
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002308 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309}
2310
2311static expr_ty
2312ast_for_binop(struct compiling *c, const node *n)
2313{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002316 BinOp(BinOp(A, op, B), op, C).
2317 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318
Guido van Rossumd8faa362007-04-27 19:54:29 +00002319 int i, nops;
2320 expr_ty expr1, expr2, result;
2321 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322
Guido van Rossumd8faa362007-04-27 19:54:29 +00002323 expr1 = ast_for_expr(c, CHILD(n, 0));
2324 if (!expr1)
2325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326
Guido van Rossumd8faa362007-04-27 19:54:29 +00002327 expr2 = ast_for_expr(c, CHILD(n, 2));
2328 if (!expr2)
2329 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330
Guido van Rossumd8faa362007-04-27 19:54:29 +00002331 newoperator = get_operator(CHILD(n, 1));
2332 if (!newoperator)
2333 return NULL;
2334
2335 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2336 c->c_arena);
2337 if (!result)
2338 return NULL;
2339
2340 nops = (NCH(n) - 1) / 2;
2341 for (i = 1; i < nops; i++) {
2342 expr_ty tmp_result, tmp;
2343 const node* next_oper = CHILD(n, i * 2 + 1);
2344
2345 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002346 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 return NULL;
2348
Guido van Rossumd8faa362007-04-27 19:54:29 +00002349 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2350 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 return NULL;
2352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002354 LINENO(next_oper), next_oper->n_col_offset,
2355 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002357 return NULL;
2358 result = tmp_result;
2359 }
2360 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361}
2362
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002363static expr_ty
2364ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002367 subscriptlist: subscript (',' subscript)* [',']
2368 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2369 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002370 REQ(n, trailer);
2371 if (TYPE(CHILD(n, 0)) == LPAR) {
2372 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002373 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002374 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002375 else
2376 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002377 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002378 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002379 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2380 if (!attr_id)
2381 return NULL;
2382 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002383 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002384 }
2385 else {
2386 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002387 REQ(CHILD(n, 2), RSQB);
2388 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002389 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002390 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2391 if (!slc)
2392 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002393 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2394 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002395 }
2396 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002398 by treating the sequence as a tuple literal if there are
2399 no slice features.
2400 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002401 int j;
2402 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002403 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002404 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002405 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002406 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002407 if (!slices)
2408 return NULL;
2409 for (j = 0; j < NCH(n); j += 2) {
2410 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002411 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002412 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002413 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002414 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002415 asdl_seq_SET(slices, j / 2, slc);
2416 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002417 if (!simple) {
2418 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002419 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002420 }
2421 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002422 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002423 if (!elts)
2424 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002425 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2426 slc = (slice_ty)asdl_seq_GET(slices, j);
2427 assert(slc->kind == Index_kind && slc->v.Index.value);
2428 asdl_seq_SET(elts, j, slc->v.Index.value);
2429 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002430 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002431 if (!e)
2432 return NULL;
2433 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002434 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002435 }
2436 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002437}
2438
2439static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002440ast_for_factor(struct compiling *c, const node *n)
2441{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002442 expr_ty expression;
2443
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002444 expression = ast_for_expr(c, CHILD(n, 1));
2445 if (!expression)
2446 return NULL;
2447
2448 switch (TYPE(CHILD(n, 0))) {
2449 case PLUS:
2450 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2451 c->c_arena);
2452 case MINUS:
2453 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2454 c->c_arena);
2455 case TILDE:
2456 return UnaryOp(Invert, expression, LINENO(n),
2457 n->n_col_offset, c->c_arena);
2458 }
2459 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2460 TYPE(CHILD(n, 0)));
2461 return NULL;
2462}
2463
2464static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002465ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002466{
Yury Selivanov75445082015-05-11 22:57:16 -04002467 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002468 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002469
2470 REQ(n, atom_expr);
2471 nch = NCH(n);
2472
2473 if (TYPE(CHILD(n, 0)) == AWAIT) {
2474 start = 1;
2475 assert(nch > 1);
2476 }
2477
2478 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002479 if (!e)
2480 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002481 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002482 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002483 if (start && nch == 2) {
2484 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2485 }
2486
2487 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002488 node *ch = CHILD(n, i);
2489 if (TYPE(ch) != trailer)
2490 break;
2491 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002492 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002493 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002494 tmp->lineno = e->lineno;
2495 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002496 e = tmp;
2497 }
Yury Selivanov75445082015-05-11 22:57:16 -04002498
2499 if (start) {
2500 /* there was an AWAIT */
2501 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2502 }
2503 else {
2504 return e;
2505 }
2506}
2507
2508static expr_ty
2509ast_for_power(struct compiling *c, const node *n)
2510{
2511 /* power: atom trailer* ('**' factor)*
2512 */
2513 expr_ty e;
2514 REQ(n, power);
2515 e = ast_for_atom_expr(c, CHILD(n, 0));
2516 if (!e)
2517 return NULL;
2518 if (NCH(n) == 1)
2519 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002520 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2521 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002522 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002523 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002524 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002525 }
2526 return e;
2527}
2528
Guido van Rossum0368b722007-05-11 16:50:42 +00002529static expr_ty
2530ast_for_starred(struct compiling *c, const node *n)
2531{
2532 expr_ty tmp;
2533 REQ(n, star_expr);
2534
2535 tmp = ast_for_expr(c, CHILD(n, 1));
2536 if (!tmp)
2537 return NULL;
2538
2539 /* The Load context is changed later. */
2540 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2541}
2542
2543
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544/* Do not name a variable 'expr'! Will cause a compile error.
2545*/
2546
2547static expr_ty
2548ast_for_expr(struct compiling *c, const node *n)
2549{
2550 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002551 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002552 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 and_test: not_test ('and' not_test)*
2555 not_test: 'not' not_test | comparison
2556 comparison: expr (comp_op expr)*
2557 expr: xor_expr ('|' xor_expr)*
2558 xor_expr: and_expr ('^' and_expr)*
2559 and_expr: shift_expr ('&' shift_expr)*
2560 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2561 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002562 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002564 power: atom_expr ['**' factor]
2565 atom_expr: [AWAIT] atom trailer*
2566 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 */
2568
2569 asdl_seq *seq;
2570 int i;
2571
2572 loop:
2573 switch (TYPE(n)) {
2574 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002575 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002576 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002577 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002579 else if (NCH(n) > 1)
2580 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002581 /* Fallthrough */
2582 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 case and_test:
2584 if (NCH(n) == 1) {
2585 n = CHILD(n, 0);
2586 goto loop;
2587 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002588 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 if (!seq)
2590 return NULL;
2591 for (i = 0; i < NCH(n); i += 2) {
2592 expr_ty e = ast_for_expr(c, CHILD(n, i));
2593 if (!e)
2594 return NULL;
2595 asdl_seq_SET(seq, i / 2, e);
2596 }
2597 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002598 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2599 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002600 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002601 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 case not_test:
2603 if (NCH(n) == 1) {
2604 n = CHILD(n, 0);
2605 goto loop;
2606 }
2607 else {
2608 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2609 if (!expression)
2610 return NULL;
2611
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002612 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2613 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 }
2615 case comparison:
2616 if (NCH(n) == 1) {
2617 n = CHILD(n, 0);
2618 goto loop;
2619 }
2620 else {
2621 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002622 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002623 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002624 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 if (!ops)
2626 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002627 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 return NULL;
2630 }
2631 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002632 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002634 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002635 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638
2639 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002640 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002644 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 asdl_seq_SET(cmps, i / 2, expression);
2646 }
2647 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002648 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002650 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002652 return Compare(expression, ops, cmps, LINENO(n),
2653 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 }
2655 break;
2656
Guido van Rossum0368b722007-05-11 16:50:42 +00002657 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 /* The next five cases all handle BinOps. The main body of code
2660 is the same in each case, but the switch turned inside out to
2661 reuse the code for each type of operator.
2662 */
2663 case expr:
2664 case xor_expr:
2665 case and_expr:
2666 case shift_expr:
2667 case arith_expr:
2668 case term:
2669 if (NCH(n) == 1) {
2670 n = CHILD(n, 0);
2671 goto loop;
2672 }
2673 return ast_for_binop(c, n);
2674 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002675 node *an = NULL;
2676 node *en = NULL;
2677 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002678 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002679 if (NCH(n) > 1)
2680 an = CHILD(n, 1); /* yield_arg */
2681 if (an) {
2682 en = CHILD(an, NCH(an) - 1);
2683 if (NCH(an) == 2) {
2684 is_from = 1;
2685 exp = ast_for_expr(c, en);
2686 }
2687 else
2688 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002689 if (!exp)
2690 return NULL;
2691 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002692 if (is_from)
2693 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2694 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002695 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002696 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 if (NCH(n) == 1) {
2698 n = CHILD(n, 0);
2699 goto loop;
2700 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002701 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002702 case power:
2703 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002705 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 return NULL;
2707 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002708 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 return NULL;
2710}
2711
2712static expr_ty
2713ast_for_call(struct compiling *c, const node *n, expr_ty func)
2714{
2715 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 arglist: argument (',' argument)* [',']
2717 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 */
2719
2720 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002721 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002722 asdl_seq *args;
2723 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724
2725 REQ(n, arglist);
2726
2727 nargs = 0;
2728 nkeywords = 0;
2729 ngens = 0;
2730 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002731 node *ch = CHILD(n, i);
2732 if (TYPE(ch) == argument) {
2733 if (NCH(ch) == 1)
2734 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002735 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002736 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002737 else if (TYPE(CHILD(ch, 0)) == STAR)
2738 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002740 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002741 nkeywords++;
2742 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 }
2744 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002745 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002746 "if not sole argument");
2747 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 }
2749
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002750 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002752 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002753 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002755 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002756
2757 nargs = 0; /* positional arguments + iterable argument unpackings */
2758 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2759 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002761 node *ch = CHILD(n, i);
2762 if (TYPE(ch) == argument) {
2763 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002764 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002765 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002766 /* a positional argument */
2767 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002768 if (ndoublestars) {
2769 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002770 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002771 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002772 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002773 else {
2774 ast_error(c, chch,
2775 "positional argument follows "
2776 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002777 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002778 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002779 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002780 e = ast_for_expr(c, chch);
2781 if (!e)
2782 return NULL;
2783 asdl_seq_SET(args, nargs++, e);
2784 }
2785 else if (TYPE(chch) == STAR) {
2786 /* an iterable argument unpacking */
2787 expr_ty starred;
2788 if (ndoublestars) {
2789 ast_error(c, chch,
2790 "iterable argument unpacking follows "
2791 "keyword argument unpacking");
2792 return NULL;
2793 }
2794 e = ast_for_expr(c, CHILD(ch, 1));
2795 if (!e)
2796 return NULL;
2797 starred = Starred(e, Load, LINENO(chch),
2798 chch->n_col_offset,
2799 c->c_arena);
2800 if (!starred)
2801 return NULL;
2802 asdl_seq_SET(args, nargs++, starred);
2803
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002804 }
2805 else if (TYPE(chch) == DOUBLESTAR) {
2806 /* a keyword argument unpacking */
2807 keyword_ty kw;
2808 i++;
2809 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002811 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002812 kw = keyword(NULL, e, c->c_arena);
2813 asdl_seq_SET(keywords, nkeywords++, kw);
2814 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002816 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002817 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002818 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002820 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002821 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002823 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002824 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002825 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002826 identifier key, tmp;
2827 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002829 /* chch is test, but must be an identifier? */
2830 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002832 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 /* f(lambda x: x[0] = 3) ends up getting parsed with
2834 * LHS test = lambda x: x[0], and RHS test = 3.
2835 * SF bug 132313 points out that complaining about a keyword
2836 * then is very confusing.
2837 */
2838 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002839 ast_error(c, chch,
2840 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002841 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002842 }
2843 else if (e->kind != Name_kind) {
2844 ast_error(c, chch,
2845 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002846 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002847 }
2848 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002849 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002851 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002852 for (k = 0; k < nkeywords; k++) {
2853 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002854 if (tmp && !PyUnicode_Compare(tmp, key)) {
2855 ast_error(c, chch,
2856 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002857 return NULL;
2858 }
2859 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002860 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002862 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002863 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002865 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002866 asdl_seq_SET(keywords, nkeywords++, kw);
2867 }
2868 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 }
2870
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002871 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872}
2873
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002877 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002878 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002880 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002881 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002882 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002883 }
2884 else {
2885 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002886 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002887 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002889 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 else {
2891 asdl_seq *tmp = seq_for_testlist(c, n);
2892 if (!tmp)
2893 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002894 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002896}
2897
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898static stmt_ty
2899ast_for_expr_stmt(struct compiling *c, const node *n)
2900{
2901 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002902 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2903 ('=' (yield_expr|testlist_star_expr))*)
2904 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002905 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002906 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002907 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002908 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 */
2910
2911 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002912 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 if (!e)
2914 return NULL;
2915
Thomas Wouters89f507f2006-12-13 04:49:30 +00002916 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 }
2918 else if (TYPE(CHILD(n, 1)) == augassign) {
2919 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002920 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002921 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922
Thomas Wouters89f507f2006-12-13 04:49:30 +00002923 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 if (!expr1)
2925 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002926 if(!set_context(c, expr1, Store, ch))
2927 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002928 /* set_context checks that most expressions are not the left side.
2929 Augmented assignments can only have a name, a subscript, or an
2930 attribute on the left, though, so we have to explicitly check for
2931 those. */
2932 switch (expr1->kind) {
2933 case Name_kind:
2934 case Attribute_kind:
2935 case Subscript_kind:
2936 break;
2937 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002938 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002939 return NULL;
2940 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941
Thomas Wouters89f507f2006-12-13 04:49:30 +00002942 ch = CHILD(n, 2);
2943 if (TYPE(ch) == testlist)
2944 expr2 = ast_for_testlist(c, ch);
2945 else
2946 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002947 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 return NULL;
2949
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002950 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002951 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 return NULL;
2953
Thomas Wouters89f507f2006-12-13 04:49:30 +00002954 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002956 else if (TYPE(CHILD(n, 1)) == annassign) {
2957 expr_ty expr1, expr2, expr3;
2958 node *ch = CHILD(n, 0);
2959 node *deep, *ann = CHILD(n, 1);
2960 int simple = 1;
2961
2962 /* we keep track of parens to qualify (x) as expression not name */
2963 deep = ch;
2964 while (NCH(deep) == 1) {
2965 deep = CHILD(deep, 0);
2966 }
2967 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2968 simple = 0;
2969 }
2970 expr1 = ast_for_testlist(c, ch);
2971 if (!expr1) {
2972 return NULL;
2973 }
2974 switch (expr1->kind) {
2975 case Name_kind:
2976 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2977 return NULL;
2978 }
2979 expr1->v.Name.ctx = Store;
2980 break;
2981 case Attribute_kind:
2982 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2983 return NULL;
2984 }
2985 expr1->v.Attribute.ctx = Store;
2986 break;
2987 case Subscript_kind:
2988 expr1->v.Subscript.ctx = Store;
2989 break;
2990 case List_kind:
2991 ast_error(c, ch,
2992 "only single target (not list) can be annotated");
2993 return NULL;
2994 case Tuple_kind:
2995 ast_error(c, ch,
2996 "only single target (not tuple) can be annotated");
2997 return NULL;
2998 default:
2999 ast_error(c, ch,
3000 "illegal target for annotation");
3001 return NULL;
3002 }
3003
3004 if (expr1->kind != Name_kind) {
3005 simple = 0;
3006 }
3007 ch = CHILD(ann, 1);
3008 expr2 = ast_for_expr(c, ch);
3009 if (!expr2) {
3010 return NULL;
3011 }
3012 if (NCH(ann) == 2) {
3013 return AnnAssign(expr1, expr2, NULL, simple,
3014 LINENO(n), n->n_col_offset, c->c_arena);
3015 }
3016 else {
3017 ch = CHILD(ann, 3);
3018 expr3 = ast_for_expr(c, ch);
3019 if (!expr3) {
3020 return NULL;
3021 }
3022 return AnnAssign(expr1, expr2, expr3, simple,
3023 LINENO(n), n->n_col_offset, c->c_arena);
3024 }
3025 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003027 int i;
3028 asdl_seq *targets;
3029 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030 expr_ty expression;
3031
Thomas Wouters89f507f2006-12-13 04:49:30 +00003032 /* a normal assignment */
3033 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003034 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003035 if (!targets)
3036 return NULL;
3037 for (i = 0; i < NCH(n) - 2; i += 2) {
3038 expr_ty e;
3039 node *ch = CHILD(n, i);
3040 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003041 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003042 return NULL;
3043 }
3044 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003046 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003048 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003049 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003050 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051
Thomas Wouters89f507f2006-12-13 04:49:30 +00003052 asdl_seq_SET(targets, i / 2, e);
3053 }
3054 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003055 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003056 expression = ast_for_testlist(c, value);
3057 else
3058 expression = ast_for_expr(c, value);
3059 if (!expression)
3060 return NULL;
3061 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063}
3064
Benjamin Peterson78565b22009-06-28 19:19:51 +00003065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003066static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003067ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068{
3069 asdl_seq *seq;
3070 int i;
3071 expr_ty e;
3072
3073 REQ(n, exprlist);
3074
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003075 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003077 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003079 e = ast_for_expr(c, CHILD(n, i));
3080 if (!e)
3081 return NULL;
3082 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003083 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003084 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 }
3086 return seq;
3087}
3088
3089static stmt_ty
3090ast_for_del_stmt(struct compiling *c, const node *n)
3091{
3092 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003094 /* del_stmt: 'del' exprlist */
3095 REQ(n, del_stmt);
3096
3097 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3098 if (!expr_list)
3099 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003100 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003101}
3102
3103static stmt_ty
3104ast_for_flow_stmt(struct compiling *c, const node *n)
3105{
3106 /*
3107 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3108 | yield_stmt
3109 break_stmt: 'break'
3110 continue_stmt: 'continue'
3111 return_stmt: 'return' [testlist]
3112 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003113 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114 raise_stmt: 'raise' [test [',' test [',' test]]]
3115 */
3116 node *ch;
3117
3118 REQ(n, flow_stmt);
3119 ch = CHILD(n, 0);
3120 switch (TYPE(ch)) {
3121 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003122 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003124 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003126 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3127 if (!exp)
3128 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003129 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 }
3131 case return_stmt:
3132 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003133 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003135 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 if (!expression)
3137 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003138 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 }
3140 case raise_stmt:
3141 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003142 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3143 else if (NCH(ch) >= 2) {
3144 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3146 if (!expression)
3147 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003148 if (NCH(ch) == 4) {
3149 cause = ast_for_expr(c, CHILD(ch, 3));
3150 if (!cause)
3151 return NULL;
3152 }
3153 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 }
Stefan Krahf432a322017-08-21 13:09:59 +02003155 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003157 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 "unexpected flow_stmt: %d", TYPE(ch));
3159 return NULL;
3160 }
3161}
3162
3163static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003164alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165{
3166 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003167 import_as_name: NAME ['as' NAME]
3168 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 dotted_name: NAME ('.' NAME)*
3170 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003171 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173 loop:
3174 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003175 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003176 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003177 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003178 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003179 if (!name)
3180 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003181 if (NCH(n) == 3) {
3182 node *str_node = CHILD(n, 2);
3183 str = NEW_IDENTIFIER(str_node);
3184 if (!str)
3185 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003186 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003187 return NULL;
3188 }
3189 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003190 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003191 return NULL;
3192 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003193 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003194 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 case dotted_as_name:
3196 if (NCH(n) == 1) {
3197 n = CHILD(n, 0);
3198 goto loop;
3199 }
3200 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003201 node *asname_node = CHILD(n, 2);
3202 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003203 if (!a)
3204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003206 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003207 if (!a->asname)
3208 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003209 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 return a;
3212 }
3213 break;
3214 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003215 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003216 node *name_node = CHILD(n, 0);
3217 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003218 if (!name)
3219 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003220 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003221 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003222 return alias(name, NULL, c->c_arena);
3223 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 else {
3225 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003226 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003227 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230
3231 len = 0;
3232 for (i = 0; i < NCH(n); i += 2)
3233 /* length of string plus one for the dot */
3234 len += strlen(STR(CHILD(n, i))) + 1;
3235 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003236 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 if (!str)
3238 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003239 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240 if (!s)
3241 return NULL;
3242 for (i = 0; i < NCH(n); i += 2) {
3243 char *sch = STR(CHILD(n, i));
3244 strcpy(s, STR(CHILD(n, i)));
3245 s += strlen(sch);
3246 *s++ = '.';
3247 }
3248 --s;
3249 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3251 PyBytes_GET_SIZE(str),
3252 NULL);
3253 Py_DECREF(str);
3254 if (!uni)
3255 return NULL;
3256 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003257 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003258 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3259 Py_DECREF(str);
3260 return NULL;
3261 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003262 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263 }
3264 break;
3265 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003266 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003267 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3268 Py_DECREF(str);
3269 return NULL;
3270 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003271 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003273 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 "unexpected import name: %d", TYPE(n));
3275 return NULL;
3276 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003277
3278 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279 return NULL;
3280}
3281
3282static stmt_ty
3283ast_for_import_stmt(struct compiling *c, const node *n)
3284{
3285 /*
3286 import_stmt: import_name | import_from
3287 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003288 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3289 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003291 int lineno;
3292 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293 int i;
3294 asdl_seq *aliases;
3295
3296 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003297 lineno = LINENO(n);
3298 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003300 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003302 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003303 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003304 if (!aliases)
3305 return NULL;
3306 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003307 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003308 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003312 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003314 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 int idx, ndots = 0;
3317 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003318 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003320 /* Count the number of dots (for relative imports) and check for the
3321 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003322 for (idx = 1; idx < NCH(n); idx++) {
3323 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003324 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3325 if (!mod)
3326 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003327 idx++;
3328 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003329 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003331 ndots += 3;
3332 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333 } else if (TYPE(CHILD(n, idx)) != DOT) {
3334 break;
3335 }
3336 ndots++;
3337 }
3338 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003339 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003340 case STAR:
3341 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003342 n = CHILD(n, idx);
3343 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003344 break;
3345 case LPAR:
3346 /* from ... import (x, y, z) */
3347 n = CHILD(n, idx + 1);
3348 n_children = NCH(n);
3349 break;
3350 case import_as_names:
3351 /* from ... import x, y, z */
3352 n = CHILD(n, idx);
3353 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003354 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003355 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 " surrounding parentheses");
3357 return NULL;
3358 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003359 break;
3360 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003361 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 return NULL;
3363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003365 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003366 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368
3369 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003370 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003371 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003372 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003373 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003374 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003376 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003377 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003378 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003379 if (!import_alias)
3380 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003381 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003382 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003384 if (mod != NULL)
3385 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003386 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003387 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 }
Neal Norwitz79792652005-11-14 04:25:03 +00003389 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390 "unknown import statement: starts with command '%s'",
3391 STR(CHILD(n, 0)));
3392 return NULL;
3393}
3394
3395static stmt_ty
3396ast_for_global_stmt(struct compiling *c, const node *n)
3397{
3398 /* global_stmt: 'global' NAME (',' NAME)* */
3399 identifier name;
3400 asdl_seq *s;
3401 int i;
3402
3403 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003404 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003406 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003408 name = NEW_IDENTIFIER(CHILD(n, i));
3409 if (!name)
3410 return NULL;
3411 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003413 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003414}
3415
3416static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003417ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3418{
3419 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3420 identifier name;
3421 asdl_seq *s;
3422 int i;
3423
3424 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003425 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003426 if (!s)
3427 return NULL;
3428 for (i = 1; i < NCH(n); i += 2) {
3429 name = NEW_IDENTIFIER(CHILD(n, i));
3430 if (!name)
3431 return NULL;
3432 asdl_seq_SET(s, i / 2, name);
3433 }
3434 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3435}
3436
3437static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438ast_for_assert_stmt(struct compiling *c, const node *n)
3439{
3440 /* assert_stmt: 'assert' test [',' test] */
3441 REQ(n, assert_stmt);
3442 if (NCH(n) == 2) {
3443 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3444 if (!expression)
3445 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003446 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447 }
3448 else if (NCH(n) == 4) {
3449 expr_ty expr1, expr2;
3450
3451 expr1 = ast_for_expr(c, CHILD(n, 1));
3452 if (!expr1)
3453 return NULL;
3454 expr2 = ast_for_expr(c, CHILD(n, 3));
3455 if (!expr2)
3456 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457
Thomas Wouters89f507f2006-12-13 04:49:30 +00003458 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 }
Neal Norwitz79792652005-11-14 04:25:03 +00003460 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 "improper number of parts to 'assert' statement: %d",
3462 NCH(n));
3463 return NULL;
3464}
3465
3466static asdl_seq *
3467ast_for_suite(struct compiling *c, const node *n)
3468{
3469 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003470 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 stmt_ty s;
3472 int i, total, num, end, pos = 0;
3473 node *ch;
3474
3475 REQ(n, suite);
3476
3477 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003478 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003482 n = CHILD(n, 0);
3483 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003485 */
3486 end = NCH(n) - 1;
3487 if (TYPE(CHILD(n, end - 1)) == SEMI)
3488 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003490 for (i = 0; i < end; i += 2) {
3491 ch = CHILD(n, i);
3492 s = ast_for_stmt(c, ch);
3493 if (!s)
3494 return NULL;
3495 asdl_seq_SET(seq, pos++, s);
3496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 }
3498 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003499 for (i = 2; i < (NCH(n) - 1); i++) {
3500 ch = CHILD(n, i);
3501 REQ(ch, stmt);
3502 num = num_stmts(ch);
3503 if (num == 1) {
3504 /* small_stmt or compound_stmt with only one child */
3505 s = ast_for_stmt(c, ch);
3506 if (!s)
3507 return NULL;
3508 asdl_seq_SET(seq, pos++, s);
3509 }
3510 else {
3511 int j;
3512 ch = CHILD(ch, 0);
3513 REQ(ch, simple_stmt);
3514 for (j = 0; j < NCH(ch); j += 2) {
3515 /* statement terminates with a semi-colon ';' */
3516 if (NCH(CHILD(ch, j)) == 0) {
3517 assert((j + 1) == NCH(ch));
3518 break;
3519 }
3520 s = ast_for_stmt(c, CHILD(ch, j));
3521 if (!s)
3522 return NULL;
3523 asdl_seq_SET(seq, pos++, s);
3524 }
3525 }
3526 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527 }
3528 assert(pos == seq->size);
3529 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530}
3531
INADA Naokicb41b272017-02-23 00:31:59 +09003532static string
3533docstring_from_stmts(asdl_seq *stmts)
3534{
3535 if (stmts && stmts->size) {
3536 stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0);
3537 /* If first statement is a literal string, it's the doc string. */
3538 if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) {
3539 string doc = s->v.Expr.value->v.Str.s;
3540 /* not very efficient, but simple */
3541 memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1),
3542 (stmts->size - 1) * sizeof(void*));
3543 stmts->size--;
3544 return doc;
3545 }
3546 }
3547 return NULL;
3548}
3549
3550static asdl_seq *
3551ast_for_body(struct compiling *c, const node *n, string *docstring)
3552{
3553 asdl_seq *stmts = ast_for_suite(c, n);
3554 *docstring = docstring_from_stmts(stmts);
3555 return stmts;
3556}
3557
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558static stmt_ty
3559ast_for_if_stmt(struct compiling *c, const node *n)
3560{
3561 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3562 ['else' ':' suite]
3563 */
3564 char *s;
3565
3566 REQ(n, if_stmt);
3567
3568 if (NCH(n) == 4) {
3569 expr_ty expression;
3570 asdl_seq *suite_seq;
3571
3572 expression = ast_for_expr(c, CHILD(n, 1));
3573 if (!expression)
3574 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003576 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578
Guido van Rossumd8faa362007-04-27 19:54:29 +00003579 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3580 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583 s = STR(CHILD(n, 4));
3584 /* s[2], the third character in the string, will be
3585 's' for el_s_e, or
3586 'i' for el_i_f
3587 */
3588 if (s[2] == 's') {
3589 expr_ty expression;
3590 asdl_seq *seq1, *seq2;
3591
3592 expression = ast_for_expr(c, CHILD(n, 1));
3593 if (!expression)
3594 return NULL;
3595 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003596 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003597 return NULL;
3598 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003599 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 return NULL;
3601
Guido van Rossumd8faa362007-04-27 19:54:29 +00003602 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3603 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 }
3605 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003606 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003607 expr_ty expression;
3608 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003609 asdl_seq *orelse = NULL;
3610 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611 /* must reference the child n_elif+1 since 'else' token is third,
3612 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003613 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3614 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3615 has_else = 1;
3616 n_elif -= 3;
3617 }
3618 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619
Thomas Wouters89f507f2006-12-13 04:49:30 +00003620 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003621 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003623 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003624 if (!orelse)
3625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003627 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003629 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3630 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003632 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3633 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 asdl_seq_SET(orelse, 0,
3637 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003638 LINENO(CHILD(n, NCH(n) - 6)),
3639 CHILD(n, NCH(n) - 6)->n_col_offset,
3640 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003641 /* the just-created orelse handled the last elif */
3642 n_elif--;
3643 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644
Thomas Wouters89f507f2006-12-13 04:49:30 +00003645 for (i = 0; i < n_elif; i++) {
3646 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003647 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003648 if (!newobj)
3649 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003651 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003654 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656
Thomas Wouters89f507f2006-12-13 04:49:30 +00003657 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003659 LINENO(CHILD(n, off)),
3660 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003661 orelse = newobj;
3662 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003663 expression = ast_for_expr(c, CHILD(n, 1));
3664 if (!expression)
3665 return NULL;
3666 suite_seq = ast_for_suite(c, CHILD(n, 3));
3667 if (!suite_seq)
3668 return NULL;
3669 return If(expression, suite_seq, orelse,
3670 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003672
3673 PyErr_Format(PyExc_SystemError,
3674 "unexpected token in 'if' statement: %s", s);
3675 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676}
3677
3678static stmt_ty
3679ast_for_while_stmt(struct compiling *c, const node *n)
3680{
3681 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3682 REQ(n, while_stmt);
3683
3684 if (NCH(n) == 4) {
3685 expr_ty expression;
3686 asdl_seq *suite_seq;
3687
3688 expression = ast_for_expr(c, CHILD(n, 1));
3689 if (!expression)
3690 return NULL;
3691 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003692 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003694 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 }
3696 else if (NCH(n) == 7) {
3697 expr_ty expression;
3698 asdl_seq *seq1, *seq2;
3699
3700 expression = ast_for_expr(c, CHILD(n, 1));
3701 if (!expression)
3702 return NULL;
3703 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003704 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 return NULL;
3706 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003707 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 return NULL;
3709
Thomas Wouters89f507f2006-12-13 04:49:30 +00003710 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003712
3713 PyErr_Format(PyExc_SystemError,
3714 "wrong number of tokens for 'while' statement: %d",
3715 NCH(n));
3716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717}
3718
3719static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003720ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003722 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003724 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003725 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3727 REQ(n, for_stmt);
3728
3729 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003730 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003731 if (!seq)
3732 return NULL;
3733 }
3734
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003735 node_target = CHILD(n, 1);
3736 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003737 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003739 /* Check the # of children rather than the length of _target, since
3740 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003741 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003742 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003743 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003745 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003747 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003748 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 return NULL;
3750 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003751 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752 return NULL;
3753
Yury Selivanov75445082015-05-11 22:57:16 -04003754 if (is_async)
3755 return AsyncFor(target, expression, suite_seq, seq,
3756 LINENO(n), n->n_col_offset,
3757 c->c_arena);
3758 else
3759 return For(target, expression, suite_seq, seq,
3760 LINENO(n), n->n_col_offset,
3761 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762}
3763
3764static excepthandler_ty
3765ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3766{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003767 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 REQ(exc, except_clause);
3769 REQ(body, suite);
3770
3771 if (NCH(exc) == 1) {
3772 asdl_seq *suite_seq = ast_for_suite(c, body);
3773 if (!suite_seq)
3774 return NULL;
3775
Neal Norwitzad74aa82008-03-31 05:14:30 +00003776 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003777 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003778 }
3779 else if (NCH(exc) == 2) {
3780 expr_ty expression;
3781 asdl_seq *suite_seq;
3782
3783 expression = ast_for_expr(c, CHILD(exc, 1));
3784 if (!expression)
3785 return NULL;
3786 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003787 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 return NULL;
3789
Neal Norwitzad74aa82008-03-31 05:14:30 +00003790 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003791 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 }
3793 else if (NCH(exc) == 4) {
3794 asdl_seq *suite_seq;
3795 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003796 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003797 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003799 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003802 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 return NULL;
3804 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003805 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 return NULL;
3807
Neal Norwitzad74aa82008-03-31 05:14:30 +00003808 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003809 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003811
3812 PyErr_Format(PyExc_SystemError,
3813 "wrong number of children for 'except' clause: %d",
3814 NCH(exc));
3815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816}
3817
3818static stmt_ty
3819ast_for_try_stmt(struct compiling *c, const node *n)
3820{
Neal Norwitzf599f422005-12-17 21:33:47 +00003821 const int nch = NCH(n);
3822 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003823 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003824
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 REQ(n, try_stmt);
3826
Neal Norwitzf599f422005-12-17 21:33:47 +00003827 body = ast_for_suite(c, CHILD(n, 2));
3828 if (body == NULL)
3829 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830
Neal Norwitzf599f422005-12-17 21:33:47 +00003831 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3832 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3833 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3834 /* we can assume it's an "else",
3835 because nch >= 9 for try-else-finally and
3836 it would otherwise have a type of except_clause */
3837 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3838 if (orelse == NULL)
3839 return NULL;
3840 n_except--;
3841 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842
Neal Norwitzf599f422005-12-17 21:33:47 +00003843 finally = ast_for_suite(c, CHILD(n, nch - 1));
3844 if (finally == NULL)
3845 return NULL;
3846 n_except--;
3847 }
3848 else {
3849 /* we can assume it's an "else",
3850 otherwise it would have a type of except_clause */
3851 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3852 if (orelse == NULL)
3853 return NULL;
3854 n_except--;
3855 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003857 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003858 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859 return NULL;
3860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861
Neal Norwitzf599f422005-12-17 21:33:47 +00003862 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003863 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003864 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003865 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003866 if (handlers == NULL)
3867 return NULL;
3868
3869 for (i = 0; i < n_except; i++) {
3870 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3871 CHILD(n, 5 + i * 3));
3872 if (!e)
3873 return NULL;
3874 asdl_seq_SET(handlers, i, e);
3875 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003876 }
3877
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003878 assert(finally != NULL || asdl_seq_LEN(handlers));
3879 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880}
3881
Georg Brandl0c315622009-05-25 21:10:36 +00003882/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003883static withitem_ty
3884ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003885{
3886 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003887
Georg Brandl0c315622009-05-25 21:10:36 +00003888 REQ(n, with_item);
3889 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003890 if (!context_expr)
3891 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003892 if (NCH(n) == 3) {
3893 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003894
3895 if (!optional_vars) {
3896 return NULL;
3897 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003898 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003899 return NULL;
3900 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003901 }
3902
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003903 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003904}
3905
Georg Brandl0c315622009-05-25 21:10:36 +00003906/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3907static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003908ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003909{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003910 int i, n_items;
3911 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003912
3913 REQ(n, with_stmt);
3914
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003915 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003916 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003917 if (!items)
3918 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003919 for (i = 1; i < NCH(n) - 2; i += 2) {
3920 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3921 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003922 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003923 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003924 }
3925
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003926 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3927 if (!body)
3928 return NULL;
3929
Yury Selivanov75445082015-05-11 22:57:16 -04003930 if (is_async)
3931 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3932 else
3933 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003934}
3935
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003937ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003939 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003940 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003941 asdl_seq *s;
INADA Naokicb41b272017-02-23 00:31:59 +09003942 string docstring;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003943 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003944
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945 REQ(n, classdef);
3946
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003947 if (NCH(n) == 4) { /* class NAME ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003948 s = ast_for_body(c, CHILD(n, 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003949 if (!s)
3950 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003951 classname = NEW_IDENTIFIER(CHILD(n, 1));
3952 if (!classname)
3953 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003954 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003955 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003956 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3957 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003959
3960 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003961 s = ast_for_body(c, CHILD(n, 5), &docstring);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003962 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003963 return NULL;
3964 classname = NEW_IDENTIFIER(CHILD(n, 1));
3965 if (!classname)
3966 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003967 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003968 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003969 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3970 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971 }
3972
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003973 /* class NAME '(' arglist ')' ':' suite */
3974 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003975 {
3976 PyObject *dummy_name;
3977 expr_ty dummy;
3978 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3979 if (!dummy_name)
3980 return NULL;
3981 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3982 call = ast_for_call(c, CHILD(n, 3), dummy);
3983 if (!call)
3984 return NULL;
3985 }
INADA Naokicb41b272017-02-23 00:31:59 +09003986 s = ast_for_body(c, CHILD(n, 6), &docstring);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003987 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003989 classname = NEW_IDENTIFIER(CHILD(n, 1));
3990 if (!classname)
3991 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003992 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003993 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003994
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003995 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
INADA Naokicb41b272017-02-23 00:31:59 +09003996 decorator_seq, docstring, LINENO(n), n->n_col_offset,
3997 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003998}
3999
4000static stmt_ty
4001ast_for_stmt(struct compiling *c, const node *n)
4002{
4003 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004004 assert(NCH(n) == 1);
4005 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 }
4007 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004008 assert(num_stmts(n) == 1);
4009 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 }
4011 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004012 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004013 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4014 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004015 */
4016 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 case expr_stmt:
4018 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 case del_stmt:
4020 return ast_for_del_stmt(c, n);
4021 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004022 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023 case flow_stmt:
4024 return ast_for_flow_stmt(c, n);
4025 case import_stmt:
4026 return ast_for_import_stmt(c, n);
4027 case global_stmt:
4028 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004029 case nonlocal_stmt:
4030 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 case assert_stmt:
4032 return ast_for_assert_stmt(c, n);
4033 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004034 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4036 TYPE(n), NCH(n));
4037 return NULL;
4038 }
4039 }
4040 else {
4041 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004042 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004043 */
4044 node *ch = CHILD(n, 0);
4045 REQ(n, compound_stmt);
4046 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047 case if_stmt:
4048 return ast_for_if_stmt(c, ch);
4049 case while_stmt:
4050 return ast_for_while_stmt(c, ch);
4051 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004052 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 case try_stmt:
4054 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004055 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004056 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004057 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004058 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004060 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 case decorated:
4062 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004063 case async_stmt:
4064 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004066 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4068 TYPE(n), NCH(n));
4069 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004070 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071 }
4072}
4073
4074static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004075parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004077 const char *end;
4078 long x;
4079 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004080 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004081 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004083 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004084 errno = 0;
4085 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004086 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004087 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004088 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004089 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004090 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004091 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004092 }
4093 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004094 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004095 if (*end == '\0') {
4096 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004097 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004098 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004099 }
4100 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004101 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004102 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004103 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4104 if (compl.imag == -1.0 && PyErr_Occurred())
4105 return NULL;
4106 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004107 }
4108 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004109 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004110 dx = PyOS_string_to_double(s, NULL, NULL);
4111 if (dx == -1.0 && PyErr_Occurred())
4112 return NULL;
4113 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004114 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115}
4116
4117static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004118parsenumber(struct compiling *c, const char *s)
4119{
4120 char *dup, *end;
4121 PyObject *res = NULL;
4122
4123 assert(s != NULL);
4124
4125 if (strchr(s, '_') == NULL) {
4126 return parsenumber_raw(c, s);
4127 }
4128 /* Create a duplicate without underscores. */
4129 dup = PyMem_Malloc(strlen(s) + 1);
4130 end = dup;
4131 for (; *s; s++) {
4132 if (*s != '_') {
4133 *end++ = *s;
4134 }
4135 }
4136 *end = '\0';
4137 res = parsenumber_raw(c, dup);
4138 PyMem_Free(dup);
4139 return res;
4140}
4141
4142static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004143decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004144{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004145 const char *s, *t;
4146 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004147 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4148 while (s < end && (*s & 0x80)) s++;
4149 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004150 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151}
4152
Eric V. Smith56466482016-10-31 14:46:26 -04004153static int
4154warn_invalid_escape_sequence(struct compiling *c, const node *n,
4155 char first_invalid_escape_char)
4156{
4157 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4158 first_invalid_escape_char);
4159 if (msg == NULL) {
4160 return -1;
4161 }
4162 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4163 c->c_filename, LINENO(n),
4164 NULL, NULL) < 0 &&
4165 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4166 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004167 const char *s;
4168
4169 /* Replace the DeprecationWarning exception with a SyntaxError
4170 to get a more accurate error report */
4171 PyErr_Clear();
4172
4173 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004174 if (s != NULL) {
4175 ast_error(c, n, s);
4176 }
4177 Py_DECREF(msg);
4178 return -1;
4179 }
4180 Py_DECREF(msg);
4181 return 0;
4182}
4183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004184static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004185decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4186 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004187{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004188 PyObject *v, *u;
4189 char *buf;
4190 char *p;
4191 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004192
Benjamin Peterson202803a2016-02-25 22:34:45 -08004193 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004194 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004195 return NULL;
4196 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4197 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4198 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4199 if (u == NULL)
4200 return NULL;
4201 p = buf = PyBytes_AsString(u);
4202 end = s + len;
4203 while (s < end) {
4204 if (*s == '\\') {
4205 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004206 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004207 strcpy(p, "u005c");
4208 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004209 if (s >= end)
4210 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004211 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004212 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004213 if (*s & 0x80) { /* XXX inefficient */
4214 PyObject *w;
4215 int kind;
4216 void *data;
4217 Py_ssize_t len, i;
4218 w = decode_utf8(c, &s, end);
4219 if (w == NULL) {
4220 Py_DECREF(u);
4221 return NULL;
4222 }
4223 kind = PyUnicode_KIND(w);
4224 data = PyUnicode_DATA(w);
4225 len = PyUnicode_GET_LENGTH(w);
4226 for (i = 0; i < len; i++) {
4227 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4228 sprintf(p, "\\U%08x", chr);
4229 p += 10;
4230 }
4231 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004232 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004233 Py_DECREF(w);
4234 } else {
4235 *p++ = *s++;
4236 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004237 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004238 len = p - buf;
4239 s = buf;
4240
Eric V. Smith56466482016-10-31 14:46:26 -04004241 const char *first_invalid_escape;
4242 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4243
4244 if (v != NULL && first_invalid_escape != NULL) {
4245 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4246 /* We have not decref u before because first_invalid_escape points
4247 inside u. */
4248 Py_XDECREF(u);
4249 Py_DECREF(v);
4250 return NULL;
4251 }
4252 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004253 Py_XDECREF(u);
4254 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004255}
4256
Eric V. Smith56466482016-10-31 14:46:26 -04004257static PyObject *
4258decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4259 size_t len)
4260{
4261 const char *first_invalid_escape;
4262 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4263 &first_invalid_escape);
4264 if (result == NULL)
4265 return NULL;
4266
4267 if (first_invalid_escape != NULL) {
4268 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4269 Py_DECREF(result);
4270 return NULL;
4271 }
4272 }
4273 return result;
4274}
4275
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004276/* Shift locations for the given node and all its children by adding `lineno`
4277 and `col_offset` to existing locations. */
4278static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4279{
4280 n->n_col_offset = n->n_col_offset + col_offset;
4281 for (int i = 0; i < NCH(n); ++i) {
4282 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4283 /* Shifting column offsets unnecessary if there's been newlines. */
4284 col_offset = 0;
4285 }
4286 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4287 }
4288 n->n_lineno = n->n_lineno + lineno;
4289}
4290
4291/* Fix locations for the given node and its children.
4292
4293 `parent` is the enclosing node.
4294 `n` is the node which locations are going to be fixed relative to parent.
4295 `expr_str` is the child node's string representation, incuding braces.
4296*/
4297static void
4298fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4299{
4300 char *substr = NULL;
4301 char *start;
4302 int lines = LINENO(parent) - 1;
4303 int cols = parent->n_col_offset;
4304 /* Find the full fstring to fix location information in `n`. */
4305 while (parent && parent->n_type != STRING)
4306 parent = parent->n_child;
4307 if (parent && parent->n_str) {
4308 substr = strstr(parent->n_str, expr_str);
4309 if (substr) {
4310 start = substr;
4311 while (start > parent->n_str) {
4312 if (start[0] == '\n')
4313 break;
4314 start--;
4315 }
4316 cols += substr - start;
4317 /* Fix lineno in mulitline strings. */
4318 while ((substr = strchr(substr + 1, '\n')))
4319 lines--;
4320 }
4321 }
4322 fstring_shift_node_locations(n, lines, cols);
4323}
4324
Eric V. Smith451d0e32016-09-09 21:56:20 -04004325/* Compile this expression in to an expr_ty. Add parens around the
4326 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004327static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004328fstring_compile_expr(const char *expr_start, const char *expr_end,
4329 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004330
Eric V. Smith235a6f02015-09-19 14:51:32 -04004331{
4332 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004333 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004334 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004335 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004336 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004337 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004338
Eric V. Smith1d44c412015-09-23 07:49:00 -04004339 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004340 assert(*(expr_start-1) == '{');
4341 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004342
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004343 /* If the substring is all whitespace, it's an error. We need to catch this
4344 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4345 because turning the expression '' in to '()' would go from being invalid
4346 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004347 for (s = expr_start; s != expr_end; s++) {
4348 char c = *s;
4349 /* The Python parser ignores only the following whitespace
4350 characters (\r already is converted to \n). */
4351 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352 break;
4353 }
4354 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004355 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004356 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004357 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004358 }
4359
Eric V. Smith451d0e32016-09-09 21:56:20 -04004360 len = expr_end - expr_start;
4361 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4362 str = PyMem_RawMalloc(len + 3);
4363 if (str == NULL)
4364 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004365
Eric V. Smith451d0e32016-09-09 21:56:20 -04004366 str[0] = '(';
4367 memcpy(str+1, expr_start, len);
4368 str[len+1] = ')';
4369 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004370
4371 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004372 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4373 Py_eval_input, 0);
4374 if (!mod_n) {
4375 PyMem_RawFree(str);
4376 return NULL;
4377 }
4378 /* Reuse str to find the correct column offset. */
4379 str[0] = '{';
4380 str[len+1] = '}';
4381 fstring_fix_node_location(n, mod_n, str);
4382 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004383 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004384 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004385 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004386 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004387 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004388}
4389
4390/* Return -1 on error.
4391
4392 Return 0 if we reached the end of the literal.
4393
4394 Return 1 if we haven't reached the end of the literal, but we want
4395 the caller to process the literal up to this point. Used for
4396 doubled braces.
4397*/
4398static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004399fstring_find_literal(const char **str, const char *end, int raw,
4400 PyObject **literal, int recurse_lvl,
4401 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004402{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004403 /* Get any literal string. It ends when we hit an un-doubled left
4404 brace (which isn't part of a unicode name escape such as
4405 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004406
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004407 const char *s = *str;
4408 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004409 int result = 0;
4410
Eric V. Smith235a6f02015-09-19 14:51:32 -04004411 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004412 while (s < end) {
4413 char ch = *s++;
4414 if (!raw && ch == '\\' && s < end) {
4415 ch = *s++;
4416 if (ch == 'N') {
4417 if (s < end && *s++ == '{') {
4418 while (s < end && *s++ != '}') {
4419 }
4420 continue;
4421 }
4422 break;
4423 }
4424 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4425 return -1;
4426 }
4427 }
4428 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004429 /* Check for doubled braces, but only at the top level. If
4430 we checked at every level, then f'{0:{3}}' would fail
4431 with the two closing braces. */
4432 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004433 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004434 /* We're going to tell the caller that the literal ends
4435 here, but that they should continue scanning. But also
4436 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004437 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004438 result = 1;
4439 goto done;
4440 }
4441
4442 /* Where a single '{' is the start of a new expression, a
4443 single '}' is not allowed. */
4444 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004445 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004446 ast_error(c, n, "f-string: single '}' is not allowed");
4447 return -1;
4448 }
4449 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004450 /* We're either at a '{', which means we're starting another
4451 expression; or a '}', which means we're at the end of this
4452 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004453 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004454 break;
4455 }
4456 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004457 *str = s;
4458 assert(s <= end);
4459 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004460done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004461 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004462 if (raw)
4463 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004464 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004465 NULL, NULL);
4466 else
Eric V. Smith56466482016-10-31 14:46:26 -04004467 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004468 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004469 if (!*literal)
4470 return -1;
4471 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004472 return result;
4473}
4474
4475/* Forward declaration because parsing is recursive. */
4476static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004477fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004478 struct compiling *c, const node *n);
4479
Eric V. Smith451d0e32016-09-09 21:56:20 -04004480/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004481 expression (so it must be a '{'). Returns the FormattedValue node,
4482 which includes the expression, conversion character, and
4483 format_spec expression.
4484
4485 Note that I don't do a perfect job here: I don't make sure that a
4486 closing brace doesn't match an opening paren, for example. It
4487 doesn't need to error on all invalid expressions, just correctly
4488 find the end of all valid ones. Any errors inside the expression
4489 will be caught when we parse it later. */
4490static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004491fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004492 expr_ty *expression, struct compiling *c, const node *n)
4493{
4494 /* Return -1 on error, else 0. */
4495
Eric V. Smith451d0e32016-09-09 21:56:20 -04004496 const char *expr_start;
4497 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004498 expr_ty simple_expression;
4499 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004500 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004501
4502 /* 0 if we're not in a string, else the quote char we're trying to
4503 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004504 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004505
4506 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4507 int string_type = 0;
4508
4509 /* Keep track of nesting level for braces/parens/brackets in
4510 expressions. */
4511 Py_ssize_t nested_depth = 0;
4512
4513 /* Can only nest one level deep. */
4514 if (recurse_lvl >= 2) {
4515 ast_error(c, n, "f-string: expressions nested too deeply");
4516 return -1;
4517 }
4518
4519 /* The first char must be a left brace, or we wouldn't have gotten
4520 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004521 assert(**str == '{');
4522 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004523
Eric V. Smith451d0e32016-09-09 21:56:20 -04004524 expr_start = *str;
4525 for (; *str < end; (*str)++) {
4526 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004527
4528 /* Loop invariants. */
4529 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004530 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004531 if (quote_char)
4532 assert(string_type == 1 || string_type == 3);
4533 else
4534 assert(string_type == 0);
4535
Eric V. Smith451d0e32016-09-09 21:56:20 -04004536 ch = **str;
4537 /* Nowhere inside an expression is a backslash allowed. */
4538 if (ch == '\\') {
4539 /* Error: can't include a backslash character, inside
4540 parens or strings or not. */
4541 ast_error(c, n, "f-string expression part "
4542 "cannot include a backslash");
4543 return -1;
4544 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004545 if (quote_char) {
4546 /* We're inside a string. See if we're at the end. */
4547 /* This code needs to implement the same non-error logic
4548 as tok_get from tokenizer.c, at the letter_quote
4549 label. To actually share that code would be a
4550 nightmare. But, it's unlikely to change and is small,
4551 so duplicate it here. Note we don't need to catch all
4552 of the errors, since they'll be caught when parsing the
4553 expression. We just need to match the non-error
4554 cases. Thus we can ignore \n in single-quoted strings,
4555 for example. Or non-terminated strings. */
4556 if (ch == quote_char) {
4557 /* Does this match the string_type (single or triple
4558 quoted)? */
4559 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004560 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004561 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004562 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004563 string_type = 0;
4564 quote_char = 0;
4565 continue;
4566 }
4567 } else {
4568 /* We're at the end of a normal string. */
4569 quote_char = 0;
4570 string_type = 0;
4571 continue;
4572 }
4573 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004574 } else if (ch == '\'' || ch == '"') {
4575 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004576 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004577 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004578 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004579 } else {
4580 /* Start of a normal string. */
4581 string_type = 1;
4582 }
4583 /* Start looking for the end of the string. */
4584 quote_char = ch;
4585 } else if (ch == '[' || ch == '{' || ch == '(') {
4586 nested_depth++;
4587 } else if (nested_depth != 0 &&
4588 (ch == ']' || ch == '}' || ch == ')')) {
4589 nested_depth--;
4590 } else if (ch == '#') {
4591 /* Error: can't include a comment character, inside parens
4592 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004593 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004594 return -1;
4595 } else if (nested_depth == 0 &&
4596 (ch == '!' || ch == ':' || ch == '}')) {
4597 /* First, test for the special case of "!=". Since '=' is
4598 not an allowed conversion character, nothing is lost in
4599 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004600 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004601 /* This isn't a conversion character, just continue. */
4602 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004603 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004604 /* Normal way out of this loop. */
4605 break;
4606 } else {
4607 /* Just consume this char and loop around. */
4608 }
4609 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004610 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004611 /* If we leave this loop in a string or with mismatched parens, we
4612 don't care. We'll get a syntax error when compiling the
4613 expression. But, we can produce a better error message, so
4614 let's just do that.*/
4615 if (quote_char) {
4616 ast_error(c, n, "f-string: unterminated string");
4617 return -1;
4618 }
4619 if (nested_depth) {
4620 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4621 return -1;
4622 }
4623
Eric V. Smith451d0e32016-09-09 21:56:20 -04004624 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004625 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004626
4627 /* Compile the expression as soon as possible, so we show errors
4628 related to the expression before errors related to the
4629 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004630 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004631 if (!simple_expression)
4632 return -1;
4633
4634 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004635 if (**str == '!') {
4636 *str += 1;
4637 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004638 goto unexpected_end_of_string;
4639
Eric V. Smith451d0e32016-09-09 21:56:20 -04004640 conversion = **str;
4641 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004642
4643 /* Validate the conversion. */
4644 if (!(conversion == 's' || conversion == 'r'
4645 || conversion == 'a')) {
4646 ast_error(c, n, "f-string: invalid conversion character: "
4647 "expected 's', 'r', or 'a'");
4648 return -1;
4649 }
4650 }
4651
4652 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004653 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004654 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004655 if (**str == ':') {
4656 *str += 1;
4657 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004658 goto unexpected_end_of_string;
4659
4660 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004661 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004662 if (!format_spec)
4663 return -1;
4664 }
4665
Eric V. Smith451d0e32016-09-09 21:56:20 -04004666 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004667 goto unexpected_end_of_string;
4668
4669 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004670 assert(*str < end);
4671 assert(**str == '}');
4672 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004673
Eric V. Smith451d0e32016-09-09 21:56:20 -04004674 /* And now create the FormattedValue node that represents this
4675 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004676 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004677 format_spec, LINENO(n), n->n_col_offset,
4678 c->c_arena);
4679 if (!*expression)
4680 return -1;
4681
4682 return 0;
4683
4684unexpected_end_of_string:
4685 ast_error(c, n, "f-string: expecting '}'");
4686 return -1;
4687}
4688
4689/* Return -1 on error.
4690
4691 Return 0 if we have a literal (possible zero length) and an
4692 expression (zero length if at the end of the string.
4693
4694 Return 1 if we have a literal, but no expression, and we want the
4695 caller to call us again. This is used to deal with doubled
4696 braces.
4697
4698 When called multiple times on the string 'a{{b{0}c', this function
4699 will return:
4700
4701 1. the literal 'a{' with no expression, and a return value
4702 of 1. Despite the fact that there's no expression, the return
4703 value of 1 means we're not finished yet.
4704
4705 2. the literal 'b' and the expression '0', with a return value of
4706 0. The fact that there's an expression means we're not finished.
4707
4708 3. literal 'c' with no expression and a return value of 0. The
4709 combination of the return value of 0 with no expression means
4710 we're finished.
4711*/
4712static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004713fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4714 int recurse_lvl, PyObject **literal,
4715 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004716 struct compiling *c, const node *n)
4717{
4718 int result;
4719
4720 assert(*literal == NULL && *expression == NULL);
4721
4722 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004723 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004724 if (result < 0)
4725 goto error;
4726
4727 assert(result == 0 || result == 1);
4728
4729 if (result == 1)
4730 /* We have a literal, but don't look at the expression. */
4731 return 1;
4732
Eric V. Smith451d0e32016-09-09 21:56:20 -04004733 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004734 /* We're at the end of the string or the end of a nested
4735 f-string: no expression. The top-level error case where we
4736 expect to be at the end of the string but we're at a '}' is
4737 handled later. */
4738 return 0;
4739
4740 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004741 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004742
Eric V. Smith451d0e32016-09-09 21:56:20 -04004743 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004744 goto error;
4745
4746 return 0;
4747
4748error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004749 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004750 return -1;
4751}
4752
4753#define EXPRLIST_N_CACHED 64
4754
4755typedef struct {
4756 /* Incrementally build an array of expr_ty, so be used in an
4757 asdl_seq. Cache some small but reasonably sized number of
4758 expr_ty's, and then after that start dynamically allocating,
4759 doubling the number allocated each time. Note that the f-string
4760 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4761 Str for the literal 'a'. So you add expr_ty's about twice as
4762 fast as you add exressions in an f-string. */
4763
4764 Py_ssize_t allocated; /* Number we've allocated. */
4765 Py_ssize_t size; /* Number we've used. */
4766 expr_ty *p; /* Pointer to the memory we're actually
4767 using. Will point to 'data' until we
4768 start dynamically allocating. */
4769 expr_ty data[EXPRLIST_N_CACHED];
4770} ExprList;
4771
4772#ifdef NDEBUG
4773#define ExprList_check_invariants(l)
4774#else
4775static void
4776ExprList_check_invariants(ExprList *l)
4777{
4778 /* Check our invariants. Make sure this object is "live", and
4779 hasn't been deallocated. */
4780 assert(l->size >= 0);
4781 assert(l->p != NULL);
4782 if (l->size <= EXPRLIST_N_CACHED)
4783 assert(l->data == l->p);
4784}
4785#endif
4786
4787static void
4788ExprList_Init(ExprList *l)
4789{
4790 l->allocated = EXPRLIST_N_CACHED;
4791 l->size = 0;
4792
4793 /* Until we start allocating dynamically, p points to data. */
4794 l->p = l->data;
4795
4796 ExprList_check_invariants(l);
4797}
4798
4799static int
4800ExprList_Append(ExprList *l, expr_ty exp)
4801{
4802 ExprList_check_invariants(l);
4803 if (l->size >= l->allocated) {
4804 /* We need to alloc (or realloc) the memory. */
4805 Py_ssize_t new_size = l->allocated * 2;
4806
4807 /* See if we've ever allocated anything dynamically. */
4808 if (l->p == l->data) {
4809 Py_ssize_t i;
4810 /* We're still using the cached data. Switch to
4811 alloc-ing. */
4812 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4813 if (!l->p)
4814 return -1;
4815 /* Copy the cached data into the new buffer. */
4816 for (i = 0; i < l->size; i++)
4817 l->p[i] = l->data[i];
4818 } else {
4819 /* Just realloc. */
4820 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4821 if (!tmp) {
4822 PyMem_RawFree(l->p);
4823 l->p = NULL;
4824 return -1;
4825 }
4826 l->p = tmp;
4827 }
4828
4829 l->allocated = new_size;
4830 assert(l->allocated == 2 * l->size);
4831 }
4832
4833 l->p[l->size++] = exp;
4834
4835 ExprList_check_invariants(l);
4836 return 0;
4837}
4838
4839static void
4840ExprList_Dealloc(ExprList *l)
4841{
4842 ExprList_check_invariants(l);
4843
4844 /* If there's been an error, or we've never dynamically allocated,
4845 do nothing. */
4846 if (!l->p || l->p == l->data) {
4847 /* Do nothing. */
4848 } else {
4849 /* We have dynamically allocated. Free the memory. */
4850 PyMem_RawFree(l->p);
4851 }
4852 l->p = NULL;
4853 l->size = -1;
4854}
4855
4856static asdl_seq *
4857ExprList_Finish(ExprList *l, PyArena *arena)
4858{
4859 asdl_seq *seq;
4860
4861 ExprList_check_invariants(l);
4862
4863 /* Allocate the asdl_seq and copy the expressions in to it. */
4864 seq = _Py_asdl_seq_new(l->size, arena);
4865 if (seq) {
4866 Py_ssize_t i;
4867 for (i = 0; i < l->size; i++)
4868 asdl_seq_SET(seq, i, l->p[i]);
4869 }
4870 ExprList_Dealloc(l);
4871 return seq;
4872}
4873
4874/* The FstringParser is designed to add a mix of strings and
4875 f-strings, and concat them together as needed. Ultimately, it
4876 generates an expr_ty. */
4877typedef struct {
4878 PyObject *last_str;
4879 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004880 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004881} FstringParser;
4882
4883#ifdef NDEBUG
4884#define FstringParser_check_invariants(state)
4885#else
4886static void
4887FstringParser_check_invariants(FstringParser *state)
4888{
4889 if (state->last_str)
4890 assert(PyUnicode_CheckExact(state->last_str));
4891 ExprList_check_invariants(&state->expr_list);
4892}
4893#endif
4894
4895static void
4896FstringParser_Init(FstringParser *state)
4897{
4898 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004899 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004900 ExprList_Init(&state->expr_list);
4901 FstringParser_check_invariants(state);
4902}
4903
4904static void
4905FstringParser_Dealloc(FstringParser *state)
4906{
4907 FstringParser_check_invariants(state);
4908
4909 Py_XDECREF(state->last_str);
4910 ExprList_Dealloc(&state->expr_list);
4911}
4912
4913/* Make a Str node, but decref the PyUnicode object being added. */
4914static expr_ty
4915make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4916{
4917 PyObject *s = *str;
4918 *str = NULL;
4919 assert(PyUnicode_CheckExact(s));
4920 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4921 Py_DECREF(s);
4922 return NULL;
4923 }
4924 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4925}
4926
4927/* Add a non-f-string (that is, a regular literal string). str is
4928 decref'd. */
4929static int
4930FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4931{
4932 FstringParser_check_invariants(state);
4933
4934 assert(PyUnicode_CheckExact(str));
4935
4936 if (PyUnicode_GET_LENGTH(str) == 0) {
4937 Py_DECREF(str);
4938 return 0;
4939 }
4940
4941 if (!state->last_str) {
4942 /* We didn't have a string before, so just remember this one. */
4943 state->last_str = str;
4944 } else {
4945 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004946 PyUnicode_AppendAndDel(&state->last_str, str);
4947 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004948 return -1;
4949 }
4950 FstringParser_check_invariants(state);
4951 return 0;
4952}
4953
Eric V. Smith451d0e32016-09-09 21:56:20 -04004954/* Parse an f-string. The f-string is in *str to end, with no
4955 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004956static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004957FstringParser_ConcatFstring(FstringParser *state, const char **str,
4958 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004959 struct compiling *c, const node *n)
4960{
4961 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004962 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963
4964 /* Parse the f-string. */
4965 while (1) {
4966 PyObject *literal = NULL;
4967 expr_ty expression = NULL;
4968
4969 /* If there's a zero length literal in front of the
4970 expression, literal will be NULL. If we're at the end of
4971 the f-string, expression will be NULL (unless result == 1,
4972 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004973 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004974 &literal, &expression,
4975 c, n);
4976 if (result < 0)
4977 return -1;
4978
4979 /* Add the literal, if any. */
4980 if (!literal) {
4981 /* Do nothing. Just leave last_str alone (and possibly
4982 NULL). */
4983 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004984 /* Note that the literal can be zero length, if the
4985 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004986 state->last_str = literal;
4987 literal = NULL;
4988 } else {
4989 /* We have a literal, concatenate it. */
4990 assert(PyUnicode_GET_LENGTH(literal) != 0);
4991 if (FstringParser_ConcatAndDel(state, literal) < 0)
4992 return -1;
4993 literal = NULL;
4994 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995
4996 /* We've dealt with the literal now. It can't be leaked on further
4997 errors. */
4998 assert(literal == NULL);
4999
5000 /* See if we should just loop around to get the next literal
5001 and expression, while ignoring the expression this
5002 time. This is used for un-doubling braces, as an
5003 optimization. */
5004 if (result == 1)
5005 continue;
5006
5007 if (!expression)
5008 /* We're done with this f-string. */
5009 break;
5010
5011 /* We know we have an expression. Convert any existing string
5012 to a Str node. */
5013 if (!state->last_str) {
5014 /* Do nothing. No previous literal. */
5015 } else {
5016 /* Convert the existing last_str literal to a Str node. */
5017 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5018 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5019 return -1;
5020 }
5021
5022 if (ExprList_Append(&state->expr_list, expression) < 0)
5023 return -1;
5024 }
5025
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 /* If recurse_lvl is zero, then we must be at the end of the
5027 string. Otherwise, we must be at a right brace. */
5028
Eric V. Smith451d0e32016-09-09 21:56:20 -04005029 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005030 ast_error(c, n, "f-string: unexpected end of string");
5031 return -1;
5032 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005033 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005034 ast_error(c, n, "f-string: expecting '}'");
5035 return -1;
5036 }
5037
5038 FstringParser_check_invariants(state);
5039 return 0;
5040}
5041
5042/* Convert the partial state reflected in last_str and expr_list to an
5043 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5044static expr_ty
5045FstringParser_Finish(FstringParser *state, struct compiling *c,
5046 const node *n)
5047{
5048 asdl_seq *seq;
5049
5050 FstringParser_check_invariants(state);
5051
5052 /* If we're just a constant string with no expressions, return
5053 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005054 if (!state->fmode) {
5055 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005056 if (!state->last_str) {
5057 /* Create a zero length string. */
5058 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5059 if (!state->last_str)
5060 goto error;
5061 }
5062 return make_str_node_and_del(&state->last_str, c, n);
5063 }
5064
5065 /* Create a Str node out of last_str, if needed. It will be the
5066 last node in our expression list. */
5067 if (state->last_str) {
5068 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5069 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5070 goto error;
5071 }
5072 /* This has already been freed. */
5073 assert(state->last_str == NULL);
5074
5075 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5076 if (!seq)
5077 goto error;
5078
Eric V. Smith235a6f02015-09-19 14:51:32 -04005079 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5080
5081error:
5082 FstringParser_Dealloc(state);
5083 return NULL;
5084}
5085
Eric V. Smith451d0e32016-09-09 21:56:20 -04005086/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5087 at end, parse it into an expr_ty. Return NULL on error. Adjust
5088 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005089static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005091 struct compiling *c, const node *n)
5092{
5093 FstringParser state;
5094
5095 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005097 c, n) < 0) {
5098 FstringParser_Dealloc(&state);
5099 return NULL;
5100 }
5101
5102 return FstringParser_Finish(&state, c, n);
5103}
5104
5105/* n is a Python string literal, including the bracketing quote
5106 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005107 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005108 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005109 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5110 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005111*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005112static int
5113parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5114 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005115{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005116 size_t len;
5117 const char *s = STR(n);
5118 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005119 int fmode = 0;
5120 *bytesmode = 0;
5121 *rawmode = 0;
5122 *result = NULL;
5123 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005124 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005125 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005126 if (quote == 'b' || quote == 'B') {
5127 quote = *++s;
5128 *bytesmode = 1;
5129 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005130 else if (quote == 'u' || quote == 'U') {
5131 quote = *++s;
5132 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005133 else if (quote == 'r' || quote == 'R') {
5134 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005135 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005136 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005137 else if (quote == 'f' || quote == 'F') {
5138 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005139 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005140 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005141 else {
5142 break;
5143 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005144 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005145 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005146 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005147 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005148 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005149 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005150 if (quote != '\'' && quote != '\"') {
5151 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005152 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005153 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005154 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005155 s++;
5156 len = strlen(s);
5157 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005159 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005160 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005161 }
5162 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005163 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005164 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005165 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005166 }
5167 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005168 /* A triple quoted string. We've already skipped one quote at
5169 the start and one at the end of the string. Now skip the
5170 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005171 s += 2;
5172 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005173 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005174 if (s[--len] != quote || s[--len] != quote) {
5175 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005177 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005178 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005179
Eric V. Smith451d0e32016-09-09 21:56:20 -04005180 if (fmode) {
5181 /* Just return the bytes. The caller will parse the resulting
5182 string. */
5183 *fstr = s;
5184 *fstrlen = len;
5185 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005186 }
5187
Eric V. Smith451d0e32016-09-09 21:56:20 -04005188 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005189 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005190 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005191 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005192 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005193 const char *ch;
5194 for (ch = s; *ch; ch++) {
5195 if (Py_CHARMASK(*ch) >= 0x80) {
5196 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005197 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005198 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005199 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005200 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005201 if (*rawmode)
5202 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005203 else
Eric V. Smith56466482016-10-31 14:46:26 -04005204 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005205 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005206 if (*rawmode)
5207 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005208 else
Eric V. Smith56466482016-10-31 14:46:26 -04005209 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005210 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005211 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005212}
5213
Eric V. Smith235a6f02015-09-19 14:51:32 -04005214/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5215 each STRING atom, and process it as needed. For bytes, just
5216 concatenate them together, and the result will be a Bytes node. For
5217 normal strings and f-strings, concatenate them together. The result
5218 will be a Str node if there were no f-strings; a FormattedValue
5219 node if there's just an f-string (with no leading or trailing
5220 literals), or a JoinedStr node if there are multiple f-strings or
5221 any literals involved. */
5222static expr_ty
5223parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005224{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005225 int bytesmode = 0;
5226 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005227 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005228
5229 FstringParser state;
5230 FstringParser_Init(&state);
5231
5232 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005233 int this_bytesmode;
5234 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005235 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005236 const char *fstr;
5237 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005238
5239 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005240 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5241 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005242 goto error;
5243
5244 /* Check that we're not mixing bytes with unicode. */
5245 if (i != 0 && bytesmode != this_bytesmode) {
5246 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005247 /* s is NULL if the current string part is an f-string. */
5248 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005249 goto error;
5250 }
5251 bytesmode = this_bytesmode;
5252
Eric V. Smith451d0e32016-09-09 21:56:20 -04005253 if (fstr != NULL) {
5254 int result;
5255 assert(s == NULL && !bytesmode);
5256 /* This is an f-string. Parse and concatenate it. */
5257 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5258 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005259 if (result < 0)
5260 goto error;
5261 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005262 /* A string or byte string. */
5263 assert(s != NULL && fstr == NULL);
5264
Eric V. Smith451d0e32016-09-09 21:56:20 -04005265 assert(bytesmode ? PyBytes_CheckExact(s) :
5266 PyUnicode_CheckExact(s));
5267
Eric V. Smith451d0e32016-09-09 21:56:20 -04005268 if (bytesmode) {
5269 /* For bytes, concat as we go. */
5270 if (i == 0) {
5271 /* First time, just remember this value. */
5272 bytes_str = s;
5273 } else {
5274 PyBytes_ConcatAndDel(&bytes_str, s);
5275 if (!bytes_str)
5276 goto error;
5277 }
5278 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005279 /* This is a regular string. Concatenate it. */
5280 if (FstringParser_ConcatAndDel(&state, s) < 0)
5281 goto error;
5282 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005283 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005284 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005285 if (bytesmode) {
5286 /* Just return the bytes object and we're done. */
5287 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5288 goto error;
5289 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005291
Eric V. Smith235a6f02015-09-19 14:51:32 -04005292 /* We're not a bytes string, bytes_str should never have been set. */
5293 assert(bytes_str == NULL);
5294
5295 return FstringParser_Finish(&state, c, n);
5296
5297error:
5298 Py_XDECREF(bytes_str);
5299 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005300 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005301}