blob: fd42c0073de70143019279389e4a458213320c35 [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. */
597 PyObject *c_normalize_args; /* Normalization argument tuple. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598};
599
600static asdl_seq *seq_for_testlist(struct compiling *, const node *);
601static expr_ty ast_for_expr(struct compiling *, const node *);
602static stmt_ty ast_for_stmt(struct compiling *, const node *);
INADA Naokicb41b272017-02-23 00:31:59 +0900603static asdl_seq *ast_for_body(struct compiling *c, const node *n,
604 string *docstring);
605static string docstring_from_stmts(asdl_seq *stmts);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000606static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
607 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000608static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000609static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610
Yury Selivanov75445082015-05-11 22:57:16 -0400611static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
612static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614/* Note different signature for ast_for_call */
615static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
616
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000617static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400618static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619
Nick Coghlan650f0d02007-04-15 12:05:43 +0000620#define COMP_GENEXP 0
621#define COMP_LISTCOMP 1
622#define COMP_SETCOMP 2
623
Benjamin Peterson55e00432012-01-16 17:22:31 -0500624static int
625init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000626{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500627 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
628 if (!m)
629 return 0;
630 c->c_normalize = PyObject_GetAttrString(m, "normalize");
631 Py_DECREF(m);
632 if (!c->c_normalize)
633 return 0;
634 c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500635 if (!c->c_normalize_args) {
636 Py_CLEAR(c->c_normalize);
637 return 0;
638 }
Christian Heimes72f562f2013-07-24 21:02:17 +0200639 PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500640 return 1;
641}
642
643static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400644new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500645{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400646 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500647 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000648 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500649 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500650 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000651 /* Check whether there are non-ASCII characters in the
652 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500653 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200654 PyObject *id2;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500655 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500656 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200657 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500658 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500659 PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
660 id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500661 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200662 if (!id2)
663 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200664 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000665 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000666 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200667 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
668 Py_DECREF(id);
669 return NULL;
670 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000671 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672}
673
Benjamin Peterson55e00432012-01-16 17:22:31 -0500674#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400677ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400679 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680
Victor Stinner14e461d2013-08-26 22:28:21 +0200681 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000683 Py_INCREF(Py_None);
684 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200686 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400687 if (!tmp)
688 return 0;
689 errstr = PyUnicode_FromString(errmsg);
690 if (!errstr) {
691 Py_DECREF(tmp);
692 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000693 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 Py_DECREF(errstr);
696 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400697 if (value) {
698 PyErr_SetObject(PyExc_SyntaxError, value);
699 Py_DECREF(value);
700 }
701 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702}
703
704/* num_stmts() returns number of contained statements.
705
706 Use this routine to determine how big a sequence is needed for
707 the statements in a parse tree. Its raison d'etre is this bit of
708 grammar:
709
710 stmt: simple_stmt | compound_stmt
711 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
712
713 A simple_stmt can contain multiple small_stmt elements joined
714 by semicolons. If the arg is a simple_stmt, the number of
715 small_stmt elements is returned.
716*/
717
718static int
719num_stmts(const node *n)
720{
721 int i, l;
722 node *ch;
723
724 switch (TYPE(n)) {
725 case single_input:
726 if (TYPE(CHILD(n, 0)) == NEWLINE)
727 return 0;
728 else
729 return num_stmts(CHILD(n, 0));
730 case file_input:
731 l = 0;
732 for (i = 0; i < NCH(n); i++) {
733 ch = CHILD(n, i);
734 if (TYPE(ch) == stmt)
735 l += num_stmts(ch);
736 }
737 return l;
738 case stmt:
739 return num_stmts(CHILD(n, 0));
740 case compound_stmt:
741 return 1;
742 case simple_stmt:
743 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
744 case suite:
745 if (NCH(n) == 1)
746 return num_stmts(CHILD(n, 0));
747 else {
748 l = 0;
749 for (i = 2; i < (NCH(n) - 1); i++)
750 l += num_stmts(CHILD(n, i));
751 return l;
752 }
753 default: {
754 char buf[128];
755
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000756 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 TYPE(n), NCH(n));
758 Py_FatalError(buf);
759 }
760 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700761 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762}
763
764/* Transform the CST rooted at node * to the appropriate AST
765*/
766
767mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200768PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
769 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000771 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 asdl_seq *stmts = NULL;
773 stmt_ty s;
774 node *ch;
775 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500776 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400778 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200779 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400780 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800781 c.c_normalize = NULL;
782 c.c_normalize_args = NULL;
783
784 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786
Jeremy Hyltona8293132006-02-28 17:58:27 +0000787 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 switch (TYPE(n)) {
789 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200790 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500792 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 for (i = 0; i < NCH(n) - 1; i++) {
794 ch = CHILD(n, i);
795 if (TYPE(ch) == NEWLINE)
796 continue;
797 REQ(ch, stmt);
798 num = num_stmts(ch);
799 if (num == 1) {
800 s = ast_for_stmt(&c, ch);
801 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500802 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000803 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
805 else {
806 ch = CHILD(ch, 0);
807 REQ(ch, simple_stmt);
808 for (j = 0; j < num; j++) {
809 s = ast_for_stmt(&c, CHILD(ch, j * 2));
810 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500811 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000812 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 }
814 }
815 }
INADA Naokicb41b272017-02-23 00:31:59 +0900816 res = Module(stmts, docstring_from_stmts(stmts), arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 case eval_input: {
819 expr_ty testlist_ast;
820
Nick Coghlan650f0d02007-04-15 12:05:43 +0000821 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000822 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500824 goto out;
825 res = Expression(testlist_ast, arena);
826 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 }
828 case single_input:
829 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200830 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500832 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
834 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000835 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500836 goto out;
837 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 }
839 else {
840 n = CHILD(n, 0);
841 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200842 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500844 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000846 s = ast_for_stmt(&c, n);
847 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500848 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 asdl_seq_SET(stmts, 0, s);
850 }
851 else {
852 /* Only a simple_stmt can contain multiple statements. */
853 REQ(n, simple_stmt);
854 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 if (TYPE(CHILD(n, i)) == NEWLINE)
856 break;
857 s = ast_for_stmt(&c, CHILD(n, i));
858 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500859 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 asdl_seq_SET(stmts, i / 2, s);
861 }
862 }
863
Benjamin Peterson55e00432012-01-16 17:22:31 -0500864 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500866 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000868 PyErr_Format(PyExc_SystemError,
869 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500870 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500872 out:
873 if (c.c_normalize) {
874 Py_DECREF(c.c_normalize);
875 PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
876 Py_DECREF(c.c_normalize_args);
877 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500878 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879}
880
Victor Stinner14e461d2013-08-26 22:28:21 +0200881mod_ty
882PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
883 PyArena *arena)
884{
885 mod_ty mod;
886 PyObject *filename;
887 filename = PyUnicode_DecodeFSDefault(filename_str);
888 if (filename == NULL)
889 return NULL;
890 mod = PyAST_FromNodeObject(n, flags, filename, arena);
891 Py_DECREF(filename);
892 return mod;
893
894}
895
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
897*/
898
899static operator_ty
900get_operator(const node *n)
901{
902 switch (TYPE(n)) {
903 case VBAR:
904 return BitOr;
905 case CIRCUMFLEX:
906 return BitXor;
907 case AMPER:
908 return BitAnd;
909 case LEFTSHIFT:
910 return LShift;
911 case RIGHTSHIFT:
912 return RShift;
913 case PLUS:
914 return Add;
915 case MINUS:
916 return Sub;
917 case STAR:
918 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400919 case AT:
920 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 case SLASH:
922 return Div;
923 case DOUBLESLASH:
924 return FloorDiv;
925 case PERCENT:
926 return Mod;
927 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 }
930}
931
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200932static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000933 "None",
934 "True",
935 "False",
936 NULL,
937};
938
939static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400940forbidden_name(struct compiling *c, identifier name, const node *n,
941 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000942{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000943 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200944 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400945 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000946 return 1;
947 }
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +0200948 if (_PyUnicode_EqualToASCIIString(name, "async") ||
949 _PyUnicode_EqualToASCIIString(name, "await"))
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400950 {
951 PyObject *message = PyUnicode_FromString(
952 "'async' and 'await' will become reserved keywords"
953 " in Python 3.7");
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500954 int ret;
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400955 if (message == NULL) {
956 return 1;
957 }
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500958 ret = PyErr_WarnExplicitObject(
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400959 PyExc_DeprecationWarning,
960 message,
961 c->c_filename,
962 LINENO(n),
963 NULL,
Yury Selivanov1a9d6872016-11-08 16:54:18 -0500964 NULL);
965 Py_DECREF(message);
966 if (ret < 0) {
Yury Selivanov8987c9d2016-09-15 12:50:23 -0400967 return 1;
968 }
969 }
Benjamin Peterson70f52762009-06-28 23:32:44 +0000970 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200971 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000972 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200973 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400974 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000975 return 1;
976 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000977 }
978 }
979 return 0;
980}
981
Jeremy Hyltona8293132006-02-28 17:58:27 +0000982/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983
984 Only sets context for expr kinds that "can appear in assignment context"
985 (according to ../Parser/Python.asdl). For other expr kinds, it sets
986 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987*/
988
989static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000990set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991{
992 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000993 /* If a particular expression type can't be used for assign / delete,
994 set expr_name to its name and an error message will be generated.
995 */
996 const char* expr_name = NULL;
997
998 /* The ast defines augmented store and load contexts, but the
999 implementation here doesn't actually use them. The code may be
1000 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001001 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001002 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +00001003 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001004 */
1005 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006
1007 switch (e->kind) {
1008 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001009 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001010 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001011 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001012 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001014 e->v.Subscript.ctx = ctx;
1015 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001016 case Starred_kind:
1017 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001018 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001019 return 0;
1020 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001022 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001023 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001024 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001025 }
1026 e->v.Name.ctx = ctx;
1027 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001029 e->v.List.ctx = ctx;
1030 s = e->v.List.elts;
1031 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001033 e->v.Tuple.ctx = ctx;
1034 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001035 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001036 case Lambda_kind:
1037 expr_name = "lambda";
1038 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001040 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001041 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001042 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001044 case UnaryOp_kind:
1045 expr_name = "operator";
1046 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001048 expr_name = "generator expression";
1049 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001050 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001051 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001052 expr_name = "yield expression";
1053 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001054 case Await_kind:
1055 expr_name = "await expression";
1056 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001057 case ListComp_kind:
1058 expr_name = "list comprehension";
1059 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001060 case SetComp_kind:
1061 expr_name = "set comprehension";
1062 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001063 case DictComp_kind:
1064 expr_name = "dict comprehension";
1065 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001066 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001067 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 case Num_kind:
1069 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001070 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001071 case JoinedStr_kind:
1072 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001073 expr_name = "literal";
1074 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001075 case NameConstant_kind:
1076 expr_name = "keyword";
1077 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001078 case Ellipsis_kind:
1079 expr_name = "Ellipsis";
1080 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001081 case Compare_kind:
1082 expr_name = "comparison";
1083 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001084 case IfExp_kind:
1085 expr_name = "conditional expression";
1086 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001087 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 PyErr_Format(PyExc_SystemError,
1089 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001090 e->kind, e->lineno);
1091 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001093 /* Check for error string set by switch */
1094 if (expr_name) {
1095 char buf[300];
1096 PyOS_snprintf(buf, sizeof(buf),
1097 "can't %s %s",
1098 ctx == Store ? "assign to" : "delete",
1099 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001100 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001101 }
1102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105 */
1106 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001107 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108
Thomas Wouters89f507f2006-12-13 04:49:30 +00001109 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001110 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001111 return 0;
1112 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 }
1114 return 1;
1115}
1116
1117static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001118ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119{
1120 REQ(n, augassign);
1121 n = CHILD(n, 0);
1122 switch (STR(n)[0]) {
1123 case '+':
1124 return Add;
1125 case '-':
1126 return Sub;
1127 case '/':
1128 if (STR(n)[1] == '/')
1129 return FloorDiv;
1130 else
1131 return Div;
1132 case '%':
1133 return Mod;
1134 case '<':
1135 return LShift;
1136 case '>':
1137 return RShift;
1138 case '&':
1139 return BitAnd;
1140 case '^':
1141 return BitXor;
1142 case '|':
1143 return BitOr;
1144 case '*':
1145 if (STR(n)[1] == '*')
1146 return Pow;
1147 else
1148 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001149 case '@':
1150 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001152 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001153 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 }
1155}
1156
1157static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001158ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001160 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 |'is' 'not'
1162 */
1163 REQ(n, comp_op);
1164 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001165 n = CHILD(n, 0);
1166 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 case LESS:
1168 return Lt;
1169 case GREATER:
1170 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001171 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 return Eq;
1173 case LESSEQUAL:
1174 return LtE;
1175 case GREATEREQUAL:
1176 return GtE;
1177 case NOTEQUAL:
1178 return NotEq;
1179 case NAME:
1180 if (strcmp(STR(n), "in") == 0)
1181 return In;
1182 if (strcmp(STR(n), "is") == 0)
1183 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001184 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001186 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001188 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001189 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 }
1191 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001192 /* handle "not in" and "is not" */
1193 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 case NAME:
1195 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1196 return NotIn;
1197 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1198 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001199 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001201 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001203 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001204 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 }
Neal Norwitz79792652005-11-14 04:25:03 +00001206 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001208 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209}
1210
1211static asdl_seq *
1212seq_for_testlist(struct compiling *c, const node *n)
1213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001215 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1216 */
Armin Rigo31441302005-10-21 12:57:31 +00001217 asdl_seq *seq;
1218 expr_ty expression;
1219 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001220 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001222 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 if (!seq)
1224 return NULL;
1225
1226 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001228 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229
Benjamin Peterson4905e802009-09-27 02:43:28 +00001230 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001231 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233
1234 assert(i / 2 < seq->size);
1235 asdl_seq_SET(seq, i / 2, expression);
1236 }
1237 return seq;
1238}
1239
Neal Norwitzc1505362006-12-28 06:47:50 +00001240static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001241ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001242{
1243 identifier name;
1244 expr_ty annotation = NULL;
1245 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001246 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001247
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001248 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001249 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001250 name = NEW_IDENTIFIER(ch);
1251 if (!name)
1252 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001253 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001254 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001255
1256 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1257 annotation = ast_for_expr(c, CHILD(n, 2));
1258 if (!annotation)
1259 return NULL;
1260 }
1261
Victor Stinnerc106c682015-11-06 17:01:48 +01001262 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001263 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001264 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001265 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266}
1267
Guido van Rossum4f72a782006-10-27 23:31:49 +00001268/* returns -1 if failed to handle keyword only arguments
1269 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001270 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001271 ^^^
1272 start pointing here
1273 */
1274static int
1275handle_keywordonly_args(struct compiling *c, const node *n, int start,
1276 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1277{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001278 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001279 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001280 expr_ty expression, annotation;
1281 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 int i = start;
1283 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001284
1285 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001286 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001287 return -1;
1288 }
1289 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001290 while (i < NCH(n)) {
1291 ch = CHILD(n, i);
1292 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001293 case vfpdef:
1294 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001295 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001296 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001297 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001298 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001299 asdl_seq_SET(kwdefaults, j, expression);
1300 i += 2; /* '=' and test */
1301 }
1302 else { /* setting NULL if no default value exists */
1303 asdl_seq_SET(kwdefaults, j, NULL);
1304 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001305 if (NCH(ch) == 3) {
1306 /* ch is NAME ':' test */
1307 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001308 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001309 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001310 }
1311 else {
1312 annotation = NULL;
1313 }
1314 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001315 argname = NEW_IDENTIFIER(ch);
1316 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001317 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001318 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001319 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001320 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1321 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001322 if (!arg)
1323 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001324 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001325 i += 2; /* the name and the comma */
1326 break;
1327 case DOUBLESTAR:
1328 return i;
1329 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001330 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 goto error;
1332 }
1333 }
1334 return i;
1335 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001337}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338
Jeremy Hyltona8293132006-02-28 17:58:27 +00001339/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340
1341static arguments_ty
1342ast_for_arguments(struct compiling *c, const node *n)
1343{
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 /* This function handles both typedargslist (function definition)
1345 and varargslist (lambda definition).
1346
1347 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001348 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1349 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1350 | '**' tfpdef [',']]]
1351 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1352 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001353 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001354 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1355 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1356 | '**' vfpdef [',']]]
1357 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1358 | '**' vfpdef [',']
1359 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001360 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001361
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001363 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1364 int nposdefaults = 0, found_default = 0;
1365 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001366 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001367 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 node *ch;
1369
1370 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001372 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001373 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001375 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376
Jeremy Hyltone921e022008-07-17 16:37:17 +00001377 /* First count the number of positional args & defaults. The
1378 variable i is the loop index for this for loop and the next.
1379 The next loop picks up where the first leaves off.
1380 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001382 ch = CHILD(n, i);
1383 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001384 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001385 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001386 if (i < NCH(n) && /* skip argument following star */
1387 (TYPE(CHILD(n, i)) == tfpdef ||
1388 TYPE(CHILD(n, i)) == vfpdef)) {
1389 i++;
1390 }
1391 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001392 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001393 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001394 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 defaults for keyword only args */
1399 for ( ; i < NCH(n); ++i) {
1400 ch = CHILD(n, i);
1401 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001402 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001403 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001404 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001405 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001406 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001408 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001409 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001410 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001412 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001414 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 since we set NULL as default for keyword only argument w/o default
1417 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001418 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001419 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001421 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001422
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001423 /* tfpdef: NAME [':' test]
1424 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 */
1426 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001427 j = 0; /* index for defaults */
1428 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001430 ch = CHILD(n, i);
1431 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001432 case tfpdef:
1433 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1435 anything other than EQUAL or a comma? */
1436 /* XXX Should NCH(n) check be made a separate check? */
1437 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001438 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1439 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001440 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 assert(posdefaults != NULL);
1442 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001444 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001446 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001447 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001449 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001450 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001451 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001452 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001453 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001454 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 i += 2; /* the name and the comma */
1456 break;
1457 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001458 if (i+1 >= NCH(n) ||
1459 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001460 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001461 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001462 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001463 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001464 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001465 if (TYPE(ch) == COMMA) {
1466 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001467 i += 2; /* now follows keyword only arguments */
1468 res = handle_keywordonly_args(c, n, i,
1469 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001470 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001471 i = res; /* res has new position to process */
1472 }
1473 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001474 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001475 if (!vararg)
1476 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001477
Guido van Rossum4f72a782006-10-27 23:31:49 +00001478 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001479 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1480 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001481 int res = 0;
1482 res = handle_keywordonly_args(c, n, i,
1483 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001484 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001485 i = res; /* res has new position to process */
1486 }
1487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 break;
1489 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001490 ch = CHILD(n, i+1); /* tfpdef */
1491 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001492 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001493 if (!kwarg)
1494 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 i += 3;
1496 break;
1497 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001498 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 "unexpected node in varargslist: %d @ %d",
1500 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001501 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001502 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001504 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505}
1506
1507static expr_ty
1508ast_for_dotted_name(struct compiling *c, const node *n)
1509{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001510 expr_ty e;
1511 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001512 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 int i;
1514
1515 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001516
1517 lineno = LINENO(n);
1518 col_offset = n->n_col_offset;
1519
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 id = NEW_IDENTIFIER(CHILD(n, 0));
1521 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001522 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001523 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001525 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526
1527 for (i = 2; i < NCH(n); i+=2) {
1528 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001529 if (!id)
1530 return NULL;
1531 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1532 if (!e)
1533 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 }
1535
1536 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537}
1538
1539static expr_ty
1540ast_for_decorator(struct compiling *c, const node *n)
1541{
1542 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1543 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001544 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001547 REQ(CHILD(n, 0), AT);
1548 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1551 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001552 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001555 d = name_expr;
1556 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557 }
1558 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001559 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001560 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001561 if (!d)
1562 return NULL;
1563 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 }
1565 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001566 d = ast_for_call(c, CHILD(n, 3), name_expr);
1567 if (!d)
1568 return NULL;
1569 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 }
1571
1572 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573}
1574
1575static asdl_seq*
1576ast_for_decorators(struct compiling *c, const node *n)
1577{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001578 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001579 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001583 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 if (!decorator_seq)
1585 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001588 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001589 if (!d)
1590 return NULL;
1591 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 }
1593 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594}
1595
1596static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001597ast_for_funcdef_impl(struct compiling *c, const node *n,
1598 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001600 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001601 identifier name;
1602 arguments_ty args;
1603 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001604 expr_ty returns = NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09001605 string docstring;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001606 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607
1608 REQ(n, funcdef);
1609
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 name = NEW_IDENTIFIER(CHILD(n, name_i));
1611 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001612 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001613 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001614 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1616 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001617 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001618 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1619 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1620 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001621 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001622 name_i += 2;
1623 }
INADA Naokicb41b272017-02-23 00:31:59 +09001624 body = ast_for_body(c, CHILD(n, name_i + 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001626 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627
Yury Selivanov75445082015-05-11 22:57:16 -04001628 if (is_async)
1629 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001630 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001631 n->n_col_offset, c->c_arena);
1632 else
1633 return FunctionDef(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}
1637
1638static stmt_ty
1639ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1640{
1641 /* async_funcdef: ASYNC funcdef */
1642 REQ(n, async_funcdef);
1643 REQ(CHILD(n, 0), ASYNC);
1644 REQ(CHILD(n, 1), funcdef);
1645
1646 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1647 1 /* is_async */);
1648}
1649
1650static stmt_ty
1651ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1652{
1653 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1654 return ast_for_funcdef_impl(c, n, decorator_seq,
1655 0 /* is_async */);
1656}
1657
1658
1659static stmt_ty
1660ast_for_async_stmt(struct compiling *c, const node *n)
1661{
1662 /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */
1663 REQ(n, async_stmt);
1664 REQ(CHILD(n, 0), ASYNC);
1665
1666 switch (TYPE(CHILD(n, 1))) {
1667 case funcdef:
1668 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1669 1 /* is_async */);
1670 case with_stmt:
1671 return ast_for_with_stmt(c, CHILD(n, 1),
1672 1 /* is_async */);
1673
1674 case for_stmt:
1675 return ast_for_for_stmt(c, CHILD(n, 1),
1676 1 /* is_async */);
1677
1678 default:
1679 PyErr_Format(PyExc_SystemError,
1680 "invalid async stament: %s",
1681 STR(CHILD(n, 1)));
1682 return NULL;
1683 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684}
1685
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001686static stmt_ty
1687ast_for_decorated(struct compiling *c, const node *n)
1688{
Yury Selivanov75445082015-05-11 22:57:16 -04001689 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001690 stmt_ty thing = NULL;
1691 asdl_seq *decorator_seq = NULL;
1692
1693 REQ(n, decorated);
1694
1695 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1696 if (!decorator_seq)
1697 return NULL;
1698
1699 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001700 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001702
1703 if (TYPE(CHILD(n, 1)) == funcdef) {
1704 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1705 } else if (TYPE(CHILD(n, 1)) == classdef) {
1706 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001707 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1708 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001709 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001710 /* we count the decorators in when talking about the class' or
1711 * function's line number */
1712 if (thing) {
1713 thing->lineno = LINENO(n);
1714 thing->col_offset = n->n_col_offset;
1715 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001716 return thing;
1717}
1718
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719static expr_ty
1720ast_for_lambdef(struct compiling *c, const node *n)
1721{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001722 /* lambdef: 'lambda' [varargslist] ':' test
1723 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 arguments_ty args;
1725 expr_ty expression;
1726
1727 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001728 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 if (!args)
1730 return NULL;
1731 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001732 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001733 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 }
1735 else {
1736 args = ast_for_arguments(c, CHILD(n, 1));
1737 if (!args)
1738 return NULL;
1739 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001740 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 }
1743
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001744 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745}
1746
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001747static expr_ty
1748ast_for_ifexpr(struct compiling *c, const node *n)
1749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001751 expr_ty expression, body, orelse;
1752
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001753 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001754 body = ast_for_expr(c, CHILD(n, 0));
1755 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001756 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001757 expression = ast_for_expr(c, CHILD(n, 2));
1758 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001759 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001760 orelse = ast_for_expr(c, CHILD(n, 4));
1761 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001762 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001763 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1764 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001765}
1766
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001768 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769
Nick Coghlan650f0d02007-04-15 12:05:43 +00001770 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771*/
1772
1773static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001774count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001776 int n_fors = 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001777 int is_async;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778
Guido van Rossumd8faa362007-04-27 19:54:29 +00001779 count_comp_for:
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001780 is_async = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001781 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001782 REQ(n, comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001783 if (TYPE(CHILD(n, 0)) == ASYNC) {
1784 is_async = 1;
1785 }
1786 if (NCH(n) == (5 + is_async)) {
1787 n = CHILD(n, 4 + is_async);
1788 }
1789 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001791 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001792 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001793 REQ(n, comp_iter);
1794 n = CHILD(n, 0);
1795 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001796 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001797 else if (TYPE(n) == comp_if) {
1798 if (NCH(n) == 3) {
1799 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001800 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001801 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001802 else
1803 return n_fors;
1804 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001805
Guido van Rossumd8faa362007-04-27 19:54:29 +00001806 /* Should never be reached */
1807 PyErr_SetString(PyExc_SystemError,
1808 "logic error in count_comp_fors");
1809 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810}
1811
Nick Coghlan650f0d02007-04-15 12:05:43 +00001812/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813
Nick Coghlan650f0d02007-04-15 12:05:43 +00001814 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815*/
1816
1817static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001818count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001820 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821
Guido van Rossumd8faa362007-04-27 19:54:29 +00001822 while (1) {
1823 REQ(n, comp_iter);
1824 if (TYPE(CHILD(n, 0)) == comp_for)
1825 return n_ifs;
1826 n = CHILD(n, 0);
1827 REQ(n, comp_if);
1828 n_ifs++;
1829 if (NCH(n) == 2)
1830 return n_ifs;
1831 n = CHILD(n, 2);
1832 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833}
1834
Guido van Rossum992d4a32007-07-11 13:09:30 +00001835static asdl_seq *
1836ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001839 asdl_seq *comps;
1840
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001841 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 if (n_fors == -1)
1843 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001844
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001845 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001846 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001848
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001850 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001852 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001853 node *for_ch;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001854 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855
Guido van Rossum992d4a32007-07-11 13:09:30 +00001856 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001858 if (TYPE(CHILD(n, 0)) == ASYNC) {
1859 is_async = 1;
1860 }
1861
1862 for_ch = CHILD(n, 1 + is_async);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001863 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001864 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 return NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001866 expression = ast_for_expr(c, CHILD(n, 3 + is_async));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001867 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869
Thomas Wouters89f507f2006-12-13 04:49:30 +00001870 /* Check the # of children rather than the length of t, since
1871 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001872 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001873 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001874 comp = comprehension(first, expression, NULL,
1875 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001877 comp = comprehension(Tuple(t, Store, first->lineno,
1878 first->col_offset, c->c_arena),
1879 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001880 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001882
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001883 if (NCH(n) == (5 + is_async)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884 int j, n_ifs;
1885 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001887 n = CHILD(n, 4 + is_async);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001888 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001889 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001892 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001893 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001897 REQ(n, comp_iter);
1898 n = CHILD(n, 0);
1899 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001902 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001903 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001904 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001905 if (NCH(n) == 3)
1906 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001908 /* on exit, must guarantee that n is a comp_for */
1909 if (TYPE(n) == comp_iter)
1910 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001911 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001913 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001915 return comps;
1916}
1917
1918static expr_ty
1919ast_for_itercomp(struct compiling *c, const node *n, int type)
1920{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001921 /* testlist_comp: (test|star_expr)
1922 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001923 expr_ty elt;
1924 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001925 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926
Guido van Rossum992d4a32007-07-11 13:09:30 +00001927 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001929 ch = CHILD(n, 0);
1930 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001931 if (!elt)
1932 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001933 if (elt->kind == Starred_kind) {
1934 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1935 return NULL;
1936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937
Guido van Rossum992d4a32007-07-11 13:09:30 +00001938 comps = ast_for_comprehension(c, CHILD(n, 1));
1939 if (!comps)
1940 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001941
1942 if (type == COMP_GENEXP)
1943 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1944 else if (type == COMP_LISTCOMP)
1945 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1946 else if (type == COMP_SETCOMP)
1947 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1948 else
1949 /* Should never happen */
1950 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951}
1952
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001953/* Fills in the key, value pair corresponding to the dict element. In case
1954 * of an unpacking, key is NULL. *i is advanced by the number of ast
1955 * elements. Iff successful, nonzero is returned.
1956 */
1957static int
1958ast_for_dictelement(struct compiling *c, const node *n, int *i,
1959 expr_ty *key, expr_ty *value)
1960{
1961 expr_ty expression;
1962 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1963 assert(NCH(n) - *i >= 2);
1964
1965 expression = ast_for_expr(c, CHILD(n, *i + 1));
1966 if (!expression)
1967 return 0;
1968 *key = NULL;
1969 *value = expression;
1970
1971 *i += 2;
1972 }
1973 else {
1974 assert(NCH(n) - *i >= 3);
1975
1976 expression = ast_for_expr(c, CHILD(n, *i));
1977 if (!expression)
1978 return 0;
1979 *key = expression;
1980
1981 REQ(CHILD(n, *i + 1), COLON);
1982
1983 expression = ast_for_expr(c, CHILD(n, *i + 2));
1984 if (!expression)
1985 return 0;
1986 *value = expression;
1987
1988 *i += 3;
1989 }
1990 return 1;
1991}
1992
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001994ast_for_dictcomp(struct compiling *c, const node *n)
1995{
1996 expr_ty key, value;
1997 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001998 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002000 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002001 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002002 assert(key);
2003 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002005 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002006 if (!comps)
2007 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008
Guido van Rossum992d4a32007-07-11 13:09:30 +00002009 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2010}
2011
2012static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002013ast_for_dictdisplay(struct compiling *c, const node *n)
2014{
2015 int i;
2016 int j;
2017 int size;
2018 asdl_seq *keys, *values;
2019
2020 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2021 keys = _Py_asdl_seq_new(size, c->c_arena);
2022 if (!keys)
2023 return NULL;
2024
2025 values = _Py_asdl_seq_new(size, c->c_arena);
2026 if (!values)
2027 return NULL;
2028
2029 j = 0;
2030 for (i = 0; i < NCH(n); i++) {
2031 expr_ty key, value;
2032
2033 if (!ast_for_dictelement(c, n, &i, &key, &value))
2034 return NULL;
2035 asdl_seq_SET(keys, j, key);
2036 asdl_seq_SET(values, j, value);
2037
2038 j++;
2039 }
2040 keys->size = j;
2041 values->size = j;
2042 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2043}
2044
2045static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002046ast_for_genexp(struct compiling *c, const node *n)
2047{
2048 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002049 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002050}
2051
2052static expr_ty
2053ast_for_listcomp(struct compiling *c, const node *n)
2054{
2055 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002056 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002057}
2058
2059static expr_ty
2060ast_for_setcomp(struct compiling *c, const node *n)
2061{
2062 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002063 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002064}
2065
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002066static expr_ty
2067ast_for_setdisplay(struct compiling *c, const node *n)
2068{
2069 int i;
2070 int size;
2071 asdl_seq *elts;
2072
2073 assert(TYPE(n) == (dictorsetmaker));
2074 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2075 elts = _Py_asdl_seq_new(size, c->c_arena);
2076 if (!elts)
2077 return NULL;
2078 for (i = 0; i < NCH(n); i += 2) {
2079 expr_ty expression;
2080 expression = ast_for_expr(c, CHILD(n, i));
2081 if (!expression)
2082 return NULL;
2083 asdl_seq_SET(elts, i / 2, expression);
2084 }
2085 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2086}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002087
2088static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089ast_for_atom(struct compiling *c, const node *n)
2090{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002091 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2092 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002093 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 */
2095 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002098 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002099 PyObject *name;
2100 const char *s = STR(ch);
2101 size_t len = strlen(s);
2102 if (len >= 4 && len <= 5) {
2103 if (!strcmp(s, "None"))
2104 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2105 if (!strcmp(s, "True"))
2106 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2107 if (!strcmp(s, "False"))
2108 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2109 }
2110 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002111 if (!name)
2112 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002113 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002114 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2115 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002117 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002118 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002119 const char *errtype = NULL;
2120 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2121 errtype = "unicode error";
2122 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2123 errtype = "value error";
2124 if (errtype) {
2125 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002126 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002127 PyObject *type, *value, *tback, *errstr;
2128 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002129 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002130 if (errstr)
2131 s = PyUnicode_AsUTF8(errstr);
2132 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002133 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002134 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002135 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002136 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002137 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002138 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002139 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002140 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002141 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002142 Py_XDECREF(tback);
2143 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002144 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002145 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002146 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 }
2148 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002149 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002150 if (!pynum)
2151 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002152
Victor Stinner43d81952013-07-17 00:57:58 +02002153 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2154 Py_DECREF(pynum);
2155 return NULL;
2156 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002157 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 }
Georg Brandldde00282007-03-18 19:01:53 +00002159 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002160 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002162 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163
Thomas Wouters89f507f2006-12-13 04:49:30 +00002164 if (TYPE(ch) == RPAR)
2165 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166
Thomas Wouters89f507f2006-12-13 04:49:30 +00002167 if (TYPE(ch) == yield_expr)
2168 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002171 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002172 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002173
Nick Coghlan650f0d02007-04-15 12:05:43 +00002174 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002176 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177
Thomas Wouters89f507f2006-12-13 04:49:30 +00002178 if (TYPE(ch) == RSQB)
2179 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180
Nick Coghlan650f0d02007-04-15 12:05:43 +00002181 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002182 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2183 asdl_seq *elts = seq_for_testlist(c, ch);
2184 if (!elts)
2185 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002186
Thomas Wouters89f507f2006-12-13 04:49:30 +00002187 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2188 }
2189 else
2190 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002192 /* dictorsetmaker: ( ((test ':' test | '**' test)
2193 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2194 * ((test | '*' test)
2195 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002196 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002197 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002198 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002199 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002200 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002201 }
2202 else {
2203 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2204 if (NCH(ch) == 1 ||
2205 (NCH(ch) > 1 &&
2206 TYPE(CHILD(ch, 1)) == COMMA)) {
2207 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002208 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002209 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002210 else if (NCH(ch) > 1 &&
2211 TYPE(CHILD(ch, 1)) == comp_for) {
2212 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002213 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002214 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002215 else if (NCH(ch) > 3 - is_dict &&
2216 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2217 /* It's a dictionary comprehension. */
2218 if (is_dict) {
2219 ast_error(c, n, "dict unpacking cannot be used in "
2220 "dict comprehension");
2221 return NULL;
2222 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002223 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002224 }
2225 else {
2226 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002227 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002228 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002229 if (res) {
2230 res->lineno = LINENO(n);
2231 res->col_offset = n->n_col_offset;
2232 }
2233 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002237 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2238 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 }
2240}
2241
2242static slice_ty
2243ast_for_slice(struct compiling *c, const node *n)
2244{
2245 node *ch;
2246 expr_ty lower = NULL, upper = NULL, step = NULL;
2247
2248 REQ(n, subscript);
2249
2250 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002251 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 sliceop: ':' [test]
2253 */
2254 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 if (NCH(n) == 1 && TYPE(ch) == test) {
2256 /* 'step' variable hold no significance in terms of being used over
2257 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 if (!step)
2260 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261
Thomas Wouters89f507f2006-12-13 04:49:30 +00002262 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 }
2264
2265 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002266 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 if (!lower)
2268 return NULL;
2269 }
2270
2271 /* If there's an upper bound it's in the second or third position. */
2272 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002273 if (NCH(n) > 1) {
2274 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Thomas Wouters89f507f2006-12-13 04:49:30 +00002276 if (TYPE(n2) == test) {
2277 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 if (!upper)
2279 return NULL;
2280 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002281 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002283 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284
Thomas Wouters89f507f2006-12-13 04:49:30 +00002285 if (TYPE(n2) == test) {
2286 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 if (!upper)
2288 return NULL;
2289 }
2290 }
2291
2292 ch = CHILD(n, NCH(n) - 1);
2293 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002294 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002295 ch = CHILD(ch, 1);
2296 if (TYPE(ch) == test) {
2297 step = ast_for_expr(c, ch);
2298 if (!step)
2299 return NULL;
2300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 }
2302 }
2303
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002304 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305}
2306
2307static expr_ty
2308ast_for_binop(struct compiling *c, const node *n)
2309{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002310 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002312 BinOp(BinOp(A, op, B), op, C).
2313 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314
Guido van Rossumd8faa362007-04-27 19:54:29 +00002315 int i, nops;
2316 expr_ty expr1, expr2, result;
2317 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318
Guido van Rossumd8faa362007-04-27 19:54:29 +00002319 expr1 = ast_for_expr(c, CHILD(n, 0));
2320 if (!expr1)
2321 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322
Guido van Rossumd8faa362007-04-27 19:54:29 +00002323 expr2 = ast_for_expr(c, CHILD(n, 2));
2324 if (!expr2)
2325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326
Guido van Rossumd8faa362007-04-27 19:54:29 +00002327 newoperator = get_operator(CHILD(n, 1));
2328 if (!newoperator)
2329 return NULL;
2330
2331 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2332 c->c_arena);
2333 if (!result)
2334 return NULL;
2335
2336 nops = (NCH(n) - 1) / 2;
2337 for (i = 1; i < nops; i++) {
2338 expr_ty tmp_result, tmp;
2339 const node* next_oper = CHILD(n, i * 2 + 1);
2340
2341 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002342 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 return NULL;
2344
Guido van Rossumd8faa362007-04-27 19:54:29 +00002345 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2346 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 return NULL;
2348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002350 LINENO(next_oper), next_oper->n_col_offset,
2351 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002353 return NULL;
2354 result = tmp_result;
2355 }
2356 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357}
2358
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002359static expr_ty
2360ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002363 subscriptlist: subscript (',' subscript)* [',']
2364 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2365 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002366 REQ(n, trailer);
2367 if (TYPE(CHILD(n, 0)) == LPAR) {
2368 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002369 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002370 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002371 else
2372 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002373 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002374 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002375 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2376 if (!attr_id)
2377 return NULL;
2378 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002379 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002380 }
2381 else {
2382 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002383 REQ(CHILD(n, 2), RSQB);
2384 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002385 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002386 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2387 if (!slc)
2388 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002389 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2390 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002391 }
2392 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002394 by treating the sequence as a tuple literal if there are
2395 no slice features.
2396 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002397 int j;
2398 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002399 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002400 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002401 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002402 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002403 if (!slices)
2404 return NULL;
2405 for (j = 0; j < NCH(n); j += 2) {
2406 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002407 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002408 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002409 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002410 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002411 asdl_seq_SET(slices, j / 2, slc);
2412 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002413 if (!simple) {
2414 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002415 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002416 }
2417 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002418 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002419 if (!elts)
2420 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002421 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2422 slc = (slice_ty)asdl_seq_GET(slices, j);
2423 assert(slc->kind == Index_kind && slc->v.Index.value);
2424 asdl_seq_SET(elts, j, slc->v.Index.value);
2425 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002426 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002427 if (!e)
2428 return NULL;
2429 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002430 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002431 }
2432 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002433}
2434
2435static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002436ast_for_factor(struct compiling *c, const node *n)
2437{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002438 expr_ty expression;
2439
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002440 expression = ast_for_expr(c, CHILD(n, 1));
2441 if (!expression)
2442 return NULL;
2443
2444 switch (TYPE(CHILD(n, 0))) {
2445 case PLUS:
2446 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2447 c->c_arena);
2448 case MINUS:
2449 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2450 c->c_arena);
2451 case TILDE:
2452 return UnaryOp(Invert, expression, LINENO(n),
2453 n->n_col_offset, c->c_arena);
2454 }
2455 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2456 TYPE(CHILD(n, 0)));
2457 return NULL;
2458}
2459
2460static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002461ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002462{
Yury Selivanov75445082015-05-11 22:57:16 -04002463 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002464 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002465
2466 REQ(n, atom_expr);
2467 nch = NCH(n);
2468
2469 if (TYPE(CHILD(n, 0)) == AWAIT) {
2470 start = 1;
2471 assert(nch > 1);
2472 }
2473
2474 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002475 if (!e)
2476 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002477 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002478 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002479 if (start && nch == 2) {
2480 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2481 }
2482
2483 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002484 node *ch = CHILD(n, i);
2485 if (TYPE(ch) != trailer)
2486 break;
2487 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002488 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002489 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002490 tmp->lineno = e->lineno;
2491 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002492 e = tmp;
2493 }
Yury Selivanov75445082015-05-11 22:57:16 -04002494
2495 if (start) {
2496 /* there was an AWAIT */
2497 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2498 }
2499 else {
2500 return e;
2501 }
2502}
2503
2504static expr_ty
2505ast_for_power(struct compiling *c, const node *n)
2506{
2507 /* power: atom trailer* ('**' factor)*
2508 */
2509 expr_ty e;
2510 REQ(n, power);
2511 e = ast_for_atom_expr(c, CHILD(n, 0));
2512 if (!e)
2513 return NULL;
2514 if (NCH(n) == 1)
2515 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002516 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2517 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002518 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002519 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002520 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002521 }
2522 return e;
2523}
2524
Guido van Rossum0368b722007-05-11 16:50:42 +00002525static expr_ty
2526ast_for_starred(struct compiling *c, const node *n)
2527{
2528 expr_ty tmp;
2529 REQ(n, star_expr);
2530
2531 tmp = ast_for_expr(c, CHILD(n, 1));
2532 if (!tmp)
2533 return NULL;
2534
2535 /* The Load context is changed later. */
2536 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2537}
2538
2539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540/* Do not name a variable 'expr'! Will cause a compile error.
2541*/
2542
2543static expr_ty
2544ast_for_expr(struct compiling *c, const node *n)
2545{
2546 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002547 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002548 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 and_test: not_test ('and' not_test)*
2551 not_test: 'not' not_test | comparison
2552 comparison: expr (comp_op expr)*
2553 expr: xor_expr ('|' xor_expr)*
2554 xor_expr: and_expr ('^' and_expr)*
2555 and_expr: shift_expr ('&' shift_expr)*
2556 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2557 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002558 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002560 power: atom_expr ['**' factor]
2561 atom_expr: [AWAIT] atom trailer*
2562 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 */
2564
2565 asdl_seq *seq;
2566 int i;
2567
2568 loop:
2569 switch (TYPE(n)) {
2570 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002571 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002572 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002573 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002575 else if (NCH(n) > 1)
2576 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002577 /* Fallthrough */
2578 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002579 case and_test:
2580 if (NCH(n) == 1) {
2581 n = CHILD(n, 0);
2582 goto loop;
2583 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002584 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 if (!seq)
2586 return NULL;
2587 for (i = 0; i < NCH(n); i += 2) {
2588 expr_ty e = ast_for_expr(c, CHILD(n, i));
2589 if (!e)
2590 return NULL;
2591 asdl_seq_SET(seq, i / 2, e);
2592 }
2593 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002594 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2595 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002596 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002597 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 case not_test:
2599 if (NCH(n) == 1) {
2600 n = CHILD(n, 0);
2601 goto loop;
2602 }
2603 else {
2604 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2605 if (!expression)
2606 return NULL;
2607
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002608 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2609 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 }
2611 case comparison:
2612 if (NCH(n) == 1) {
2613 n = CHILD(n, 0);
2614 goto loop;
2615 }
2616 else {
2617 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002618 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002619 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002620 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 if (!ops)
2622 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002623 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 return NULL;
2626 }
2627 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002628 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002630 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002631 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002633 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634
2635 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002636 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002638 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002640 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 asdl_seq_SET(cmps, i / 2, expression);
2642 }
2643 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002644 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002646 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002648 return Compare(expression, ops, cmps, LINENO(n),
2649 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 }
2651 break;
2652
Guido van Rossum0368b722007-05-11 16:50:42 +00002653 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 /* The next five cases all handle BinOps. The main body of code
2656 is the same in each case, but the switch turned inside out to
2657 reuse the code for each type of operator.
2658 */
2659 case expr:
2660 case xor_expr:
2661 case and_expr:
2662 case shift_expr:
2663 case arith_expr:
2664 case term:
2665 if (NCH(n) == 1) {
2666 n = CHILD(n, 0);
2667 goto loop;
2668 }
2669 return ast_for_binop(c, n);
2670 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002671 node *an = NULL;
2672 node *en = NULL;
2673 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002674 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002675 if (NCH(n) > 1)
2676 an = CHILD(n, 1); /* yield_arg */
2677 if (an) {
2678 en = CHILD(an, NCH(an) - 1);
2679 if (NCH(an) == 2) {
2680 is_from = 1;
2681 exp = ast_for_expr(c, en);
2682 }
2683 else
2684 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002685 if (!exp)
2686 return NULL;
2687 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002688 if (is_from)
2689 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2690 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002691 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002692 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 if (NCH(n) == 1) {
2694 n = CHILD(n, 0);
2695 goto loop;
2696 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002697 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002698 case power:
2699 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002701 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 return NULL;
2703 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002704 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 return NULL;
2706}
2707
2708static expr_ty
2709ast_for_call(struct compiling *c, const node *n, expr_ty func)
2710{
2711 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002712 arglist: argument (',' argument)* [',']
2713 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 */
2715
2716 int i, nargs, nkeywords, ngens;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002717 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002718 asdl_seq *args;
2719 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720
2721 REQ(n, arglist);
2722
2723 nargs = 0;
2724 nkeywords = 0;
2725 ngens = 0;
2726 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002727 node *ch = CHILD(n, i);
2728 if (TYPE(ch) == argument) {
2729 if (NCH(ch) == 1)
2730 nargs++;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002731 else if (TYPE(CHILD(ch, 1)) == comp_for)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002732 ngens++;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002733 else if (TYPE(CHILD(ch, 0)) == STAR)
2734 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002736 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002737 nkeywords++;
2738 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 }
2740 if (ngens > 1 || (ngens && (nargs || nkeywords))) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002741 ast_error(c, n, "Generator expression must be parenthesized "
Thomas Wouters89f507f2006-12-13 04:49:30 +00002742 "if not sole argument");
2743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744 }
2745
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002746 args = _Py_asdl_seq_new(nargs + ngens, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002748 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002749 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002751 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002752
2753 nargs = 0; /* positional arguments + iterable argument unpackings */
2754 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2755 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002757 node *ch = CHILD(n, i);
2758 if (TYPE(ch) == argument) {
2759 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002760 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002761 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002762 /* a positional argument */
2763 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002764 if (ndoublestars) {
2765 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002766 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002767 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002768 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002769 else {
2770 ast_error(c, chch,
2771 "positional argument follows "
2772 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002773 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002774 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002775 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002776 e = ast_for_expr(c, chch);
2777 if (!e)
2778 return NULL;
2779 asdl_seq_SET(args, nargs++, e);
2780 }
2781 else if (TYPE(chch) == STAR) {
2782 /* an iterable argument unpacking */
2783 expr_ty starred;
2784 if (ndoublestars) {
2785 ast_error(c, chch,
2786 "iterable argument unpacking follows "
2787 "keyword argument unpacking");
2788 return NULL;
2789 }
2790 e = ast_for_expr(c, CHILD(ch, 1));
2791 if (!e)
2792 return NULL;
2793 starred = Starred(e, Load, LINENO(chch),
2794 chch->n_col_offset,
2795 c->c_arena);
2796 if (!starred)
2797 return NULL;
2798 asdl_seq_SET(args, nargs++, starred);
2799
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002800 }
2801 else if (TYPE(chch) == DOUBLESTAR) {
2802 /* a keyword argument unpacking */
2803 keyword_ty kw;
2804 i++;
2805 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002807 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002808 kw = keyword(NULL, e, c->c_arena);
2809 asdl_seq_SET(keywords, nkeywords++, kw);
2810 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002812 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002813 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002814 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002816 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002819 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002820 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002821 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002822 identifier key, tmp;
2823 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002825 /* chch is test, but must be an identifier? */
2826 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 /* f(lambda x: x[0] = 3) ends up getting parsed with
2830 * LHS test = lambda x: x[0], and RHS test = 3.
2831 * SF bug 132313 points out that complaining about a keyword
2832 * then is very confusing.
2833 */
2834 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002835 ast_error(c, chch,
2836 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002837 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002838 }
2839 else if (e->kind != Name_kind) {
2840 ast_error(c, chch,
2841 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002842 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002843 }
2844 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002845 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002847 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002848 for (k = 0; k < nkeywords; k++) {
2849 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002850 if (tmp && !PyUnicode_Compare(tmp, key)) {
2851 ast_error(c, chch,
2852 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002853 return NULL;
2854 }
2855 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002856 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002858 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002859 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002861 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002862 asdl_seq_SET(keywords, nkeywords++, kw);
2863 }
2864 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 }
2866
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002867 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868}
2869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002871ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002873 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002874 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002876 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002877 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002878 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002879 }
2880 else {
2881 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002882 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002883 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002885 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886 else {
2887 asdl_seq *tmp = seq_for_testlist(c, n);
2888 if (!tmp)
2889 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002890 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002892}
2893
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894static stmt_ty
2895ast_for_expr_stmt(struct compiling *c, const node *n)
2896{
2897 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002898 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2899 ('=' (yield_expr|testlist_star_expr))*)
2900 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002901 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002902 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002903 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002904 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002905 */
2906
2907 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002908 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 if (!e)
2910 return NULL;
2911
Thomas Wouters89f507f2006-12-13 04:49:30 +00002912 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 }
2914 else if (TYPE(CHILD(n, 1)) == augassign) {
2915 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002916 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002917 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918
Thomas Wouters89f507f2006-12-13 04:49:30 +00002919 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 if (!expr1)
2921 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002922 if(!set_context(c, expr1, Store, ch))
2923 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002924 /* set_context checks that most expressions are not the left side.
2925 Augmented assignments can only have a name, a subscript, or an
2926 attribute on the left, though, so we have to explicitly check for
2927 those. */
2928 switch (expr1->kind) {
2929 case Name_kind:
2930 case Attribute_kind:
2931 case Subscript_kind:
2932 break;
2933 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002934 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002935 return NULL;
2936 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937
Thomas Wouters89f507f2006-12-13 04:49:30 +00002938 ch = CHILD(n, 2);
2939 if (TYPE(ch) == testlist)
2940 expr2 = ast_for_testlist(c, ch);
2941 else
2942 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002943 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944 return NULL;
2945
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002946 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002947 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948 return NULL;
2949
Thomas Wouters89f507f2006-12-13 04:49:30 +00002950 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002952 else if (TYPE(CHILD(n, 1)) == annassign) {
2953 expr_ty expr1, expr2, expr3;
2954 node *ch = CHILD(n, 0);
2955 node *deep, *ann = CHILD(n, 1);
2956 int simple = 1;
2957
2958 /* we keep track of parens to qualify (x) as expression not name */
2959 deep = ch;
2960 while (NCH(deep) == 1) {
2961 deep = CHILD(deep, 0);
2962 }
2963 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2964 simple = 0;
2965 }
2966 expr1 = ast_for_testlist(c, ch);
2967 if (!expr1) {
2968 return NULL;
2969 }
2970 switch (expr1->kind) {
2971 case Name_kind:
2972 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2973 return NULL;
2974 }
2975 expr1->v.Name.ctx = Store;
2976 break;
2977 case Attribute_kind:
2978 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2979 return NULL;
2980 }
2981 expr1->v.Attribute.ctx = Store;
2982 break;
2983 case Subscript_kind:
2984 expr1->v.Subscript.ctx = Store;
2985 break;
2986 case List_kind:
2987 ast_error(c, ch,
2988 "only single target (not list) can be annotated");
2989 return NULL;
2990 case Tuple_kind:
2991 ast_error(c, ch,
2992 "only single target (not tuple) can be annotated");
2993 return NULL;
2994 default:
2995 ast_error(c, ch,
2996 "illegal target for annotation");
2997 return NULL;
2998 }
2999
3000 if (expr1->kind != Name_kind) {
3001 simple = 0;
3002 }
3003 ch = CHILD(ann, 1);
3004 expr2 = ast_for_expr(c, ch);
3005 if (!expr2) {
3006 return NULL;
3007 }
3008 if (NCH(ann) == 2) {
3009 return AnnAssign(expr1, expr2, NULL, simple,
3010 LINENO(n), n->n_col_offset, c->c_arena);
3011 }
3012 else {
3013 ch = CHILD(ann, 3);
3014 expr3 = ast_for_expr(c, ch);
3015 if (!expr3) {
3016 return NULL;
3017 }
3018 return AnnAssign(expr1, expr2, expr3, simple,
3019 LINENO(n), n->n_col_offset, c->c_arena);
3020 }
3021 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003023 int i;
3024 asdl_seq *targets;
3025 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 expr_ty expression;
3027
Thomas Wouters89f507f2006-12-13 04:49:30 +00003028 /* a normal assignment */
3029 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003030 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003031 if (!targets)
3032 return NULL;
3033 for (i = 0; i < NCH(n) - 2; i += 2) {
3034 expr_ty e;
3035 node *ch = CHILD(n, i);
3036 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003037 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003038 return NULL;
3039 }
3040 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003042 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003044 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003045 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003046 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047
Thomas Wouters89f507f2006-12-13 04:49:30 +00003048 asdl_seq_SET(targets, i / 2, e);
3049 }
3050 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003051 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003052 expression = ast_for_testlist(c, value);
3053 else
3054 expression = ast_for_expr(c, value);
3055 if (!expression)
3056 return NULL;
3057 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059}
3060
Benjamin Peterson78565b22009-06-28 19:19:51 +00003061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003063ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064{
3065 asdl_seq *seq;
3066 int i;
3067 expr_ty e;
3068
3069 REQ(n, exprlist);
3070
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003071 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003075 e = ast_for_expr(c, CHILD(n, i));
3076 if (!e)
3077 return NULL;
3078 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003079 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003080 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081 }
3082 return seq;
3083}
3084
3085static stmt_ty
3086ast_for_del_stmt(struct compiling *c, const node *n)
3087{
3088 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090 /* del_stmt: 'del' exprlist */
3091 REQ(n, del_stmt);
3092
3093 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3094 if (!expr_list)
3095 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003096 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003097}
3098
3099static stmt_ty
3100ast_for_flow_stmt(struct compiling *c, const node *n)
3101{
3102 /*
3103 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3104 | yield_stmt
3105 break_stmt: 'break'
3106 continue_stmt: 'continue'
3107 return_stmt: 'return' [testlist]
3108 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003109 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 raise_stmt: 'raise' [test [',' test [',' test]]]
3111 */
3112 node *ch;
3113
3114 REQ(n, flow_stmt);
3115 ch = CHILD(n, 0);
3116 switch (TYPE(ch)) {
3117 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003118 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003120 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003122 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3123 if (!exp)
3124 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003125 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 }
3127 case return_stmt:
3128 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003129 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003131 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 if (!expression)
3133 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003134 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 }
3136 case raise_stmt:
3137 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003138 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3139 else if (NCH(ch) >= 2) {
3140 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3142 if (!expression)
3143 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003144 if (NCH(ch) == 4) {
3145 cause = ast_for_expr(c, CHILD(ch, 3));
3146 if (!cause)
3147 return NULL;
3148 }
3149 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 }
Stefan Krahf432a322017-08-21 13:09:59 +02003151 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003153 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 "unexpected flow_stmt: %d", TYPE(ch));
3155 return NULL;
3156 }
3157}
3158
3159static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003160alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161{
3162 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003163 import_as_name: NAME ['as' NAME]
3164 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 dotted_name: NAME ('.' NAME)*
3166 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003167 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003168
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 loop:
3170 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003171 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003173 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003174 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003175 if (!name)
3176 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003177 if (NCH(n) == 3) {
3178 node *str_node = CHILD(n, 2);
3179 str = NEW_IDENTIFIER(str_node);
3180 if (!str)
3181 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003182 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003183 return NULL;
3184 }
3185 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003186 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003187 return NULL;
3188 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003189 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003190 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 case dotted_as_name:
3192 if (NCH(n) == 1) {
3193 n = CHILD(n, 0);
3194 goto loop;
3195 }
3196 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003197 node *asname_node = CHILD(n, 2);
3198 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003199 if (!a)
3200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003202 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003203 if (!a->asname)
3204 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003205 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 return a;
3208 }
3209 break;
3210 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003211 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003212 node *name_node = CHILD(n, 0);
3213 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003214 if (!name)
3215 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003216 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003217 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003218 return alias(name, NULL, c->c_arena);
3219 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 else {
3221 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003222 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003223 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226
3227 len = 0;
3228 for (i = 0; i < NCH(n); i += 2)
3229 /* length of string plus one for the dot */
3230 len += strlen(STR(CHILD(n, i))) + 1;
3231 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003232 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 if (!str)
3234 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003235 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 if (!s)
3237 return NULL;
3238 for (i = 0; i < NCH(n); i += 2) {
3239 char *sch = STR(CHILD(n, i));
3240 strcpy(s, STR(CHILD(n, i)));
3241 s += strlen(sch);
3242 *s++ = '.';
3243 }
3244 --s;
3245 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3247 PyBytes_GET_SIZE(str),
3248 NULL);
3249 Py_DECREF(str);
3250 if (!uni)
3251 return NULL;
3252 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003253 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003254 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3255 Py_DECREF(str);
3256 return NULL;
3257 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003258 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 }
3260 break;
3261 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003262 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003263 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3264 Py_DECREF(str);
3265 return NULL;
3266 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003267 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003269 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 "unexpected import name: %d", TYPE(n));
3271 return NULL;
3272 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003273
3274 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275 return NULL;
3276}
3277
3278static stmt_ty
3279ast_for_import_stmt(struct compiling *c, const node *n)
3280{
3281 /*
3282 import_stmt: import_name | import_from
3283 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003284 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3285 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003287 int lineno;
3288 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 int i;
3290 asdl_seq *aliases;
3291
3292 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003293 lineno = LINENO(n);
3294 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003296 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003299 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003300 if (!aliases)
3301 return NULL;
3302 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003303 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003304 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003306 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003310 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003312 int idx, ndots = 0;
3313 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003314 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003316 /* Count the number of dots (for relative imports) and check for the
3317 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 for (idx = 1; idx < NCH(n); idx++) {
3319 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003320 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3321 if (!mod)
3322 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 idx++;
3324 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003325 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003327 ndots += 3;
3328 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003329 } else if (TYPE(CHILD(n, idx)) != DOT) {
3330 break;
3331 }
3332 ndots++;
3333 }
3334 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003335 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003336 case STAR:
3337 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 n = CHILD(n, idx);
3339 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 break;
3341 case LPAR:
3342 /* from ... import (x, y, z) */
3343 n = CHILD(n, idx + 1);
3344 n_children = NCH(n);
3345 break;
3346 case import_as_names:
3347 /* from ... import x, y, z */
3348 n = CHILD(n, idx);
3349 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003350 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003351 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 " surrounding parentheses");
3353 return NULL;
3354 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 break;
3356 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003357 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003358 return NULL;
3359 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003361 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364
3365 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003366 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003367 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003368 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003370 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003372 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003374 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003375 if (!import_alias)
3376 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003377 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003378 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003380 if (mod != NULL)
3381 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003382 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003383 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 }
Neal Norwitz79792652005-11-14 04:25:03 +00003385 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 "unknown import statement: starts with command '%s'",
3387 STR(CHILD(n, 0)));
3388 return NULL;
3389}
3390
3391static stmt_ty
3392ast_for_global_stmt(struct compiling *c, const node *n)
3393{
3394 /* global_stmt: 'global' NAME (',' NAME)* */
3395 identifier name;
3396 asdl_seq *s;
3397 int i;
3398
3399 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003400 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003404 name = NEW_IDENTIFIER(CHILD(n, i));
3405 if (!name)
3406 return NULL;
3407 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003409 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410}
3411
3412static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003413ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3414{
3415 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3416 identifier name;
3417 asdl_seq *s;
3418 int i;
3419
3420 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003421 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003422 if (!s)
3423 return NULL;
3424 for (i = 1; i < NCH(n); i += 2) {
3425 name = NEW_IDENTIFIER(CHILD(n, i));
3426 if (!name)
3427 return NULL;
3428 asdl_seq_SET(s, i / 2, name);
3429 }
3430 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3431}
3432
3433static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434ast_for_assert_stmt(struct compiling *c, const node *n)
3435{
3436 /* assert_stmt: 'assert' test [',' test] */
3437 REQ(n, assert_stmt);
3438 if (NCH(n) == 2) {
3439 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3440 if (!expression)
3441 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003442 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443 }
3444 else if (NCH(n) == 4) {
3445 expr_ty expr1, expr2;
3446
3447 expr1 = ast_for_expr(c, CHILD(n, 1));
3448 if (!expr1)
3449 return NULL;
3450 expr2 = ast_for_expr(c, CHILD(n, 3));
3451 if (!expr2)
3452 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453
Thomas Wouters89f507f2006-12-13 04:49:30 +00003454 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 }
Neal Norwitz79792652005-11-14 04:25:03 +00003456 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 "improper number of parts to 'assert' statement: %d",
3458 NCH(n));
3459 return NULL;
3460}
3461
3462static asdl_seq *
3463ast_for_suite(struct compiling *c, const node *n)
3464{
3465 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003466 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 stmt_ty s;
3468 int i, total, num, end, pos = 0;
3469 node *ch;
3470
3471 REQ(n, suite);
3472
3473 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003474 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 n = CHILD(n, 0);
3479 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003481 */
3482 end = NCH(n) - 1;
3483 if (TYPE(CHILD(n, end - 1)) == SEMI)
3484 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003486 for (i = 0; i < end; i += 2) {
3487 ch = CHILD(n, i);
3488 s = ast_for_stmt(c, ch);
3489 if (!s)
3490 return NULL;
3491 asdl_seq_SET(seq, pos++, s);
3492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 }
3494 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003495 for (i = 2; i < (NCH(n) - 1); i++) {
3496 ch = CHILD(n, i);
3497 REQ(ch, stmt);
3498 num = num_stmts(ch);
3499 if (num == 1) {
3500 /* small_stmt or compound_stmt with only one child */
3501 s = ast_for_stmt(c, ch);
3502 if (!s)
3503 return NULL;
3504 asdl_seq_SET(seq, pos++, s);
3505 }
3506 else {
3507 int j;
3508 ch = CHILD(ch, 0);
3509 REQ(ch, simple_stmt);
3510 for (j = 0; j < NCH(ch); j += 2) {
3511 /* statement terminates with a semi-colon ';' */
3512 if (NCH(CHILD(ch, j)) == 0) {
3513 assert((j + 1) == NCH(ch));
3514 break;
3515 }
3516 s = ast_for_stmt(c, CHILD(ch, j));
3517 if (!s)
3518 return NULL;
3519 asdl_seq_SET(seq, pos++, s);
3520 }
3521 }
3522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523 }
3524 assert(pos == seq->size);
3525 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526}
3527
INADA Naokicb41b272017-02-23 00:31:59 +09003528static string
3529docstring_from_stmts(asdl_seq *stmts)
3530{
3531 if (stmts && stmts->size) {
3532 stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0);
3533 /* If first statement is a literal string, it's the doc string. */
3534 if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) {
3535 string doc = s->v.Expr.value->v.Str.s;
3536 /* not very efficient, but simple */
3537 memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1),
3538 (stmts->size - 1) * sizeof(void*));
3539 stmts->size--;
3540 return doc;
3541 }
3542 }
3543 return NULL;
3544}
3545
3546static asdl_seq *
3547ast_for_body(struct compiling *c, const node *n, string *docstring)
3548{
3549 asdl_seq *stmts = ast_for_suite(c, n);
3550 *docstring = docstring_from_stmts(stmts);
3551 return stmts;
3552}
3553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554static stmt_ty
3555ast_for_if_stmt(struct compiling *c, const node *n)
3556{
3557 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3558 ['else' ':' suite]
3559 */
3560 char *s;
3561
3562 REQ(n, if_stmt);
3563
3564 if (NCH(n) == 4) {
3565 expr_ty expression;
3566 asdl_seq *suite_seq;
3567
3568 expression = ast_for_expr(c, CHILD(n, 1));
3569 if (!expression)
3570 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003572 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574
Guido van Rossumd8faa362007-04-27 19:54:29 +00003575 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3576 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003578
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 s = STR(CHILD(n, 4));
3580 /* s[2], the third character in the string, will be
3581 's' for el_s_e, or
3582 'i' for el_i_f
3583 */
3584 if (s[2] == 's') {
3585 expr_ty expression;
3586 asdl_seq *seq1, *seq2;
3587
3588 expression = ast_for_expr(c, CHILD(n, 1));
3589 if (!expression)
3590 return NULL;
3591 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003592 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593 return NULL;
3594 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003595 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 return NULL;
3597
Guido van Rossumd8faa362007-04-27 19:54:29 +00003598 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3599 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 }
3601 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003602 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003603 expr_ty expression;
3604 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003605 asdl_seq *orelse = NULL;
3606 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 /* must reference the child n_elif+1 since 'else' token is third,
3608 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003609 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3610 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3611 has_else = 1;
3612 n_elif -= 3;
3613 }
3614 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615
Thomas Wouters89f507f2006-12-13 04:49:30 +00003616 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003617 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003619 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003620 if (!orelse)
3621 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003623 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003625 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3626 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003628 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3629 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 asdl_seq_SET(orelse, 0,
3633 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003634 LINENO(CHILD(n, NCH(n) - 6)),
3635 CHILD(n, NCH(n) - 6)->n_col_offset,
3636 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003637 /* the just-created orelse handled the last elif */
3638 n_elif--;
3639 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640
Thomas Wouters89f507f2006-12-13 04:49:30 +00003641 for (i = 0; i < n_elif; i++) {
3642 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003643 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003644 if (!newobj)
3645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003647 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003650 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003651 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652
Thomas Wouters89f507f2006-12-13 04:49:30 +00003653 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003655 LINENO(CHILD(n, off)),
3656 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003657 orelse = newobj;
3658 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003659 expression = ast_for_expr(c, CHILD(n, 1));
3660 if (!expression)
3661 return NULL;
3662 suite_seq = ast_for_suite(c, CHILD(n, 3));
3663 if (!suite_seq)
3664 return NULL;
3665 return If(expression, suite_seq, orelse,
3666 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003668
3669 PyErr_Format(PyExc_SystemError,
3670 "unexpected token in 'if' statement: %s", s);
3671 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672}
3673
3674static stmt_ty
3675ast_for_while_stmt(struct compiling *c, const node *n)
3676{
3677 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3678 REQ(n, while_stmt);
3679
3680 if (NCH(n) == 4) {
3681 expr_ty expression;
3682 asdl_seq *suite_seq;
3683
3684 expression = ast_for_expr(c, CHILD(n, 1));
3685 if (!expression)
3686 return NULL;
3687 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003688 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003690 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 }
3692 else if (NCH(n) == 7) {
3693 expr_ty expression;
3694 asdl_seq *seq1, *seq2;
3695
3696 expression = ast_for_expr(c, CHILD(n, 1));
3697 if (!expression)
3698 return NULL;
3699 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003700 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701 return NULL;
3702 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003703 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 return NULL;
3705
Thomas Wouters89f507f2006-12-13 04:49:30 +00003706 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003708
3709 PyErr_Format(PyExc_SystemError,
3710 "wrong number of tokens for 'while' statement: %d",
3711 NCH(n));
3712 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713}
3714
3715static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003716ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003718 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003720 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003721 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3723 REQ(n, for_stmt);
3724
3725 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003726 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 if (!seq)
3728 return NULL;
3729 }
3730
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003731 node_target = CHILD(n, 1);
3732 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003733 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003734 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003735 /* Check the # of children rather than the length of _target, since
3736 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003737 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003738 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003739 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003741 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003743 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003744 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 return NULL;
3746 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003747 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 return NULL;
3749
Yury Selivanov75445082015-05-11 22:57:16 -04003750 if (is_async)
3751 return AsyncFor(target, expression, suite_seq, seq,
3752 LINENO(n), n->n_col_offset,
3753 c->c_arena);
3754 else
3755 return For(target, expression, suite_seq, seq,
3756 LINENO(n), n->n_col_offset,
3757 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758}
3759
3760static excepthandler_ty
3761ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3762{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003763 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764 REQ(exc, except_clause);
3765 REQ(body, suite);
3766
3767 if (NCH(exc) == 1) {
3768 asdl_seq *suite_seq = ast_for_suite(c, body);
3769 if (!suite_seq)
3770 return NULL;
3771
Neal Norwitzad74aa82008-03-31 05:14:30 +00003772 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003773 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 }
3775 else if (NCH(exc) == 2) {
3776 expr_ty expression;
3777 asdl_seq *suite_seq;
3778
3779 expression = ast_for_expr(c, CHILD(exc, 1));
3780 if (!expression)
3781 return NULL;
3782 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003783 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 return NULL;
3785
Neal Norwitzad74aa82008-03-31 05:14:30 +00003786 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003787 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 }
3789 else if (NCH(exc) == 4) {
3790 asdl_seq *suite_seq;
3791 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003792 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003793 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003795 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003796 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003798 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799 return NULL;
3800 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003801 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 return NULL;
3803
Neal Norwitzad74aa82008-03-31 05:14:30 +00003804 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003805 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003807
3808 PyErr_Format(PyExc_SystemError,
3809 "wrong number of children for 'except' clause: %d",
3810 NCH(exc));
3811 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812}
3813
3814static stmt_ty
3815ast_for_try_stmt(struct compiling *c, const node *n)
3816{
Neal Norwitzf599f422005-12-17 21:33:47 +00003817 const int nch = NCH(n);
3818 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003819 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003820
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 REQ(n, try_stmt);
3822
Neal Norwitzf599f422005-12-17 21:33:47 +00003823 body = ast_for_suite(c, CHILD(n, 2));
3824 if (body == NULL)
3825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826
Neal Norwitzf599f422005-12-17 21:33:47 +00003827 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3828 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3829 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3830 /* we can assume it's an "else",
3831 because nch >= 9 for try-else-finally and
3832 it would otherwise have a type of except_clause */
3833 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3834 if (orelse == NULL)
3835 return NULL;
3836 n_except--;
3837 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838
Neal Norwitzf599f422005-12-17 21:33:47 +00003839 finally = ast_for_suite(c, CHILD(n, nch - 1));
3840 if (finally == NULL)
3841 return NULL;
3842 n_except--;
3843 }
3844 else {
3845 /* we can assume it's an "else",
3846 otherwise it would have a type of except_clause */
3847 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3848 if (orelse == NULL)
3849 return NULL;
3850 n_except--;
3851 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003853 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003854 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003855 return NULL;
3856 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857
Neal Norwitzf599f422005-12-17 21:33:47 +00003858 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003859 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003860 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003861 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003862 if (handlers == NULL)
3863 return NULL;
3864
3865 for (i = 0; i < n_except; i++) {
3866 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3867 CHILD(n, 5 + i * 3));
3868 if (!e)
3869 return NULL;
3870 asdl_seq_SET(handlers, i, e);
3871 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003872 }
3873
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003874 assert(finally != NULL || asdl_seq_LEN(handlers));
3875 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876}
3877
Georg Brandl0c315622009-05-25 21:10:36 +00003878/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003879static withitem_ty
3880ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003881{
3882 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003883
Georg Brandl0c315622009-05-25 21:10:36 +00003884 REQ(n, with_item);
3885 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003886 if (!context_expr)
3887 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003888 if (NCH(n) == 3) {
3889 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003890
3891 if (!optional_vars) {
3892 return NULL;
3893 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003894 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003895 return NULL;
3896 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003897 }
3898
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003899 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003900}
3901
Georg Brandl0c315622009-05-25 21:10:36 +00003902/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3903static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003904ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003905{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003906 int i, n_items;
3907 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003908
3909 REQ(n, with_stmt);
3910
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003911 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003912 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003913 if (!items)
3914 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003915 for (i = 1; i < NCH(n) - 2; i += 2) {
3916 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3917 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003918 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003919 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003920 }
3921
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003922 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3923 if (!body)
3924 return NULL;
3925
Yury Selivanov75445082015-05-11 22:57:16 -04003926 if (is_async)
3927 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3928 else
3929 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003930}
3931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003933ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003935 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003936 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003937 asdl_seq *s;
INADA Naokicb41b272017-02-23 00:31:59 +09003938 string docstring;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003939 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003940
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 REQ(n, classdef);
3942
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003943 if (NCH(n) == 4) { /* class NAME ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003944 s = ast_for_body(c, CHILD(n, 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945 if (!s)
3946 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003947 classname = NEW_IDENTIFIER(CHILD(n, 1));
3948 if (!classname)
3949 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003950 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003951 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003952 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3953 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003955
3956 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003957 s = ast_for_body(c, CHILD(n, 5), &docstring);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003958 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003959 return NULL;
3960 classname = NEW_IDENTIFIER(CHILD(n, 1));
3961 if (!classname)
3962 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003963 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003964 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003965 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3966 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003967 }
3968
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003969 /* class NAME '(' arglist ')' ':' suite */
3970 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003971 {
3972 PyObject *dummy_name;
3973 expr_ty dummy;
3974 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3975 if (!dummy_name)
3976 return NULL;
3977 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3978 call = ast_for_call(c, CHILD(n, 3), dummy);
3979 if (!call)
3980 return NULL;
3981 }
INADA Naokicb41b272017-02-23 00:31:59 +09003982 s = ast_for_body(c, CHILD(n, 6), &docstring);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003983 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003985 classname = NEW_IDENTIFIER(CHILD(n, 1));
3986 if (!classname)
3987 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003988 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003989 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003990
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003991 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
INADA Naokicb41b272017-02-23 00:31:59 +09003992 decorator_seq, docstring, LINENO(n), n->n_col_offset,
3993 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003994}
3995
3996static stmt_ty
3997ast_for_stmt(struct compiling *c, const node *n)
3998{
3999 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004000 assert(NCH(n) == 1);
4001 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 }
4003 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004004 assert(num_stmts(n) == 1);
4005 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 }
4007 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004008 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004009 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4010 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004011 */
4012 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 case expr_stmt:
4014 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015 case del_stmt:
4016 return ast_for_del_stmt(c, n);
4017 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004018 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 case flow_stmt:
4020 return ast_for_flow_stmt(c, n);
4021 case import_stmt:
4022 return ast_for_import_stmt(c, n);
4023 case global_stmt:
4024 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004025 case nonlocal_stmt:
4026 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027 case assert_stmt:
4028 return ast_for_assert_stmt(c, n);
4029 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004030 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4032 TYPE(n), NCH(n));
4033 return NULL;
4034 }
4035 }
4036 else {
4037 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004038 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004039 */
4040 node *ch = CHILD(n, 0);
4041 REQ(n, compound_stmt);
4042 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004043 case if_stmt:
4044 return ast_for_if_stmt(c, ch);
4045 case while_stmt:
4046 return ast_for_while_stmt(c, ch);
4047 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004048 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004049 case try_stmt:
4050 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004051 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004052 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004054 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004055 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004056 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 case decorated:
4058 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004059 case async_stmt:
4060 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004062 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4064 TYPE(n), NCH(n));
4065 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004066 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 }
4068}
4069
4070static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004071parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004073 const char *end;
4074 long x;
4075 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004076 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004077 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004078
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004079 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004080 errno = 0;
4081 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004082 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004083 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004084 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004085 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004086 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004087 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004088 }
4089 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004090 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004091 if (*end == '\0') {
4092 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004093 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004094 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004095 }
4096 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004097 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004098 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004099 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4100 if (compl.imag == -1.0 && PyErr_Occurred())
4101 return NULL;
4102 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004103 }
4104 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004105 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004106 dx = PyOS_string_to_double(s, NULL, NULL);
4107 if (dx == -1.0 && PyErr_Occurred())
4108 return NULL;
4109 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004110 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004111}
4112
4113static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004114parsenumber(struct compiling *c, const char *s)
4115{
4116 char *dup, *end;
4117 PyObject *res = NULL;
4118
4119 assert(s != NULL);
4120
4121 if (strchr(s, '_') == NULL) {
4122 return parsenumber_raw(c, s);
4123 }
4124 /* Create a duplicate without underscores. */
4125 dup = PyMem_Malloc(strlen(s) + 1);
4126 end = dup;
4127 for (; *s; s++) {
4128 if (*s != '_') {
4129 *end++ = *s;
4130 }
4131 }
4132 *end = '\0';
4133 res = parsenumber_raw(c, dup);
4134 PyMem_Free(dup);
4135 return res;
4136}
4137
4138static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004139decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004141 const char *s, *t;
4142 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004143 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4144 while (s < end && (*s & 0x80)) s++;
4145 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004146 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004147}
4148
Eric V. Smith56466482016-10-31 14:46:26 -04004149static int
4150warn_invalid_escape_sequence(struct compiling *c, const node *n,
4151 char first_invalid_escape_char)
4152{
4153 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4154 first_invalid_escape_char);
4155 if (msg == NULL) {
4156 return -1;
4157 }
4158 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4159 c->c_filename, LINENO(n),
4160 NULL, NULL) < 0 &&
4161 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4162 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004163 const char *s;
4164
4165 /* Replace the DeprecationWarning exception with a SyntaxError
4166 to get a more accurate error report */
4167 PyErr_Clear();
4168
4169 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004170 if (s != NULL) {
4171 ast_error(c, n, s);
4172 }
4173 Py_DECREF(msg);
4174 return -1;
4175 }
4176 Py_DECREF(msg);
4177 return 0;
4178}
4179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004180static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004181decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4182 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004183{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004184 PyObject *v, *u;
4185 char *buf;
4186 char *p;
4187 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004188
Benjamin Peterson202803a2016-02-25 22:34:45 -08004189 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004190 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004191 return NULL;
4192 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4193 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4194 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4195 if (u == NULL)
4196 return NULL;
4197 p = buf = PyBytes_AsString(u);
4198 end = s + len;
4199 while (s < end) {
4200 if (*s == '\\') {
4201 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004202 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004203 strcpy(p, "u005c");
4204 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004205 if (s >= end)
4206 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004207 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004208 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004209 if (*s & 0x80) { /* XXX inefficient */
4210 PyObject *w;
4211 int kind;
4212 void *data;
4213 Py_ssize_t len, i;
4214 w = decode_utf8(c, &s, end);
4215 if (w == NULL) {
4216 Py_DECREF(u);
4217 return NULL;
4218 }
4219 kind = PyUnicode_KIND(w);
4220 data = PyUnicode_DATA(w);
4221 len = PyUnicode_GET_LENGTH(w);
4222 for (i = 0; i < len; i++) {
4223 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4224 sprintf(p, "\\U%08x", chr);
4225 p += 10;
4226 }
4227 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004228 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004229 Py_DECREF(w);
4230 } else {
4231 *p++ = *s++;
4232 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004233 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004234 len = p - buf;
4235 s = buf;
4236
Eric V. Smith56466482016-10-31 14:46:26 -04004237 const char *first_invalid_escape;
4238 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4239
4240 if (v != NULL && first_invalid_escape != NULL) {
4241 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4242 /* We have not decref u before because first_invalid_escape points
4243 inside u. */
4244 Py_XDECREF(u);
4245 Py_DECREF(v);
4246 return NULL;
4247 }
4248 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004249 Py_XDECREF(u);
4250 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004251}
4252
Eric V. Smith56466482016-10-31 14:46:26 -04004253static PyObject *
4254decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4255 size_t len)
4256{
4257 const char *first_invalid_escape;
4258 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4259 &first_invalid_escape);
4260 if (result == NULL)
4261 return NULL;
4262
4263 if (first_invalid_escape != NULL) {
4264 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4265 Py_DECREF(result);
4266 return NULL;
4267 }
4268 }
4269 return result;
4270}
4271
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004272/* Shift locations for the given node and all its children by adding `lineno`
4273 and `col_offset` to existing locations. */
4274static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4275{
4276 n->n_col_offset = n->n_col_offset + col_offset;
4277 for (int i = 0; i < NCH(n); ++i) {
4278 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4279 /* Shifting column offsets unnecessary if there's been newlines. */
4280 col_offset = 0;
4281 }
4282 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4283 }
4284 n->n_lineno = n->n_lineno + lineno;
4285}
4286
4287/* Fix locations for the given node and its children.
4288
4289 `parent` is the enclosing node.
4290 `n` is the node which locations are going to be fixed relative to parent.
4291 `expr_str` is the child node's string representation, incuding braces.
4292*/
4293static void
4294fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4295{
4296 char *substr = NULL;
4297 char *start;
4298 int lines = LINENO(parent) - 1;
4299 int cols = parent->n_col_offset;
4300 /* Find the full fstring to fix location information in `n`. */
4301 while (parent && parent->n_type != STRING)
4302 parent = parent->n_child;
4303 if (parent && parent->n_str) {
4304 substr = strstr(parent->n_str, expr_str);
4305 if (substr) {
4306 start = substr;
4307 while (start > parent->n_str) {
4308 if (start[0] == '\n')
4309 break;
4310 start--;
4311 }
4312 cols += substr - start;
4313 /* Fix lineno in mulitline strings. */
4314 while ((substr = strchr(substr + 1, '\n')))
4315 lines--;
4316 }
4317 }
4318 fstring_shift_node_locations(n, lines, cols);
4319}
4320
Eric V. Smith451d0e32016-09-09 21:56:20 -04004321/* Compile this expression in to an expr_ty. Add parens around the
4322 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004323static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004324fstring_compile_expr(const char *expr_start, const char *expr_end,
4325 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004326
Eric V. Smith235a6f02015-09-19 14:51:32 -04004327{
4328 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004329 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004330 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004331 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004332 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004333 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004334
Eric V. Smith1d44c412015-09-23 07:49:00 -04004335 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004336 assert(*(expr_start-1) == '{');
4337 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004338
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004339 /* If the substring is all whitespace, it's an error. We need to catch this
4340 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4341 because turning the expression '' in to '()' would go from being invalid
4342 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004343 for (s = expr_start; s != expr_end; s++) {
4344 char c = *s;
4345 /* The Python parser ignores only the following whitespace
4346 characters (\r already is converted to \n). */
4347 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004348 break;
4349 }
4350 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004351 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004352 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004353 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004354 }
4355
Eric V. Smith451d0e32016-09-09 21:56:20 -04004356 len = expr_end - expr_start;
4357 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4358 str = PyMem_RawMalloc(len + 3);
4359 if (str == NULL)
4360 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004361
Eric V. Smith451d0e32016-09-09 21:56:20 -04004362 str[0] = '(';
4363 memcpy(str+1, expr_start, len);
4364 str[len+1] = ')';
4365 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004366
4367 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004368 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4369 Py_eval_input, 0);
4370 if (!mod_n) {
4371 PyMem_RawFree(str);
4372 return NULL;
4373 }
4374 /* Reuse str to find the correct column offset. */
4375 str[0] = '{';
4376 str[len+1] = '}';
4377 fstring_fix_node_location(n, mod_n, str);
4378 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004379 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004380 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004381 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004382 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004383 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004384}
4385
4386/* Return -1 on error.
4387
4388 Return 0 if we reached the end of the literal.
4389
4390 Return 1 if we haven't reached the end of the literal, but we want
4391 the caller to process the literal up to this point. Used for
4392 doubled braces.
4393*/
4394static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004395fstring_find_literal(const char **str, const char *end, int raw,
4396 PyObject **literal, int recurse_lvl,
4397 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004398{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004399 /* Get any literal string. It ends when we hit an un-doubled left
4400 brace (which isn't part of a unicode name escape such as
4401 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004402
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004403 const char *s = *str;
4404 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004405 int result = 0;
4406
Eric V. Smith235a6f02015-09-19 14:51:32 -04004407 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004408 while (s < end) {
4409 char ch = *s++;
4410 if (!raw && ch == '\\' && s < end) {
4411 ch = *s++;
4412 if (ch == 'N') {
4413 if (s < end && *s++ == '{') {
4414 while (s < end && *s++ != '}') {
4415 }
4416 continue;
4417 }
4418 break;
4419 }
4420 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4421 return -1;
4422 }
4423 }
4424 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004425 /* Check for doubled braces, but only at the top level. If
4426 we checked at every level, then f'{0:{3}}' would fail
4427 with the two closing braces. */
4428 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004429 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004430 /* We're going to tell the caller that the literal ends
4431 here, but that they should continue scanning. But also
4432 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004433 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004434 result = 1;
4435 goto done;
4436 }
4437
4438 /* Where a single '{' is the start of a new expression, a
4439 single '}' is not allowed. */
4440 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004441 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004442 ast_error(c, n, "f-string: single '}' is not allowed");
4443 return -1;
4444 }
4445 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004446 /* We're either at a '{', which means we're starting another
4447 expression; or a '}', which means we're at the end of this
4448 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004449 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004450 break;
4451 }
4452 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004453 *str = s;
4454 assert(s <= end);
4455 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004456done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004457 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004458 if (raw)
4459 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004460 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004461 NULL, NULL);
4462 else
Eric V. Smith56466482016-10-31 14:46:26 -04004463 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004464 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004465 if (!*literal)
4466 return -1;
4467 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004468 return result;
4469}
4470
4471/* Forward declaration because parsing is recursive. */
4472static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004473fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004474 struct compiling *c, const node *n);
4475
Eric V. Smith451d0e32016-09-09 21:56:20 -04004476/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004477 expression (so it must be a '{'). Returns the FormattedValue node,
4478 which includes the expression, conversion character, and
4479 format_spec expression.
4480
4481 Note that I don't do a perfect job here: I don't make sure that a
4482 closing brace doesn't match an opening paren, for example. It
4483 doesn't need to error on all invalid expressions, just correctly
4484 find the end of all valid ones. Any errors inside the expression
4485 will be caught when we parse it later. */
4486static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004487fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004488 expr_ty *expression, struct compiling *c, const node *n)
4489{
4490 /* Return -1 on error, else 0. */
4491
Eric V. Smith451d0e32016-09-09 21:56:20 -04004492 const char *expr_start;
4493 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004494 expr_ty simple_expression;
4495 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004496 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004497
4498 /* 0 if we're not in a string, else the quote char we're trying to
4499 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004500 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004501
4502 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4503 int string_type = 0;
4504
4505 /* Keep track of nesting level for braces/parens/brackets in
4506 expressions. */
4507 Py_ssize_t nested_depth = 0;
4508
4509 /* Can only nest one level deep. */
4510 if (recurse_lvl >= 2) {
4511 ast_error(c, n, "f-string: expressions nested too deeply");
4512 return -1;
4513 }
4514
4515 /* The first char must be a left brace, or we wouldn't have gotten
4516 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004517 assert(**str == '{');
4518 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004519
Eric V. Smith451d0e32016-09-09 21:56:20 -04004520 expr_start = *str;
4521 for (; *str < end; (*str)++) {
4522 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004523
4524 /* Loop invariants. */
4525 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004526 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004527 if (quote_char)
4528 assert(string_type == 1 || string_type == 3);
4529 else
4530 assert(string_type == 0);
4531
Eric V. Smith451d0e32016-09-09 21:56:20 -04004532 ch = **str;
4533 /* Nowhere inside an expression is a backslash allowed. */
4534 if (ch == '\\') {
4535 /* Error: can't include a backslash character, inside
4536 parens or strings or not. */
4537 ast_error(c, n, "f-string expression part "
4538 "cannot include a backslash");
4539 return -1;
4540 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004541 if (quote_char) {
4542 /* We're inside a string. See if we're at the end. */
4543 /* This code needs to implement the same non-error logic
4544 as tok_get from tokenizer.c, at the letter_quote
4545 label. To actually share that code would be a
4546 nightmare. But, it's unlikely to change and is small,
4547 so duplicate it here. Note we don't need to catch all
4548 of the errors, since they'll be caught when parsing the
4549 expression. We just need to match the non-error
4550 cases. Thus we can ignore \n in single-quoted strings,
4551 for example. Or non-terminated strings. */
4552 if (ch == quote_char) {
4553 /* Does this match the string_type (single or triple
4554 quoted)? */
4555 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004556 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004557 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004558 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004559 string_type = 0;
4560 quote_char = 0;
4561 continue;
4562 }
4563 } else {
4564 /* We're at the end of a normal string. */
4565 quote_char = 0;
4566 string_type = 0;
4567 continue;
4568 }
4569 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004570 } else if (ch == '\'' || ch == '"') {
4571 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004572 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004573 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004574 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004575 } else {
4576 /* Start of a normal string. */
4577 string_type = 1;
4578 }
4579 /* Start looking for the end of the string. */
4580 quote_char = ch;
4581 } else if (ch == '[' || ch == '{' || ch == '(') {
4582 nested_depth++;
4583 } else if (nested_depth != 0 &&
4584 (ch == ']' || ch == '}' || ch == ')')) {
4585 nested_depth--;
4586 } else if (ch == '#') {
4587 /* Error: can't include a comment character, inside parens
4588 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004589 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004590 return -1;
4591 } else if (nested_depth == 0 &&
4592 (ch == '!' || ch == ':' || ch == '}')) {
4593 /* First, test for the special case of "!=". Since '=' is
4594 not an allowed conversion character, nothing is lost in
4595 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004596 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004597 /* This isn't a conversion character, just continue. */
4598 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004599 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004600 /* Normal way out of this loop. */
4601 break;
4602 } else {
4603 /* Just consume this char and loop around. */
4604 }
4605 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004606 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004607 /* If we leave this loop in a string or with mismatched parens, we
4608 don't care. We'll get a syntax error when compiling the
4609 expression. But, we can produce a better error message, so
4610 let's just do that.*/
4611 if (quote_char) {
4612 ast_error(c, n, "f-string: unterminated string");
4613 return -1;
4614 }
4615 if (nested_depth) {
4616 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4617 return -1;
4618 }
4619
Eric V. Smith451d0e32016-09-09 21:56:20 -04004620 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004621 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004622
4623 /* Compile the expression as soon as possible, so we show errors
4624 related to the expression before errors related to the
4625 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004626 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004627 if (!simple_expression)
4628 return -1;
4629
4630 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004631 if (**str == '!') {
4632 *str += 1;
4633 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004634 goto unexpected_end_of_string;
4635
Eric V. Smith451d0e32016-09-09 21:56:20 -04004636 conversion = **str;
4637 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004638
4639 /* Validate the conversion. */
4640 if (!(conversion == 's' || conversion == 'r'
4641 || conversion == 'a')) {
4642 ast_error(c, n, "f-string: invalid conversion character: "
4643 "expected 's', 'r', or 'a'");
4644 return -1;
4645 }
4646 }
4647
4648 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004649 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004650 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004651 if (**str == ':') {
4652 *str += 1;
4653 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004654 goto unexpected_end_of_string;
4655
4656 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004657 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004658 if (!format_spec)
4659 return -1;
4660 }
4661
Eric V. Smith451d0e32016-09-09 21:56:20 -04004662 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004663 goto unexpected_end_of_string;
4664
4665 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004666 assert(*str < end);
4667 assert(**str == '}');
4668 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004669
Eric V. Smith451d0e32016-09-09 21:56:20 -04004670 /* And now create the FormattedValue node that represents this
4671 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004672 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004673 format_spec, LINENO(n), n->n_col_offset,
4674 c->c_arena);
4675 if (!*expression)
4676 return -1;
4677
4678 return 0;
4679
4680unexpected_end_of_string:
4681 ast_error(c, n, "f-string: expecting '}'");
4682 return -1;
4683}
4684
4685/* Return -1 on error.
4686
4687 Return 0 if we have a literal (possible zero length) and an
4688 expression (zero length if at the end of the string.
4689
4690 Return 1 if we have a literal, but no expression, and we want the
4691 caller to call us again. This is used to deal with doubled
4692 braces.
4693
4694 When called multiple times on the string 'a{{b{0}c', this function
4695 will return:
4696
4697 1. the literal 'a{' with no expression, and a return value
4698 of 1. Despite the fact that there's no expression, the return
4699 value of 1 means we're not finished yet.
4700
4701 2. the literal 'b' and the expression '0', with a return value of
4702 0. The fact that there's an expression means we're not finished.
4703
4704 3. literal 'c' with no expression and a return value of 0. The
4705 combination of the return value of 0 with no expression means
4706 we're finished.
4707*/
4708static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004709fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4710 int recurse_lvl, PyObject **literal,
4711 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004712 struct compiling *c, const node *n)
4713{
4714 int result;
4715
4716 assert(*literal == NULL && *expression == NULL);
4717
4718 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004719 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004720 if (result < 0)
4721 goto error;
4722
4723 assert(result == 0 || result == 1);
4724
4725 if (result == 1)
4726 /* We have a literal, but don't look at the expression. */
4727 return 1;
4728
Eric V. Smith451d0e32016-09-09 21:56:20 -04004729 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004730 /* We're at the end of the string or the end of a nested
4731 f-string: no expression. The top-level error case where we
4732 expect to be at the end of the string but we're at a '}' is
4733 handled later. */
4734 return 0;
4735
4736 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004737 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004738
Eric V. Smith451d0e32016-09-09 21:56:20 -04004739 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004740 goto error;
4741
4742 return 0;
4743
4744error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004745 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004746 return -1;
4747}
4748
4749#define EXPRLIST_N_CACHED 64
4750
4751typedef struct {
4752 /* Incrementally build an array of expr_ty, so be used in an
4753 asdl_seq. Cache some small but reasonably sized number of
4754 expr_ty's, and then after that start dynamically allocating,
4755 doubling the number allocated each time. Note that the f-string
4756 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4757 Str for the literal 'a'. So you add expr_ty's about twice as
4758 fast as you add exressions in an f-string. */
4759
4760 Py_ssize_t allocated; /* Number we've allocated. */
4761 Py_ssize_t size; /* Number we've used. */
4762 expr_ty *p; /* Pointer to the memory we're actually
4763 using. Will point to 'data' until we
4764 start dynamically allocating. */
4765 expr_ty data[EXPRLIST_N_CACHED];
4766} ExprList;
4767
4768#ifdef NDEBUG
4769#define ExprList_check_invariants(l)
4770#else
4771static void
4772ExprList_check_invariants(ExprList *l)
4773{
4774 /* Check our invariants. Make sure this object is "live", and
4775 hasn't been deallocated. */
4776 assert(l->size >= 0);
4777 assert(l->p != NULL);
4778 if (l->size <= EXPRLIST_N_CACHED)
4779 assert(l->data == l->p);
4780}
4781#endif
4782
4783static void
4784ExprList_Init(ExprList *l)
4785{
4786 l->allocated = EXPRLIST_N_CACHED;
4787 l->size = 0;
4788
4789 /* Until we start allocating dynamically, p points to data. */
4790 l->p = l->data;
4791
4792 ExprList_check_invariants(l);
4793}
4794
4795static int
4796ExprList_Append(ExprList *l, expr_ty exp)
4797{
4798 ExprList_check_invariants(l);
4799 if (l->size >= l->allocated) {
4800 /* We need to alloc (or realloc) the memory. */
4801 Py_ssize_t new_size = l->allocated * 2;
4802
4803 /* See if we've ever allocated anything dynamically. */
4804 if (l->p == l->data) {
4805 Py_ssize_t i;
4806 /* We're still using the cached data. Switch to
4807 alloc-ing. */
4808 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4809 if (!l->p)
4810 return -1;
4811 /* Copy the cached data into the new buffer. */
4812 for (i = 0; i < l->size; i++)
4813 l->p[i] = l->data[i];
4814 } else {
4815 /* Just realloc. */
4816 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4817 if (!tmp) {
4818 PyMem_RawFree(l->p);
4819 l->p = NULL;
4820 return -1;
4821 }
4822 l->p = tmp;
4823 }
4824
4825 l->allocated = new_size;
4826 assert(l->allocated == 2 * l->size);
4827 }
4828
4829 l->p[l->size++] = exp;
4830
4831 ExprList_check_invariants(l);
4832 return 0;
4833}
4834
4835static void
4836ExprList_Dealloc(ExprList *l)
4837{
4838 ExprList_check_invariants(l);
4839
4840 /* If there's been an error, or we've never dynamically allocated,
4841 do nothing. */
4842 if (!l->p || l->p == l->data) {
4843 /* Do nothing. */
4844 } else {
4845 /* We have dynamically allocated. Free the memory. */
4846 PyMem_RawFree(l->p);
4847 }
4848 l->p = NULL;
4849 l->size = -1;
4850}
4851
4852static asdl_seq *
4853ExprList_Finish(ExprList *l, PyArena *arena)
4854{
4855 asdl_seq *seq;
4856
4857 ExprList_check_invariants(l);
4858
4859 /* Allocate the asdl_seq and copy the expressions in to it. */
4860 seq = _Py_asdl_seq_new(l->size, arena);
4861 if (seq) {
4862 Py_ssize_t i;
4863 for (i = 0; i < l->size; i++)
4864 asdl_seq_SET(seq, i, l->p[i]);
4865 }
4866 ExprList_Dealloc(l);
4867 return seq;
4868}
4869
4870/* The FstringParser is designed to add a mix of strings and
4871 f-strings, and concat them together as needed. Ultimately, it
4872 generates an expr_ty. */
4873typedef struct {
4874 PyObject *last_str;
4875 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004876 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004877} FstringParser;
4878
4879#ifdef NDEBUG
4880#define FstringParser_check_invariants(state)
4881#else
4882static void
4883FstringParser_check_invariants(FstringParser *state)
4884{
4885 if (state->last_str)
4886 assert(PyUnicode_CheckExact(state->last_str));
4887 ExprList_check_invariants(&state->expr_list);
4888}
4889#endif
4890
4891static void
4892FstringParser_Init(FstringParser *state)
4893{
4894 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004895 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004896 ExprList_Init(&state->expr_list);
4897 FstringParser_check_invariants(state);
4898}
4899
4900static void
4901FstringParser_Dealloc(FstringParser *state)
4902{
4903 FstringParser_check_invariants(state);
4904
4905 Py_XDECREF(state->last_str);
4906 ExprList_Dealloc(&state->expr_list);
4907}
4908
4909/* Make a Str node, but decref the PyUnicode object being added. */
4910static expr_ty
4911make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4912{
4913 PyObject *s = *str;
4914 *str = NULL;
4915 assert(PyUnicode_CheckExact(s));
4916 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4917 Py_DECREF(s);
4918 return NULL;
4919 }
4920 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4921}
4922
4923/* Add a non-f-string (that is, a regular literal string). str is
4924 decref'd. */
4925static int
4926FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4927{
4928 FstringParser_check_invariants(state);
4929
4930 assert(PyUnicode_CheckExact(str));
4931
4932 if (PyUnicode_GET_LENGTH(str) == 0) {
4933 Py_DECREF(str);
4934 return 0;
4935 }
4936
4937 if (!state->last_str) {
4938 /* We didn't have a string before, so just remember this one. */
4939 state->last_str = str;
4940 } else {
4941 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004942 PyUnicode_AppendAndDel(&state->last_str, str);
4943 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944 return -1;
4945 }
4946 FstringParser_check_invariants(state);
4947 return 0;
4948}
4949
Eric V. Smith451d0e32016-09-09 21:56:20 -04004950/* Parse an f-string. The f-string is in *str to end, with no
4951 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004952static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004953FstringParser_ConcatFstring(FstringParser *state, const char **str,
4954 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004955 struct compiling *c, const node *n)
4956{
4957 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004958 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004959
4960 /* Parse the f-string. */
4961 while (1) {
4962 PyObject *literal = NULL;
4963 expr_ty expression = NULL;
4964
4965 /* If there's a zero length literal in front of the
4966 expression, literal will be NULL. If we're at the end of
4967 the f-string, expression will be NULL (unless result == 1,
4968 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004969 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004970 &literal, &expression,
4971 c, n);
4972 if (result < 0)
4973 return -1;
4974
4975 /* Add the literal, if any. */
4976 if (!literal) {
4977 /* Do nothing. Just leave last_str alone (and possibly
4978 NULL). */
4979 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004980 /* Note that the literal can be zero length, if the
4981 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004982 state->last_str = literal;
4983 literal = NULL;
4984 } else {
4985 /* We have a literal, concatenate it. */
4986 assert(PyUnicode_GET_LENGTH(literal) != 0);
4987 if (FstringParser_ConcatAndDel(state, literal) < 0)
4988 return -1;
4989 literal = NULL;
4990 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004991
4992 /* We've dealt with the literal now. It can't be leaked on further
4993 errors. */
4994 assert(literal == NULL);
4995
4996 /* See if we should just loop around to get the next literal
4997 and expression, while ignoring the expression this
4998 time. This is used for un-doubling braces, as an
4999 optimization. */
5000 if (result == 1)
5001 continue;
5002
5003 if (!expression)
5004 /* We're done with this f-string. */
5005 break;
5006
5007 /* We know we have an expression. Convert any existing string
5008 to a Str node. */
5009 if (!state->last_str) {
5010 /* Do nothing. No previous literal. */
5011 } else {
5012 /* Convert the existing last_str literal to a Str node. */
5013 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5014 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5015 return -1;
5016 }
5017
5018 if (ExprList_Append(&state->expr_list, expression) < 0)
5019 return -1;
5020 }
5021
Eric V. Smith235a6f02015-09-19 14:51:32 -04005022 /* If recurse_lvl is zero, then we must be at the end of the
5023 string. Otherwise, we must be at a right brace. */
5024
Eric V. Smith451d0e32016-09-09 21:56:20 -04005025 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 ast_error(c, n, "f-string: unexpected end of string");
5027 return -1;
5028 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005029 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005030 ast_error(c, n, "f-string: expecting '}'");
5031 return -1;
5032 }
5033
5034 FstringParser_check_invariants(state);
5035 return 0;
5036}
5037
5038/* Convert the partial state reflected in last_str and expr_list to an
5039 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5040static expr_ty
5041FstringParser_Finish(FstringParser *state, struct compiling *c,
5042 const node *n)
5043{
5044 asdl_seq *seq;
5045
5046 FstringParser_check_invariants(state);
5047
5048 /* If we're just a constant string with no expressions, return
5049 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005050 if (!state->fmode) {
5051 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005052 if (!state->last_str) {
5053 /* Create a zero length string. */
5054 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5055 if (!state->last_str)
5056 goto error;
5057 }
5058 return make_str_node_and_del(&state->last_str, c, n);
5059 }
5060
5061 /* Create a Str node out of last_str, if needed. It will be the
5062 last node in our expression list. */
5063 if (state->last_str) {
5064 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5065 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5066 goto error;
5067 }
5068 /* This has already been freed. */
5069 assert(state->last_str == NULL);
5070
5071 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5072 if (!seq)
5073 goto error;
5074
Eric V. Smith235a6f02015-09-19 14:51:32 -04005075 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5076
5077error:
5078 FstringParser_Dealloc(state);
5079 return NULL;
5080}
5081
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5083 at end, parse it into an expr_ty. Return NULL on error. Adjust
5084 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005085static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005086fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005087 struct compiling *c, const node *n)
5088{
5089 FstringParser state;
5090
5091 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005092 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005093 c, n) < 0) {
5094 FstringParser_Dealloc(&state);
5095 return NULL;
5096 }
5097
5098 return FstringParser_Finish(&state, c, n);
5099}
5100
5101/* n is a Python string literal, including the bracketing quote
5102 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005103 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005105 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5106 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005107*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005108static int
5109parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5110 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005111{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005112 size_t len;
5113 const char *s = STR(n);
5114 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005115 int fmode = 0;
5116 *bytesmode = 0;
5117 *rawmode = 0;
5118 *result = NULL;
5119 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005120 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005121 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005122 if (quote == 'b' || quote == 'B') {
5123 quote = *++s;
5124 *bytesmode = 1;
5125 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005126 else if (quote == 'u' || quote == 'U') {
5127 quote = *++s;
5128 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005129 else if (quote == 'r' || quote == 'R') {
5130 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005131 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005132 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005133 else if (quote == 'f' || quote == 'F') {
5134 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005135 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005136 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005137 else {
5138 break;
5139 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005140 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005141 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005142 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005143 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005144 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005145 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005146 if (quote != '\'' && quote != '\"') {
5147 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005148 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005149 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005150 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005151 s++;
5152 len = strlen(s);
5153 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005154 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005155 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005156 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005157 }
5158 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005159 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005160 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005161 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005162 }
5163 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005164 /* A triple quoted string. We've already skipped one quote at
5165 the start and one at the end of the string. Now skip the
5166 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005167 s += 2;
5168 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005169 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005170 if (s[--len] != quote || s[--len] != quote) {
5171 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005172 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005173 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005174 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005175
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 if (fmode) {
5177 /* Just return the bytes. The caller will parse the resulting
5178 string. */
5179 *fstr = s;
5180 *fstrlen = len;
5181 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005182 }
5183
Eric V. Smith451d0e32016-09-09 21:56:20 -04005184 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005185 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005186 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005187 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005188 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005189 const char *ch;
5190 for (ch = s; *ch; ch++) {
5191 if (Py_CHARMASK(*ch) >= 0x80) {
5192 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005193 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005194 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005195 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005196 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005197 if (*rawmode)
5198 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005199 else
Eric V. Smith56466482016-10-31 14:46:26 -04005200 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005201 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005202 if (*rawmode)
5203 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005204 else
Eric V. Smith56466482016-10-31 14:46:26 -04005205 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005206 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005207 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005208}
5209
Eric V. Smith235a6f02015-09-19 14:51:32 -04005210/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5211 each STRING atom, and process it as needed. For bytes, just
5212 concatenate them together, and the result will be a Bytes node. For
5213 normal strings and f-strings, concatenate them together. The result
5214 will be a Str node if there were no f-strings; a FormattedValue
5215 node if there's just an f-string (with no leading or trailing
5216 literals), or a JoinedStr node if there are multiple f-strings or
5217 any literals involved. */
5218static expr_ty
5219parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005220{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005221 int bytesmode = 0;
5222 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005223 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005224
5225 FstringParser state;
5226 FstringParser_Init(&state);
5227
5228 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005229 int this_bytesmode;
5230 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005231 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005232 const char *fstr;
5233 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005234
5235 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005236 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5237 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005238 goto error;
5239
5240 /* Check that we're not mixing bytes with unicode. */
5241 if (i != 0 && bytesmode != this_bytesmode) {
5242 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005243 /* s is NULL if the current string part is an f-string. */
5244 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005245 goto error;
5246 }
5247 bytesmode = this_bytesmode;
5248
Eric V. Smith451d0e32016-09-09 21:56:20 -04005249 if (fstr != NULL) {
5250 int result;
5251 assert(s == NULL && !bytesmode);
5252 /* This is an f-string. Parse and concatenate it. */
5253 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5254 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005255 if (result < 0)
5256 goto error;
5257 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005258 /* A string or byte string. */
5259 assert(s != NULL && fstr == NULL);
5260
Eric V. Smith451d0e32016-09-09 21:56:20 -04005261 assert(bytesmode ? PyBytes_CheckExact(s) :
5262 PyUnicode_CheckExact(s));
5263
Eric V. Smith451d0e32016-09-09 21:56:20 -04005264 if (bytesmode) {
5265 /* For bytes, concat as we go. */
5266 if (i == 0) {
5267 /* First time, just remember this value. */
5268 bytes_str = s;
5269 } else {
5270 PyBytes_ConcatAndDel(&bytes_str, s);
5271 if (!bytes_str)
5272 goto error;
5273 }
5274 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005275 /* This is a regular string. Concatenate it. */
5276 if (FstringParser_ConcatAndDel(&state, s) < 0)
5277 goto error;
5278 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005279 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005280 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005281 if (bytesmode) {
5282 /* Just return the bytes object and we're done. */
5283 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5284 goto error;
5285 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5286 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005287
Eric V. Smith235a6f02015-09-19 14:51:32 -04005288 /* We're not a bytes string, bytes_str should never have been set. */
5289 assert(bytes_str == NULL);
5290
5291 return FstringParser_Finish(&state, c, n);
5292
5293error:
5294 Py_XDECREF(bytes_str);
5295 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005297}