@@ -103,6 +103,40 @@ void cpp_typecheckt::show_instantiation_stack(std::ostream &out)
103
103
}
104
104
}
105
105
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, id_set);
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
+
106
140
const symbolt &cpp_typecheckt::class_template_symbol (
107
141
const source_locationt &source_location,
108
142
const symbolt &template_symbol,
@@ -167,7 +201,8 @@ const symbolt &cpp_typecheckt::class_template_symbol(
167
201
symbol_table.move (new_symbol, s_ptr);
168
202
169
203
// put into template scope
170
- cpp_idt &id=cpp_scopes.put_into_scope (*s_ptr, *template_scope);
204
+ cpp_scopet &sub_scope = sub_scope_for_instantiation (*template_scope, suffix);
205
+ cpp_idt &id = cpp_scopes.put_into_scope (*s_ptr, sub_scope);
171
206
172
207
id.id_class =cpp_idt::id_classt::CLASS;
173
208
id.is_scope =true ;
@@ -318,18 +353,12 @@ const symbolt &cpp_typecheckt::instantiate_template(
318
353
class_name=cpp_scopes.current_scope ().get_parent ().identifier ;
319
354
320
355
// sub-scope for fixing the prefix
321
- std::string subscope_name= id2string ( template_scope-> identifier )+ suffix;
356
+ cpp_scopet &sub_scope = sub_scope_for_instantiation (* template_scope, suffix) ;
322
357
323
358
// 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 ())
328
359
{
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);
360
+ cpp_scopet::id_sett id_set;
361
+ sub_scope.lookup (template_symbol.base_name , cpp_scopet::SCOPE_ONLY, id_set);
333
362
334
363
if (id_set.size ()==1 )
335
364
{
@@ -349,20 +378,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
349
378
return symb;
350
379
}
351
380
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 ());
363
381
cpp_scopes.go_to (sub_scope);
364
- cpp_scopes.id_map .insert (
365
- cpp_scopest::id_mapt::value_type (subscope_name, &sub_scope));
366
382
}
367
383
368
384
// store the information that the template has
0 commit comments