1818
1919
2020import operator
21+ import re
2122
2223import nodes
2324import codegen
@@ -146,11 +147,14 @@ def named_type(self, type):
146147
147148 def declare (self , definition_type , name ):
148149 schema_name = self .schema_name
149- self .statements .append ("%(definition_type)s* %(schema_name)s_%(name)s_type = 0;" % locals ())
150+ # self.statements.append("%(definition_type)s* %(schema_name)s_%(name)s_type = 0;" % locals())
150151 self .names .append (name )
151152
152153 def begin_schema (self ):
153154 self .names .sort (key = str .lower )
155+ schema_name = self .schema_name
156+ num_names = len (self .names )
157+ self .statements .append ("declaration* %(schema_name)s_types[%(num_names)d] = {nullptr};" % locals ())
154158
155159 self .statements .append ("{factory_placeholder}" )
156160
@@ -172,7 +176,7 @@ def typedef(self, name, declared_type):
172176 schema_name = self .schema_name
173177 index_in_schema = self .names .index (name )
174178 self .statements .append (
175- ' %(schema_name)s_%(name)s_type = new type_declaration("%(name)s", %(index_in_schema)d, %(declared_type)s);'
179+ ' %(schema_name)s_types[%(index_in_schema)d] = new type_declaration("%(name)s", %(index_in_schema)d, %(declared_type)s);'
176180 % locals ()
177181 )
178182
@@ -183,18 +187,18 @@ def enumeration(self, name, enum):
183187 self .statements .append (" std::vector<std::string> items; items.reserve(%d);" % len (enum .values ))
184188 self .statements .extend (map (lambda v : ' items.push_back("%s");' % v , sorted (enum .values )))
185189 self .statements .append (
186- ' %(schema_name)s_%(name)s_type = new enumeration_type("%(name)s", %(index_in_schema)d, items);'
190+ ' %(schema_name)s_types[%(index_in_schema)d] = new enumeration_type("%(name)s", %(index_in_schema)d, items);'
187191 % locals ()
188192 )
189193 self .statements .append (" }" )
190194
191195 def entity (self , name , type ):
192196 schema_name = self .schema_name
193197 index_in_schema = self .names .index (name )
194- supertype = "0" if len (type .supertypes ) == 0 else "%s_%s_type " % (self .schema_name , type .supertypes [0 ])
198+ supertype = "0" if len (type .supertypes ) == 0 else "%s_types[%d] " % (self .schema_name , self . names . index ( type .supertypes [0 ]) )
195199 is_abstract = "true" if type .abstract else "false"
196200 self .statements .append (
197- ' %(schema_name)s_%(name)s_type = new entity("%(name)s", %(is_abstract)s, %(index_in_schema)d, %(supertype)s);'
201+ ' %(schema_name)s_types[%(index_in_schema)d] = new entity("%(name)s", %(is_abstract)s, %(index_in_schema)d, (entity*) %(supertype)s);'
198202 % locals ()
199203 )
200204
@@ -204,15 +208,16 @@ def select(self, name, type):
204208 self .statements .append (" {" )
205209 self .statements .append (" std::vector<const declaration*> items; items.reserve(%d);" % len (type .values ))
206210 self .statements .extend (
207- map (lambda v : " items.push_back(%s_%s_type );" % (self .schema_name , v ), sorted (map (str , type .values )))
211+ map (lambda v : " items.push_back(%s_types[%d] );" % (self .schema_name , self . names . index ( v ) ), sorted (map (str , type .values )))
208212 )
209213 self .statements .append (
210- ' %(schema_name)s_%(name)s_type = new select_type("%(name)s", %(index_in_schema)d, items);'
214+ ' %(schema_name)s_types[%(index_in_schema)d] = new select_type("%(name)s", %(index_in_schema)d, items);'
211215 % locals ()
212216 )
213217 self .statements .append (" }" )
214218
215219 def entity_attributes (self , name , attribute_definitions , is_derived ):
220+ index_in_schema = self .names .index (name )
216221 schema_name = self .schema_name
217222 self .statements .append (" {" )
218223 self .statements .append (
@@ -228,31 +233,37 @@ def entity_attributes(self, name, attribute_definitions, is_derived):
228233 self .statements .append (
229234 " " + " " .join (map (lambda b : "derived.push_back(%s);" % str (b ).lower (), is_derived ))
230235 )
231- self .statements .append (" %(schema_name)s_%(name)s_type ->set_attributes(attributes, derived);" % locals ())
236+ self .statements .append (" ((entity*) %(schema_name)s_types[%(index_in_schema)d]) ->set_attributes(attributes, derived);" % locals ())
232237 self .statements .append (" }" )
233238
234239 def inverse_attributes (self , name , inv_attrs ):
235240 schema_name = self .schema_name
241+ index_in_schema = self .names .index (name )
236242 self .statements .append (" {" )
237243 self .statements .append (
238244 " std::vector<const inverse_attribute*> attributes; attributes.reserve(%d);" % len (inv_attrs )
239245 )
240246 for attr_name , aggr_type , bound1 , bound2 , entity_ref , attribute_entity , attribute_entity_index in inv_attrs :
247+ opposite_index_in_schema = self .names .index (entity_ref )
248+ opposite1 = '%(schema_name)s_types[%(opposite_index_in_schema)d]' % locals ()
249+ opposite_index_in_schema = self .names .index (attribute_entity )
250+ opposite2 = '%(schema_name)s_types[%(opposite_index_in_schema)d]' % locals ()
241251 self .statements .append (
242- ' attributes.push_back(new inverse_attribute("%(attr_name)s", inverse_attribute::%(aggr_type)s_type, %(bound1)d, %(bound2)d, %(schema_name)s_%(entity_ref)s_type, %(schema_name)s_%(attribute_entity)s_type ->attributes()[%(attribute_entity_index)d]));'
252+ ' attributes.push_back(new inverse_attribute("%(attr_name)s", inverse_attribute::%(aggr_type)s_type, %(bound1)d, %(bound2)d, ((entity*) %(opposite1)s), ((entity*) %(opposite2)s) ->attributes()[%(attribute_entity_index)d]));'
243253 % locals ()
244254 )
245- self .statements .append (" %(schema_name)s_%(name)s_type ->set_inverse_attributes(attributes);" % locals ())
255+ self .statements .append (" ((entity*) %(schema_name)s_types[%(index_in_schema)d]) ->set_inverse_attributes(attributes);" % locals ())
246256 self .statements .append (" }" )
247257
248258 def entity_subtypes (self , name , tys ):
249259 schema_name = self .schema_name
260+ index_in_schema = self .names .index (name )
250261 self .statements .append (" {" )
251262 self .statements .append (" std::vector<const entity*> defs; defs.reserve(%d);" % len (tys ))
252263 self .statements .append (
253- (" " + "" .join (map (lambda t : ("defs.push_back(%%(schema_name)s_%s_type) ;" % t ), tys ))) % locals ()
264+ (" " + "" .join (map (lambda t : ("defs.push_back(((entity*) %%(schema_name)s_types[%d])) ;" % self . names . index ( t ) ), tys ))) % locals ()
254265 )
255- self .statements .append (" %(schema_name)s_%(name)s_type ->set_subtypes(defs);" % locals ())
266+ self .statements .append (" ((entity*) %(schema_name)s_types[%(index_in_schema)d]) ->set_subtypes(defs);" % locals ())
256267 self .statements .append (" }" )
257268
258269 def finalize (self , can_be_instantiated_set ):
@@ -266,7 +277,8 @@ def finalize(self, can_be_instantiated_set):
266277 " std::vector<const declaration*> declarations; declarations.reserve(%(num_declarations)d);" % locals ()
267278 )
268279 for type_name in self .names :
269- self .statements .append (" declarations.push_back(%(schema_name)s_%(type_name)s_type);" % locals ())
280+ index_in_schema = self .names .index (type_name )
281+ self .statements .append (" declarations.push_back(%(schema_name)s_types[%(index_in_schema)d]);" % locals ())
270282
271283 self .statements .append (
272284 ' return new schema_definition("%(schema_name)s", declarations, new %(schema_name)s_instance_factory());'
@@ -352,6 +364,23 @@ class UnmetDependenciesException(Exception):
352364
353365 x = code (schema_name )
354366
367+ def transform_to_indexed (fn ):
368+ def wrapper (* args , ** kwargs ):
369+ declared_type = fn (* args , ** kwargs )
370+ if 'simple_type' in declared_type :
371+ pass
372+ else :
373+ match = re .search (r'\((\w+?_[\w+]+?_\w+?)\)' , declared_type )
374+ if match :
375+ old_decl = match .group (1 )
376+ tn = old_decl .rsplit ('_' , 2 )[1 ]
377+ idx = x .names .index (tn )
378+ snu = schema_name .upper ()
379+ declared_type = declared_type .replace (old_decl , '%(snu)s_types[%(idx)d]' % locals ())
380+ return declared_type
381+ return wrapper if code == EarlyBoundCodeWriter else fn
382+
383+ @transform_to_indexed
355384 def get_declared_type (type , emitted_names = None ):
356385 if isinstance (type , nodes .SimpleType ):
357386 type = type .type
@@ -469,9 +498,8 @@ def write(name):
469498 if write (name ):
470499 emitted .add (name .lower ())
471500 declarations_by_index .append (name )
472- declared_types .append ("%(schema_name)s_%(name)s_type" % locals ())
473501
474- num_declarations = len (declared_types )
502+ num_declarations = len (emitted )
475503
476504 for name , type in mapping .schema .entities .items ():
477505 derived = set (mapping .derived_in_supertype (type ))
0 commit comments