blob: c88e7e337332a46e159c56c7a82cc02a7223dfeb [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
14
Benjamin Peterson832bfe22011-08-09 16:15:04 -050015static int validate_stmts(asdl_seq *);
16static int validate_exprs(asdl_seq *, expr_context_ty, int);
17static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
18static int validate_stmt(stmt_ty);
19static int validate_expr(expr_ty, expr_context_ty);
20
21static int
22validate_comprehension(asdl_seq *gens)
23{
24 int i;
25 if (!asdl_seq_LEN(gens)) {
26 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
27 return 0;
28 }
29 for (i = 0; i < asdl_seq_LEN(gens); i++) {
30 comprehension_ty comp = asdl_seq_GET(gens, i);
31 if (!validate_expr(comp->target, Store) ||
32 !validate_expr(comp->iter, Load) ||
33 !validate_exprs(comp->ifs, Load, 0))
34 return 0;
35 }
36 return 1;
37}
38
39static int
40validate_slice(slice_ty slice)
41{
42 switch (slice->kind) {
43 case Slice_kind:
44 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
45 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
46 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
47 case ExtSlice_kind: {
48 int i;
49 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
50 return 0;
51 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
52 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
53 return 0;
54 return 1;
55 }
56 case Index_kind:
57 return validate_expr(slice->v.Index.value, Load);
58 default:
59 PyErr_SetString(PyExc_SystemError, "unknown slice node");
60 return 0;
61 }
62}
63
64static int
65validate_keywords(asdl_seq *keywords)
66{
67 int i;
68 for (i = 0; i < asdl_seq_LEN(keywords); i++)
69 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
70 return 0;
71 return 1;
72}
73
74static int
75validate_args(asdl_seq *args)
76{
77 int i;
78 for (i = 0; i < asdl_seq_LEN(args); i++) {
79 arg_ty arg = asdl_seq_GET(args, i);
80 if (arg->annotation && !validate_expr(arg->annotation, Load))
81 return 0;
82 }
83 return 1;
84}
85
86static const char *
87expr_context_name(expr_context_ty ctx)
88{
89 switch (ctx) {
90 case Load:
91 return "Load";
92 case Store:
93 return "Store";
94 case Del:
95 return "Del";
96 case AugLoad:
97 return "AugLoad";
98 case AugStore:
99 return "AugStore";
100 case Param:
101 return "Param";
102 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700103 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500104 }
105}
106
107static int
108validate_arguments(arguments_ty args)
109{
110 if (!validate_args(args->args))
111 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700112 if (args->vararg && args->vararg->annotation
113 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500114 return 0;
115 }
116 if (!validate_args(args->kwonlyargs))
117 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100118 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700119 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500120 return 0;
121 }
122 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
123 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
124 return 0;
125 }
126 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
127 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
128 "kw_defaults on arguments");
129 return 0;
130 }
131 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
132}
133
134static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100135validate_constant(PyObject *value)
136{
137 if (value == Py_None || value == Py_Ellipsis)
138 return 1;
139
140 if (PyLong_CheckExact(value)
141 || PyFloat_CheckExact(value)
142 || PyComplex_CheckExact(value)
143 || PyBool_Check(value)
144 || PyUnicode_CheckExact(value)
145 || PyBytes_CheckExact(value))
146 return 1;
147
148 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
149 PyObject *it;
150
151 it = PyObject_GetIter(value);
152 if (it == NULL)
153 return 0;
154
155 while (1) {
156 PyObject *item = PyIter_Next(it);
157 if (item == NULL) {
158 if (PyErr_Occurred()) {
159 Py_DECREF(it);
160 return 0;
161 }
162 break;
163 }
164
165 if (!validate_constant(item)) {
166 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100167 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100168 return 0;
169 }
Victor Stinner726f6902016-01-27 00:11:47 +0100170 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100171 }
172
173 Py_DECREF(it);
174 return 1;
175 }
176
177 return 0;
178}
179
180static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500181validate_expr(expr_ty exp, expr_context_ty ctx)
182{
183 int check_ctx = 1;
184 expr_context_ty actual_ctx;
185
186 /* First check expression context. */
187 switch (exp->kind) {
188 case Attribute_kind:
189 actual_ctx = exp->v.Attribute.ctx;
190 break;
191 case Subscript_kind:
192 actual_ctx = exp->v.Subscript.ctx;
193 break;
194 case Starred_kind:
195 actual_ctx = exp->v.Starred.ctx;
196 break;
197 case Name_kind:
198 actual_ctx = exp->v.Name.ctx;
199 break;
200 case List_kind:
201 actual_ctx = exp->v.List.ctx;
202 break;
203 case Tuple_kind:
204 actual_ctx = exp->v.Tuple.ctx;
205 break;
206 default:
207 if (ctx != Load) {
208 PyErr_Format(PyExc_ValueError, "expression which can't be "
209 "assigned to in %s context", expr_context_name(ctx));
210 return 0;
211 }
212 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100213 /* set actual_ctx to prevent gcc warning */
214 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500215 }
216 if (check_ctx && actual_ctx != ctx) {
217 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
218 expr_context_name(ctx), expr_context_name(actual_ctx));
219 return 0;
220 }
221
222 /* Now validate expression. */
223 switch (exp->kind) {
224 case BoolOp_kind:
225 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
226 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
227 return 0;
228 }
229 return validate_exprs(exp->v.BoolOp.values, Load, 0);
230 case BinOp_kind:
231 return validate_expr(exp->v.BinOp.left, Load) &&
232 validate_expr(exp->v.BinOp.right, Load);
233 case UnaryOp_kind:
234 return validate_expr(exp->v.UnaryOp.operand, Load);
235 case Lambda_kind:
236 return validate_arguments(exp->v.Lambda.args) &&
237 validate_expr(exp->v.Lambda.body, Load);
238 case IfExp_kind:
239 return validate_expr(exp->v.IfExp.test, Load) &&
240 validate_expr(exp->v.IfExp.body, Load) &&
241 validate_expr(exp->v.IfExp.orelse, Load);
242 case Dict_kind:
243 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
244 PyErr_SetString(PyExc_ValueError,
245 "Dict doesn't have the same number of keys as values");
246 return 0;
247 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400248 /* null_ok=1 for keys expressions to allow dict unpacking to work in
249 dict literals, i.e. ``{**{a:b}}`` */
250 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
251 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500252 case Set_kind:
253 return validate_exprs(exp->v.Set.elts, Load, 0);
254#define COMP(NAME) \
255 case NAME ## _kind: \
256 return validate_comprehension(exp->v.NAME.generators) && \
257 validate_expr(exp->v.NAME.elt, Load);
258 COMP(ListComp)
259 COMP(SetComp)
260 COMP(GeneratorExp)
261#undef COMP
262 case DictComp_kind:
263 return validate_comprehension(exp->v.DictComp.generators) &&
264 validate_expr(exp->v.DictComp.key, Load) &&
265 validate_expr(exp->v.DictComp.value, Load);
266 case Yield_kind:
267 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500268 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000269 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400270 case Await_kind:
271 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500272 case Compare_kind:
273 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
274 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
275 return 0;
276 }
277 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
278 asdl_seq_LEN(exp->v.Compare.ops)) {
279 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
280 "of comparators and operands");
281 return 0;
282 }
283 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
284 validate_expr(exp->v.Compare.left, Load);
285 case Call_kind:
286 return validate_expr(exp->v.Call.func, Load) &&
287 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400288 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100289 case Constant_kind:
290 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100291 PyErr_Format(PyExc_TypeError,
292 "got an invalid type in Constant: %s",
293 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 return 0;
295 }
296 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500297 case Num_kind: {
298 PyObject *n = exp->v.Num.n;
299 if (!PyLong_CheckExact(n) && !PyFloat_CheckExact(n) &&
300 !PyComplex_CheckExact(n)) {
301 PyErr_SetString(PyExc_TypeError, "non-numeric type in Num");
302 return 0;
303 }
304 return 1;
305 }
306 case Str_kind: {
307 PyObject *s = exp->v.Str.s;
308 if (!PyUnicode_CheckExact(s)) {
309 PyErr_SetString(PyExc_TypeError, "non-string type in Str");
310 return 0;
311 }
312 return 1;
313 }
Eric V. Smith235a6f02015-09-19 14:51:32 -0400314 case JoinedStr_kind:
315 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
316 case FormattedValue_kind:
317 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
318 return 0;
319 if (exp->v.FormattedValue.format_spec)
320 return validate_expr(exp->v.FormattedValue.format_spec, Load);
321 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Bytes_kind: {
323 PyObject *b = exp->v.Bytes.s;
324 if (!PyBytes_CheckExact(b)) {
325 PyErr_SetString(PyExc_TypeError, "non-bytes type in Bytes");
326 return 0;
327 }
328 return 1;
329 }
330 case Attribute_kind:
331 return validate_expr(exp->v.Attribute.value, Load);
332 case Subscript_kind:
333 return validate_slice(exp->v.Subscript.slice) &&
334 validate_expr(exp->v.Subscript.value, Load);
335 case Starred_kind:
336 return validate_expr(exp->v.Starred.value, ctx);
337 case List_kind:
338 return validate_exprs(exp->v.List.elts, ctx, 0);
339 case Tuple_kind:
340 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
341 /* These last cases don't have any checking. */
342 case Name_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -0500343 case NameConstant_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344 case Ellipsis_kind:
345 return 1;
346 default:
347 PyErr_SetString(PyExc_SystemError, "unexpected expression");
348 return 0;
349 }
350}
351
352static int
353validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
354{
355 if (asdl_seq_LEN(seq))
356 return 1;
357 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
358 return 0;
359}
360
361static int
362validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
363{
364 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
365 validate_exprs(targets, ctx, 0);
366}
367
368static int
INADA Naokicb41b272017-02-23 00:31:59 +0900369validate_body(asdl_seq *body, const char *owner, int allowempty)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500370{
INADA Naokicb41b272017-02-23 00:31:59 +0900371 if (!allowempty && !validate_nonempty_seq(body, "body", owner)) {
372 return 0;
373 }
374 return validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500375}
376
377static int
378validate_stmt(stmt_ty stmt)
379{
380 int i;
381 switch (stmt->kind) {
382 case FunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900383 return validate_body(stmt->v.FunctionDef.body, "FunctionDef",
384 stmt->v.FunctionDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500385 validate_arguments(stmt->v.FunctionDef.args) &&
386 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
387 (!stmt->v.FunctionDef.returns ||
388 validate_expr(stmt->v.FunctionDef.returns, Load));
389 case ClassDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900390 return validate_body(stmt->v.ClassDef.body, "ClassDef",
391 stmt->v.ClassDef.docstring != NULL) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500392 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
393 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400394 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500395 case Return_kind:
396 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
397 case Delete_kind:
398 return validate_assignlist(stmt->v.Delete.targets, Del);
399 case Assign_kind:
400 return validate_assignlist(stmt->v.Assign.targets, Store) &&
401 validate_expr(stmt->v.Assign.value, Load);
402 case AugAssign_kind:
403 return validate_expr(stmt->v.AugAssign.target, Store) &&
404 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700405 case AnnAssign_kind:
406 if (stmt->v.AnnAssign.target->kind != Name_kind &&
407 stmt->v.AnnAssign.simple) {
408 PyErr_SetString(PyExc_TypeError,
409 "AnnAssign with simple non-Name target");
410 return 0;
411 }
412 return validate_expr(stmt->v.AnnAssign.target, Store) &&
413 (!stmt->v.AnnAssign.value ||
414 validate_expr(stmt->v.AnnAssign.value, Load)) &&
415 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500416 case For_kind:
417 return validate_expr(stmt->v.For.target, Store) &&
418 validate_expr(stmt->v.For.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900419 validate_body(stmt->v.For.body, "For", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500420 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400421 case AsyncFor_kind:
422 return validate_expr(stmt->v.AsyncFor.target, Store) &&
423 validate_expr(stmt->v.AsyncFor.iter, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900424 validate_body(stmt->v.AsyncFor.body, "AsyncFor", 0) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400425 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500426 case While_kind:
427 return validate_expr(stmt->v.While.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900428 validate_body(stmt->v.While.body, "While", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500429 validate_stmts(stmt->v.While.orelse);
430 case If_kind:
431 return validate_expr(stmt->v.If.test, Load) &&
INADA Naokicb41b272017-02-23 00:31:59 +0900432 validate_body(stmt->v.If.body, "If", 0) &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500433 validate_stmts(stmt->v.If.orelse);
434 case With_kind:
435 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
436 return 0;
437 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
438 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
439 if (!validate_expr(item->context_expr, Load) ||
440 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
441 return 0;
442 }
INADA Naokicb41b272017-02-23 00:31:59 +0900443 return validate_body(stmt->v.With.body, "With", 0);
Yury Selivanov75445082015-05-11 22:57:16 -0400444 case AsyncWith_kind:
445 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
446 return 0;
447 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
448 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
449 if (!validate_expr(item->context_expr, Load) ||
450 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
451 return 0;
452 }
INADA Naokicb41b272017-02-23 00:31:59 +0900453 return validate_body(stmt->v.AsyncWith.body, "AsyncWith", 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500454 case Raise_kind:
455 if (stmt->v.Raise.exc) {
456 return validate_expr(stmt->v.Raise.exc, Load) &&
457 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
458 }
459 if (stmt->v.Raise.cause) {
460 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
461 return 0;
462 }
463 return 1;
464 case Try_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900465 if (!validate_body(stmt->v.Try.body, "Try", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500466 return 0;
467 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
468 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
469 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
470 return 0;
471 }
472 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
473 asdl_seq_LEN(stmt->v.Try.orelse)) {
474 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
475 return 0;
476 }
477 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
478 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
479 if ((handler->v.ExceptHandler.type &&
480 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
INADA Naokicb41b272017-02-23 00:31:59 +0900481 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler", 0))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500482 return 0;
483 }
484 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
485 validate_stmts(stmt->v.Try.finalbody)) &&
486 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
487 validate_stmts(stmt->v.Try.orelse));
488 case Assert_kind:
489 return validate_expr(stmt->v.Assert.test, Load) &&
490 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
491 case Import_kind:
492 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
493 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300494 if (stmt->v.ImportFrom.level < 0) {
495 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500496 return 0;
497 }
498 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
499 case Global_kind:
500 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
501 case Nonlocal_kind:
502 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
503 case Expr_kind:
504 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400505 case AsyncFunctionDef_kind:
INADA Naokicb41b272017-02-23 00:31:59 +0900506 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef",
507 stmt->v.AsyncFunctionDef.docstring != NULL) &&
Yury Selivanov75445082015-05-11 22:57:16 -0400508 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
509 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
510 (!stmt->v.AsyncFunctionDef.returns ||
511 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500512 case Pass_kind:
513 case Break_kind:
514 case Continue_kind:
515 return 1;
516 default:
517 PyErr_SetString(PyExc_SystemError, "unexpected statement");
518 return 0;
519 }
520}
521
522static int
523validate_stmts(asdl_seq *seq)
524{
525 int i;
526 for (i = 0; i < asdl_seq_LEN(seq); i++) {
527 stmt_ty stmt = asdl_seq_GET(seq, i);
528 if (stmt) {
529 if (!validate_stmt(stmt))
530 return 0;
531 }
532 else {
533 PyErr_SetString(PyExc_ValueError,
534 "None disallowed in statement list");
535 return 0;
536 }
537 }
538 return 1;
539}
540
541static int
542validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
543{
544 int i;
545 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
546 expr_ty expr = asdl_seq_GET(exprs, i);
547 if (expr) {
548 if (!validate_expr(expr, ctx))
549 return 0;
550 }
551 else if (!null_ok) {
552 PyErr_SetString(PyExc_ValueError,
553 "None disallowed in expression list");
554 return 0;
555 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100556
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500557 }
558 return 1;
559}
560
561int
562PyAST_Validate(mod_ty mod)
563{
564 int res = 0;
565
566 switch (mod->kind) {
567 case Module_kind:
568 res = validate_stmts(mod->v.Module.body);
569 break;
570 case Interactive_kind:
571 res = validate_stmts(mod->v.Interactive.body);
572 break;
573 case Expression_kind:
574 res = validate_expr(mod->v.Expression.body, Load);
575 break;
576 case Suite_kind:
577 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
578 break;
579 default:
580 PyErr_SetString(PyExc_SystemError, "impossible module node");
581 res = 0;
582 break;
583 }
584 return res;
585}
586
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500587/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500588#include "grammar.h"
589#include "parsetok.h"
590#include "graminit.h"
591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592/* Data structure used internally */
593struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400594 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200595 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500596 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597};
598
599static asdl_seq *seq_for_testlist(struct compiling *, const node *);
600static expr_ty ast_for_expr(struct compiling *, const node *);
601static stmt_ty ast_for_stmt(struct compiling *, const node *);
INADA Naokicb41b272017-02-23 00:31:59 +0900602static asdl_seq *ast_for_body(struct compiling *c, const node *n,
603 string *docstring);
604static string docstring_from_stmts(asdl_seq *stmts);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000605static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
606 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000607static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000608static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
Yury Selivanov75445082015-05-11 22:57:16 -0400610static stmt_ty ast_for_with_stmt(struct compiling *, const node *, int);
611static stmt_ty ast_for_for_stmt(struct compiling *, const node *, int);
612
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613/* Note different signature for ast_for_call */
614static expr_ty ast_for_call(struct compiling *, const node *, expr_ty);
615
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000616static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400617static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618
Nick Coghlan650f0d02007-04-15 12:05:43 +0000619#define COMP_GENEXP 0
620#define COMP_LISTCOMP 1
621#define COMP_SETCOMP 2
622
Benjamin Peterson55e00432012-01-16 17:22:31 -0500623static int
624init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000625{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500626 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
627 if (!m)
628 return 0;
629 c->c_normalize = PyObject_GetAttrString(m, "normalize");
630 Py_DECREF(m);
631 if (!c->c_normalize)
632 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500633 return 1;
634}
635
636static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400637new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500638{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400639 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500640 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000641 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500642 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500643 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 /* Check whether there are non-ASCII characters in the
645 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500646 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200647 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300648 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500649 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500650 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200651 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500652 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300653 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
654 if (form == NULL) {
655 Py_DECREF(id);
656 return NULL;
657 }
658 PyObject *args[2] = {form, id};
659 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500660 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200661 if (!id2)
662 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300663 if (!PyUnicode_Check(id2)) {
664 PyErr_Format(PyExc_TypeError,
665 "unicodedata.normalize() must return a string, not "
666 "%.200s",
667 Py_TYPE(id2)->tp_name);
668 Py_DECREF(id2);
669 return NULL;
670 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200671 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000672 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000673 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200674 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
675 Py_DECREF(id);
676 return NULL;
677 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000678 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679}
680
Benjamin Peterson55e00432012-01-16 17:22:31 -0500681#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683static int
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400684ast_error(struct compiling *c, const node *n, const char *errmsg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400686 PyObject *value, *errstr, *loc, *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687
Victor Stinner14e461d2013-08-26 22:28:21 +0200688 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000690 Py_INCREF(Py_None);
691 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200693 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400694 if (!tmp)
695 return 0;
696 errstr = PyUnicode_FromString(errmsg);
697 if (!errstr) {
698 Py_DECREF(tmp);
699 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000700 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 Py_DECREF(errstr);
703 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400704 if (value) {
705 PyErr_SetObject(PyExc_SyntaxError, value);
706 Py_DECREF(value);
707 }
708 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709}
710
711/* num_stmts() returns number of contained statements.
712
713 Use this routine to determine how big a sequence is needed for
714 the statements in a parse tree. Its raison d'etre is this bit of
715 grammar:
716
717 stmt: simple_stmt | compound_stmt
718 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
719
720 A simple_stmt can contain multiple small_stmt elements joined
721 by semicolons. If the arg is a simple_stmt, the number of
722 small_stmt elements is returned.
723*/
724
725static int
726num_stmts(const node *n)
727{
728 int i, l;
729 node *ch;
730
731 switch (TYPE(n)) {
732 case single_input:
733 if (TYPE(CHILD(n, 0)) == NEWLINE)
734 return 0;
735 else
736 return num_stmts(CHILD(n, 0));
737 case file_input:
738 l = 0;
739 for (i = 0; i < NCH(n); i++) {
740 ch = CHILD(n, i);
741 if (TYPE(ch) == stmt)
742 l += num_stmts(ch);
743 }
744 return l;
745 case stmt:
746 return num_stmts(CHILD(n, 0));
747 case compound_stmt:
748 return 1;
749 case simple_stmt:
750 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
751 case suite:
752 if (NCH(n) == 1)
753 return num_stmts(CHILD(n, 0));
754 else {
755 l = 0;
756 for (i = 2; i < (NCH(n) - 1); i++)
757 l += num_stmts(CHILD(n, i));
758 return l;
759 }
760 default: {
761 char buf[128];
762
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000763 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 TYPE(n), NCH(n));
765 Py_FatalError(buf);
766 }
767 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700768 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769}
770
771/* Transform the CST rooted at node * to the appropriate AST
772*/
773
774mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200775PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
776 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000778 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 asdl_seq *stmts = NULL;
780 stmt_ty s;
781 node *ch;
782 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500783 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400785 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200786 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400787 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800788 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800789
790 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Jeremy Hyltona8293132006-02-28 17:58:27 +0000793 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 switch (TYPE(n)) {
795 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200796 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500798 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 for (i = 0; i < NCH(n) - 1; i++) {
800 ch = CHILD(n, i);
801 if (TYPE(ch) == NEWLINE)
802 continue;
803 REQ(ch, stmt);
804 num = num_stmts(ch);
805 if (num == 1) {
806 s = ast_for_stmt(&c, ch);
807 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500808 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000809 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 else {
812 ch = CHILD(ch, 0);
813 REQ(ch, simple_stmt);
814 for (j = 0; j < num; j++) {
815 s = ast_for_stmt(&c, CHILD(ch, j * 2));
816 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500817 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000818 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 }
820 }
821 }
INADA Naokicb41b272017-02-23 00:31:59 +0900822 res = Module(stmts, docstring_from_stmts(stmts), arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500823 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 case eval_input: {
825 expr_ty testlist_ast;
826
Nick Coghlan650f0d02007-04-15 12:05:43 +0000827 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000828 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500830 goto out;
831 res = Expression(testlist_ast, arena);
832 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 }
834 case single_input:
835 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200836 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500838 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
840 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000841 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 goto out;
843 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 }
845 else {
846 n = CHILD(n, 0);
847 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200848 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500850 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000852 s = ast_for_stmt(&c, n);
853 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500854 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 asdl_seq_SET(stmts, 0, s);
856 }
857 else {
858 /* Only a simple_stmt can contain multiple statements. */
859 REQ(n, simple_stmt);
860 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 if (TYPE(CHILD(n, i)) == NEWLINE)
862 break;
863 s = ast_for_stmt(&c, CHILD(n, i));
864 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500865 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 asdl_seq_SET(stmts, i / 2, s);
867 }
868 }
869
Benjamin Peterson55e00432012-01-16 17:22:31 -0500870 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500872 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000874 PyErr_Format(PyExc_SystemError,
875 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500876 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500878 out:
879 if (c.c_normalize) {
880 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500881 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500882 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883}
884
Victor Stinner14e461d2013-08-26 22:28:21 +0200885mod_ty
886PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
887 PyArena *arena)
888{
889 mod_ty mod;
890 PyObject *filename;
891 filename = PyUnicode_DecodeFSDefault(filename_str);
892 if (filename == NULL)
893 return NULL;
894 mod = PyAST_FromNodeObject(n, flags, filename, arena);
895 Py_DECREF(filename);
896 return mod;
897
898}
899
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
901*/
902
903static operator_ty
904get_operator(const node *n)
905{
906 switch (TYPE(n)) {
907 case VBAR:
908 return BitOr;
909 case CIRCUMFLEX:
910 return BitXor;
911 case AMPER:
912 return BitAnd;
913 case LEFTSHIFT:
914 return LShift;
915 case RIGHTSHIFT:
916 return RShift;
917 case PLUS:
918 return Add;
919 case MINUS:
920 return Sub;
921 case STAR:
922 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400923 case AT:
924 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925 case SLASH:
926 return Div;
927 case DOUBLESLASH:
928 return FloorDiv;
929 case PERCENT:
930 return Mod;
931 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 }
934}
935
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200936static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000937 "None",
938 "True",
939 "False",
940 NULL,
941};
942
943static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400944forbidden_name(struct compiling *c, identifier name, const node *n,
945 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000946{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000947 assert(PyUnicode_Check(name));
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200948 if (_PyUnicode_EqualToASCIIString(name, "__debug__")) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400949 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000950 return 1;
951 }
952 if (full_checks) {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200953 const char * const *p;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000954 for (p = FORBIDDEN; *p; p++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200955 if (_PyUnicode_EqualToASCIIString(name, *p)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400956 ast_error(c, n, "assignment to keyword");
Benjamin Peterson70f52762009-06-28 23:32:44 +0000957 return 1;
958 }
Guido van Rossume7ba4952007-06-06 23:52:48 +0000959 }
960 }
961 return 0;
962}
963
Jeremy Hyltona8293132006-02-28 17:58:27 +0000964/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965
966 Only sets context for expr kinds that "can appear in assignment context"
967 (according to ../Parser/Python.asdl). For other expr kinds, it sets
968 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969*/
970
971static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000972set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973{
974 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 /* If a particular expression type can't be used for assign / delete,
976 set expr_name to its name and an error message will be generated.
977 */
978 const char* expr_name = NULL;
979
980 /* The ast defines augmented store and load contexts, but the
981 implementation here doesn't actually use them. The code may be
982 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000983 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000984 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000985 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000986 */
987 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988
989 switch (e->kind) {
990 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000991 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400992 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000993 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000994 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000996 e->v.Subscript.ctx = ctx;
997 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000998 case Starred_kind:
999 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001000 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001001 return 0;
1002 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001004 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001005 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001006 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001007 }
1008 e->v.Name.ctx = ctx;
1009 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001011 e->v.List.ctx = ctx;
1012 s = e->v.List.elts;
1013 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +03001015 e->v.Tuple.ctx = ctx;
1016 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001017 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001018 case Lambda_kind:
1019 expr_name = "lambda";
1020 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001022 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001023 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001024 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001026 case UnaryOp_kind:
1027 expr_name = "operator";
1028 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001030 expr_name = "generator expression";
1031 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001032 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001033 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001034 expr_name = "yield expression";
1035 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001036 case Await_kind:
1037 expr_name = "await expression";
1038 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001039 case ListComp_kind:
1040 expr_name = "list comprehension";
1041 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001042 case SetComp_kind:
1043 expr_name = "set comprehension";
1044 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001045 case DictComp_kind:
1046 expr_name = "dict comprehension";
1047 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001048 case Dict_kind:
Neal Norwitzc1505362006-12-28 06:47:50 +00001049 case Set_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 case Num_kind:
1051 case Str_kind:
Benjamin Petersonbd3e3622011-04-12 18:33:28 -05001052 case Bytes_kind:
Eric V. Smith235a6f02015-09-19 14:51:32 -04001053 case JoinedStr_kind:
1054 case FormattedValue_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001055 expr_name = "literal";
1056 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05001057 case NameConstant_kind:
1058 expr_name = "keyword";
1059 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001060 case Ellipsis_kind:
1061 expr_name = "Ellipsis";
1062 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001063 case Compare_kind:
1064 expr_name = "comparison";
1065 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001066 case IfExp_kind:
1067 expr_name = "conditional expression";
1068 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001069 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 PyErr_Format(PyExc_SystemError,
1071 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001072 e->kind, e->lineno);
1073 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001075 /* Check for error string set by switch */
1076 if (expr_name) {
1077 char buf[300];
1078 PyOS_snprintf(buf, sizeof(buf),
1079 "can't %s %s",
1080 ctx == Store ? "assign to" : "delete",
1081 expr_name);
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001082 return ast_error(c, n, buf);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001083 }
1084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 */
1088 if (s) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001089 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090
Thomas Wouters89f507f2006-12-13 04:49:30 +00001091 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001092 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001093 return 0;
1094 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 }
1096 return 1;
1097}
1098
1099static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001100ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101{
1102 REQ(n, augassign);
1103 n = CHILD(n, 0);
1104 switch (STR(n)[0]) {
1105 case '+':
1106 return Add;
1107 case '-':
1108 return Sub;
1109 case '/':
1110 if (STR(n)[1] == '/')
1111 return FloorDiv;
1112 else
1113 return Div;
1114 case '%':
1115 return Mod;
1116 case '<':
1117 return LShift;
1118 case '>':
1119 return RShift;
1120 case '&':
1121 return BitAnd;
1122 case '^':
1123 return BitXor;
1124 case '|':
1125 return BitOr;
1126 case '*':
1127 if (STR(n)[1] == '*')
1128 return Pow;
1129 else
1130 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001131 case '@':
1132 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001134 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 }
1137}
1138
1139static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001140ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001142 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 |'is' 'not'
1144 */
1145 REQ(n, comp_op);
1146 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001147 n = CHILD(n, 0);
1148 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 case LESS:
1150 return Lt;
1151 case GREATER:
1152 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001153 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return Eq;
1155 case LESSEQUAL:
1156 return LtE;
1157 case GREATEREQUAL:
1158 return GtE;
1159 case NOTEQUAL:
1160 return NotEq;
1161 case NAME:
1162 if (strcmp(STR(n), "in") == 0)
1163 return In;
1164 if (strcmp(STR(n), "is") == 0)
1165 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001166 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001168 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 }
1173 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001174 /* handle "not in" and "is not" */
1175 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 case NAME:
1177 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1178 return NotIn;
1179 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1180 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001181 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001183 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001186 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 }
Neal Norwitz79792652005-11-14 04:25:03 +00001188 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001190 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191}
1192
1193static asdl_seq *
1194seq_for_testlist(struct compiling *c, const node *n)
1195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001197 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1198 */
Armin Rigo31441302005-10-21 12:57:31 +00001199 asdl_seq *seq;
1200 expr_ty expression;
1201 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001202 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001204 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (!seq)
1206 return NULL;
1207
1208 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001210 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211
Benjamin Peterson4905e802009-09-27 02:43:28 +00001212 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001213 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215
1216 assert(i / 2 < seq->size);
1217 asdl_seq_SET(seq, i / 2, expression);
1218 }
1219 return seq;
1220}
1221
Neal Norwitzc1505362006-12-28 06:47:50 +00001222static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001223ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001224{
1225 identifier name;
1226 expr_ty annotation = NULL;
1227 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001228 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001229
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001230 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001231 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001232 name = NEW_IDENTIFIER(ch);
1233 if (!name)
1234 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001235 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001236 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001237
1238 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1239 annotation = ast_for_expr(c, CHILD(n, 2));
1240 if (!annotation)
1241 return NULL;
1242 }
1243
Victor Stinnerc106c682015-11-06 17:01:48 +01001244 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001245 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001246 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001247 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248}
1249
Guido van Rossum4f72a782006-10-27 23:31:49 +00001250/* returns -1 if failed to handle keyword only arguments
1251 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001252 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001253 ^^^
1254 start pointing here
1255 */
1256static int
1257handle_keywordonly_args(struct compiling *c, const node *n, int start,
1258 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1259{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001260 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001261 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001262 expr_ty expression, annotation;
1263 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001264 int i = start;
1265 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001266
1267 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001268 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001269 return -1;
1270 }
1271 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001272 while (i < NCH(n)) {
1273 ch = CHILD(n, i);
1274 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001275 case vfpdef:
1276 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001277 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001278 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001279 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001280 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001281 asdl_seq_SET(kwdefaults, j, expression);
1282 i += 2; /* '=' and test */
1283 }
1284 else { /* setting NULL if no default value exists */
1285 asdl_seq_SET(kwdefaults, j, NULL);
1286 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001287 if (NCH(ch) == 3) {
1288 /* ch is NAME ':' test */
1289 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001290 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001292 }
1293 else {
1294 annotation = NULL;
1295 }
1296 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001297 argname = NEW_IDENTIFIER(ch);
1298 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001299 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001300 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001301 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001302 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1303 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001304 if (!arg)
1305 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001306 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001307 i += 2; /* the name and the comma */
1308 break;
1309 case DOUBLESTAR:
1310 return i;
1311 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001312 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001313 goto error;
1314 }
1315 }
1316 return i;
1317 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001319}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320
Jeremy Hyltona8293132006-02-28 17:58:27 +00001321/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322
1323static arguments_ty
1324ast_for_arguments(struct compiling *c, const node *n)
1325{
Neal Norwitzc1505362006-12-28 06:47:50 +00001326 /* This function handles both typedargslist (function definition)
1327 and varargslist (lambda definition).
1328
1329 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001330 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1331 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1332 | '**' tfpdef [',']]]
1333 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1334 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001335 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001336 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1337 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1338 | '**' vfpdef [',']]]
1339 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1340 | '**' vfpdef [',']
1341 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001342 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001343
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001345 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1346 int nposdefaults = 0, found_default = 0;
1347 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001348 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001349 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 node *ch;
1351
1352 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001353 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001354 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001355 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001357 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358
Jeremy Hyltone921e022008-07-17 16:37:17 +00001359 /* First count the number of positional args & defaults. The
1360 variable i is the loop index for this for loop and the next.
1361 The next loop picks up where the first leaves off.
1362 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001364 ch = CHILD(n, i);
1365 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001366 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001367 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001368 if (i < NCH(n) && /* skip argument following star */
1369 (TYPE(CHILD(n, i)) == tfpdef ||
1370 TYPE(CHILD(n, i)) == vfpdef)) {
1371 i++;
1372 }
1373 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001375 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001376 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001377 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 defaults for keyword only args */
1381 for ( ; i < NCH(n); ++i) {
1382 ch = CHILD(n, i);
1383 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001384 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001386 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001387 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001388 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001390 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001392 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001394 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001395 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001396 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001398 since we set NULL as default for keyword only argument w/o default
1399 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001400 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001401 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001403 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001405 /* tfpdef: NAME [':' test]
1406 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 */
1408 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001409 j = 0; /* index for defaults */
1410 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001412 ch = CHILD(n, i);
1413 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001414 case tfpdef:
1415 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1417 anything other than EQUAL or a comma? */
1418 /* XXX Should NCH(n) check be made a separate check? */
1419 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001420 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1421 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001422 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423 assert(posdefaults != NULL);
1424 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001426 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001428 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001429 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001430 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001431 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001432 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001433 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001434 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001435 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001436 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 i += 2; /* the name and the comma */
1438 break;
1439 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001440 if (i+1 >= NCH(n) ||
1441 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001442 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001443 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001444 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001446 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001447 if (TYPE(ch) == COMMA) {
1448 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449 i += 2; /* now follows keyword only arguments */
1450 res = handle_keywordonly_args(c, n, i,
1451 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001452 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001453 i = res; /* res has new position to process */
1454 }
1455 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001456 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001457 if (!vararg)
1458 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001459
Guido van Rossum4f72a782006-10-27 23:31:49 +00001460 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001461 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1462 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001463 int res = 0;
1464 res = handle_keywordonly_args(c, n, i,
1465 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001466 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001467 i = res; /* res has new position to process */
1468 }
1469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 break;
1471 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001472 ch = CHILD(n, i+1); /* tfpdef */
1473 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001474 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001475 if (!kwarg)
1476 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 i += 3;
1478 break;
1479 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001480 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 "unexpected node in varargslist: %d @ %d",
1482 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001483 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001486 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487}
1488
1489static expr_ty
1490ast_for_dotted_name(struct compiling *c, const node *n)
1491{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001492 expr_ty e;
1493 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001494 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 int i;
1496
1497 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001498
1499 lineno = LINENO(n);
1500 col_offset = n->n_col_offset;
1501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 id = NEW_IDENTIFIER(CHILD(n, 0));
1503 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001504 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001505 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508
1509 for (i = 2; i < NCH(n); i+=2) {
1510 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001511 if (!id)
1512 return NULL;
1513 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1514 if (!e)
1515 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 }
1517
1518 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519}
1520
1521static expr_ty
1522ast_for_decorator(struct compiling *c, const node *n)
1523{
1524 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1525 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001526 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001529 REQ(CHILD(n, 0), AT);
1530 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1533 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001534 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001537 d = name_expr;
1538 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 }
1540 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001541 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001542 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001543 if (!d)
1544 return NULL;
1545 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 }
1547 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001548 d = ast_for_call(c, CHILD(n, 3), name_expr);
1549 if (!d)
1550 return NULL;
1551 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552 }
1553
1554 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555}
1556
1557static asdl_seq*
1558ast_for_decorators(struct compiling *c, const node *n)
1559{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001560 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001561 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001565 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 if (!decorator_seq)
1567 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001570 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001571 if (!d)
1572 return NULL;
1573 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 }
1575 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576}
1577
1578static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001579ast_for_funcdef_impl(struct compiling *c, const node *n,
1580 asdl_seq *decorator_seq, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001582 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
Neal Norwitz84456bd2005-12-18 03:16:20 +00001583 identifier name;
1584 arguments_ty args;
1585 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001586 expr_ty returns = NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09001587 string docstring;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001588 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589
1590 REQ(n, funcdef);
1591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 name = NEW_IDENTIFIER(CHILD(n, name_i));
1593 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001594 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001595 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001596 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1598 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001599 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001600 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1601 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1602 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001603 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001604 name_i += 2;
1605 }
INADA Naokicb41b272017-02-23 00:31:59 +09001606 body = ast_for_body(c, CHILD(n, name_i + 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001608 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609
Yury Selivanov75445082015-05-11 22:57:16 -04001610 if (is_async)
1611 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001612 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001613 n->n_col_offset, c->c_arena);
1614 else
1615 return FunctionDef(name, args, body, decorator_seq, returns,
INADA Naokicb41b272017-02-23 00:31:59 +09001616 docstring, LINENO(n),
Yury Selivanov75445082015-05-11 22:57:16 -04001617 n->n_col_offset, c->c_arena);
1618}
1619
1620static stmt_ty
1621ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1622{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001623 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001624 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001625 REQ(CHILD(n, 0), NAME);
1626 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001627 REQ(CHILD(n, 1), funcdef);
1628
1629 return ast_for_funcdef_impl(c, CHILD(n, 1), decorator_seq,
1630 1 /* is_async */);
1631}
1632
1633static stmt_ty
1634ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1635{
1636 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1637 return ast_for_funcdef_impl(c, n, decorator_seq,
1638 0 /* is_async */);
1639}
1640
1641
1642static stmt_ty
1643ast_for_async_stmt(struct compiling *c, const node *n)
1644{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001645 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001646 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001647 REQ(CHILD(n, 0), NAME);
1648 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001649
1650 switch (TYPE(CHILD(n, 1))) {
1651 case funcdef:
1652 return ast_for_funcdef_impl(c, CHILD(n, 1), NULL,
1653 1 /* is_async */);
1654 case with_stmt:
1655 return ast_for_with_stmt(c, CHILD(n, 1),
1656 1 /* is_async */);
1657
1658 case for_stmt:
1659 return ast_for_for_stmt(c, CHILD(n, 1),
1660 1 /* is_async */);
1661
1662 default:
1663 PyErr_Format(PyExc_SystemError,
1664 "invalid async stament: %s",
1665 STR(CHILD(n, 1)));
1666 return NULL;
1667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668}
1669
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001670static stmt_ty
1671ast_for_decorated(struct compiling *c, const node *n)
1672{
Yury Selivanov75445082015-05-11 22:57:16 -04001673 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001674 stmt_ty thing = NULL;
1675 asdl_seq *decorator_seq = NULL;
1676
1677 REQ(n, decorated);
1678
1679 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1680 if (!decorator_seq)
1681 return NULL;
1682
1683 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001684 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001686
1687 if (TYPE(CHILD(n, 1)) == funcdef) {
1688 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1689 } else if (TYPE(CHILD(n, 1)) == classdef) {
1690 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001691 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1692 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001693 }
Christian Heimes09aaa882008-02-23 15:01:06 +00001694 /* we count the decorators in when talking about the class' or
1695 * function's line number */
1696 if (thing) {
1697 thing->lineno = LINENO(n);
1698 thing->col_offset = n->n_col_offset;
1699 }
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001700 return thing;
1701}
1702
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703static expr_ty
1704ast_for_lambdef(struct compiling *c, const node *n)
1705{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001706 /* lambdef: 'lambda' [varargslist] ':' test
1707 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 arguments_ty args;
1709 expr_ty expression;
1710
1711 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001712 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 if (!args)
1714 return NULL;
1715 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001716 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718 }
1719 else {
1720 args = ast_for_arguments(c, CHILD(n, 1));
1721 if (!args)
1722 return NULL;
1723 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001724 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 }
1727
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001728 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729}
1730
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001731static expr_ty
1732ast_for_ifexpr(struct compiling *c, const node *n)
1733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001735 expr_ty expression, body, orelse;
1736
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001737 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001738 body = ast_for_expr(c, CHILD(n, 0));
1739 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001740 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001741 expression = ast_for_expr(c, CHILD(n, 2));
1742 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001743 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001744 orelse = ast_for_expr(c, CHILD(n, 4));
1745 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001746 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001747 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1748 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001749}
1750
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001752 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753
Nick Coghlan650f0d02007-04-15 12:05:43 +00001754 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755*/
1756
1757static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001758count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001760 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761
Guido van Rossumd8faa362007-04-27 19:54:29 +00001762 count_comp_for:
1763 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001764 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001765 if (NCH(n) == 2) {
1766 REQ(CHILD(n, 0), NAME);
1767 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1768 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001769 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001770 else if (NCH(n) == 1) {
1771 n = CHILD(n, 0);
1772 }
1773 else {
1774 goto error;
1775 }
1776 if (NCH(n) == (5)) {
1777 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001778 }
1779 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001780 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001781 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001782 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001783 REQ(n, comp_iter);
1784 n = CHILD(n, 0);
1785 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001786 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001787 else if (TYPE(n) == comp_if) {
1788 if (NCH(n) == 3) {
1789 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001791 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001792 else
1793 return n_fors;
1794 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001795
Jelle Zijlstraac317702017-10-05 20:24:46 -07001796 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001797 /* Should never be reached */
1798 PyErr_SetString(PyExc_SystemError,
1799 "logic error in count_comp_fors");
1800 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801}
1802
Nick Coghlan650f0d02007-04-15 12:05:43 +00001803/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804
Nick Coghlan650f0d02007-04-15 12:05:43 +00001805 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806*/
1807
1808static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001809count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001811 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812
Guido van Rossumd8faa362007-04-27 19:54:29 +00001813 while (1) {
1814 REQ(n, comp_iter);
1815 if (TYPE(CHILD(n, 0)) == comp_for)
1816 return n_ifs;
1817 n = CHILD(n, 0);
1818 REQ(n, comp_if);
1819 n_ifs++;
1820 if (NCH(n) == 2)
1821 return n_ifs;
1822 n = CHILD(n, 2);
1823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824}
1825
Guido van Rossum992d4a32007-07-11 13:09:30 +00001826static asdl_seq *
1827ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001830 asdl_seq *comps;
1831
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001832 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 if (n_fors == -1)
1834 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001835
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001836 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001837 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001839
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001841 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001843 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001844 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001845 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001846 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847
Guido van Rossum992d4a32007-07-11 13:09:30 +00001848 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849
Jelle Zijlstraac317702017-10-05 20:24:46 -07001850 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001851 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001852 REQ(CHILD(n, 0), NAME);
1853 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1854 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001855 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001856 else {
1857 sync_n = CHILD(n, 0);
1858 }
1859 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001860
Jelle Zijlstraac317702017-10-05 20:24:46 -07001861 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001862 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001863 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001865 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001866 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001868
Thomas Wouters89f507f2006-12-13 04:49:30 +00001869 /* Check the # of children rather than the length of t, since
1870 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001871 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001872 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001873 comp = comprehension(first, expression, NULL,
1874 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001876 comp = comprehension(Tuple(t, Store, first->lineno,
1877 first->col_offset, c->c_arena),
1878 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001879 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001881
Jelle Zijlstraac317702017-10-05 20:24:46 -07001882 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 int j, n_ifs;
1884 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885
Jelle Zijlstraac317702017-10-05 20:24:46 -07001886 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001887 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001888 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001890
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001891 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001896 REQ(n, comp_iter);
1897 n = CHILD(n, 0);
1898 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899
Guido van Rossum992d4a32007-07-11 13:09:30 +00001900 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001901 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001902 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001903 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001904 if (NCH(n) == 3)
1905 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 /* on exit, must guarantee that n is a comp_for */
1908 if (TYPE(n) == comp_iter)
1909 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001910 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001912 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001914 return comps;
1915}
1916
1917static expr_ty
1918ast_for_itercomp(struct compiling *c, const node *n, int type)
1919{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001920 /* testlist_comp: (test|star_expr)
1921 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001922 expr_ty elt;
1923 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001924 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925
Guido van Rossum992d4a32007-07-11 13:09:30 +00001926 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001928 ch = CHILD(n, 0);
1929 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001930 if (!elt)
1931 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001932 if (elt->kind == Starred_kind) {
1933 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1934 return NULL;
1935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936
Guido van Rossum992d4a32007-07-11 13:09:30 +00001937 comps = ast_for_comprehension(c, CHILD(n, 1));
1938 if (!comps)
1939 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001940
1941 if (type == COMP_GENEXP)
1942 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1943 else if (type == COMP_LISTCOMP)
1944 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1945 else if (type == COMP_SETCOMP)
1946 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1947 else
1948 /* Should never happen */
1949 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950}
1951
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001952/* Fills in the key, value pair corresponding to the dict element. In case
1953 * of an unpacking, key is NULL. *i is advanced by the number of ast
1954 * elements. Iff successful, nonzero is returned.
1955 */
1956static int
1957ast_for_dictelement(struct compiling *c, const node *n, int *i,
1958 expr_ty *key, expr_ty *value)
1959{
1960 expr_ty expression;
1961 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1962 assert(NCH(n) - *i >= 2);
1963
1964 expression = ast_for_expr(c, CHILD(n, *i + 1));
1965 if (!expression)
1966 return 0;
1967 *key = NULL;
1968 *value = expression;
1969
1970 *i += 2;
1971 }
1972 else {
1973 assert(NCH(n) - *i >= 3);
1974
1975 expression = ast_for_expr(c, CHILD(n, *i));
1976 if (!expression)
1977 return 0;
1978 *key = expression;
1979
1980 REQ(CHILD(n, *i + 1), COLON);
1981
1982 expression = ast_for_expr(c, CHILD(n, *i + 2));
1983 if (!expression)
1984 return 0;
1985 *value = expression;
1986
1987 *i += 3;
1988 }
1989 return 1;
1990}
1991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001993ast_for_dictcomp(struct compiling *c, const node *n)
1994{
1995 expr_ty key, value;
1996 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001997 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001999 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002000 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002001 assert(key);
2002 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002004 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002005 if (!comps)
2006 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007
Guido van Rossum992d4a32007-07-11 13:09:30 +00002008 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
2009}
2010
2011static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002012ast_for_dictdisplay(struct compiling *c, const node *n)
2013{
2014 int i;
2015 int j;
2016 int size;
2017 asdl_seq *keys, *values;
2018
2019 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2020 keys = _Py_asdl_seq_new(size, c->c_arena);
2021 if (!keys)
2022 return NULL;
2023
2024 values = _Py_asdl_seq_new(size, c->c_arena);
2025 if (!values)
2026 return NULL;
2027
2028 j = 0;
2029 for (i = 0; i < NCH(n); i++) {
2030 expr_ty key, value;
2031
2032 if (!ast_for_dictelement(c, n, &i, &key, &value))
2033 return NULL;
2034 asdl_seq_SET(keys, j, key);
2035 asdl_seq_SET(values, j, value);
2036
2037 j++;
2038 }
2039 keys->size = j;
2040 values->size = j;
2041 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2042}
2043
2044static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002045ast_for_genexp(struct compiling *c, const node *n)
2046{
2047 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002048 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002049}
2050
2051static expr_ty
2052ast_for_listcomp(struct compiling *c, const node *n)
2053{
2054 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002055 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002056}
2057
2058static expr_ty
2059ast_for_setcomp(struct compiling *c, const node *n)
2060{
2061 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002062 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002063}
2064
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002065static expr_ty
2066ast_for_setdisplay(struct compiling *c, const node *n)
2067{
2068 int i;
2069 int size;
2070 asdl_seq *elts;
2071
2072 assert(TYPE(n) == (dictorsetmaker));
2073 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2074 elts = _Py_asdl_seq_new(size, c->c_arena);
2075 if (!elts)
2076 return NULL;
2077 for (i = 0; i < NCH(n); i += 2) {
2078 expr_ty expression;
2079 expression = ast_for_expr(c, CHILD(n, i));
2080 if (!expression)
2081 return NULL;
2082 asdl_seq_SET(elts, i / 2, expression);
2083 }
2084 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2085}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002086
2087static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088ast_for_atom(struct compiling *c, const node *n)
2089{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002090 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2091 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002092 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 */
2094 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002097 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002098 PyObject *name;
2099 const char *s = STR(ch);
2100 size_t len = strlen(s);
2101 if (len >= 4 && len <= 5) {
2102 if (!strcmp(s, "None"))
2103 return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
2104 if (!strcmp(s, "True"))
2105 return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
2106 if (!strcmp(s, "False"))
2107 return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
2108 }
2109 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002110 if (!name)
2111 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002112 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002113 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2114 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002116 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002117 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002118 const char *errtype = NULL;
2119 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2120 errtype = "unicode error";
2121 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2122 errtype = "value error";
2123 if (errtype) {
2124 char buf[128];
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002125 const char *s = NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002126 PyObject *type, *value, *tback, *errstr;
2127 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002128 errstr = PyObject_Str(value);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002129 if (errstr)
2130 s = PyUnicode_AsUTF8(errstr);
2131 if (s) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002132 PyOS_snprintf(buf, sizeof(buf), "(%s) %s", errtype, s);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002133 } else {
Victor Stinner00723e02015-09-03 12:57:11 +02002134 PyErr_Clear();
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002135 PyOS_snprintf(buf, sizeof(buf), "(%s) unknown error", errtype);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002136 }
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02002137 Py_XDECREF(errstr);
Serhiy Storchaka801d9552013-02-10 17:42:01 +02002138 ast_error(c, n, buf);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002139 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002140 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002141 Py_XDECREF(tback);
2142 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002144 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002145 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 }
2147 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002148 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002149 if (!pynum)
2150 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002151
Victor Stinner43d81952013-07-17 00:57:58 +02002152 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2153 Py_DECREF(pynum);
2154 return NULL;
2155 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002156 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 }
Georg Brandldde00282007-03-18 19:01:53 +00002158 case ELLIPSIS: /* Ellipsis */
Neal Norwitzc1505362006-12-28 06:47:50 +00002159 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002161 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162
Thomas Wouters89f507f2006-12-13 04:49:30 +00002163 if (TYPE(ch) == RPAR)
2164 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165
Thomas Wouters89f507f2006-12-13 04:49:30 +00002166 if (TYPE(ch) == yield_expr)
2167 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002170 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002171 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002172
Nick Coghlan650f0d02007-04-15 12:05:43 +00002173 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002175 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176
Thomas Wouters89f507f2006-12-13 04:49:30 +00002177 if (TYPE(ch) == RSQB)
2178 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179
Nick Coghlan650f0d02007-04-15 12:05:43 +00002180 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002181 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2182 asdl_seq *elts = seq_for_testlist(c, ch);
2183 if (!elts)
2184 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002185
Thomas Wouters89f507f2006-12-13 04:49:30 +00002186 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2187 }
2188 else
2189 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002191 /* dictorsetmaker: ( ((test ':' test | '**' test)
2192 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2193 * ((test | '*' test)
2194 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002195 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002196 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002197 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002198 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002199 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002200 }
2201 else {
2202 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2203 if (NCH(ch) == 1 ||
2204 (NCH(ch) > 1 &&
2205 TYPE(CHILD(ch, 1)) == COMMA)) {
2206 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002207 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002208 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002209 else if (NCH(ch) > 1 &&
2210 TYPE(CHILD(ch, 1)) == comp_for) {
2211 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002212 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002213 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002214 else if (NCH(ch) > 3 - is_dict &&
2215 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2216 /* It's a dictionary comprehension. */
2217 if (is_dict) {
2218 ast_error(c, n, "dict unpacking cannot be used in "
2219 "dict comprehension");
2220 return NULL;
2221 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002222 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002223 }
2224 else {
2225 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002226 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002227 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002228 if (res) {
2229 res->lineno = LINENO(n);
2230 res->col_offset = n->n_col_offset;
2231 }
2232 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002233 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002236 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2237 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 }
2239}
2240
2241static slice_ty
2242ast_for_slice(struct compiling *c, const node *n)
2243{
2244 node *ch;
2245 expr_ty lower = NULL, upper = NULL, step = NULL;
2246
2247 REQ(n, subscript);
2248
2249 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002250 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 sliceop: ':' [test]
2252 */
2253 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 if (NCH(n) == 1 && TYPE(ch) == test) {
2255 /* 'step' variable hold no significance in terms of being used over
2256 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 if (!step)
2259 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260
Thomas Wouters89f507f2006-12-13 04:49:30 +00002261 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 }
2263
2264 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002265 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 if (!lower)
2267 return NULL;
2268 }
2269
2270 /* If there's an upper bound it's in the second or third position. */
2271 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002272 if (NCH(n) > 1) {
2273 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274
Thomas Wouters89f507f2006-12-13 04:49:30 +00002275 if (TYPE(n2) == test) {
2276 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 if (!upper)
2278 return NULL;
2279 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002282 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Thomas Wouters89f507f2006-12-13 04:49:30 +00002284 if (TYPE(n2) == test) {
2285 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 if (!upper)
2287 return NULL;
2288 }
2289 }
2290
2291 ch = CHILD(n, NCH(n) - 1);
2292 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002293 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002294 ch = CHILD(ch, 1);
2295 if (TYPE(ch) == test) {
2296 step = ast_for_expr(c, ch);
2297 if (!step)
2298 return NULL;
2299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 }
2301 }
2302
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002303 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304}
2305
2306static expr_ty
2307ast_for_binop(struct compiling *c, const node *n)
2308{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002309 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311 BinOp(BinOp(A, op, B), op, C).
2312 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 int i, nops;
2315 expr_ty expr1, expr2, result;
2316 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317
Guido van Rossumd8faa362007-04-27 19:54:29 +00002318 expr1 = ast_for_expr(c, CHILD(n, 0));
2319 if (!expr1)
2320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321
Guido van Rossumd8faa362007-04-27 19:54:29 +00002322 expr2 = ast_for_expr(c, CHILD(n, 2));
2323 if (!expr2)
2324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325
Guido van Rossumd8faa362007-04-27 19:54:29 +00002326 newoperator = get_operator(CHILD(n, 1));
2327 if (!newoperator)
2328 return NULL;
2329
2330 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2331 c->c_arena);
2332 if (!result)
2333 return NULL;
2334
2335 nops = (NCH(n) - 1) / 2;
2336 for (i = 1; i < nops; i++) {
2337 expr_ty tmp_result, tmp;
2338 const node* next_oper = CHILD(n, i * 2 + 1);
2339
2340 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 return NULL;
2343
Guido van Rossumd8faa362007-04-27 19:54:29 +00002344 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2345 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 return NULL;
2347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002349 LINENO(next_oper), next_oper->n_col_offset,
2350 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002352 return NULL;
2353 result = tmp_result;
2354 }
2355 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356}
2357
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002358static expr_ty
2359ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002362 subscriptlist: subscript (',' subscript)* [',']
2363 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2364 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002365 REQ(n, trailer);
2366 if (TYPE(CHILD(n, 0)) == LPAR) {
2367 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002368 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002369 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002370 else
2371 return ast_for_call(c, CHILD(n, 1), left_expr);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002372 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002373 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002374 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2375 if (!attr_id)
2376 return NULL;
2377 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002378 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002379 }
2380 else {
2381 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002382 REQ(CHILD(n, 2), RSQB);
2383 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002384 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002385 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2386 if (!slc)
2387 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002388 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2389 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002390 }
2391 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002393 by treating the sequence as a tuple literal if there are
2394 no slice features.
2395 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002396 int j;
2397 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002398 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002399 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002400 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002401 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002402 if (!slices)
2403 return NULL;
2404 for (j = 0; j < NCH(n); j += 2) {
2405 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002406 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002407 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002408 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002409 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002410 asdl_seq_SET(slices, j / 2, slc);
2411 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002412 if (!simple) {
2413 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002414 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002415 }
2416 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002417 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002418 if (!elts)
2419 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002420 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2421 slc = (slice_ty)asdl_seq_GET(slices, j);
2422 assert(slc->kind == Index_kind && slc->v.Index.value);
2423 asdl_seq_SET(elts, j, slc->v.Index.value);
2424 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002425 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002426 if (!e)
2427 return NULL;
2428 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002429 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002430 }
2431 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002432}
2433
2434static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002435ast_for_factor(struct compiling *c, const node *n)
2436{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002437 expr_ty expression;
2438
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002439 expression = ast_for_expr(c, CHILD(n, 1));
2440 if (!expression)
2441 return NULL;
2442
2443 switch (TYPE(CHILD(n, 0))) {
2444 case PLUS:
2445 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2446 c->c_arena);
2447 case MINUS:
2448 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2449 c->c_arena);
2450 case TILDE:
2451 return UnaryOp(Invert, expression, LINENO(n),
2452 n->n_col_offset, c->c_arena);
2453 }
2454 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2455 TYPE(CHILD(n, 0)));
2456 return NULL;
2457}
2458
2459static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002460ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002461{
Yury Selivanov75445082015-05-11 22:57:16 -04002462 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002463 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002464
2465 REQ(n, atom_expr);
2466 nch = NCH(n);
2467
Jelle Zijlstraac317702017-10-05 20:24:46 -07002468 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002469 start = 1;
2470 assert(nch > 1);
2471 }
2472
2473 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002474 if (!e)
2475 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002476 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002477 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002478 if (start && nch == 2) {
2479 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2480 }
2481
2482 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002483 node *ch = CHILD(n, i);
2484 if (TYPE(ch) != trailer)
2485 break;
2486 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002487 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002488 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002489 tmp->lineno = e->lineno;
2490 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002491 e = tmp;
2492 }
Yury Selivanov75445082015-05-11 22:57:16 -04002493
2494 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002495 /* there was an 'await' */
Yury Selivanov75445082015-05-11 22:57:16 -04002496 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2497 }
2498 else {
2499 return e;
2500 }
2501}
2502
2503static expr_ty
2504ast_for_power(struct compiling *c, const node *n)
2505{
2506 /* power: atom trailer* ('**' factor)*
2507 */
2508 expr_ty e;
2509 REQ(n, power);
2510 e = ast_for_atom_expr(c, CHILD(n, 0));
2511 if (!e)
2512 return NULL;
2513 if (NCH(n) == 1)
2514 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002515 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2516 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002517 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002518 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002519 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002520 }
2521 return e;
2522}
2523
Guido van Rossum0368b722007-05-11 16:50:42 +00002524static expr_ty
2525ast_for_starred(struct compiling *c, const node *n)
2526{
2527 expr_ty tmp;
2528 REQ(n, star_expr);
2529
2530 tmp = ast_for_expr(c, CHILD(n, 1));
2531 if (!tmp)
2532 return NULL;
2533
2534 /* The Load context is changed later. */
2535 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2536}
2537
2538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539/* Do not name a variable 'expr'! Will cause a compile error.
2540*/
2541
2542static expr_ty
2543ast_for_expr(struct compiling *c, const node *n)
2544{
2545 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002546 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002547 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 and_test: not_test ('and' not_test)*
2550 not_test: 'not' not_test | comparison
2551 comparison: expr (comp_op expr)*
2552 expr: xor_expr ('|' xor_expr)*
2553 xor_expr: and_expr ('^' and_expr)*
2554 and_expr: shift_expr ('&' shift_expr)*
2555 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2556 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002557 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002559 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002560 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002561 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 */
2563
2564 asdl_seq *seq;
2565 int i;
2566
2567 loop:
2568 switch (TYPE(n)) {
2569 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002570 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002571 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002572 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002574 else if (NCH(n) > 1)
2575 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002576 /* Fallthrough */
2577 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 case and_test:
2579 if (NCH(n) == 1) {
2580 n = CHILD(n, 0);
2581 goto loop;
2582 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002583 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584 if (!seq)
2585 return NULL;
2586 for (i = 0; i < NCH(n); i += 2) {
2587 expr_ty e = ast_for_expr(c, CHILD(n, i));
2588 if (!e)
2589 return NULL;
2590 asdl_seq_SET(seq, i / 2, e);
2591 }
2592 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002593 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2594 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002595 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002596 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 case not_test:
2598 if (NCH(n) == 1) {
2599 n = CHILD(n, 0);
2600 goto loop;
2601 }
2602 else {
2603 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2604 if (!expression)
2605 return NULL;
2606
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002607 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2608 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 }
2610 case comparison:
2611 if (NCH(n) == 1) {
2612 n = CHILD(n, 0);
2613 goto loop;
2614 }
2615 else {
2616 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002617 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002618 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002619 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 if (!ops)
2621 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002622 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 return NULL;
2625 }
2626 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002627 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002629 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002630 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002632 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
2634 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002635 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 asdl_seq_SET(cmps, i / 2, expression);
2641 }
2642 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002643 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002645 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002647 return Compare(expression, ops, cmps, LINENO(n),
2648 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650 break;
2651
Guido van Rossum0368b722007-05-11 16:50:42 +00002652 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 /* The next five cases all handle BinOps. The main body of code
2655 is the same in each case, but the switch turned inside out to
2656 reuse the code for each type of operator.
2657 */
2658 case expr:
2659 case xor_expr:
2660 case and_expr:
2661 case shift_expr:
2662 case arith_expr:
2663 case term:
2664 if (NCH(n) == 1) {
2665 n = CHILD(n, 0);
2666 goto loop;
2667 }
2668 return ast_for_binop(c, n);
2669 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002670 node *an = NULL;
2671 node *en = NULL;
2672 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002673 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002674 if (NCH(n) > 1)
2675 an = CHILD(n, 1); /* yield_arg */
2676 if (an) {
2677 en = CHILD(an, NCH(an) - 1);
2678 if (NCH(an) == 2) {
2679 is_from = 1;
2680 exp = ast_for_expr(c, en);
2681 }
2682 else
2683 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002684 if (!exp)
2685 return NULL;
2686 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002687 if (is_from)
2688 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2689 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002690 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002691 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 if (NCH(n) == 1) {
2693 n = CHILD(n, 0);
2694 goto loop;
2695 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002696 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002697 case power:
2698 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002700 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 return NULL;
2702 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002703 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 return NULL;
2705}
2706
2707static expr_ty
2708ast_for_call(struct compiling *c, const node *n, expr_ty func)
2709{
2710 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002711 arglist: argument (',' argument)* [',']
2712 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 */
2714
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002715 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002717 asdl_seq *args;
2718 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719
2720 REQ(n, arglist);
2721
2722 nargs = 0;
2723 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002725 node *ch = CHILD(n, i);
2726 if (TYPE(ch) == argument) {
2727 if (NCH(ch) == 1)
2728 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002729 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2730 nargs++;
2731 if (NCH(n) > 1) {
2732 ast_error(c, ch, "Generator expression must be parenthesized");
2733 return NULL;
2734 }
2735 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002736 else if (TYPE(CHILD(ch, 0)) == STAR)
2737 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002739 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002740 nkeywords++;
2741 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002744 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002746 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002747 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002749 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002750
2751 nargs = 0; /* positional arguments + iterable argument unpackings */
2752 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2753 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002755 node *ch = CHILD(n, i);
2756 if (TYPE(ch) == argument) {
2757 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002758 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002759 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002760 /* a positional argument */
2761 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002762 if (ndoublestars) {
2763 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002764 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002765 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002766 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002767 else {
2768 ast_error(c, chch,
2769 "positional argument follows "
2770 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002771 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002772 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002773 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002774 e = ast_for_expr(c, chch);
2775 if (!e)
2776 return NULL;
2777 asdl_seq_SET(args, nargs++, e);
2778 }
2779 else if (TYPE(chch) == STAR) {
2780 /* an iterable argument unpacking */
2781 expr_ty starred;
2782 if (ndoublestars) {
2783 ast_error(c, chch,
2784 "iterable argument unpacking follows "
2785 "keyword argument unpacking");
2786 return NULL;
2787 }
2788 e = ast_for_expr(c, CHILD(ch, 1));
2789 if (!e)
2790 return NULL;
2791 starred = Starred(e, Load, LINENO(chch),
2792 chch->n_col_offset,
2793 c->c_arena);
2794 if (!starred)
2795 return NULL;
2796 asdl_seq_SET(args, nargs++, starred);
2797
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002798 }
2799 else if (TYPE(chch) == DOUBLESTAR) {
2800 /* a keyword argument unpacking */
2801 keyword_ty kw;
2802 i++;
2803 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002805 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002806 kw = keyword(NULL, e, c->c_arena);
2807 asdl_seq_SET(keywords, nkeywords++, kw);
2808 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002810 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002811 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002812 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002814 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002815 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002817 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002818 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002819 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002820 identifier key, tmp;
2821 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002823 /* chch is test, but must be an identifier? */
2824 e = ast_for_expr(c, chch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 /* f(lambda x: x[0] = 3) ends up getting parsed with
2828 * LHS test = lambda x: x[0], and RHS test = 3.
2829 * SF bug 132313 points out that complaining about a keyword
2830 * then is very confusing.
2831 */
2832 if (e->kind == Lambda_kind) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002833 ast_error(c, chch,
2834 "lambda cannot contain assignment");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002835 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002836 }
2837 else if (e->kind != Name_kind) {
2838 ast_error(c, chch,
2839 "keyword can't be an expression");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002840 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002841 }
2842 else if (forbidden_name(c, e->v.Name.id, ch, 1)) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002843 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002845 key = e->v.Name.id;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002846 for (k = 0; k < nkeywords; k++) {
2847 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002848 if (tmp && !PyUnicode_Compare(tmp, key)) {
2849 ast_error(c, chch,
2850 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002851 return NULL;
2852 }
2853 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002854 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002856 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002857 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002859 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002860 asdl_seq_SET(keywords, nkeywords++, kw);
2861 }
2862 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 }
2864
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002865 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866}
2867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002869ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002871 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002872 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002874 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002876 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002877 }
2878 else {
2879 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002880 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002881 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002883 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 else {
2885 asdl_seq *tmp = seq_for_testlist(c, n);
2886 if (!tmp)
2887 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002888 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002890}
2891
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892static stmt_ty
2893ast_for_expr_stmt(struct compiling *c, const node *n)
2894{
2895 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002896 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2897 ('=' (yield_expr|testlist_star_expr))*)
2898 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002899 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002900 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002901 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002902 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 */
2904
2905 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002906 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 if (!e)
2908 return NULL;
2909
Thomas Wouters89f507f2006-12-13 04:49:30 +00002910 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911 }
2912 else if (TYPE(CHILD(n, 1)) == augassign) {
2913 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002914 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916
Thomas Wouters89f507f2006-12-13 04:49:30 +00002917 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918 if (!expr1)
2919 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002920 if(!set_context(c, expr1, Store, ch))
2921 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002922 /* set_context checks that most expressions are not the left side.
2923 Augmented assignments can only have a name, a subscript, or an
2924 attribute on the left, though, so we have to explicitly check for
2925 those. */
2926 switch (expr1->kind) {
2927 case Name_kind:
2928 case Attribute_kind:
2929 case Subscript_kind:
2930 break;
2931 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002932 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002933 return NULL;
2934 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935
Thomas Wouters89f507f2006-12-13 04:49:30 +00002936 ch = CHILD(n, 2);
2937 if (TYPE(ch) == testlist)
2938 expr2 = ast_for_testlist(c, ch);
2939 else
2940 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002941 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942 return NULL;
2943
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002944 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002945 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 return NULL;
2947
Thomas Wouters89f507f2006-12-13 04:49:30 +00002948 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002950 else if (TYPE(CHILD(n, 1)) == annassign) {
2951 expr_ty expr1, expr2, expr3;
2952 node *ch = CHILD(n, 0);
2953 node *deep, *ann = CHILD(n, 1);
2954 int simple = 1;
2955
2956 /* we keep track of parens to qualify (x) as expression not name */
2957 deep = ch;
2958 while (NCH(deep) == 1) {
2959 deep = CHILD(deep, 0);
2960 }
2961 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2962 simple = 0;
2963 }
2964 expr1 = ast_for_testlist(c, ch);
2965 if (!expr1) {
2966 return NULL;
2967 }
2968 switch (expr1->kind) {
2969 case Name_kind:
2970 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2971 return NULL;
2972 }
2973 expr1->v.Name.ctx = Store;
2974 break;
2975 case Attribute_kind:
2976 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2977 return NULL;
2978 }
2979 expr1->v.Attribute.ctx = Store;
2980 break;
2981 case Subscript_kind:
2982 expr1->v.Subscript.ctx = Store;
2983 break;
2984 case List_kind:
2985 ast_error(c, ch,
2986 "only single target (not list) can be annotated");
2987 return NULL;
2988 case Tuple_kind:
2989 ast_error(c, ch,
2990 "only single target (not tuple) can be annotated");
2991 return NULL;
2992 default:
2993 ast_error(c, ch,
2994 "illegal target for annotation");
2995 return NULL;
2996 }
2997
2998 if (expr1->kind != Name_kind) {
2999 simple = 0;
3000 }
3001 ch = CHILD(ann, 1);
3002 expr2 = ast_for_expr(c, ch);
3003 if (!expr2) {
3004 return NULL;
3005 }
3006 if (NCH(ann) == 2) {
3007 return AnnAssign(expr1, expr2, NULL, simple,
3008 LINENO(n), n->n_col_offset, c->c_arena);
3009 }
3010 else {
3011 ch = CHILD(ann, 3);
3012 expr3 = ast_for_expr(c, ch);
3013 if (!expr3) {
3014 return NULL;
3015 }
3016 return AnnAssign(expr1, expr2, expr3, simple,
3017 LINENO(n), n->n_col_offset, c->c_arena);
3018 }
3019 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003021 int i;
3022 asdl_seq *targets;
3023 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 expr_ty expression;
3025
Thomas Wouters89f507f2006-12-13 04:49:30 +00003026 /* a normal assignment */
3027 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003028 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003029 if (!targets)
3030 return NULL;
3031 for (i = 0; i < NCH(n) - 2; i += 2) {
3032 expr_ty e;
3033 node *ch = CHILD(n, i);
3034 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003035 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003036 return NULL;
3037 }
3038 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003042 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003043 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045
Thomas Wouters89f507f2006-12-13 04:49:30 +00003046 asdl_seq_SET(targets, i / 2, e);
3047 }
3048 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003049 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003050 expression = ast_for_testlist(c, value);
3051 else
3052 expression = ast_for_expr(c, value);
3053 if (!expression)
3054 return NULL;
3055 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057}
3058
Benjamin Peterson78565b22009-06-28 19:19:51 +00003059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003061ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062{
3063 asdl_seq *seq;
3064 int i;
3065 expr_ty e;
3066
3067 REQ(n, exprlist);
3068
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003069 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003071 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003073 e = ast_for_expr(c, CHILD(n, i));
3074 if (!e)
3075 return NULL;
3076 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003077 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003078 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 }
3080 return seq;
3081}
3082
3083static stmt_ty
3084ast_for_del_stmt(struct compiling *c, const node *n)
3085{
3086 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088 /* del_stmt: 'del' exprlist */
3089 REQ(n, del_stmt);
3090
3091 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3092 if (!expr_list)
3093 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003094 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095}
3096
3097static stmt_ty
3098ast_for_flow_stmt(struct compiling *c, const node *n)
3099{
3100 /*
3101 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3102 | yield_stmt
3103 break_stmt: 'break'
3104 continue_stmt: 'continue'
3105 return_stmt: 'return' [testlist]
3106 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003107 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 raise_stmt: 'raise' [test [',' test [',' test]]]
3109 */
3110 node *ch;
3111
3112 REQ(n, flow_stmt);
3113 ch = CHILD(n, 0);
3114 switch (TYPE(ch)) {
3115 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003116 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003118 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003120 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3121 if (!exp)
3122 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003123 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 }
3125 case return_stmt:
3126 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003127 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003129 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 if (!expression)
3131 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003132 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 }
3134 case raise_stmt:
3135 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003136 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3137 else if (NCH(ch) >= 2) {
3138 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3140 if (!expression)
3141 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003142 if (NCH(ch) == 4) {
3143 cause = ast_for_expr(c, CHILD(ch, 3));
3144 if (!cause)
3145 return NULL;
3146 }
3147 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 }
Stefan Krahf432a322017-08-21 13:09:59 +02003149 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003151 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 "unexpected flow_stmt: %d", TYPE(ch));
3153 return NULL;
3154 }
3155}
3156
3157static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003158alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159{
3160 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003161 import_as_name: NAME ['as' NAME]
3162 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 dotted_name: NAME ('.' NAME)*
3164 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003165 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003166
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 loop:
3168 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003169 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003170 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003171 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003173 if (!name)
3174 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003175 if (NCH(n) == 3) {
3176 node *str_node = CHILD(n, 2);
3177 str = NEW_IDENTIFIER(str_node);
3178 if (!str)
3179 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003180 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003181 return NULL;
3182 }
3183 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003184 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003185 return NULL;
3186 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003187 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003188 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 case dotted_as_name:
3190 if (NCH(n) == 1) {
3191 n = CHILD(n, 0);
3192 goto loop;
3193 }
3194 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003195 node *asname_node = CHILD(n, 2);
3196 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003197 if (!a)
3198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003200 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003201 if (!a->asname)
3202 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003203 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 return a;
3206 }
3207 break;
3208 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003209 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003210 node *name_node = CHILD(n, 0);
3211 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003212 if (!name)
3213 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003214 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003215 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003216 return alias(name, NULL, c->c_arena);
3217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218 else {
3219 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003220 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003221 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224
3225 len = 0;
3226 for (i = 0; i < NCH(n); i += 2)
3227 /* length of string plus one for the dot */
3228 len += strlen(STR(CHILD(n, i))) + 1;
3229 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003230 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 if (!str)
3232 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003233 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 if (!s)
3235 return NULL;
3236 for (i = 0; i < NCH(n); i += 2) {
3237 char *sch = STR(CHILD(n, i));
3238 strcpy(s, STR(CHILD(n, i)));
3239 s += strlen(sch);
3240 *s++ = '.';
3241 }
3242 --s;
3243 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3245 PyBytes_GET_SIZE(str),
3246 NULL);
3247 Py_DECREF(str);
3248 if (!uni)
3249 return NULL;
3250 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003251 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003252 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3253 Py_DECREF(str);
3254 return NULL;
3255 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003256 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257 }
3258 break;
3259 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003260 str = PyUnicode_InternFromString("*");
Victor Stinner43d81952013-07-17 00:57:58 +02003261 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3262 Py_DECREF(str);
3263 return NULL;
3264 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003265 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003267 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 "unexpected import name: %d", TYPE(n));
3269 return NULL;
3270 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003271
3272 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 return NULL;
3274}
3275
3276static stmt_ty
3277ast_for_import_stmt(struct compiling *c, const node *n)
3278{
3279 /*
3280 import_stmt: import_name | import_from
3281 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003282 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3283 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003285 int lineno;
3286 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 int i;
3288 asdl_seq *aliases;
3289
3290 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003291 lineno = LINENO(n);
3292 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003294 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003297 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 if (!aliases)
3299 return NULL;
3300 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003301 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003302 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003304 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003306 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003308 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003310 int idx, ndots = 0;
3311 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003312 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003314 /* Count the number of dots (for relative imports) and check for the
3315 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003316 for (idx = 1; idx < NCH(n); idx++) {
3317 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003318 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3319 if (!mod)
3320 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 idx++;
3322 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003323 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003325 ndots += 3;
3326 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003327 } else if (TYPE(CHILD(n, idx)) != DOT) {
3328 break;
3329 }
3330 ndots++;
3331 }
3332 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003333 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003334 case STAR:
3335 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003336 n = CHILD(n, idx);
3337 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 break;
3339 case LPAR:
3340 /* from ... import (x, y, z) */
3341 n = CHILD(n, idx + 1);
3342 n_children = NCH(n);
3343 break;
3344 case import_as_names:
3345 /* from ... import x, y, z */
3346 n = CHILD(n, idx);
3347 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003348 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003349 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350 " surrounding parentheses");
3351 return NULL;
3352 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353 break;
3354 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003355 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003356 return NULL;
3357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003359 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003360 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
3363 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003364 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003365 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003366 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003368 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003370 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003371 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003372 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003373 if (!import_alias)
3374 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003375 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003378 if (mod != NULL)
3379 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003380 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003381 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382 }
Neal Norwitz79792652005-11-14 04:25:03 +00003383 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 "unknown import statement: starts with command '%s'",
3385 STR(CHILD(n, 0)));
3386 return NULL;
3387}
3388
3389static stmt_ty
3390ast_for_global_stmt(struct compiling *c, const node *n)
3391{
3392 /* global_stmt: 'global' NAME (',' NAME)* */
3393 identifier name;
3394 asdl_seq *s;
3395 int i;
3396
3397 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003398 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003400 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402 name = NEW_IDENTIFIER(CHILD(n, i));
3403 if (!name)
3404 return NULL;
3405 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003407 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408}
3409
3410static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003411ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3412{
3413 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3414 identifier name;
3415 asdl_seq *s;
3416 int i;
3417
3418 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003419 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003420 if (!s)
3421 return NULL;
3422 for (i = 1; i < NCH(n); i += 2) {
3423 name = NEW_IDENTIFIER(CHILD(n, i));
3424 if (!name)
3425 return NULL;
3426 asdl_seq_SET(s, i / 2, name);
3427 }
3428 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3429}
3430
3431static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432ast_for_assert_stmt(struct compiling *c, const node *n)
3433{
3434 /* assert_stmt: 'assert' test [',' test] */
3435 REQ(n, assert_stmt);
3436 if (NCH(n) == 2) {
3437 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3438 if (!expression)
3439 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003440 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 }
3442 else if (NCH(n) == 4) {
3443 expr_ty expr1, expr2;
3444
3445 expr1 = ast_for_expr(c, CHILD(n, 1));
3446 if (!expr1)
3447 return NULL;
3448 expr2 = ast_for_expr(c, CHILD(n, 3));
3449 if (!expr2)
3450 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451
Thomas Wouters89f507f2006-12-13 04:49:30 +00003452 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 }
Neal Norwitz79792652005-11-14 04:25:03 +00003454 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 "improper number of parts to 'assert' statement: %d",
3456 NCH(n));
3457 return NULL;
3458}
3459
3460static asdl_seq *
3461ast_for_suite(struct compiling *c, const node *n)
3462{
3463 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003464 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465 stmt_ty s;
3466 int i, total, num, end, pos = 0;
3467 node *ch;
3468
3469 REQ(n, suite);
3470
3471 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003472 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003474 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 n = CHILD(n, 0);
3477 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003479 */
3480 end = NCH(n) - 1;
3481 if (TYPE(CHILD(n, end - 1)) == SEMI)
3482 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003484 for (i = 0; i < end; i += 2) {
3485 ch = CHILD(n, i);
3486 s = ast_for_stmt(c, ch);
3487 if (!s)
3488 return NULL;
3489 asdl_seq_SET(seq, pos++, s);
3490 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491 }
3492 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003493 for (i = 2; i < (NCH(n) - 1); i++) {
3494 ch = CHILD(n, i);
3495 REQ(ch, stmt);
3496 num = num_stmts(ch);
3497 if (num == 1) {
3498 /* small_stmt or compound_stmt with only one child */
3499 s = ast_for_stmt(c, ch);
3500 if (!s)
3501 return NULL;
3502 asdl_seq_SET(seq, pos++, s);
3503 }
3504 else {
3505 int j;
3506 ch = CHILD(ch, 0);
3507 REQ(ch, simple_stmt);
3508 for (j = 0; j < NCH(ch); j += 2) {
3509 /* statement terminates with a semi-colon ';' */
3510 if (NCH(CHILD(ch, j)) == 0) {
3511 assert((j + 1) == NCH(ch));
3512 break;
3513 }
3514 s = ast_for_stmt(c, CHILD(ch, j));
3515 if (!s)
3516 return NULL;
3517 asdl_seq_SET(seq, pos++, s);
3518 }
3519 }
3520 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521 }
3522 assert(pos == seq->size);
3523 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524}
3525
INADA Naokicb41b272017-02-23 00:31:59 +09003526static string
3527docstring_from_stmts(asdl_seq *stmts)
3528{
3529 if (stmts && stmts->size) {
3530 stmt_ty s = (stmt_ty)asdl_seq_GET(stmts, 0);
3531 /* If first statement is a literal string, it's the doc string. */
3532 if (s->kind == Expr_kind && s->v.Expr.value->kind == Str_kind) {
3533 string doc = s->v.Expr.value->v.Str.s;
3534 /* not very efficient, but simple */
3535 memmove(&asdl_seq_GET(stmts, 0), &asdl_seq_GET(stmts, 1),
3536 (stmts->size - 1) * sizeof(void*));
3537 stmts->size--;
3538 return doc;
3539 }
3540 }
3541 return NULL;
3542}
3543
3544static asdl_seq *
3545ast_for_body(struct compiling *c, const node *n, string *docstring)
3546{
3547 asdl_seq *stmts = ast_for_suite(c, n);
3548 *docstring = docstring_from_stmts(stmts);
3549 return stmts;
3550}
3551
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552static stmt_ty
3553ast_for_if_stmt(struct compiling *c, const node *n)
3554{
3555 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3556 ['else' ':' suite]
3557 */
3558 char *s;
3559
3560 REQ(n, if_stmt);
3561
3562 if (NCH(n) == 4) {
3563 expr_ty expression;
3564 asdl_seq *suite_seq;
3565
3566 expression = ast_for_expr(c, CHILD(n, 1));
3567 if (!expression)
3568 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003570 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572
Guido van Rossumd8faa362007-04-27 19:54:29 +00003573 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3574 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003576
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 s = STR(CHILD(n, 4));
3578 /* s[2], the third character in the string, will be
3579 's' for el_s_e, or
3580 'i' for el_i_f
3581 */
3582 if (s[2] == 's') {
3583 expr_ty expression;
3584 asdl_seq *seq1, *seq2;
3585
3586 expression = ast_for_expr(c, CHILD(n, 1));
3587 if (!expression)
3588 return NULL;
3589 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003590 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 return NULL;
3592 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003593 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 return NULL;
3595
Guido van Rossumd8faa362007-04-27 19:54:29 +00003596 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3597 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 }
3599 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003600 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003601 expr_ty expression;
3602 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 asdl_seq *orelse = NULL;
3604 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 /* must reference the child n_elif+1 since 'else' token is third,
3606 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003607 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3608 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3609 has_else = 1;
3610 n_elif -= 3;
3611 }
3612 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613
Thomas Wouters89f507f2006-12-13 04:49:30 +00003614 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003615 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003617 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003618 if (!orelse)
3619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003621 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003623 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3624 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003626 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3627 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 asdl_seq_SET(orelse, 0,
3631 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003632 LINENO(CHILD(n, NCH(n) - 6)),
3633 CHILD(n, NCH(n) - 6)->n_col_offset,
3634 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003635 /* the just-created orelse handled the last elif */
3636 n_elif--;
3637 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638
Thomas Wouters89f507f2006-12-13 04:49:30 +00003639 for (i = 0; i < n_elif; i++) {
3640 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003641 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003642 if (!newobj)
3643 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003645 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003648 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650
Thomas Wouters89f507f2006-12-13 04:49:30 +00003651 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003653 LINENO(CHILD(n, off)),
3654 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003655 orelse = newobj;
3656 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003657 expression = ast_for_expr(c, CHILD(n, 1));
3658 if (!expression)
3659 return NULL;
3660 suite_seq = ast_for_suite(c, CHILD(n, 3));
3661 if (!suite_seq)
3662 return NULL;
3663 return If(expression, suite_seq, orelse,
3664 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003666
3667 PyErr_Format(PyExc_SystemError,
3668 "unexpected token in 'if' statement: %s", s);
3669 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670}
3671
3672static stmt_ty
3673ast_for_while_stmt(struct compiling *c, const node *n)
3674{
3675 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3676 REQ(n, while_stmt);
3677
3678 if (NCH(n) == 4) {
3679 expr_ty expression;
3680 asdl_seq *suite_seq;
3681
3682 expression = ast_for_expr(c, CHILD(n, 1));
3683 if (!expression)
3684 return NULL;
3685 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003686 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003688 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689 }
3690 else if (NCH(n) == 7) {
3691 expr_ty expression;
3692 asdl_seq *seq1, *seq2;
3693
3694 expression = ast_for_expr(c, CHILD(n, 1));
3695 if (!expression)
3696 return NULL;
3697 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003698 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 return NULL;
3700 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003701 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 return NULL;
3703
Thomas Wouters89f507f2006-12-13 04:49:30 +00003704 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003706
3707 PyErr_Format(PyExc_SystemError,
3708 "wrong number of tokens for 'while' statement: %d",
3709 NCH(n));
3710 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711}
3712
3713static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003714ast_for_for_stmt(struct compiling *c, const node *n, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715{
Neal Norwitz84456bd2005-12-18 03:16:20 +00003716 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003718 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003719 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3721 REQ(n, for_stmt);
3722
3723 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003724 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 if (!seq)
3726 return NULL;
3727 }
3728
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003729 node_target = CHILD(n, 1);
3730 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003731 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003733 /* Check the # of children rather than the length of _target, since
3734 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003735 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003736 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003737 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003739 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003741 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003742 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743 return NULL;
3744 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003745 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746 return NULL;
3747
Yury Selivanov75445082015-05-11 22:57:16 -04003748 if (is_async)
3749 return AsyncFor(target, expression, suite_seq, seq,
3750 LINENO(n), n->n_col_offset,
3751 c->c_arena);
3752 else
3753 return For(target, expression, suite_seq, seq,
3754 LINENO(n), n->n_col_offset,
3755 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756}
3757
3758static excepthandler_ty
3759ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3760{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003761 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 REQ(exc, except_clause);
3763 REQ(body, suite);
3764
3765 if (NCH(exc) == 1) {
3766 asdl_seq *suite_seq = ast_for_suite(c, body);
3767 if (!suite_seq)
3768 return NULL;
3769
Neal Norwitzad74aa82008-03-31 05:14:30 +00003770 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003771 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 }
3773 else if (NCH(exc) == 2) {
3774 expr_ty expression;
3775 asdl_seq *suite_seq;
3776
3777 expression = ast_for_expr(c, CHILD(exc, 1));
3778 if (!expression)
3779 return NULL;
3780 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003781 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 return NULL;
3783
Neal Norwitzad74aa82008-03-31 05:14:30 +00003784 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003785 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 }
3787 else if (NCH(exc) == 4) {
3788 asdl_seq *suite_seq;
3789 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003790 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003791 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003793 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003796 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797 return NULL;
3798 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003799 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 return NULL;
3801
Neal Norwitzad74aa82008-03-31 05:14:30 +00003802 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003803 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003805
3806 PyErr_Format(PyExc_SystemError,
3807 "wrong number of children for 'except' clause: %d",
3808 NCH(exc));
3809 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810}
3811
3812static stmt_ty
3813ast_for_try_stmt(struct compiling *c, const node *n)
3814{
Neal Norwitzf599f422005-12-17 21:33:47 +00003815 const int nch = NCH(n);
3816 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003817 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 REQ(n, try_stmt);
3820
Neal Norwitzf599f422005-12-17 21:33:47 +00003821 body = ast_for_suite(c, CHILD(n, 2));
3822 if (body == NULL)
3823 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824
Neal Norwitzf599f422005-12-17 21:33:47 +00003825 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3826 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3827 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3828 /* we can assume it's an "else",
3829 because nch >= 9 for try-else-finally and
3830 it would otherwise have a type of except_clause */
3831 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3832 if (orelse == NULL)
3833 return NULL;
3834 n_except--;
3835 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836
Neal Norwitzf599f422005-12-17 21:33:47 +00003837 finally = ast_for_suite(c, CHILD(n, nch - 1));
3838 if (finally == NULL)
3839 return NULL;
3840 n_except--;
3841 }
3842 else {
3843 /* we can assume it's an "else",
3844 otherwise it would have a type of except_clause */
3845 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3846 if (orelse == NULL)
3847 return NULL;
3848 n_except--;
3849 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003851 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003852 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 return NULL;
3854 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855
Neal Norwitzf599f422005-12-17 21:33:47 +00003856 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003857 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003858 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003859 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003860 if (handlers == NULL)
3861 return NULL;
3862
3863 for (i = 0; i < n_except; i++) {
3864 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3865 CHILD(n, 5 + i * 3));
3866 if (!e)
3867 return NULL;
3868 asdl_seq_SET(handlers, i, e);
3869 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003870 }
3871
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003872 assert(finally != NULL || asdl_seq_LEN(handlers));
3873 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003874}
3875
Georg Brandl0c315622009-05-25 21:10:36 +00003876/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003877static withitem_ty
3878ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003879{
3880 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003881
Georg Brandl0c315622009-05-25 21:10:36 +00003882 REQ(n, with_item);
3883 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003884 if (!context_expr)
3885 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003886 if (NCH(n) == 3) {
3887 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003888
3889 if (!optional_vars) {
3890 return NULL;
3891 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003892 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003893 return NULL;
3894 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003895 }
3896
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003897 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003898}
3899
Georg Brandl0c315622009-05-25 21:10:36 +00003900/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3901static stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04003902ast_for_with_stmt(struct compiling *c, const node *n, int is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003903{
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003904 int i, n_items;
3905 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003906
3907 REQ(n, with_stmt);
3908
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003909 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003910 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003911 if (!items)
3912 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003913 for (i = 1; i < NCH(n) - 2; i += 2) {
3914 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3915 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003916 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003917 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003918 }
3919
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003920 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3921 if (!body)
3922 return NULL;
3923
Yury Selivanov75445082015-05-11 22:57:16 -04003924 if (is_async)
3925 return AsyncWith(items, body, LINENO(n), n->n_col_offset, c->c_arena);
3926 else
3927 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003928}
3929
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003931ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003933 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003934 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003935 asdl_seq *s;
INADA Naokicb41b272017-02-23 00:31:59 +09003936 string docstring;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003937 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003938
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 REQ(n, classdef);
3940
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003941 if (NCH(n) == 4) { /* class NAME ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003942 s = ast_for_body(c, CHILD(n, 3), &docstring);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 if (!s)
3944 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003945 classname = NEW_IDENTIFIER(CHILD(n, 1));
3946 if (!classname)
3947 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003948 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003949 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003950 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3951 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003953
3954 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
INADA Naokicb41b272017-02-23 00:31:59 +09003955 s = ast_for_body(c, CHILD(n, 5), &docstring);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003956 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003957 return NULL;
3958 classname = NEW_IDENTIFIER(CHILD(n, 1));
3959 if (!classname)
3960 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003961 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003962 return NULL;
INADA Naokicb41b272017-02-23 00:31:59 +09003963 return ClassDef(classname, NULL, NULL, s, decorator_seq, docstring,
3964 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 }
3966
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003967 /* class NAME '(' arglist ')' ':' suite */
3968 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003969 {
3970 PyObject *dummy_name;
3971 expr_ty dummy;
3972 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3973 if (!dummy_name)
3974 return NULL;
3975 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
3976 call = ast_for_call(c, CHILD(n, 3), dummy);
3977 if (!call)
3978 return NULL;
3979 }
INADA Naokicb41b272017-02-23 00:31:59 +09003980 s = ast_for_body(c, CHILD(n, 6), &docstring);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003981 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003983 classname = NEW_IDENTIFIER(CHILD(n, 1));
3984 if (!classname)
3985 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003986 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003987 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003988
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003989 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
INADA Naokicb41b272017-02-23 00:31:59 +09003990 decorator_seq, docstring, LINENO(n), n->n_col_offset,
3991 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992}
3993
3994static stmt_ty
3995ast_for_stmt(struct compiling *c, const node *n)
3996{
3997 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003998 assert(NCH(n) == 1);
3999 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000 }
4001 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004002 assert(num_stmts(n) == 1);
4003 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 }
4005 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004006 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004007 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4008 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004009 */
4010 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 case expr_stmt:
4012 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 case del_stmt:
4014 return ast_for_del_stmt(c, n);
4015 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00004016 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 case flow_stmt:
4018 return ast_for_flow_stmt(c, n);
4019 case import_stmt:
4020 return ast_for_import_stmt(c, n);
4021 case global_stmt:
4022 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004023 case nonlocal_stmt:
4024 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 case assert_stmt:
4026 return ast_for_assert_stmt(c, n);
4027 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004028 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4030 TYPE(n), NCH(n));
4031 return NULL;
4032 }
4033 }
4034 else {
4035 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004036 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004037 */
4038 node *ch = CHILD(n, 0);
4039 REQ(n, compound_stmt);
4040 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 case if_stmt:
4042 return ast_for_if_stmt(c, ch);
4043 case while_stmt:
4044 return ast_for_while_stmt(c, ch);
4045 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004046 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004047 case try_stmt:
4048 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004049 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004050 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004052 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004053 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004054 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 case decorated:
4056 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004057 case async_stmt:
4058 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004060 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004061 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4062 TYPE(n), NCH(n));
4063 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004064 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065 }
4066}
4067
4068static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004069parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004071 const char *end;
4072 long x;
4073 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004074 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004075 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004077 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004078 errno = 0;
4079 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004080 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004081 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004082 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004083 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004084 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004085 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004086 }
4087 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004088 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004089 if (*end == '\0') {
4090 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004091 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004092 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004093 }
4094 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004095 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004096 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004097 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4098 if (compl.imag == -1.0 && PyErr_Occurred())
4099 return NULL;
4100 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004101 }
4102 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004103 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004104 dx = PyOS_string_to_double(s, NULL, NULL);
4105 if (dx == -1.0 && PyErr_Occurred())
4106 return NULL;
4107 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004108 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004109}
4110
4111static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004112parsenumber(struct compiling *c, const char *s)
4113{
4114 char *dup, *end;
4115 PyObject *res = NULL;
4116
4117 assert(s != NULL);
4118
4119 if (strchr(s, '_') == NULL) {
4120 return parsenumber_raw(c, s);
4121 }
4122 /* Create a duplicate without underscores. */
4123 dup = PyMem_Malloc(strlen(s) + 1);
4124 end = dup;
4125 for (; *s; s++) {
4126 if (*s != '_') {
4127 *end++ = *s;
4128 }
4129 }
4130 *end = '\0';
4131 res = parsenumber_raw(c, dup);
4132 PyMem_Free(dup);
4133 return res;
4134}
4135
4136static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004137decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004138{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004139 const char *s, *t;
4140 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004141 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4142 while (s < end && (*s & 0x80)) s++;
4143 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004144 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145}
4146
Eric V. Smith56466482016-10-31 14:46:26 -04004147static int
4148warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004149 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004150{
4151 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4152 first_invalid_escape_char);
4153 if (msg == NULL) {
4154 return -1;
4155 }
4156 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg,
4157 c->c_filename, LINENO(n),
4158 NULL, NULL) < 0 &&
4159 PyErr_ExceptionMatches(PyExc_DeprecationWarning))
4160 {
Victor Stinnerf9cca362016-11-15 09:12:10 +01004161 const char *s;
4162
4163 /* Replace the DeprecationWarning exception with a SyntaxError
4164 to get a more accurate error report */
4165 PyErr_Clear();
4166
4167 s = PyUnicode_AsUTF8(msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004168 if (s != NULL) {
4169 ast_error(c, n, s);
4170 }
4171 Py_DECREF(msg);
4172 return -1;
4173 }
4174 Py_DECREF(msg);
4175 return 0;
4176}
4177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004179decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4180 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004182 PyObject *v, *u;
4183 char *buf;
4184 char *p;
4185 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004186
Benjamin Peterson202803a2016-02-25 22:34:45 -08004187 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004188 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004189 return NULL;
4190 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4191 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4192 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4193 if (u == NULL)
4194 return NULL;
4195 p = buf = PyBytes_AsString(u);
4196 end = s + len;
4197 while (s < end) {
4198 if (*s == '\\') {
4199 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004200 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004201 strcpy(p, "u005c");
4202 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004203 if (s >= end)
4204 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004205 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004206 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004207 if (*s & 0x80) { /* XXX inefficient */
4208 PyObject *w;
4209 int kind;
4210 void *data;
4211 Py_ssize_t len, i;
4212 w = decode_utf8(c, &s, end);
4213 if (w == NULL) {
4214 Py_DECREF(u);
4215 return NULL;
4216 }
4217 kind = PyUnicode_KIND(w);
4218 data = PyUnicode_DATA(w);
4219 len = PyUnicode_GET_LENGTH(w);
4220 for (i = 0; i < len; i++) {
4221 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4222 sprintf(p, "\\U%08x", chr);
4223 p += 10;
4224 }
4225 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004226 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004227 Py_DECREF(w);
4228 } else {
4229 *p++ = *s++;
4230 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004231 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004232 len = p - buf;
4233 s = buf;
4234
Eric V. Smith56466482016-10-31 14:46:26 -04004235 const char *first_invalid_escape;
4236 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4237
4238 if (v != NULL && first_invalid_escape != NULL) {
4239 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4240 /* We have not decref u before because first_invalid_escape points
4241 inside u. */
4242 Py_XDECREF(u);
4243 Py_DECREF(v);
4244 return NULL;
4245 }
4246 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004247 Py_XDECREF(u);
4248 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004249}
4250
Eric V. Smith56466482016-10-31 14:46:26 -04004251static PyObject *
4252decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4253 size_t len)
4254{
4255 const char *first_invalid_escape;
4256 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4257 &first_invalid_escape);
4258 if (result == NULL)
4259 return NULL;
4260
4261 if (first_invalid_escape != NULL) {
4262 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4263 Py_DECREF(result);
4264 return NULL;
4265 }
4266 }
4267 return result;
4268}
4269
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004270/* Shift locations for the given node and all its children by adding `lineno`
4271 and `col_offset` to existing locations. */
4272static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4273{
4274 n->n_col_offset = n->n_col_offset + col_offset;
4275 for (int i = 0; i < NCH(n); ++i) {
4276 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4277 /* Shifting column offsets unnecessary if there's been newlines. */
4278 col_offset = 0;
4279 }
4280 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4281 }
4282 n->n_lineno = n->n_lineno + lineno;
4283}
4284
4285/* Fix locations for the given node and its children.
4286
4287 `parent` is the enclosing node.
4288 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004289 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004290*/
4291static void
4292fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4293{
4294 char *substr = NULL;
4295 char *start;
4296 int lines = LINENO(parent) - 1;
4297 int cols = parent->n_col_offset;
4298 /* Find the full fstring to fix location information in `n`. */
4299 while (parent && parent->n_type != STRING)
4300 parent = parent->n_child;
4301 if (parent && parent->n_str) {
4302 substr = strstr(parent->n_str, expr_str);
4303 if (substr) {
4304 start = substr;
4305 while (start > parent->n_str) {
4306 if (start[0] == '\n')
4307 break;
4308 start--;
4309 }
4310 cols += substr - start;
4311 /* Fix lineno in mulitline strings. */
4312 while ((substr = strchr(substr + 1, '\n')))
4313 lines--;
4314 }
4315 }
4316 fstring_shift_node_locations(n, lines, cols);
4317}
4318
Eric V. Smith451d0e32016-09-09 21:56:20 -04004319/* Compile this expression in to an expr_ty. Add parens around the
4320 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004321static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004322fstring_compile_expr(const char *expr_start, const char *expr_end,
4323 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004324
Eric V. Smith235a6f02015-09-19 14:51:32 -04004325{
4326 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004327 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004328 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004329 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004330 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004331 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004332
Eric V. Smith1d44c412015-09-23 07:49:00 -04004333 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004334 assert(*(expr_start-1) == '{');
4335 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004336
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004337 /* If the substring is all whitespace, it's an error. We need to catch this
4338 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4339 because turning the expression '' in to '()' would go from being invalid
4340 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004341 for (s = expr_start; s != expr_end; s++) {
4342 char c = *s;
4343 /* The Python parser ignores only the following whitespace
4344 characters (\r already is converted to \n). */
4345 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004346 break;
4347 }
4348 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004349 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004350 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004351 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352 }
4353
Eric V. Smith451d0e32016-09-09 21:56:20 -04004354 len = expr_end - expr_start;
4355 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4356 str = PyMem_RawMalloc(len + 3);
4357 if (str == NULL)
4358 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004359
Eric V. Smith451d0e32016-09-09 21:56:20 -04004360 str[0] = '(';
4361 memcpy(str+1, expr_start, len);
4362 str[len+1] = ')';
4363 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004364
4365 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004366 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4367 Py_eval_input, 0);
4368 if (!mod_n) {
4369 PyMem_RawFree(str);
4370 return NULL;
4371 }
4372 /* Reuse str to find the correct column offset. */
4373 str[0] = '{';
4374 str[len+1] = '}';
4375 fstring_fix_node_location(n, mod_n, str);
4376 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004377 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004378 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004379 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004380 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004381 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004382}
4383
4384/* Return -1 on error.
4385
4386 Return 0 if we reached the end of the literal.
4387
4388 Return 1 if we haven't reached the end of the literal, but we want
4389 the caller to process the literal up to this point. Used for
4390 doubled braces.
4391*/
4392static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004393fstring_find_literal(const char **str, const char *end, int raw,
4394 PyObject **literal, int recurse_lvl,
4395 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004396{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004397 /* Get any literal string. It ends when we hit an un-doubled left
4398 brace (which isn't part of a unicode name escape such as
4399 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004400
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004401 const char *s = *str;
4402 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004403 int result = 0;
4404
Eric V. Smith235a6f02015-09-19 14:51:32 -04004405 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004406 while (s < end) {
4407 char ch = *s++;
4408 if (!raw && ch == '\\' && s < end) {
4409 ch = *s++;
4410 if (ch == 'N') {
4411 if (s < end && *s++ == '{') {
4412 while (s < end && *s++ != '}') {
4413 }
4414 continue;
4415 }
4416 break;
4417 }
4418 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4419 return -1;
4420 }
4421 }
4422 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004423 /* Check for doubled braces, but only at the top level. If
4424 we checked at every level, then f'{0:{3}}' would fail
4425 with the two closing braces. */
4426 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004427 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004428 /* We're going to tell the caller that the literal ends
4429 here, but that they should continue scanning. But also
4430 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004431 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004432 result = 1;
4433 goto done;
4434 }
4435
4436 /* Where a single '{' is the start of a new expression, a
4437 single '}' is not allowed. */
4438 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004439 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004440 ast_error(c, n, "f-string: single '}' is not allowed");
4441 return -1;
4442 }
4443 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004444 /* We're either at a '{', which means we're starting another
4445 expression; or a '}', which means we're at the end of this
4446 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004447 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004448 break;
4449 }
4450 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004451 *str = s;
4452 assert(s <= end);
4453 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004454done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004455 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004456 if (raw)
4457 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004458 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004459 NULL, NULL);
4460 else
Eric V. Smith56466482016-10-31 14:46:26 -04004461 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004462 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004463 if (!*literal)
4464 return -1;
4465 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004466 return result;
4467}
4468
4469/* Forward declaration because parsing is recursive. */
4470static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004471fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004472 struct compiling *c, const node *n);
4473
Eric V. Smith451d0e32016-09-09 21:56:20 -04004474/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004475 expression (so it must be a '{'). Returns the FormattedValue node,
4476 which includes the expression, conversion character, and
4477 format_spec expression.
4478
4479 Note that I don't do a perfect job here: I don't make sure that a
4480 closing brace doesn't match an opening paren, for example. It
4481 doesn't need to error on all invalid expressions, just correctly
4482 find the end of all valid ones. Any errors inside the expression
4483 will be caught when we parse it later. */
4484static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004485fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004486 expr_ty *expression, struct compiling *c, const node *n)
4487{
4488 /* Return -1 on error, else 0. */
4489
Eric V. Smith451d0e32016-09-09 21:56:20 -04004490 const char *expr_start;
4491 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004492 expr_ty simple_expression;
4493 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004494 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004495
4496 /* 0 if we're not in a string, else the quote char we're trying to
4497 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004498 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004499
4500 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4501 int string_type = 0;
4502
4503 /* Keep track of nesting level for braces/parens/brackets in
4504 expressions. */
4505 Py_ssize_t nested_depth = 0;
4506
4507 /* Can only nest one level deep. */
4508 if (recurse_lvl >= 2) {
4509 ast_error(c, n, "f-string: expressions nested too deeply");
4510 return -1;
4511 }
4512
4513 /* The first char must be a left brace, or we wouldn't have gotten
4514 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004515 assert(**str == '{');
4516 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004517
Eric V. Smith451d0e32016-09-09 21:56:20 -04004518 expr_start = *str;
4519 for (; *str < end; (*str)++) {
4520 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004521
4522 /* Loop invariants. */
4523 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004524 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004525 if (quote_char)
4526 assert(string_type == 1 || string_type == 3);
4527 else
4528 assert(string_type == 0);
4529
Eric V. Smith451d0e32016-09-09 21:56:20 -04004530 ch = **str;
4531 /* Nowhere inside an expression is a backslash allowed. */
4532 if (ch == '\\') {
4533 /* Error: can't include a backslash character, inside
4534 parens or strings or not. */
4535 ast_error(c, n, "f-string expression part "
4536 "cannot include a backslash");
4537 return -1;
4538 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004539 if (quote_char) {
4540 /* We're inside a string. See if we're at the end. */
4541 /* This code needs to implement the same non-error logic
4542 as tok_get from tokenizer.c, at the letter_quote
4543 label. To actually share that code would be a
4544 nightmare. But, it's unlikely to change and is small,
4545 so duplicate it here. Note we don't need to catch all
4546 of the errors, since they'll be caught when parsing the
4547 expression. We just need to match the non-error
4548 cases. Thus we can ignore \n in single-quoted strings,
4549 for example. Or non-terminated strings. */
4550 if (ch == quote_char) {
4551 /* Does this match the string_type (single or triple
4552 quoted)? */
4553 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004554 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004555 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004556 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004557 string_type = 0;
4558 quote_char = 0;
4559 continue;
4560 }
4561 } else {
4562 /* We're at the end of a normal string. */
4563 quote_char = 0;
4564 string_type = 0;
4565 continue;
4566 }
4567 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004568 } else if (ch == '\'' || ch == '"') {
4569 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004570 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004571 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004572 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004573 } else {
4574 /* Start of a normal string. */
4575 string_type = 1;
4576 }
4577 /* Start looking for the end of the string. */
4578 quote_char = ch;
4579 } else if (ch == '[' || ch == '{' || ch == '(') {
4580 nested_depth++;
4581 } else if (nested_depth != 0 &&
4582 (ch == ']' || ch == '}' || ch == ')')) {
4583 nested_depth--;
4584 } else if (ch == '#') {
4585 /* Error: can't include a comment character, inside parens
4586 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004587 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004588 return -1;
4589 } else if (nested_depth == 0 &&
4590 (ch == '!' || ch == ':' || ch == '}')) {
4591 /* First, test for the special case of "!=". Since '=' is
4592 not an allowed conversion character, nothing is lost in
4593 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004594 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004595 /* This isn't a conversion character, just continue. */
4596 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004597 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004598 /* Normal way out of this loop. */
4599 break;
4600 } else {
4601 /* Just consume this char and loop around. */
4602 }
4603 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004604 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004605 /* If we leave this loop in a string or with mismatched parens, we
4606 don't care. We'll get a syntax error when compiling the
4607 expression. But, we can produce a better error message, so
4608 let's just do that.*/
4609 if (quote_char) {
4610 ast_error(c, n, "f-string: unterminated string");
4611 return -1;
4612 }
4613 if (nested_depth) {
4614 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4615 return -1;
4616 }
4617
Eric V. Smith451d0e32016-09-09 21:56:20 -04004618 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004619 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004620
4621 /* Compile the expression as soon as possible, so we show errors
4622 related to the expression before errors related to the
4623 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004624 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004625 if (!simple_expression)
4626 return -1;
4627
4628 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004629 if (**str == '!') {
4630 *str += 1;
4631 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004632 goto unexpected_end_of_string;
4633
Eric V. Smith451d0e32016-09-09 21:56:20 -04004634 conversion = **str;
4635 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004636
4637 /* Validate the conversion. */
4638 if (!(conversion == 's' || conversion == 'r'
4639 || conversion == 'a')) {
4640 ast_error(c, n, "f-string: invalid conversion character: "
4641 "expected 's', 'r', or 'a'");
4642 return -1;
4643 }
4644 }
4645
4646 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004647 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004648 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004649 if (**str == ':') {
4650 *str += 1;
4651 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004652 goto unexpected_end_of_string;
4653
4654 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004655 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004656 if (!format_spec)
4657 return -1;
4658 }
4659
Eric V. Smith451d0e32016-09-09 21:56:20 -04004660 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004661 goto unexpected_end_of_string;
4662
4663 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004664 assert(*str < end);
4665 assert(**str == '}');
4666 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004667
Eric V. Smith451d0e32016-09-09 21:56:20 -04004668 /* And now create the FormattedValue node that represents this
4669 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004670 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004671 format_spec, LINENO(n), n->n_col_offset,
4672 c->c_arena);
4673 if (!*expression)
4674 return -1;
4675
4676 return 0;
4677
4678unexpected_end_of_string:
4679 ast_error(c, n, "f-string: expecting '}'");
4680 return -1;
4681}
4682
4683/* Return -1 on error.
4684
4685 Return 0 if we have a literal (possible zero length) and an
4686 expression (zero length if at the end of the string.
4687
4688 Return 1 if we have a literal, but no expression, and we want the
4689 caller to call us again. This is used to deal with doubled
4690 braces.
4691
4692 When called multiple times on the string 'a{{b{0}c', this function
4693 will return:
4694
4695 1. the literal 'a{' with no expression, and a return value
4696 of 1. Despite the fact that there's no expression, the return
4697 value of 1 means we're not finished yet.
4698
4699 2. the literal 'b' and the expression '0', with a return value of
4700 0. The fact that there's an expression means we're not finished.
4701
4702 3. literal 'c' with no expression and a return value of 0. The
4703 combination of the return value of 0 with no expression means
4704 we're finished.
4705*/
4706static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004707fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4708 int recurse_lvl, PyObject **literal,
4709 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004710 struct compiling *c, const node *n)
4711{
4712 int result;
4713
4714 assert(*literal == NULL && *expression == NULL);
4715
4716 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004717 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004718 if (result < 0)
4719 goto error;
4720
4721 assert(result == 0 || result == 1);
4722
4723 if (result == 1)
4724 /* We have a literal, but don't look at the expression. */
4725 return 1;
4726
Eric V. Smith451d0e32016-09-09 21:56:20 -04004727 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004728 /* We're at the end of the string or the end of a nested
4729 f-string: no expression. The top-level error case where we
4730 expect to be at the end of the string but we're at a '}' is
4731 handled later. */
4732 return 0;
4733
4734 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004735 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004736
Eric V. Smith451d0e32016-09-09 21:56:20 -04004737 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004738 goto error;
4739
4740 return 0;
4741
4742error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004743 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004744 return -1;
4745}
4746
4747#define EXPRLIST_N_CACHED 64
4748
4749typedef struct {
4750 /* Incrementally build an array of expr_ty, so be used in an
4751 asdl_seq. Cache some small but reasonably sized number of
4752 expr_ty's, and then after that start dynamically allocating,
4753 doubling the number allocated each time. Note that the f-string
4754 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
4755 Str for the literal 'a'. So you add expr_ty's about twice as
4756 fast as you add exressions in an f-string. */
4757
4758 Py_ssize_t allocated; /* Number we've allocated. */
4759 Py_ssize_t size; /* Number we've used. */
4760 expr_ty *p; /* Pointer to the memory we're actually
4761 using. Will point to 'data' until we
4762 start dynamically allocating. */
4763 expr_ty data[EXPRLIST_N_CACHED];
4764} ExprList;
4765
4766#ifdef NDEBUG
4767#define ExprList_check_invariants(l)
4768#else
4769static void
4770ExprList_check_invariants(ExprList *l)
4771{
4772 /* Check our invariants. Make sure this object is "live", and
4773 hasn't been deallocated. */
4774 assert(l->size >= 0);
4775 assert(l->p != NULL);
4776 if (l->size <= EXPRLIST_N_CACHED)
4777 assert(l->data == l->p);
4778}
4779#endif
4780
4781static void
4782ExprList_Init(ExprList *l)
4783{
4784 l->allocated = EXPRLIST_N_CACHED;
4785 l->size = 0;
4786
4787 /* Until we start allocating dynamically, p points to data. */
4788 l->p = l->data;
4789
4790 ExprList_check_invariants(l);
4791}
4792
4793static int
4794ExprList_Append(ExprList *l, expr_ty exp)
4795{
4796 ExprList_check_invariants(l);
4797 if (l->size >= l->allocated) {
4798 /* We need to alloc (or realloc) the memory. */
4799 Py_ssize_t new_size = l->allocated * 2;
4800
4801 /* See if we've ever allocated anything dynamically. */
4802 if (l->p == l->data) {
4803 Py_ssize_t i;
4804 /* We're still using the cached data. Switch to
4805 alloc-ing. */
4806 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4807 if (!l->p)
4808 return -1;
4809 /* Copy the cached data into the new buffer. */
4810 for (i = 0; i < l->size; i++)
4811 l->p[i] = l->data[i];
4812 } else {
4813 /* Just realloc. */
4814 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4815 if (!tmp) {
4816 PyMem_RawFree(l->p);
4817 l->p = NULL;
4818 return -1;
4819 }
4820 l->p = tmp;
4821 }
4822
4823 l->allocated = new_size;
4824 assert(l->allocated == 2 * l->size);
4825 }
4826
4827 l->p[l->size++] = exp;
4828
4829 ExprList_check_invariants(l);
4830 return 0;
4831}
4832
4833static void
4834ExprList_Dealloc(ExprList *l)
4835{
4836 ExprList_check_invariants(l);
4837
4838 /* If there's been an error, or we've never dynamically allocated,
4839 do nothing. */
4840 if (!l->p || l->p == l->data) {
4841 /* Do nothing. */
4842 } else {
4843 /* We have dynamically allocated. Free the memory. */
4844 PyMem_RawFree(l->p);
4845 }
4846 l->p = NULL;
4847 l->size = -1;
4848}
4849
4850static asdl_seq *
4851ExprList_Finish(ExprList *l, PyArena *arena)
4852{
4853 asdl_seq *seq;
4854
4855 ExprList_check_invariants(l);
4856
4857 /* Allocate the asdl_seq and copy the expressions in to it. */
4858 seq = _Py_asdl_seq_new(l->size, arena);
4859 if (seq) {
4860 Py_ssize_t i;
4861 for (i = 0; i < l->size; i++)
4862 asdl_seq_SET(seq, i, l->p[i]);
4863 }
4864 ExprList_Dealloc(l);
4865 return seq;
4866}
4867
4868/* The FstringParser is designed to add a mix of strings and
4869 f-strings, and concat them together as needed. Ultimately, it
4870 generates an expr_ty. */
4871typedef struct {
4872 PyObject *last_str;
4873 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004874 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004875} FstringParser;
4876
4877#ifdef NDEBUG
4878#define FstringParser_check_invariants(state)
4879#else
4880static void
4881FstringParser_check_invariants(FstringParser *state)
4882{
4883 if (state->last_str)
4884 assert(PyUnicode_CheckExact(state->last_str));
4885 ExprList_check_invariants(&state->expr_list);
4886}
4887#endif
4888
4889static void
4890FstringParser_Init(FstringParser *state)
4891{
4892 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004893 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004894 ExprList_Init(&state->expr_list);
4895 FstringParser_check_invariants(state);
4896}
4897
4898static void
4899FstringParser_Dealloc(FstringParser *state)
4900{
4901 FstringParser_check_invariants(state);
4902
4903 Py_XDECREF(state->last_str);
4904 ExprList_Dealloc(&state->expr_list);
4905}
4906
4907/* Make a Str node, but decref the PyUnicode object being added. */
4908static expr_ty
4909make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4910{
4911 PyObject *s = *str;
4912 *str = NULL;
4913 assert(PyUnicode_CheckExact(s));
4914 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4915 Py_DECREF(s);
4916 return NULL;
4917 }
4918 return Str(s, LINENO(n), n->n_col_offset, c->c_arena);
4919}
4920
4921/* Add a non-f-string (that is, a regular literal string). str is
4922 decref'd. */
4923static int
4924FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4925{
4926 FstringParser_check_invariants(state);
4927
4928 assert(PyUnicode_CheckExact(str));
4929
4930 if (PyUnicode_GET_LENGTH(str) == 0) {
4931 Py_DECREF(str);
4932 return 0;
4933 }
4934
4935 if (!state->last_str) {
4936 /* We didn't have a string before, so just remember this one. */
4937 state->last_str = str;
4938 } else {
4939 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004940 PyUnicode_AppendAndDel(&state->last_str, str);
4941 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004942 return -1;
4943 }
4944 FstringParser_check_invariants(state);
4945 return 0;
4946}
4947
Eric V. Smith451d0e32016-09-09 21:56:20 -04004948/* Parse an f-string. The f-string is in *str to end, with no
4949 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004950static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004951FstringParser_ConcatFstring(FstringParser *state, const char **str,
4952 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004953 struct compiling *c, const node *n)
4954{
4955 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004956 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004957
4958 /* Parse the f-string. */
4959 while (1) {
4960 PyObject *literal = NULL;
4961 expr_ty expression = NULL;
4962
4963 /* If there's a zero length literal in front of the
4964 expression, literal will be NULL. If we're at the end of
4965 the f-string, expression will be NULL (unless result == 1,
4966 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004967 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004968 &literal, &expression,
4969 c, n);
4970 if (result < 0)
4971 return -1;
4972
4973 /* Add the literal, if any. */
4974 if (!literal) {
4975 /* Do nothing. Just leave last_str alone (and possibly
4976 NULL). */
4977 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004978 /* Note that the literal can be zero length, if the
4979 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004980 state->last_str = literal;
4981 literal = NULL;
4982 } else {
4983 /* We have a literal, concatenate it. */
4984 assert(PyUnicode_GET_LENGTH(literal) != 0);
4985 if (FstringParser_ConcatAndDel(state, literal) < 0)
4986 return -1;
4987 literal = NULL;
4988 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004989
4990 /* We've dealt with the literal now. It can't be leaked on further
4991 errors. */
4992 assert(literal == NULL);
4993
4994 /* See if we should just loop around to get the next literal
4995 and expression, while ignoring the expression this
4996 time. This is used for un-doubling braces, as an
4997 optimization. */
4998 if (result == 1)
4999 continue;
5000
5001 if (!expression)
5002 /* We're done with this f-string. */
5003 break;
5004
5005 /* We know we have an expression. Convert any existing string
5006 to a Str node. */
5007 if (!state->last_str) {
5008 /* Do nothing. No previous literal. */
5009 } else {
5010 /* Convert the existing last_str literal to a Str node. */
5011 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5012 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5013 return -1;
5014 }
5015
5016 if (ExprList_Append(&state->expr_list, expression) < 0)
5017 return -1;
5018 }
5019
Eric V. Smith235a6f02015-09-19 14:51:32 -04005020 /* If recurse_lvl is zero, then we must be at the end of the
5021 string. Otherwise, we must be at a right brace. */
5022
Eric V. Smith451d0e32016-09-09 21:56:20 -04005023 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005024 ast_error(c, n, "f-string: unexpected end of string");
5025 return -1;
5026 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005027 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005028 ast_error(c, n, "f-string: expecting '}'");
5029 return -1;
5030 }
5031
5032 FstringParser_check_invariants(state);
5033 return 0;
5034}
5035
5036/* Convert the partial state reflected in last_str and expr_list to an
5037 expr_ty. The expr_ty can be a Str, or a JoinedStr. */
5038static expr_ty
5039FstringParser_Finish(FstringParser *state, struct compiling *c,
5040 const node *n)
5041{
5042 asdl_seq *seq;
5043
5044 FstringParser_check_invariants(state);
5045
5046 /* If we're just a constant string with no expressions, return
5047 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005048 if (!state->fmode) {
5049 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005050 if (!state->last_str) {
5051 /* Create a zero length string. */
5052 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5053 if (!state->last_str)
5054 goto error;
5055 }
5056 return make_str_node_and_del(&state->last_str, c, n);
5057 }
5058
5059 /* Create a Str node out of last_str, if needed. It will be the
5060 last node in our expression list. */
5061 if (state->last_str) {
5062 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5063 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5064 goto error;
5065 }
5066 /* This has already been freed. */
5067 assert(state->last_str == NULL);
5068
5069 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5070 if (!seq)
5071 goto error;
5072
Eric V. Smith235a6f02015-09-19 14:51:32 -04005073 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5074
5075error:
5076 FstringParser_Dealloc(state);
5077 return NULL;
5078}
5079
Eric V. Smith451d0e32016-09-09 21:56:20 -04005080/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5081 at end, parse it into an expr_ty. Return NULL on error. Adjust
5082 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005083static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005084fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005085 struct compiling *c, const node *n)
5086{
5087 FstringParser state;
5088
5089 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005091 c, n) < 0) {
5092 FstringParser_Dealloc(&state);
5093 return NULL;
5094 }
5095
5096 return FstringParser_Finish(&state, c, n);
5097}
5098
5099/* n is a Python string literal, including the bracketing quote
5100 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005101 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005102 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005103 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5104 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005105*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005106static int
5107parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5108 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005109{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005110 size_t len;
5111 const char *s = STR(n);
5112 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005113 int fmode = 0;
5114 *bytesmode = 0;
5115 *rawmode = 0;
5116 *result = NULL;
5117 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005118 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005119 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005120 if (quote == 'b' || quote == 'B') {
5121 quote = *++s;
5122 *bytesmode = 1;
5123 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005124 else if (quote == 'u' || quote == 'U') {
5125 quote = *++s;
5126 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005127 else if (quote == 'r' || quote == 'R') {
5128 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005129 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005130 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005131 else if (quote == 'f' || quote == 'F') {
5132 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005133 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005134 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005135 else {
5136 break;
5137 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005138 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005139 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005140 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005141 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005142 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005143 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005144 if (quote != '\'' && quote != '\"') {
5145 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005146 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005147 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005148 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005149 s++;
5150 len = strlen(s);
5151 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005153 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005154 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005155 }
5156 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005157 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005158 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005159 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005160 }
5161 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005162 /* A triple quoted string. We've already skipped one quote at
5163 the start and one at the end of the string. Now skip the
5164 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005165 s += 2;
5166 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005167 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005168 if (s[--len] != quote || s[--len] != quote) {
5169 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005170 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005171 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005172 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005173
Eric V. Smith451d0e32016-09-09 21:56:20 -04005174 if (fmode) {
5175 /* Just return the bytes. The caller will parse the resulting
5176 string. */
5177 *fstr = s;
5178 *fstrlen = len;
5179 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005180 }
5181
Eric V. Smith451d0e32016-09-09 21:56:20 -04005182 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005183 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005184 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005185 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005186 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005187 const char *ch;
5188 for (ch = s; *ch; ch++) {
5189 if (Py_CHARMASK(*ch) >= 0x80) {
5190 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005191 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005192 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005193 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005194 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005195 if (*rawmode)
5196 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005197 else
Eric V. Smith56466482016-10-31 14:46:26 -04005198 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005199 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005200 if (*rawmode)
5201 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005202 else
Eric V. Smith56466482016-10-31 14:46:26 -04005203 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005204 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005205 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005206}
5207
Eric V. Smith235a6f02015-09-19 14:51:32 -04005208/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5209 each STRING atom, and process it as needed. For bytes, just
5210 concatenate them together, and the result will be a Bytes node. For
5211 normal strings and f-strings, concatenate them together. The result
5212 will be a Str node if there were no f-strings; a FormattedValue
5213 node if there's just an f-string (with no leading or trailing
5214 literals), or a JoinedStr node if there are multiple f-strings or
5215 any literals involved. */
5216static expr_ty
5217parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005218{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005219 int bytesmode = 0;
5220 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005221 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005222
5223 FstringParser state;
5224 FstringParser_Init(&state);
5225
5226 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005227 int this_bytesmode;
5228 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005229 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005230 const char *fstr;
5231 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005232
5233 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005234 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5235 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005236 goto error;
5237
5238 /* Check that we're not mixing bytes with unicode. */
5239 if (i != 0 && bytesmode != this_bytesmode) {
5240 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005241 /* s is NULL if the current string part is an f-string. */
5242 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005243 goto error;
5244 }
5245 bytesmode = this_bytesmode;
5246
Eric V. Smith451d0e32016-09-09 21:56:20 -04005247 if (fstr != NULL) {
5248 int result;
5249 assert(s == NULL && !bytesmode);
5250 /* This is an f-string. Parse and concatenate it. */
5251 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5252 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005253 if (result < 0)
5254 goto error;
5255 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005256 /* A string or byte string. */
5257 assert(s != NULL && fstr == NULL);
5258
Eric V. Smith451d0e32016-09-09 21:56:20 -04005259 assert(bytesmode ? PyBytes_CheckExact(s) :
5260 PyUnicode_CheckExact(s));
5261
Eric V. Smith451d0e32016-09-09 21:56:20 -04005262 if (bytesmode) {
5263 /* For bytes, concat as we go. */
5264 if (i == 0) {
5265 /* First time, just remember this value. */
5266 bytes_str = s;
5267 } else {
5268 PyBytes_ConcatAndDel(&bytes_str, s);
5269 if (!bytes_str)
5270 goto error;
5271 }
5272 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005273 /* This is a regular string. Concatenate it. */
5274 if (FstringParser_ConcatAndDel(&state, s) < 0)
5275 goto error;
5276 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005277 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005278 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005279 if (bytesmode) {
5280 /* Just return the bytes object and we're done. */
5281 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5282 goto error;
5283 return Bytes(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
5284 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005285
Eric V. Smith235a6f02015-09-19 14:51:32 -04005286 /* We're not a bytes string, bytes_str should never have been set. */
5287 assert(bytes_str == NULL);
5288
5289 return FstringParser_Finish(&state, c, n);
5290
5291error:
5292 Py_XDECREF(bytes_str);
5293 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005294 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005295}