Skip to content

Commit ab0812c

Browse files
committed
Factor out set-up of sub_scope for template instantiation
1 parent c0a4278 commit ab0812c

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
@@ -120,6 +120,40 @@ void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
120120
}
121121
}
122122

123+
/// Set up a scope as subscope of the template scope
124+
cpp_scopet &cpp_typecheckt::sub_scope_for_instantiation(
125+
cpp_scopet &template_scope,
126+
const std::string &suffix)
127+
{
128+
cpp_scopet::id_sett id_set;
129+
template_scope.lookup(suffix, cpp_scopet::SCOPE_ONLY, id_set);
130+
131+
CHECK_RETURN(id_set.size() <= 1);
132+
133+
if(id_set.size() == 1)
134+
{
135+
cpp_idt &cpp_id = **id_set.begin();
136+
CHECK_RETURN(cpp_id.is_template_scope());
137+
138+
return static_cast<cpp_scopet &>(cpp_id);
139+
}
140+
else
141+
{
142+
cpp_scopet &sub_scope = template_scope.new_scope(suffix);
143+
sub_scope.id_class = cpp_idt::id_classt::TEMPLATE_SCOPE;
144+
sub_scope.prefix = template_scope.get_parent().prefix;
145+
sub_scope.suffix = suffix;
146+
sub_scope.add_using_scope(template_scope.get_parent());
147+
148+
const std::string subscope_name =
149+
id2string(template_scope.identifier) + suffix;
150+
cpp_scopes.id_map.insert(
151+
cpp_scopest::id_mapt::value_type(subscope_name, &sub_scope));
152+
153+
return sub_scope;
154+
}
155+
}
156+
123157
const symbolt &cpp_typecheckt::class_template_symbol(
124158
const source_locationt &source_location,
125159
const symbolt &template_symbol,
@@ -183,7 +217,8 @@ const symbolt &cpp_typecheckt::class_template_symbol(
183217
symbol_table.move(new_symbol, s_ptr);
184218

185219
// put into template scope
186-
cpp_idt &id=cpp_scopes.put_into_scope(*s_ptr, *template_scope);
220+
cpp_scopet &sub_scope = sub_scope_for_instantiation(*template_scope, suffix);
221+
cpp_idt &id = cpp_scopes.put_into_scope(*s_ptr, sub_scope);
187222

188223
id.id_class=cpp_idt::id_classt::CLASS;
189224
id.is_scope=true;
@@ -339,18 +374,12 @@ const symbolt &cpp_typecheckt::instantiate_template(
339374
class_name=cpp_scopes.current_scope().get_parent().identifier;
340375

341376
// sub-scope for fixing the prefix
342-
std::string subscope_name=id2string(template_scope->identifier)+suffix;
377+
cpp_scopet &sub_scope = sub_scope_for_instantiation(*template_scope, suffix);
343378

344379
// let's see if we have the instance already
345-
cpp_scopest::id_mapt::iterator scope_it=
346-
cpp_scopes.id_map.find(subscope_name);
347-
348-
if(scope_it!=cpp_scopes.id_map.end())
349380
{
350-
cpp_scopet &scope=cpp_scopes.get_scope(subscope_name);
351-
352381
cpp_scopet::id_sett id_set;
353-
scope.lookup(template_symbol.base_name, cpp_scopet::SCOPE_ONLY, id_set);
382+
sub_scope.lookup(template_symbol.base_name, cpp_scopet::SCOPE_ONLY, id_set);
354383

355384
if(id_set.size()==1)
356385
{
@@ -370,20 +399,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
370399
return symb;
371400
}
372401

373-
cpp_scopes.go_to(scope);
374-
}
375-
else
376-
{
377-
// set up a scope as subscope of the template scope
378-
cpp_scopet &sub_scope=
379-
cpp_scopes.current_scope().new_scope(subscope_name);
380-
sub_scope.id_class=cpp_idt::id_classt::TEMPLATE_SCOPE;
381-
sub_scope.prefix=template_scope->get_parent().prefix;
382-
sub_scope.suffix=suffix;
383-
sub_scope.add_using_scope(template_scope->get_parent());
384402
cpp_scopes.go_to(sub_scope);
385-
cpp_scopes.id_map.insert(
386-
cpp_scopest::id_mapt::value_type(subscope_name, &sub_scope));
387403
}
388404

389405
// 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)