@@ -120,6 +120,40 @@ void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
120
120
}
121
121
}
122
122
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
+
123
157
const symbolt &cpp_typecheckt::class_template_symbol (
124
158
const source_locationt &source_location,
125
159
const symbolt &template_symbol,
@@ -183,7 +217,8 @@ const symbolt &cpp_typecheckt::class_template_symbol(
183
217
symbol_table.move (new_symbol, s_ptr);
184
218
185
219
// 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);
187
222
188
223
id.id_class =cpp_idt::id_classt::CLASS;
189
224
id.is_scope =true ;
@@ -339,18 +374,12 @@ const symbolt &cpp_typecheckt::instantiate_template(
339
374
class_name=cpp_scopes.current_scope ().get_parent ().identifier ;
340
375
341
376
// 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) ;
343
378
344
379
// 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 ())
349
380
{
350
- cpp_scopet &scope=cpp_scopes.get_scope (subscope_name);
351
-
352
381
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);
354
383
355
384
if (id_set.size ()==1 )
356
385
{
@@ -370,20 +399,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
370
399
return symb;
371
400
}
372
401
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 ());
384
402
cpp_scopes.go_to (sub_scope);
385
- cpp_scopes.id_map .insert (
386
- cpp_scopest::id_mapt::value_type (subscope_name, &sub_scope));
387
403
}
388
404
389
405
// store the information that the template has
0 commit comments