@@ -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);
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,
@@ -131,10 +165,9 @@ const symbolt &cpp_typecheckt::class_template_symbol(
131
165
INVARIANT_STRUCTURED (
132
166
template_scope!=nullptr , nullptr_exceptiont, " template_scope is null" );
133
167
134
- irep_idt identifier=
135
- id2string (template_scope->prefix )+
136
- " tag-" +id2string (template_symbol.base_name )+
137
- id2string (suffix);
168
+ irep_idt identifier = id2string (template_scope->get_parent ().prefix ) +
169
+ " tag-" + id2string (template_symbol.base_name ) +
170
+ id2string (suffix);
138
171
139
172
// already there?
140
173
symbol_tablet::symbolst::const_iterator s_it=
@@ -171,9 +204,8 @@ const symbolt &cpp_typecheckt::class_template_symbol(
171
204
172
205
id.id_class =cpp_idt::id_classt::CLASS;
173
206
id.is_scope =true ;
174
- id.prefix =template_scope->prefix +
175
- id2string (s_ptr->base_name )+
176
- id2string (suffix)+" ::" ;
207
+ id.prefix = template_scope->get_parent ().prefix +
208
+ id2string (s_ptr->base_name ) + id2string (suffix) + " ::" ;
177
209
id.class_identifier =s_ptr->name ;
178
210
id.id_class =cpp_idt::id_classt::CLASS;
179
211
@@ -318,18 +350,12 @@ const symbolt &cpp_typecheckt::instantiate_template(
318
350
class_name=cpp_scopes.current_scope ().get_parent ().identifier ;
319
351
320
352
// sub-scope for fixing the prefix
321
- std::string subscope_name= id2string ( template_scope-> identifier )+ suffix;
353
+ cpp_scopet &sub_scope = sub_scope_for_instantiation (* template_scope, suffix) ;
322
354
323
355
// 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
356
{
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);
357
+ cpp_scopet::id_sett id_set =
358
+ sub_scope.lookup (template_symbol.base_name , cpp_scopet::SCOPE_ONLY);
333
359
334
360
if (id_set.size ()==1 )
335
361
{
@@ -349,20 +375,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
349
375
return symb;
350
376
}
351
377
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
378
cpp_scopes.go_to (sub_scope);
364
- cpp_scopes.id_map .insert (
365
- cpp_scopest::id_mapt::value_type (subscope_name, &sub_scope));
366
379
}
367
380
368
381
// store the information that the template has
0 commit comments