Skip to content

Commit d87c2db

Browse files
author
Daniel Kroening
authored
Merge pull request #2203 from diffblue/typed-lookup
strongly type the lookup() methods in namespacet
2 parents 2382f27 + d77dea3 commit d87c2db

15 files changed

+80
-79
lines changed

Diff for: src/ansi-c/c_typecheck_expr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ void c_typecheck_baset::typecheck_expr_member(exprt &expr)
14691469
exprt &op0=expr.op0();
14701470
typet type=op0.type();
14711471

1472-
follow_symbol(type);
1472+
type = follow(type);
14731473

14741474
if(type.id()==ID_incomplete_struct)
14751475
{

Diff for: src/cpp/cpp_constructor.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ codet cpp_typecheckt::cpp_constructor(
3333

3434
elaborate_class_template(object_tc.type());
3535

36-
typet tmp_type(object_tc.type());
37-
follow_symbol(tmp_type);
36+
typet tmp_type(follow(object_tc.type()));
3837

3938
assert(!is_reference(tmp_type));
4039

Diff for: src/cpp/cpp_destructor.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ codet cpp_typecheckt::cpp_destructor(
2626

2727
elaborate_class_template(object.type());
2828

29-
typet tmp_type(object.type());
30-
follow_symbol(tmp_type);
29+
typet tmp_type(follow(object.type()));
3130

3231
assert(!is_reference(tmp_type));
3332

Diff for: src/cpp/cpp_instantiate_template.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void cpp_typecheckt::elaborate_class_template(
189189
if(type.id()!=ID_symbol)
190190
return;
191191

192-
const symbolt &symbol=lookup(type);
192+
const symbolt &symbol = lookup(to_symbol_type(type));
193193

194194
// Make a copy, as instantiate will destroy the symbol type!
195195
const typet t_type=symbol.type;

Diff for: src/cpp/cpp_typecheck_bases.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ void cpp_typecheckt::typecheck_compound_bases(struct_typet &type)
5151
throw 0;
5252
}
5353

54-
const symbolt &base_symbol=
55-
lookup(base_symbol_expr.type());
54+
const symbolt &base_symbol =
55+
lookup(to_symbol_type(base_symbol_expr.type()));
5656

5757
if(base_symbol.type.id()==ID_incomplete_struct)
5858
{

Diff for: src/cpp/cpp_typecheck_expr.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ void cpp_typecheckt::typecheck_expr_main(exprt &expr)
100100
typecheck_type(base);
101101
typecheck_type(deriv);
102102

103-
follow_symbol(base);
104-
follow_symbol(deriv);
103+
base = follow(base);
104+
deriv = follow(deriv);
105105

106106
if(base.id()!=ID_struct || deriv.id()!=ID_struct)
107107
expr=false_exprt();
@@ -1991,7 +1991,7 @@ void cpp_typecheckt::typecheck_side_effect_function_call(
19911991

19921992
// look at type of function
19931993

1994-
follow_symbol(expr.function().type());
1994+
expr.function().type() = follow(expr.function().type());
19951995

19961996
if(expr.function().type().id()==ID_pointer)
19971997
{

Diff for: src/cpp/cpp_typecheck_resolve.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ exprt cpp_typecheck_resolvet::convert_identifier(
352352

353353
while(followed_type.id()==ID_symbol)
354354
{
355-
typet tmp=cpp_typecheck.lookup(followed_type).type;
356-
followed_type=tmp;
355+
followed_type =
356+
cpp_typecheck.follow(to_symbol_type(followed_type));
357357
constant |= followed_type.get_bool(ID_C_constant);
358358
}
359359

@@ -1899,8 +1899,7 @@ exprt cpp_typecheck_resolvet::guess_function_template_args(
18991899
const exprt &expr,
19001900
const cpp_typecheck_fargst &fargs)
19011901
{
1902-
typet tmp=expr.type();
1903-
cpp_typecheck.follow_symbol(tmp);
1902+
typet tmp = cpp_typecheck.follow(expr.type());
19041903

19051904
if(!tmp.get_bool(ID_is_template))
19061905
return nil_exprt(); // not a template

Diff for: src/goto-instrument/wmm/fence.cpp

+23-15
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,33 @@ bool is_fence(
1919
const goto_programt::instructiont &instruction,
2020
const namespacet &ns)
2121
{
22-
return (instruction.is_function_call() && ns.lookup(
23-
to_code_function_call(instruction.code).function()).base_name == "fence")
24-
/* if assembly-sensitive algorithms are not available */
25-
|| (instruction.is_other() && instruction.code.get_statement()==ID_fence
26-
&& instruction.code.get_bool(ID_WWfence)
27-
&& instruction.code.get_bool(ID_WRfence)
28-
&& instruction.code.get_bool(ID_RWfence)
29-
&& instruction.code.get_bool(ID_RRfence));
22+
return (instruction.is_function_call() &&
23+
ns.lookup(
24+
to_symbol_expr(
25+
to_code_function_call(instruction.code).function()))
26+
.base_name == "fence")
27+
/* if assembly-sensitive algorithms are not available */
28+
|| (instruction.is_other() &&
29+
instruction.code.get_statement() == ID_fence &&
30+
instruction.code.get_bool(ID_WWfence) &&
31+
instruction.code.get_bool(ID_WRfence) &&
32+
instruction.code.get_bool(ID_RWfence) &&
33+
instruction.code.get_bool(ID_RRfence));
3034
}
3135

3236
bool is_lwfence(
3337
const goto_programt::instructiont &instruction,
3438
const namespacet &ns)
3539
{
36-
return (instruction.is_function_call() && ns.lookup(
37-
to_code_function_call(instruction.code).function()).base_name == "lwfence")
38-
/* if assembly-sensitive algorithms are not available */
39-
|| (instruction.is_other() && instruction.code.get_statement()==ID_fence
40-
&& instruction.code.get_bool(ID_WWfence)
41-
&& instruction.code.get_bool(ID_RWfence)
42-
&& instruction.code.get_bool(ID_RRfence));
40+
return (instruction.is_function_call() &&
41+
ns.lookup(
42+
to_symbol_expr(
43+
to_code_function_call(instruction.code).function()))
44+
.base_name == "lwfence")
45+
/* if assembly-sensitive algorithms are not available */
46+
|| (instruction.is_other() &&
47+
instruction.code.get_statement() == ID_fence &&
48+
instruction.code.get_bool(ID_WWfence) &&
49+
instruction.code.get_bool(ID_RWfence) &&
50+
instruction.code.get_bool(ID_RRfence));
4351
}

Diff for: src/goto-symex/symex_assign.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ void goto_symext::symex_assign_symbol(
245245
tmp_guard.append(guard);
246246

247247
// do the assignment
248-
const symbolt &symbol=ns.lookup(ssa_lhs.get_original_expr());
248+
const symbolt &symbol =
249+
ns.lookup(to_symbol_expr(ssa_lhs.get_original_expr()));
250+
249251
if(symbol.is_auxiliary)
250252
assignment_type=symex_targett::assignment_typet::HIDDEN;
251253

Diff for: src/pointer-analysis/goto_program_dereference.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bool goto_program_dereferencet::has_failed_symbol(
2828
if(expr.get_bool("#invalid_object"))
2929
return false;
3030

31-
const symbolt &ptr_symbol=ns.lookup(expr);
31+
const symbolt &ptr_symbol = ns.lookup(to_symbol_expr(expr));
3232

3333
const irep_idt &failed_symbol=
3434
ptr_symbol.type.get("#failed_symbol");

Diff for: src/util/array_name.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ std::string array_name(
3535
}
3636
else if(expr.id()==ID_symbol)
3737
{
38-
const symbolt &symbol=ns.lookup(expr);
38+
const symbolt &symbol = ns.lookup(to_symbol_expr(expr));
3939
return "array `"+id2string(symbol.base_name)+"'";
4040
}
4141
else if(expr.id()==ID_string_constant)

Diff for: src/util/namespace.cpp

+21-26
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ Author: Daniel Kroening, [email protected]
1515

1616
#include <cassert>
1717

18-
#include "string2int.h"
19-
#include "symbol_table.h"
2018
#include "prefix.h"
19+
#include "std_expr.h"
2120
#include "std_types.h"
21+
#include "string2int.h"
22+
#include "symbol_table.h"
2223

2324
unsigned get_max(
2425
const std::string &prefix,
@@ -44,43 +45,37 @@ namespace_baset::~namespace_baset()
4445
{
4546
}
4647

47-
void namespace_baset::follow_symbol(irept &irep) const
48+
const symbolt &namespace_baset::lookup(const symbol_exprt &expr) const
4849
{
49-
while(irep.id()==ID_symbol)
50-
{
51-
const symbolt &symbol=lookup(irep);
50+
return lookup(expr.get_identifier());
51+
}
5252

53-
if(symbol.is_type)
54-
{
55-
if(symbol.type.is_nil())
56-
return;
57-
else
58-
irep=symbol.type;
59-
}
60-
else
61-
{
62-
if(symbol.value.is_nil())
63-
return;
64-
else
65-
irep=symbol.value;
66-
}
67-
}
53+
const symbolt &namespace_baset::lookup(const symbol_typet &type) const
54+
{
55+
return lookup(type.get_identifier());
56+
}
57+
58+
const symbolt &namespace_baset::lookup(const tag_typet &type) const
59+
{
60+
return lookup(type.get_identifier());
6861
}
6962

7063
const typet &namespace_baset::follow(const typet &src) const
7164
{
7265
if(src.id()!=ID_symbol)
7366
return src;
7467

75-
const symbolt *symbol=&lookup(src);
68+
const symbolt *symbol = &lookup(to_symbol_type(src));
7669

7770
// let's hope it's not cyclic...
7871
while(true)
7972
{
80-
assert(symbol->is_type);
81-
if(symbol->type.id()!=ID_symbol)
73+
DATA_INVARIANT(symbol->is_type, "symbol type points to type");
74+
75+
if(symbol->type.id() == ID_symbol)
76+
symbol = &lookup(to_symbol_type(symbol->type));
77+
else
8278
return symbol->type;
83-
symbol=&lookup(symbol->type);
8479
}
8580
}
8681

@@ -112,7 +107,7 @@ void namespace_baset::follow_macros(exprt &expr) const
112107
{
113108
if(expr.id()==ID_symbol)
114109
{
115-
const symbolt &symbol=lookup(expr);
110+
const symbolt &symbol = lookup(to_symbol_expr(expr));
116111

117112
if(symbol.is_macro && !symbol.value.is_nil())
118113
{

Diff for: src/util/namespace.h

+13-12
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ class symbol_tablet;
1616
class exprt;
1717
class symbolt;
1818
class typet;
19-
class union_tag_typet;
19+
class symbol_exprt;
20+
class symbol_typet;
21+
class tag_typet;
2022
class union_typet;
21-
class struct_tag_typet;
2223
class struct_typet;
2324
class c_enum_typet;
25+
class union_tag_typet;
26+
class struct_tag_typet;
2427
class c_enum_tag_typet;
2528

2629
class namespace_baset
@@ -35,22 +38,20 @@ class namespace_baset
3538
return *symbol;
3639
}
3740

38-
const symbolt &lookup(const irept &irep) const
39-
{
40-
return lookup(irep.get(ID_identifier));
41-
}
41+
const symbolt &lookup(const symbol_exprt &) const;
42+
const symbolt &lookup(const symbol_typet &) const;
43+
const symbolt &lookup(const tag_typet &) const;
4244

4345
virtual ~namespace_baset();
4446

45-
void follow_symbol(irept &irep) const;
46-
void follow_macros(exprt &expr) const;
47-
const typet &follow(const typet &src) const;
47+
void follow_macros(exprt &) const;
48+
const typet &follow(const typet &) const;
4849

4950
// These produce union_typet, struct_typet, c_enum_typet or
5051
// the incomplete version.
51-
const typet &follow_tag(const union_tag_typet &src) const;
52-
const typet &follow_tag(const struct_tag_typet &src) const;
53-
const typet &follow_tag(const c_enum_tag_typet &src) const;
52+
const typet &follow_tag(const union_tag_typet &) const;
53+
const typet &follow_tag(const struct_tag_typet &) const;
54+
const typet &follow_tag(const c_enum_tag_typet &) const;
5455

5556
/// Returns the maximum integer n such that there is a symbol (in some of the
5657
/// symbol tables) whose name is of the form "AB" where A is \p prefix and B

Diff for: src/util/simplify_expr_int.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1347,8 +1347,8 @@ bool simplify_exprt::simplify_inequality(exprt &expr)
13471347
(expr.id()==ID_equal || expr.id()==ID_notequal))
13481348
return simplify_inequality_pointer_object(expr);
13491349

1350-
ns.follow_symbol(tmp0.type());
1351-
ns.follow_symbol(tmp1.type());
1350+
tmp0.type() = ns.follow(tmp0.type());
1351+
tmp1.type() = ns.follow(tmp1.type());
13521352

13531353
if(tmp0.type().id()==ID_c_enum_tag)
13541354
tmp0.type()=ns.follow_tag(to_c_enum_tag_type(tmp0.type()));

Diff for: src/util/type_eq.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "type_eq.h"
1313

14-
#include <cassert>
15-
16-
#include "type.h"
17-
#include "symbol.h"
1814
#include "namespace.h"
15+
#include "std_types.h"
16+
#include "symbol.h"
1917

2018
bool type_eq(const typet &type1, const typet &type2, const namespacet &ns)
2119
{
@@ -24,7 +22,7 @@ bool type_eq(const typet &type1, const typet &type2, const namespacet &ns)
2422

2523
if(type1.id()==ID_symbol)
2624
{
27-
const symbolt &symbol=ns.lookup(type1);
25+
const symbolt &symbol = ns.lookup(to_symbol_type(type1));
2826
if(!symbol.is_type)
2927
throw "symbol "+id2string(symbol.name)+" is not a type";
3028

@@ -33,7 +31,7 @@ bool type_eq(const typet &type1, const typet &type2, const namespacet &ns)
3331

3432
if(type2.id()==ID_symbol)
3533
{
36-
const symbolt &symbol=ns.lookup(type2);
34+
const symbolt &symbol = ns.lookup(to_symbol_type(type2));
3735
if(!symbol.is_type)
3836
throw "symbol "+id2string(symbol.name)+" is not a type";
3937

0 commit comments

Comments
 (0)