Skip to content

Commit 6c76df2

Browse files
authored
bpo-40528: move asdl identifier collection to the new metadata system (GH-26858)
1 parent b5a52ee commit 6c76df2

File tree

1 file changed

+51
-49
lines changed

1 file changed

+51
-49
lines changed

Parser/asdl_c.py

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,9 @@ class EmitVisitor(asdl.VisitorBase):
103103

104104
def __init__(self, file, metadata = None):
105105
self.file = file
106-
self.identifiers = set()
107-
self.singletons = set()
108-
self.types = set()
109106
self._metadata = metadata
110107
super(EmitVisitor, self).__init__()
111108

112-
def emit_identifier(self, name):
113-
self.identifiers.add(str(name))
114-
115-
def emit_singleton(self, name):
116-
self.singletons.add(str(name))
117-
118-
def emit_type(self, name):
119-
self.types.add(str(name))
120-
121109
def emit(self, s, depth, reflow=True):
122110
# XXX reflow long lines?
123111
if reflow:
@@ -143,6 +131,8 @@ def metadata(self, value):
143131
self._metadata = value
144132

145133
class MetadataVisitor(asdl.VisitorBase):
134+
ROOT_TYPE = "AST"
135+
146136
def __init__(self, *args, **kwargs):
147137
super().__init__(*args, **kwargs)
148138

@@ -151,8 +141,16 @@ def __init__(self, *args, **kwargs):
151141
# names where all the constructors
152142
# belonging to that type lack of any
153143
# fields.
144+
# - identifiers: All identifiers used in the AST decclarations
145+
# - singletons: List of all constructors that originates from
146+
# simple sums.
147+
# - types: List of all top level type names
148+
#
154149
self.metadata = types.SimpleNamespace(
155-
simple_sums=set()
150+
simple_sums=set(),
151+
identifiers=set(),
152+
singletons=set(),
153+
types={self.ROOT_TYPE},
156154
)
157155

158156
def visitModule(self, mod):
@@ -163,9 +161,35 @@ def visitType(self, type):
163161
self.visit(type.value, type.name)
164162

165163
def visitSum(self, sum, name):
166-
if is_simple(sum):
164+
self.metadata.types.add(name)
165+
166+
simple_sum = is_simple(sum)
167+
if simple_sum:
167168
self.metadata.simple_sums.add(name)
168169

170+
for constructor in sum.types:
171+
if simple_sum:
172+
self.metadata.singletons.add(constructor.name)
173+
self.visitConstructor(constructor)
174+
self.visitFields(sum.attributes)
175+
176+
def visitConstructor(self, constructor):
177+
self.metadata.types.add(constructor.name)
178+
self.visitFields(constructor.fields)
179+
180+
def visitProduct(self, product, name):
181+
self.metadata.types.add(name)
182+
self.visitFields(product.attributes)
183+
self.visitFields(product.fields)
184+
185+
def visitFields(self, fields):
186+
for field in fields:
187+
self.visitField(field)
188+
189+
def visitField(self, field):
190+
self.metadata.identifiers.add(field.name)
191+
192+
169193
class TypeDefVisitor(EmitVisitor):
170194
def visitModule(self, mod):
171195
for dfn in mod.dfns:
@@ -683,45 +707,33 @@ def emit_sequence_constructor(self, name, type):
683707
class PyTypesDeclareVisitor(PickleVisitor):
684708

685709
def visitProduct(self, prod, name):
686-
self.emit_type("%s_type" % name)
687710
self.emit("static PyObject* ast2obj_%s(struct ast_state *state, void*);" % name, 0)
688711
if prod.attributes:
689-
for a in prod.attributes:
690-
self.emit_identifier(a.name)
691712
self.emit("static const char * const %s_attributes[] = {" % name, 0)
692713
for a in prod.attributes:
693714
self.emit('"%s",' % a.name, 1)
694715
self.emit("};", 0)
695716
if prod.fields:
696-
for f in prod.fields:
697-
self.emit_identifier(f.name)
698717
self.emit("static const char * const %s_fields[]={" % name,0)
699718
for f in prod.fields:
700719
self.emit('"%s",' % f.name, 1)
701720
self.emit("};", 0)
702721

703722
def visitSum(self, sum, name):
704-
self.emit_type("%s_type" % name)
705723
if sum.attributes:
706-
for a in sum.attributes:
707-
self.emit_identifier(a.name)
708724
self.emit("static const char * const %s_attributes[] = {" % name, 0)
709725
for a in sum.attributes:
710726
self.emit('"%s",' % a.name, 1)
711727
self.emit("};", 0)
712728
ptype = "void*"
713729
if is_simple(sum):
714730
ptype = get_c_type(name)
715-
for t in sum.types:
716-
self.emit_singleton("%s_singleton" % t.name)
717731
self.emit("static PyObject* ast2obj_%s(struct ast_state *state, %s);" % (name, ptype), 0)
718732
for t in sum.types:
719733
self.visitConstructor(t, name)
720734

721735
def visitConstructor(self, cons, name):
722736
if cons.fields:
723-
for t in cons.fields:
724-
self.emit_identifier(t.name)
725737
self.emit("static const char * const %s_fields[]={" % cons.name, 0)
726738
for t in cons.fields:
727739
self.emit('"%s",' % t.name, 1)
@@ -1099,8 +1111,6 @@ def visitProduct(self, prod, name):
10991111
(name, name, fields, len(prod.fields)), 1)
11001112
self.emit('%s);' % reflow_c_string(asdl_of(name, prod), 2), 2, reflow=False)
11011113
self.emit("if (!state->%s_type) return 0;" % name, 1)
1102-
self.emit_type("AST_type")
1103-
self.emit_type("%s_type" % name)
11041114
if prod.attributes:
11051115
self.emit("if (!add_attributes(state, state->%s_type, %s_attributes, %d)) return 0;" %
11061116
(name, name, len(prod.attributes)), 1)
@@ -1113,7 +1123,6 @@ def visitSum(self, sum, name):
11131123
self.emit('state->%s_type = make_type(state, "%s", state->AST_type, NULL, 0,' %
11141124
(name, name), 1)
11151125
self.emit('%s);' % reflow_c_string(asdl_of(name, sum), 2), 2, reflow=False)
1116-
self.emit_type("%s_type" % name)
11171126
self.emit("if (!state->%s_type) return 0;" % name, 1)
11181127
if sum.attributes:
11191128
self.emit("if (!add_attributes(state, state->%s_type, %s_attributes, %d)) return 0;" %
@@ -1134,7 +1143,6 @@ def visitConstructor(self, cons, name, simple):
11341143
(cons.name, cons.name, name, fields, len(cons.fields)), 1)
11351144
self.emit('%s);' % reflow_c_string(asdl_of(cons.name, cons), 2), 2, reflow=False)
11361145
self.emit("if (!state->%s_type) return 0;" % cons.name, 1)
1137-
self.emit_type("%s_type" % cons.name)
11381146
self.emit_defaults(cons.name, cons.fields, 1)
11391147
if simple:
11401148
self.emit("state->%s_singleton = PyType_GenericNew((PyTypeObject *)"
@@ -1439,17 +1447,8 @@ def generate_ast_fini(module_state, f):
14391447
"""))
14401448

14411449

1442-
def generate_module_def(mod, f, internal_h):
1450+
def generate_module_def(mod, metadata, f, internal_h):
14431451
# Gather all the data needed for ModuleSpec
1444-
visitor_list = set()
1445-
with open(os.devnull, "w") as devnull:
1446-
visitor = PyTypesDeclareVisitor(devnull)
1447-
visitor.visit(mod)
1448-
visitor_list.add(visitor)
1449-
visitor = PyTypesVisitor(devnull)
1450-
visitor.visit(mod)
1451-
visitor_list.add(visitor)
1452-
14531452
state_strings = {
14541453
"ast",
14551454
"_fields",
@@ -1458,16 +1457,19 @@ def generate_module_def(mod, f, internal_h):
14581457
"__dict__",
14591458
"__module__",
14601459
"_attributes",
1460+
*metadata.identifiers
14611461
}
1462+
14621463
module_state = state_strings.copy()
1463-
for visitor in visitor_list:
1464-
for identifier in visitor.identifiers:
1465-
module_state.add(identifier)
1466-
state_strings.add(identifier)
1467-
for singleton in visitor.singletons:
1468-
module_state.add(singleton)
1469-
for tp in visitor.types:
1470-
module_state.add(tp)
1464+
module_state.update(
1465+
"%s_singleton" % singleton
1466+
for singleton in metadata.singletons
1467+
)
1468+
module_state.update(
1469+
"%s_type" % type
1470+
for type in metadata.types
1471+
)
1472+
14711473
state_strings = sorted(state_strings)
14721474
module_state = sorted(module_state)
14731475

@@ -1583,7 +1585,7 @@ def write_internal_h_footer(mod, f):
15831585
"""), file=f)
15841586

15851587
def write_source(mod, metadata, f, internal_h_file):
1586-
generate_module_def(mod, f, internal_h_file)
1588+
generate_module_def(mod, metadata, f, internal_h_file)
15871589

15881590
v = ChainOfVisitors(
15891591
SequenceConstructorVisitor(f),

0 commit comments

Comments
 (0)