Skip to content

Commit 86f53e4

Browse files
committed
Factor out set-up of sub_scope for template instantiation
1 parent 0d07f24 commit 86f53e4

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/cpp/cpp_instantiate_template.cpp

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,40 @@ void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
110110
}
111111
}
112112

113+
/// Set up a scope as subscope of the template scope
114+
cpp_scopet &cpp_typecheckt::sub_scope_for_instantiation(
115+
cpp_scopet &template_scope,
116+
const std::string &suffix)
117+
{
118+
cpp_scopet::id_sett id_set;
119+
template_scope.lookup(suffix, cpp_scopet::SCOPE_ONLY, id_set);
120+
121+
CHECK_RETURN(id_set.size() <= 1);
122+
123+
if(id_set.size() == 1)
124+
{
125+
cpp_idt &cpp_id = **id_set.begin();
126+
CHECK_RETURN(cpp_id.is_template_scope());
127+
128+
return static_cast<cpp_scopet &>(cpp_id);
129+
}
130+
else
131+
{
132+
cpp_scopet &sub_scope = template_scope.new_scope(suffix);
133+
sub_scope.id_class = cpp_idt::id_classt::TEMPLATE_SCOPE;
134+
sub_scope.prefix = template_scope.get_parent().prefix;
135+
sub_scope.suffix = suffix;
136+
sub_scope.add_using_scope(template_scope.get_parent());
137+
138+
const std::string subscope_name =
139+
id2string(template_scope.identifier) + suffix;
140+
cpp_scopes.id_map.insert(
141+
cpp_scopest::id_mapt::value_type(subscope_name, &sub_scope));
142+
143+
return sub_scope;
144+
}
145+
}
146+
113147
const symbolt &cpp_typecheckt::class_template_symbol(
114148
const source_locationt &source_location,
115149
const symbolt &template_symbol,
@@ -173,7 +207,8 @@ const symbolt &cpp_typecheckt::class_template_symbol(
173207
symbol_table.move(new_symbol, s_ptr);
174208

175209
// put into template scope
176-
cpp_idt &id=cpp_scopes.put_into_scope(*s_ptr, *template_scope);
210+
cpp_scopet &sub_scope = sub_scope_for_instantiation(*template_scope, suffix);
211+
cpp_idt &id = cpp_scopes.put_into_scope(*s_ptr, sub_scope);
177212

178213
id.id_class=cpp_idt::id_classt::CLASS;
179214
id.is_scope=true;
@@ -325,18 +360,12 @@ const symbolt &cpp_typecheckt::instantiate_template(
325360
class_name=cpp_scopes.current_scope().get_parent().identifier;
326361

327362
// sub-scope for fixing the prefix
328-
std::string subscope_name=id2string(template_scope->identifier)+suffix;
363+
cpp_scopet &sub_scope = sub_scope_for_instantiation(*template_scope, suffix);
329364

330365
// let's see if we have the instance already
331-
cpp_scopest::id_mapt::iterator scope_it=
332-
cpp_scopes.id_map.find(subscope_name);
333-
334-
if(scope_it!=cpp_scopes.id_map.end())
335366
{
336-
cpp_scopet &scope=cpp_scopes.get_scope(subscope_name);
337-
338367
cpp_scopet::id_sett id_set;
339-
scope.lookup(template_symbol.base_name, cpp_scopet::SCOPE_ONLY, id_set);
368+
sub_scope.lookup(template_symbol.base_name, cpp_scopet::SCOPE_ONLY, id_set);
340369

341370
if(id_set.size()==1)
342371
{
@@ -356,20 +385,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
356385
return symb;
357386
}
358387

359-
cpp_scopes.go_to(scope);
360-
}
361-
else
362-
{
363-
// set up a scope as subscope of the template scope
364-
cpp_scopet &sub_scope=
365-
cpp_scopes.current_scope().new_scope(subscope_name);
366-
sub_scope.id_class=cpp_idt::id_classt::TEMPLATE_SCOPE;
367-
sub_scope.prefix=template_scope->get_parent().prefix;
368-
sub_scope.suffix=suffix;
369-
sub_scope.add_using_scope(template_scope->get_parent());
370388
cpp_scopes.go_to(sub_scope);
371-
cpp_scopes.id_map.insert(
372-
cpp_scopest::id_mapt::value_type(subscope_name, &sub_scope));
373389
}
374390

375391
// store the information that the template has

src/cpp/cpp_typecheck.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ class cpp_typecheckt:public c_typecheck_baset
231231
std::string template_suffix(
232232
const cpp_template_args_tct &template_args);
233233

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

0 commit comments

Comments
 (0)