@@ -103,6 +103,40 @@ void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
103103 }
104104}
105105
106+ // / Set up a scope as subscope of the template scope
107+ cpp_scopet &cpp_typecheckt::sub_scope_for_instantiation (
108+ cpp_scopet &template_scope,
109+ const std::string &suffix)
110+ {
111+ cpp_scopet::id_sett id_set =
112+ template_scope.lookup (suffix, cpp_scopet::SCOPE_ONLY);
113+
114+ CHECK_RETURN (id_set.size () <= 1 );
115+
116+ if (id_set.size () == 1 )
117+ {
118+ cpp_idt &cpp_id = **id_set.begin ();
119+ CHECK_RETURN (cpp_id.is_template_scope ());
120+
121+ return static_cast <cpp_scopet &>(cpp_id);
122+ }
123+ else
124+ {
125+ cpp_scopet &sub_scope = template_scope.new_scope (suffix);
126+ sub_scope.id_class = cpp_idt::id_classt::TEMPLATE_SCOPE;
127+ sub_scope.prefix = template_scope.get_parent ().prefix ;
128+ sub_scope.suffix = suffix;
129+ sub_scope.add_using_scope (template_scope.get_parent ());
130+
131+ const std::string subscope_name =
132+ id2string (template_scope.identifier ) + suffix;
133+ cpp_scopes.id_map .insert (
134+ cpp_scopest::id_mapt::value_type (subscope_name, &sub_scope));
135+
136+ return sub_scope;
137+ }
138+ }
139+
106140const symbolt &cpp_typecheckt::class_template_symbol (
107141 const source_locationt &source_location,
108142 const symbolt &template_symbol,
@@ -318,18 +352,12 @@ const symbolt &cpp_typecheckt::instantiate_template(
318352 class_name=cpp_scopes.current_scope ().get_parent ().identifier ;
319353
320354 // sub-scope for fixing the prefix
321- std::string subscope_name= id2string ( template_scope-> identifier )+ suffix;
355+ cpp_scopet &sub_scope = sub_scope_for_instantiation (* template_scope, suffix) ;
322356
323357 // let's see if we have the instance already
324- cpp_scopest::id_mapt::iterator scope_it=
325- cpp_scopes.id_map .find (subscope_name);
326-
327- if (scope_it!=cpp_scopes.id_map .end ())
328358 {
329- cpp_scopet &scope=cpp_scopes.get_scope (subscope_name);
330-
331- const auto id_set =
332- scope.lookup (template_symbol.base_name , cpp_scopet::SCOPE_ONLY);
359+ cpp_scopet::id_sett id_set =
360+ sub_scope.lookup (template_symbol.base_name , cpp_scopet::SCOPE_ONLY);
333361
334362 if (id_set.size ()==1 )
335363 {
@@ -349,20 +377,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
349377 return symb;
350378 }
351379
352- cpp_scopes.go_to (scope);
353- }
354- else
355- {
356- // set up a scope as subscope of the template scope
357- cpp_scopet &sub_scope=
358- cpp_scopes.current_scope ().new_scope (subscope_name);
359- sub_scope.id_class =cpp_idt::id_classt::TEMPLATE_SCOPE;
360- sub_scope.prefix =template_scope->get_parent ().prefix ;
361- sub_scope.suffix =suffix;
362- sub_scope.add_using_scope (template_scope->get_parent ());
363380 cpp_scopes.go_to (sub_scope);
364- cpp_scopes.id_map .insert (
365- cpp_scopest::id_mapt::value_type (subscope_name, &sub_scope));
366381 }
367382
368383 // store the information that the template has
0 commit comments