Skip to content

Commit 38b6bb2

Browse files
author
kroening
committed
templates now use proper suffixes
git-svn-id: svn+ssh://svn.cprover.org/srv/svn/cbmc/trunk@5667 6afb6bc1-c8e4-404c-8f48-9ae832c5b171
1 parent 9da170a commit 38b6bb2

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

src/cpp/cpp_id.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ void cpp_idt::print_fields(std::ostream &out, unsigned indent) const
8383
for(unsigned i=0; i<indent; i++) out << ' ';
8484
out << " prefix=" << prefix << std::endl;
8585

86+
for(unsigned i=0; i<indent; i++) out << ' ';
87+
out << " suffix=" << suffix << std::endl;
88+
8689
for(unsigned i=0; i<indent; i++) out << ' ';
8790
out << " base_name=" << base_name << std::endl;
8891

src/cpp/cpp_id.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ class cpp_idt
5959
{
6060
return id_class==TYPEDEF;
6161
}
62-
62+
6363
irep_idt identifier, base_name;
6464

6565
// if it is a member or method, what class is it in?
6666
irep_idt class_identifier;
6767
exprt this_expr;
6868

6969
// scope data
70-
std::string prefix;
70+
std::string prefix, suffix;
7171
unsigned compound_counter;
7272

7373
cpp_idt &insert(const irep_idt &_base_name)

src/cpp/cpp_instantiate_template.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ const symbolt &cpp_typecheckt::class_template_symbol(
163163

164164
irep_idt identifier=
165165
id2string(template_scope->prefix)+
166-
id2string(suffix)+
167-
"tag-"+id2string(template_symbol.base_name);
166+
"tag-"+id2string(template_symbol.base_name)+
167+
id2string(suffix);
168168

169169
// already there?
170170
symbol_tablet::symbolst::const_iterator s_it=symbol_table.symbols.find(identifier);
@@ -317,7 +317,7 @@ const symbolt &cpp_typecheckt::instantiate_template(
317317
if(template_scope==NULL)
318318
{
319319
err_location(source_location);
320-
str << "identifier: " << template_symbol.name << std::endl;
320+
str << "identifier: " << template_symbol.name << '\n';
321321
throw "template instantiation error: scope not found";
322322
}
323323

@@ -391,16 +391,17 @@ const symbolt &cpp_typecheckt::instantiate_template(
391391
else
392392
{
393393
// set up a scope as subscope of the template scope
394-
std::string prefix=template_scope->get_parent().prefix+suffix;
395394
cpp_scopet &sub_scope=
396395
cpp_scopes.current_scope().new_scope(subscope_name);
397-
sub_scope.prefix=prefix;
396+
sub_scope.id_class=cpp_idt::TEMPLATE_SCOPE;
397+
sub_scope.prefix=template_scope->get_parent().prefix;
398+
sub_scope.suffix=suffix;
398399
sub_scope.add_using_scope(template_scope->get_parent());
399400
cpp_scopes.go_to(sub_scope);
400401
cpp_scopes.id_map.insert(
401402
cpp_scopest::id_mapt::value_type(subscope_name, &sub_scope));
402403
}
403-
404+
404405
// store the information that the template has
405406
// been instantiated using these arguments
406407
{

src/cpp/cpp_scope.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ class cpp_scopet:public cpp_idt
5555
id_class==NAMESPACE;
5656
}
5757

58+
inline bool is_template_scope() const
59+
{
60+
return id_class==TEMPLATE_SCOPE;
61+
}
62+
5863
cpp_scopet &get_parent() const
5964
{
6065
return static_cast<cpp_scopet &>(cpp_idt::get_parent());
@@ -82,8 +87,7 @@ class cpp_scopet:public cpp_idt
8287
using_scopes.push_back(&other);
8388
}
8489

85-
class cpp_scopet &new_scope(
86-
const irep_idt &new_scope_name);
90+
class cpp_scopet &new_scope(const irep_idt &new_scope_name);
8791
};
8892

8993
class cpp_root_scopet:public cpp_scopet

src/cpp/cpp_scopes.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ Function: cpp_scopest::new_block_scope
2727
cpp_scopet &cpp_scopest::new_block_scope()
2828
{
2929
unsigned prefix=++current_scope().compound_counter;
30-
cpp_scopet &n=new_scope(i2string(prefix));
31-
n.id_class=cpp_idt::BLOCK_SCOPE;
32-
return n;
30+
return new_scope(i2string(prefix), cpp_idt::BLOCK_SCOPE);
3331
}
3432

3533
/*******************************************************************\

src/cpp/cpp_scopes.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,34 @@ Author: Daniel Kroening, [email protected]
2020
class cpp_scopest
2121
{
2222
public:
23-
cpp_scopest()
23+
inline cpp_scopest()
2424
{
2525
current_scope_ptr=&root_scope;
2626
}
2727

2828
typedef std::set<cpp_scopet *> scope_sett;
2929
typedef std::set<cpp_idt *> id_sett;
3030

31-
cpp_scopet &current_scope()
31+
inline cpp_scopet &current_scope()
3232
{
3333
return *current_scope_ptr;
3434
}
3535

36-
cpp_scopet &new_scope(const irep_idt &new_scope_name)
36+
cpp_scopet &new_scope(
37+
const irep_idt &new_scope_name,
38+
cpp_idt::id_classt id_class)
3739
{
3840
assert(!new_scope_name.empty());
3941
cpp_scopet &n=current_scope_ptr->new_scope(new_scope_name);
42+
n.id_class=id_class;
4043
id_map[n.identifier]=&n;
4144
current_scope_ptr=&n;
4245
return n;
4346
}
4447

45-
cpp_scopet &new_namespace(const irep_idt &new_scope_name)
48+
inline cpp_scopet &new_namespace(const irep_idt &new_scope_name)
4649
{
47-
cpp_scopet &n=new_scope(new_scope_name);
48-
n.id_class=cpp_idt::NAMESPACE;
49-
return n;
50+
return new_scope(new_scope_name, cpp_idt::NAMESPACE);
5051
}
5152

5253
cpp_scopet &new_block_scope();

src/cpp/cpp_typecheck_compound_type.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,15 @@ void cpp_typecheckt::typecheck_compound_type(
191191
dest_scope=&cpp_typecheck_resolve.resolve_scope(cpp_name, base_name, t_args);
192192
}
193193
}
194-
194+
195195
// The identifier 'tag-X' matches what the C front-end does!
196196
// The hypen is deliberate to avoid collisions with other
197197
// identifiers.
198198
const irep_idt symbol_name=
199199
dest_scope->prefix+
200-
"tag-"+id2string(base_name);
201-
200+
"tag-"+id2string(base_name)+
201+
dest_scope->suffix;
202+
202203
// check if we have it already
203204

204205
symbol_tablet::symbolst::iterator previous_symbol=
@@ -207,7 +208,7 @@ void cpp_typecheckt::typecheck_compound_type(
207208
if(previous_symbol!=symbol_table.symbols.end())
208209
{
209210
// we do!
210-
211+
211212
symbolt &symbol=previous_symbol->second;
212213

213214
if(has_body)
@@ -247,8 +248,11 @@ void cpp_typecheckt::typecheck_compound_type(
247248
symbol.type.swap(type);
248249
symbol.is_type=true;
249250
symbol.is_macro=false;
250-
symbol.pretty_name=cpp_scopes.current_scope().prefix+id2string(symbol.base_name);
251-
symbol.type.set(ID_tag, symbol.pretty_name);
251+
symbol.pretty_name=
252+
cpp_scopes.current_scope().prefix+
253+
id2string(symbol.base_name)+
254+
cpp_scopes.current_scope().suffix;
255+
symbol.type.set(ID_tag, cpp_scopes.current_scope().prefix+id2string(symbol.base_name));
252256

253257
// move early, must be visible before doing body
254258
symbolt *new_symbol;
@@ -262,10 +266,11 @@ void cpp_typecheckt::typecheck_compound_type(
262266
id.id_class=cpp_idt::CLASS;
263267
id.is_scope=true;
264268
id.prefix=cpp_scopes.current_scope().prefix+
265-
id2string(new_symbol->base_name)+"::";
269+
id2string(new_symbol->base_name)+
270+
cpp_scopes.current_scope().suffix+"::";
266271
id.class_identifier=new_symbol->name;
267272
id.id_class=cpp_idt::CLASS;
268-
273+
269274
if(has_body)
270275
typecheck_compound_body(*new_symbol);
271276
else

0 commit comments

Comments
 (0)