Skip to content

Commit 4c0915f

Browse files
committed
Factor out set-up of sub_scope for template instantiation
1 parent 2466615 commit 4c0915f

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

src/cpp/cpp_instantiate_template.cpp

+37-22
Original file line numberDiff line numberDiff line change
@@ -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+
106140
const 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

src/cpp/cpp_typecheck.h

+4
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ class cpp_typecheckt:public c_typecheck_baset
230230
std::string template_suffix(
231231
const cpp_template_args_tct &template_args);
232232

233+
cpp_scopet &sub_scope_for_instantiation(
234+
cpp_scopet &template_scope,
235+
const std::string &suffix);
236+
233237
void
234238
convert_parameters(const irep_idt &current_mode, code_typet &function_type);
235239

0 commit comments

Comments
 (0)