-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
/
symtable.c
3001 lines (2780 loc) · 101 KB
/
symtable.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Python.h"
#include "pycore_ast.h" // stmt_ty
#include "pycore_parser.h" // _PyParser_ASTFromString()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_symtable.h" // PySTEntryObject
// Set this to 1 to dump all symtables to stdout for debugging
#define _PY_DUMP_SYMTABLE 0
/* error strings used for warnings */
#define GLOBAL_PARAM \
"name '%U' is parameter and global"
#define NONLOCAL_PARAM \
"name '%U' is parameter and nonlocal"
#define GLOBAL_AFTER_ASSIGN \
"name '%U' is assigned to before global declaration"
#define NONLOCAL_AFTER_ASSIGN \
"name '%U' is assigned to before nonlocal declaration"
#define GLOBAL_AFTER_USE \
"name '%U' is used prior to global declaration"
#define NONLOCAL_AFTER_USE \
"name '%U' is used prior to nonlocal declaration"
#define GLOBAL_ANNOT \
"annotated name '%U' can't be global"
#define NONLOCAL_ANNOT \
"annotated name '%U' can't be nonlocal"
#define IMPORT_STAR_WARNING "import * only allowed at module level"
#define NAMED_EXPR_COMP_IN_CLASS \
"assignment expression within a comprehension cannot be used in a class body"
#define NAMED_EXPR_COMP_IN_TYPEVAR_BOUND \
"assignment expression within a comprehension cannot be used in a TypeVar bound"
#define NAMED_EXPR_COMP_IN_TYPEALIAS \
"assignment expression within a comprehension cannot be used in a type alias"
#define NAMED_EXPR_COMP_IN_TYPEPARAM \
"assignment expression within a comprehension cannot be used within the definition of a generic"
#define NAMED_EXPR_COMP_CONFLICT \
"assignment expression cannot rebind comprehension iteration variable '%U'"
#define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \
"comprehension inner loop cannot rebind assignment expression target '%U'"
#define NAMED_EXPR_COMP_ITER_EXPR \
"assignment expression cannot be used in a comprehension iterable expression"
#define ANNOTATION_NOT_ALLOWED \
"%s cannot be used within an annotation"
#define EXPR_NOT_ALLOWED_IN_TYPE_VARIABLE \
"%s cannot be used within %s"
#define EXPR_NOT_ALLOWED_IN_TYPE_ALIAS \
"%s cannot be used within a type alias"
#define EXPR_NOT_ALLOWED_IN_TYPE_PARAMETERS \
"%s cannot be used within the definition of a generic"
#define DUPLICATE_TYPE_PARAM \
"duplicate type parameter '%U'"
#define ASYNC_WITH_OUTISDE_ASYNC_FUNC \
"'async with' outside async function"
#define ASYNC_FOR_OUTISDE_ASYNC_FUNC \
"'async for' outside async function"
#define LOCATION(x) SRC_LOCATION_FROM_AST(x)
#define SET_ERROR_LOCATION(FNAME, L) \
PyErr_RangedSyntaxLocationObject((FNAME), \
(L).lineno, (L).col_offset + 1, (L).end_lineno, (L).end_col_offset + 1)
static PySTEntryObject *
ste_new(struct symtable *st, identifier name, _Py_block_ty block,
void *key, _Py_SourceLocation loc)
{
PySTEntryObject *ste = NULL;
PyObject *k = NULL;
k = PyLong_FromVoidPtr(key);
if (k == NULL)
goto fail;
ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
if (ste == NULL) {
Py_DECREF(k);
goto fail;
}
ste->ste_table = st;
ste->ste_id = k; /* ste owns reference to k */
ste->ste_name = Py_NewRef(name);
ste->ste_symbols = NULL;
ste->ste_varnames = NULL;
ste->ste_children = NULL;
ste->ste_directives = NULL;
ste->ste_mangled_names = NULL;
ste->ste_type = block;
ste->ste_scope_info = NULL;
ste->ste_nested = 0;
ste->ste_free = 0;
ste->ste_varargs = 0;
ste->ste_varkeywords = 0;
ste->ste_annotations_used = 0;
ste->ste_loc = loc;
if (st->st_cur != NULL &&
(st->st_cur->ste_nested ||
_PyST_IsFunctionLike(st->st_cur)))
ste->ste_nested = 1;
ste->ste_child_free = 0;
ste->ste_generator = 0;
ste->ste_coroutine = 0;
ste->ste_comprehension = NoComprehension;
ste->ste_returns_value = 0;
ste->ste_needs_class_closure = 0;
ste->ste_comp_inlined = 0;
ste->ste_comp_iter_target = 0;
ste->ste_can_see_class_scope = 0;
ste->ste_comp_iter_expr = 0;
ste->ste_needs_classdict = 0;
ste->ste_annotation_block = NULL;
ste->ste_symbols = PyDict_New();
ste->ste_varnames = PyList_New(0);
ste->ste_children = PyList_New(0);
if (ste->ste_symbols == NULL
|| ste->ste_varnames == NULL
|| ste->ste_children == NULL)
goto fail;
if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
goto fail;
return ste;
fail:
Py_XDECREF(ste);
return NULL;
}
static PyObject *
ste_repr(PySTEntryObject *ste)
{
return PyUnicode_FromFormat("<symtable entry %U(%R), line %d>",
ste->ste_name, ste->ste_id, ste->ste_loc.lineno);
}
static void
ste_dealloc(PySTEntryObject *ste)
{
ste->ste_table = NULL;
Py_XDECREF(ste->ste_id);
Py_XDECREF(ste->ste_name);
Py_XDECREF(ste->ste_symbols);
Py_XDECREF(ste->ste_varnames);
Py_XDECREF(ste->ste_children);
Py_XDECREF(ste->ste_directives);
Py_XDECREF(ste->ste_annotation_block);
Py_XDECREF(ste->ste_mangled_names);
PyObject_Free(ste);
}
#define OFF(x) offsetof(PySTEntryObject, x)
static PyMemberDef ste_memberlist[] = {
{"id", _Py_T_OBJECT, OFF(ste_id), Py_READONLY},
{"name", _Py_T_OBJECT, OFF(ste_name), Py_READONLY},
{"symbols", _Py_T_OBJECT, OFF(ste_symbols), Py_READONLY},
{"varnames", _Py_T_OBJECT, OFF(ste_varnames), Py_READONLY},
{"children", _Py_T_OBJECT, OFF(ste_children), Py_READONLY},
{"nested", Py_T_INT, OFF(ste_nested), Py_READONLY},
{"type", Py_T_INT, OFF(ste_type), Py_READONLY},
{"lineno", Py_T_INT, OFF(ste_loc.lineno), Py_READONLY},
{NULL}
};
PyTypeObject PySTEntry_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"symtable entry",
sizeof(PySTEntryObject),
0,
(destructor)ste_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)ste_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
ste_memberlist, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
static int symtable_analyze(struct symtable *st);
static int symtable_enter_block(struct symtable *st, identifier name,
_Py_block_ty block, void *ast, _Py_SourceLocation loc);
static int symtable_exit_block(struct symtable *st);
static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
static int symtable_visit_expr(struct symtable *st, expr_ty s);
static int symtable_visit_type_param(struct symtable *st, type_param_ty s);
static int symtable_visit_genexp(struct symtable *st, expr_ty s);
static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
static int symtable_visit_arguments(struct symtable *st, arguments_ty);
static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
static int symtable_visit_alias(struct symtable *st, alias_ty);
static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
static int symtable_visit_keyword(struct symtable *st, keyword_ty);
static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
static int symtable_visit_annotation(struct symtable *st, expr_ty annotation, void *key);
static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
static int symtable_implicit_arg(struct symtable *st, int pos);
static int symtable_visit_annotations(struct symtable *st, stmt_ty, arguments_ty, expr_ty,
struct _symtable_entry *parent_ste);
static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
static int symtable_visit_pattern(struct symtable *st, pattern_ty s);
static int symtable_raise_if_annotation_block(struct symtable *st, const char *, expr_ty);
static int symtable_raise_if_not_coroutine(struct symtable *st, const char *msg, _Py_SourceLocation loc);
static int symtable_raise_if_comprehension_block(struct symtable *st, expr_ty);
/* For debugging purposes only */
#if _PY_DUMP_SYMTABLE
static void _dump_symtable(PySTEntryObject* ste, PyObject* prefix)
{
const char *blocktype = "";
switch (ste->ste_type) {
case FunctionBlock: blocktype = "FunctionBlock"; break;
case ClassBlock: blocktype = "ClassBlock"; break;
case ModuleBlock: blocktype = "ModuleBlock"; break;
case AnnotationBlock: blocktype = "AnnotationBlock"; break;
case TypeVariableBlock: blocktype = "TypeVariableBlock"; break;
case TypeAliasBlock: blocktype = "TypeAliasBlock"; break;
case TypeParametersBlock: blocktype = "TypeParametersBlock"; break;
}
const char *comptype = "";
switch (ste->ste_comprehension) {
case ListComprehension: comptype = " ListComprehension"; break;
case DictComprehension: comptype = " DictComprehension"; break;
case SetComprehension: comptype = " SetComprehension"; break;
case GeneratorExpression: comptype = " GeneratorExpression"; break;
case NoComprehension: break;
}
PyObject* msg = PyUnicode_FromFormat(
(
"%U=== Symtable for %U ===\n"
"%U%s%s\n"
"%U%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
"%Ulineno: %d col_offset: %d\n"
"%U--- Symbols ---\n"
),
prefix,
ste->ste_name,
prefix,
blocktype,
comptype,
prefix,
ste->ste_nested ? " nested" : "",
ste->ste_free ? " free" : "",
ste->ste_child_free ? " child_free" : "",
ste->ste_generator ? " generator" : "",
ste->ste_coroutine ? " coroutine" : "",
ste->ste_varargs ? " varargs" : "",
ste->ste_varkeywords ? " varkeywords" : "",
ste->ste_returns_value ? " returns_value" : "",
ste->ste_needs_class_closure ? " needs_class_closure" : "",
ste->ste_needs_classdict ? " needs_classdict" : "",
ste->ste_comp_inlined ? " comp_inlined" : "",
ste->ste_comp_iter_target ? " comp_iter_target" : "",
ste->ste_can_see_class_scope ? " can_see_class_scope" : "",
prefix,
ste->ste_loc.lineno,
ste->ste_loc.col_offset,
prefix
);
assert(msg != NULL);
printf("%s", PyUnicode_AsUTF8(msg));
Py_DECREF(msg);
PyObject *name, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(ste->ste_symbols, &pos, &name, &value)) {
int scope = _PyST_GetScope(ste, name);
long flags = _PyST_GetSymbol(ste, name);
printf("%s %s: ", PyUnicode_AsUTF8(prefix), PyUnicode_AsUTF8(name));
if (flags & DEF_GLOBAL) printf(" DEF_GLOBAL");
if (flags & DEF_LOCAL) printf(" DEF_LOCAL");
if (flags & DEF_PARAM) printf(" DEF_PARAM");
if (flags & DEF_NONLOCAL) printf(" DEF_NONLOCAL");
if (flags & USE) printf(" USE");
if (flags & DEF_FREE_CLASS) printf(" DEF_FREE_CLASS");
if (flags & DEF_IMPORT) printf(" DEF_IMPORT");
if (flags & DEF_ANNOT) printf(" DEF_ANNOT");
if (flags & DEF_COMP_ITER) printf(" DEF_COMP_ITER");
if (flags & DEF_TYPE_PARAM) printf(" DEF_TYPE_PARAM");
if (flags & DEF_COMP_CELL) printf(" DEF_COMP_CELL");
switch (scope) {
case LOCAL: printf(" LOCAL"); break;
case GLOBAL_EXPLICIT: printf(" GLOBAL_EXPLICIT"); break;
case GLOBAL_IMPLICIT: printf(" GLOBAL_IMPLICIT"); break;
case FREE: printf(" FREE"); break;
case CELL: printf(" CELL"); break;
}
printf("\n");
}
printf("%s--- Children ---\n", PyUnicode_AsUTF8(prefix));
PyObject *new_prefix = PyUnicode_FromFormat(" %U", prefix);
assert(new_prefix != NULL);
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(ste->ste_children); i++) {
PyObject *child = PyList_GetItem(ste->ste_children, i);
assert(child != NULL && PySTEntry_Check(child));
_dump_symtable((PySTEntryObject *)child, new_prefix);
}
Py_DECREF(new_prefix);
}
static void dump_symtable(PySTEntryObject* ste)
{
PyObject *empty = PyUnicode_FromString("");
assert(empty != NULL);
_dump_symtable(ste, empty);
Py_DECREF(empty);
}
#endif
#define DUPLICATE_ARGUMENT \
"duplicate argument '%U' in function definition"
static struct symtable *
symtable_new(void)
{
struct symtable *st;
st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
if (st == NULL) {
PyErr_NoMemory();
return NULL;
}
st->st_filename = NULL;
st->st_blocks = NULL;
if ((st->st_stack = PyList_New(0)) == NULL)
goto fail;
if ((st->st_blocks = PyDict_New()) == NULL)
goto fail;
st->st_cur = NULL;
st->st_private = NULL;
return st;
fail:
_PySymtable_Free(st);
return NULL;
}
struct symtable *
_PySymtable_Build(mod_ty mod, PyObject *filename, _PyFutureFeatures *future)
{
struct symtable *st = symtable_new();
asdl_stmt_seq *seq;
Py_ssize_t i;
PyThreadState *tstate;
int starting_recursion_depth;
if (st == NULL)
return NULL;
if (filename == NULL) {
_PySymtable_Free(st);
return NULL;
}
st->st_filename = Py_NewRef(filename);
st->st_future = future;
/* Setup recursion depth check counters */
tstate = _PyThreadState_GET();
if (!tstate) {
_PySymtable_Free(st);
return NULL;
}
/* Be careful here to prevent overflow. */
int recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
starting_recursion_depth = recursion_depth;
st->recursion_depth = starting_recursion_depth;
st->recursion_limit = Py_C_RECURSION_LIMIT;
/* Make the initial symbol information gathering pass */
_Py_SourceLocation loc0 = {0, 0, 0, 0};
if (!symtable_enter_block(st, &_Py_ID(top), ModuleBlock, (void *)mod, loc0)) {
_PySymtable_Free(st);
return NULL;
}
st->st_top = st->st_cur;
switch (mod->kind) {
case Module_kind:
seq = mod->v.Module.body;
for (i = 0; i < asdl_seq_LEN(seq); i++)
if (!symtable_visit_stmt(st,
(stmt_ty)asdl_seq_GET(seq, i)))
goto error;
break;
case Expression_kind:
if (!symtable_visit_expr(st, mod->v.Expression.body))
goto error;
break;
case Interactive_kind:
seq = mod->v.Interactive.body;
for (i = 0; i < asdl_seq_LEN(seq); i++)
if (!symtable_visit_stmt(st,
(stmt_ty)asdl_seq_GET(seq, i)))
goto error;
break;
case FunctionType_kind:
PyErr_SetString(PyExc_RuntimeError,
"this compiler does not handle FunctionTypes");
goto error;
}
if (!symtable_exit_block(st)) {
_PySymtable_Free(st);
return NULL;
}
/* Check that the recursion depth counting balanced correctly */
if (st->recursion_depth != starting_recursion_depth) {
PyErr_Format(PyExc_SystemError,
"symtable analysis recursion depth mismatch (before=%d, after=%d)",
starting_recursion_depth, st->recursion_depth);
_PySymtable_Free(st);
return NULL;
}
/* Make the second symbol analysis pass */
if (symtable_analyze(st)) {
#if _PY_DUMP_SYMTABLE
dump_symtable(st->st_top);
#endif
return st;
}
_PySymtable_Free(st);
return NULL;
error:
(void) symtable_exit_block(st);
_PySymtable_Free(st);
return NULL;
}
void
_PySymtable_Free(struct symtable *st)
{
Py_XDECREF(st->st_filename);
Py_XDECREF(st->st_blocks);
Py_XDECREF(st->st_stack);
PyMem_Free((void *)st);
}
PySTEntryObject *
_PySymtable_Lookup(struct symtable *st, void *key)
{
PyObject *k, *v;
k = PyLong_FromVoidPtr(key);
if (k == NULL)
return NULL;
if (PyDict_GetItemRef(st->st_blocks, k, &v) == 0) {
PyErr_SetString(PyExc_KeyError,
"unknown symbol table entry");
}
Py_DECREF(k);
assert(v == NULL || PySTEntry_Check(v));
return (PySTEntryObject *)v;
}
int
_PySymtable_LookupOptional(struct symtable *st, void *key,
PySTEntryObject **out)
{
PyObject *k = PyLong_FromVoidPtr(key);
if (k == NULL) {
*out = NULL;
return -1;
}
int result = PyDict_GetItemRef(st->st_blocks, k, (PyObject **)out);
Py_DECREF(k);
assert(*out == NULL || PySTEntry_Check(*out));
return result;
}
long
_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
{
PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
if (!v)
return 0;
assert(PyLong_Check(v));
return PyLong_AS_LONG(v);
}
int
_PyST_GetScope(PySTEntryObject *ste, PyObject *name)
{
long symbol = _PyST_GetSymbol(ste, name);
return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
}
int
_PyST_IsFunctionLike(PySTEntryObject *ste)
{
return ste->ste_type == FunctionBlock
|| ste->ste_type == AnnotationBlock
|| ste->ste_type == TypeVariableBlock
|| ste->ste_type == TypeAliasBlock
|| ste->ste_type == TypeParametersBlock;
}
static int
error_at_directive(PySTEntryObject *ste, PyObject *name)
{
Py_ssize_t i;
PyObject *data;
assert(ste->ste_directives);
for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
data = PyList_GET_ITEM(ste->ste_directives, i);
assert(PyTuple_CheckExact(data));
assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
PyErr_RangedSyntaxLocationObject(ste->ste_table->st_filename,
PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1,
PyLong_AsLong(PyTuple_GET_ITEM(data, 3)),
PyLong_AsLong(PyTuple_GET_ITEM(data, 4)) + 1);
return 0;
}
}
PyErr_SetString(PyExc_RuntimeError,
"BUG: internal directive bookkeeping broken");
return 0;
}
/* Analyze raw symbol information to determine scope of each name.
The next several functions are helpers for symtable_analyze(),
which determines whether a name is local, global, or free. In addition,
it determines which local variables are cell variables; they provide
bindings that are used for free variables in enclosed blocks.
There are also two kinds of global variables, implicit and explicit. An
explicit global is declared with the global statement. An implicit
global is a free variable for which the compiler has found no binding
in an enclosing function scope. The implicit global is either a global
or a builtin. Python's module and class blocks use the xxx_NAME opcodes
to handle these names to implement slightly odd semantics. In such a
block, the name is treated as global until it is assigned to; then it
is treated as a local.
The symbol table requires two passes to determine the scope of each name.
The first pass collects raw facts from the AST via the symtable_visit_*
functions: the name is a parameter here, the name is used but not defined
here, etc. The second pass analyzes these facts during a pass over the
PySTEntryObjects created during pass 1.
When a function is entered during the second pass, the parent passes
the set of all name bindings visible to its children. These bindings
are used to determine if non-local variables are free or implicit globals.
Names which are explicitly declared nonlocal must exist in this set of
visible names - if they do not, a syntax error is raised. After doing
the local analysis, it analyzes each of its child blocks using an
updated set of name bindings.
The children update the free variable set. If a local variable is added to
the free variable set by the child, the variable is marked as a cell. The
function object being defined must provide runtime storage for the variable
that may outlive the function's frame. Cell variables are removed from the
free set before the analyze function returns to its parent.
During analysis, the names are:
symbols: dict mapping from symbol names to flag values (including offset scope values)
scopes: dict mapping from symbol names to scope values (no offset)
local: set of all symbol names local to the current scope
bound: set of all symbol names local to a containing function scope
free: set of all symbol names referenced but not bound in child scopes
global: set of all symbol names explicitly declared as global
*/
#define SET_SCOPE(DICT, NAME, I) \
do { \
PyObject *o = PyLong_FromLong(I); \
if (!o) \
return 0; \
if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
Py_DECREF(o); \
return 0; \
} \
Py_DECREF(o); \
} while(0)
/* Decide on scope of name, given flags.
The namespace dictionaries may be modified to record information
about the new name. For example, a new global will add an entry to
global. A name that was global can be changed to local.
*/
static int
analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
PyObject *bound, PyObject *local, PyObject *free,
PyObject *global, PyObject *type_params, PySTEntryObject *class_entry)
{
int contains;
if (flags & DEF_GLOBAL) {
if (flags & DEF_NONLOCAL) {
PyErr_Format(PyExc_SyntaxError,
"name '%U' is nonlocal and global",
name);
return error_at_directive(ste, name);
}
SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
if (PySet_Add(global, name) < 0)
return 0;
if (bound && (PySet_Discard(bound, name) < 0))
return 0;
return 1;
}
if (flags & DEF_NONLOCAL) {
if (!bound) {
PyErr_Format(PyExc_SyntaxError,
"nonlocal declaration not allowed at module level");
return error_at_directive(ste, name);
}
contains = PySet_Contains(bound, name);
if (contains < 0) {
return 0;
}
if (!contains) {
PyErr_Format(PyExc_SyntaxError,
"no binding for nonlocal '%U' found",
name);
return error_at_directive(ste, name);
}
contains = PySet_Contains(type_params, name);
if (contains < 0) {
return 0;
}
if (contains) {
PyErr_Format(PyExc_SyntaxError,
"nonlocal binding not allowed for type parameter '%U'",
name);
return error_at_directive(ste, name);
}
SET_SCOPE(scopes, name, FREE);
ste->ste_free = 1;
return PySet_Add(free, name) >= 0;
}
if (flags & DEF_BOUND) {
SET_SCOPE(scopes, name, LOCAL);
if (PySet_Add(local, name) < 0)
return 0;
if (PySet_Discard(global, name) < 0)
return 0;
if (flags & DEF_TYPE_PARAM) {
if (PySet_Add(type_params, name) < 0)
return 0;
}
else {
if (PySet_Discard(type_params, name) < 0)
return 0;
}
return 1;
}
// If we were passed class_entry (i.e., we're in an ste_can_see_class_scope scope)
// and the bound name is in that set, then the name is potentially bound both by
// the immediately enclosing class namespace, and also by an outer function namespace.
// In that case, we want the runtime name resolution to look at only the class
// namespace and the globals (not the namespace providing the bound).
// Similarly, if the name is explicitly global in the class namespace (through the
// global statement), we want to also treat it as a global in this scope.
if (class_entry != NULL) {
long class_flags = _PyST_GetSymbol(class_entry, name);
if (class_flags & DEF_GLOBAL) {
SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
return 1;
}
else if (class_flags & DEF_BOUND && !(class_flags & DEF_NONLOCAL)) {
SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
return 1;
}
}
/* If an enclosing block has a binding for this name, it
is a free variable rather than a global variable.
Note that having a non-NULL bound implies that the block
is nested.
*/
if (bound) {
contains = PySet_Contains(bound, name);
if (contains < 0) {
return 0;
}
if (contains) {
SET_SCOPE(scopes, name, FREE);
ste->ste_free = 1;
return PySet_Add(free, name) >= 0;
}
}
/* If a parent has a global statement, then call it global
explicit? It could also be global implicit.
*/
if (global) {
contains = PySet_Contains(global, name);
if (contains < 0) {
return 0;
}
if (contains) {
SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
return 1;
}
}
if (ste->ste_nested)
ste->ste_free = 1;
SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
return 1;
}
static int
is_free_in_any_child(PySTEntryObject *entry, PyObject *key)
{
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(entry->ste_children); i++) {
PySTEntryObject *child_ste = (PySTEntryObject *)PyList_GET_ITEM(
entry->ste_children, i);
long scope = _PyST_GetScope(child_ste, key);
if (scope == FREE) {
return 1;
}
}
return 0;
}
static int
inline_comprehension(PySTEntryObject *ste, PySTEntryObject *comp,
PyObject *scopes, PyObject *comp_free,
PyObject *inlined_cells)
{
PyObject *k, *v;
Py_ssize_t pos = 0;
int remove_dunder_class = 0;
while (PyDict_Next(comp->ste_symbols, &pos, &k, &v)) {
// skip comprehension parameter
long comp_flags = PyLong_AS_LONG(v);
if (comp_flags & DEF_PARAM) {
assert(_PyUnicode_EqualToASCIIString(k, ".0"));
continue;
}
int scope = (comp_flags >> SCOPE_OFFSET) & SCOPE_MASK;
int only_flags = comp_flags & ((1 << SCOPE_OFFSET) - 1);
if (scope == CELL || only_flags & DEF_COMP_CELL) {
if (PySet_Add(inlined_cells, k) < 0) {
return 0;
}
}
PyObject *existing = PyDict_GetItemWithError(ste->ste_symbols, k);
if (existing == NULL && PyErr_Occurred()) {
return 0;
}
// __class__ is never allowed to be free through a class scope (see
// drop_class_free)
if (scope == FREE && ste->ste_type == ClassBlock &&
_PyUnicode_EqualToASCIIString(k, "__class__")) {
scope = GLOBAL_IMPLICIT;
if (PySet_Discard(comp_free, k) < 0) {
return 0;
}
remove_dunder_class = 1;
}
if (!existing) {
// name does not exist in scope, copy from comprehension
assert(scope != FREE || PySet_Contains(comp_free, k) == 1);
PyObject *v_flags = PyLong_FromLong(only_flags);
if (v_flags == NULL) {
return 0;
}
int ok = PyDict_SetItem(ste->ste_symbols, k, v_flags);
Py_DECREF(v_flags);
if (ok < 0) {
return 0;
}
SET_SCOPE(scopes, k, scope);
}
else {
if (PyLong_AsLong(existing) & DEF_BOUND) {
// free vars in comprehension that are locals in outer scope can
// now simply be locals, unless they are free in comp children,
// or if the outer scope is a class block
if (!is_free_in_any_child(comp, k) && ste->ste_type != ClassBlock) {
if (PySet_Discard(comp_free, k) < 0) {
return 0;
}
}
}
}
}
comp->ste_free = PySet_Size(comp_free) > 0;
if (remove_dunder_class && PyDict_DelItemString(comp->ste_symbols, "__class__") < 0) {
return 0;
}
return 1;
}
#undef SET_SCOPE
/* If a name is defined in free and also in locals, then this block
provides the binding for the free variable. The name should be
marked CELL in this block and removed from the free list.
Note that the current block's free variables are included in free.
That's safe because no name can be free and local in the same scope.
*/
static int
analyze_cells(PyObject *scopes, PyObject *free, PyObject *inlined_cells)
{
PyObject *name, *v, *v_cell;
int success = 0;
Py_ssize_t pos = 0;
v_cell = PyLong_FromLong(CELL);
if (!v_cell)
return 0;
while (PyDict_Next(scopes, &pos, &name, &v)) {
long scope;
assert(PyLong_Check(v));
scope = PyLong_AS_LONG(v);
if (scope != LOCAL)
continue;
int contains = PySet_Contains(free, name);
if (contains < 0) {
goto error;
}
if (!contains) {
contains = PySet_Contains(inlined_cells, name);
if (contains < 0) {
goto error;
}
if (!contains) {
continue;
}
}
/* Replace LOCAL with CELL for this name, and remove
from free. It is safe to replace the value of name
in the dict, because it will not cause a resize.
*/
if (PyDict_SetItem(scopes, name, v_cell) < 0)
goto error;
if (PySet_Discard(free, name) < 0)
goto error;
}
success = 1;
error:
Py_DECREF(v_cell);
return success;
}
static int
drop_class_free(PySTEntryObject *ste, PyObject *free)
{
int res;
res = PySet_Discard(free, &_Py_ID(__class__));
if (res < 0)
return 0;
if (res)
ste->ste_needs_class_closure = 1;
res = PySet_Discard(free, &_Py_ID(__classdict__));
if (res < 0)
return 0;
if (res)
ste->ste_needs_classdict = 1;
return 1;
}
/* Enter the final scope information into the ste_symbols dict.
*
* All arguments are dicts. Modifies symbols, others are read-only.
*/
static int
update_symbols(PyObject *symbols, PyObject *scopes,
PyObject *bound, PyObject *free,
PyObject *inlined_cells, int classflag)
{
PyObject *name = NULL, *itr = NULL;
PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Py_ssize_t pos = 0;
/* Update scope information for all symbols in this scope */
while (PyDict_Next(symbols, &pos, &name, &v)) {
long scope, flags;
assert(PyLong_Check(v));
flags = PyLong_AS_LONG(v);
int contains = PySet_Contains(inlined_cells, name);
if (contains < 0) {
return 0;
}
if (contains) {
flags |= DEF_COMP_CELL;
}
v_scope = PyDict_GetItemWithError(scopes, name);
assert(v_scope && PyLong_Check(v_scope));
scope = PyLong_AS_LONG(v_scope);
flags |= (scope << SCOPE_OFFSET);
v_new = PyLong_FromLong(flags);
if (!v_new)
return 0;
if (PyDict_SetItem(symbols, name, v_new) < 0) {
Py_DECREF(v_new);
return 0;
}
Py_DECREF(v_new);
}
/* Record not yet resolved free variables from children (if any) */
v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
if (!v_free)
return 0;
itr = PyObject_GetIter(free);
if (itr == NULL) {
Py_DECREF(v_free);
return 0;
}
while ((name = PyIter_Next(itr))) {
v = PyDict_GetItemWithError(symbols, name);
/* Handle symbol that already exists in this scope */
if (v) {
/* Handle a free variable in a method of
the class that has the same name as a local
or global in the class scope.
*/
if (classflag) {
long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
v_new = PyLong_FromLong(flags);
if (!v_new) {
goto error;
}
if (PyDict_SetItem(symbols, name, v_new) < 0) {
Py_DECREF(v_new);
goto error;
}
Py_DECREF(v_new);
}
/* It's a cell, or already free in this scope */
Py_DECREF(name);
continue;
}
else if (PyErr_Occurred()) {
goto error;
}
/* Handle global symbol */
if (bound) {
int contains = PySet_Contains(bound, name);