Skip to content

Commit 12bb475

Browse files
committed
Give languaget a private typecheck method
This is to avoid breaking the API while introducing a new parameter, symbols_to_keep. This parameter will be used in a later commit that will prevent the typecheck method from stripping out user-requested symbols.
1 parent 36103e1 commit 12bb475

File tree

5 files changed

+80
-19
lines changed

5 files changed

+80
-19
lines changed

src/ansi-c/ansi_c_language.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ bool ansi_c_languaget::parse(
105105

106106
bool ansi_c_languaget::typecheck(
107107
symbol_tablet &symbol_table,
108-
const std::string &module)
108+
const std::string &module,
109+
const bool keep_file_local)
109110
{
110111
symbol_tablet new_symbol_table;
111112

src/ansi-c/ansi_c_language.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,19 @@ class ansi_c_languaget:public languaget
5353

5454
bool typecheck(
5555
symbol_tablet &symbol_table,
56-
const std::string &module) override;
56+
const std::string &module,
57+
const bool keep_file_local) override;
58+
59+
bool can_keep_file_local() override
60+
{
61+
return true;
62+
}
63+
64+
bool
65+
typecheck(symbol_tablet &symbol_table, const std::string &module) override
66+
{
67+
return typecheck(symbol_table, module, true);
68+
}
5769

5870
void show_parse(std::ostream &out) override;
5971

src/langapi/language.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include <string>
1919
#include <unordered_set>
2020

21+
#include <util/invariant.h>
2122
#include <util/message.h>
2223
#include <util/std_types.h>
2324
#include <util/symbol.h>
@@ -123,6 +124,32 @@ class languaget:public messaget
123124
symbol_tablet &symbol_table,
124125
const std::string &module)=0;
125126

127+
/// \brief Is it possible to call three-argument typecheck() on this object?
128+
virtual bool can_keep_file_local()
129+
{
130+
return false;
131+
}
132+
133+
/// \brief typecheck without removing specified entries from the symbol table
134+
///
135+
/// Some concrete subclasses of \ref languaget discard unused symbols from a
136+
/// goto binary as part of typechecking it. This function allows the caller
137+
/// to specify a list of symbols that should be kept, even if that language's
138+
/// typecheck() implementation would normally discard those symbols.
139+
///
140+
/// This function should only be called on objects for which a call to
141+
/// can_keep_symbols() returns `true`.
142+
virtual bool typecheck(
143+
symbol_tablet &symbol_table,
144+
const std::string &module,
145+
const bool keep_file_local)
146+
{
147+
INVARIANT(
148+
false,
149+
"three-argument typecheck should only be called for files written"
150+
" in a language that allows file-local symbols, like C");
151+
}
152+
126153
// language id / description
127154

128155
virtual std::string id() const = 0;

src/langapi/language_file.cpp

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ bool language_filest::parse()
8282
return false;
8383
}
8484

85-
bool language_filest::typecheck(symbol_tablet &symbol_table)
85+
bool language_filest::typecheck(
86+
symbol_tablet &symbol_table,
87+
const bool keep_file_local)
8688
{
8789
// typecheck interfaces
8890

@@ -129,8 +131,16 @@ bool language_filest::typecheck(symbol_tablet &symbol_table)
129131
{
130132
if(file.second.modules.empty())
131133
{
132-
if(file.second.language->typecheck(symbol_table, ""))
133-
return true;
134+
if(file.second.language->can_keep_file_local())
135+
{
136+
if(file.second.language->typecheck(symbol_table, "", keep_file_local))
137+
return true;
138+
}
139+
else
140+
{
141+
if(file.second.language->typecheck(symbol_table, ""))
142+
return true;
143+
}
134144
// register lazy methods.
135145
// TODO: learn about modules and generalise this
136146
// to module-providing languages if required.
@@ -145,7 +155,7 @@ bool language_filest::typecheck(symbol_tablet &symbol_table)
145155

146156
for(auto &module : module_map)
147157
{
148-
if(typecheck_module(symbol_table, module.second))
158+
if(typecheck_module(symbol_table, module.second, keep_file_local))
149159
return true;
150160
}
151161

@@ -195,7 +205,8 @@ bool language_filest::interfaces(
195205

196206
bool language_filest::typecheck_module(
197207
symbol_tablet &symbol_table,
198-
const std::string &module)
208+
const std::string &module,
209+
const bool keep_file_local)
199210
{
200211
// check module map
201212

@@ -207,12 +218,13 @@ bool language_filest::typecheck_module(
207218
return true;
208219
}
209220

210-
return typecheck_module(symbol_table, it->second);
221+
return typecheck_module(symbol_table, it->second, keep_file_local);
211222
}
212223

213224
bool language_filest::typecheck_module(
214225
symbol_tablet &symbol_table,
215-
language_modulet &module)
226+
language_modulet &module,
227+
const bool keep_file_local)
216228
{
217229
// already typechecked?
218230

@@ -240,23 +252,29 @@ bool language_filest::typecheck_module(
240252
it!=dependency_set.end();
241253
it++)
242254
{
243-
if(typecheck_module(symbol_table, *it))
244-
{
245-
module.in_progress=false;
255+
module.in_progress = !typecheck_module(symbol_table, *it, keep_file_local);
256+
if(module.in_progress == false)
246257
return true;
247-
}
248258
}
249259

250260
// type check it
251261

252262
status() << "Type-checking " << module.name << eom;
253263

254-
if(module.file->language->typecheck(symbol_table, module.name))
264+
if(module.file->language->can_keep_file_local())
255265
{
256-
module.in_progress = false;
257-
return true;
266+
module.in_progress = !module.file->language->typecheck(
267+
symbol_table, module.name, keep_file_local);
268+
}
269+
else
270+
{
271+
module.in_progress =
272+
!module.file->language->typecheck(symbol_table, module.name);
258273
}
259274

275+
if(!module.in_progress)
276+
return true;
277+
260278
module.type_checked=true;
261279
module.in_progress=false;
262280

src/langapi/language_file.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class language_filest:public messaget
107107

108108
bool generate_support_functions(symbol_tablet &symbol_table);
109109

110-
bool typecheck(symbol_tablet &symbol_table);
110+
bool
111+
typecheck(symbol_tablet &symbol_table, const bool keep_file_local = false);
111112

112113
bool final(symbol_table_baset &symbol_table);
113114

@@ -141,11 +142,13 @@ class language_filest:public messaget
141142
protected:
142143
bool typecheck_module(
143144
symbol_tablet &symbol_table,
144-
language_modulet &module);
145+
language_modulet &module,
146+
const bool keep_file_local);
145147

146148
bool typecheck_module(
147149
symbol_tablet &symbol_table,
148-
const std::string &module);
150+
const std::string &module,
151+
const bool keep_file_local);
149152
};
150153

151154
#endif // CPROVER_UTIL_LANGUAGE_FILE_H

0 commit comments

Comments
 (0)