CBMC
language_file.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_LANGAPI_LANGUAGE_FILE_H
11 #define CPROVER_LANGAPI_LANGUAGE_FILE_H
12 
13 #include <iosfwd>
14 #include <map>
15 #include <memory> // unique_ptr
16 #include <set>
17 #include <string>
18 #include <unordered_set>
19 
20 #include <util/symbol_table_base.h>
21 
22 class message_handlert;
23 class language_filet;
24 class languaget;
25 
26 class language_modulet final
27 {
28 public:
29  std::string name;
32 
34  type_checked(false),
35  in_progress(false),
36  file(nullptr)
37  {}
38 };
39 
40 class language_filet final
41 {
42 public:
43  typedef std::set<std::string> modulest;
45 
46  std::unique_ptr<languaget> language;
47  std::string filename;
48 
49  void get_modules();
50 
52  const irep_idt &id,
53  symbol_table_baset &symbol_table,
54  message_handlert &message_handler);
55 
56  explicit language_filet(const std::string &filename);
57  language_filet(const language_filet &rhs);
58 
60 };
61 
63 {
64 private:
65  typedef std::map<std::string, language_filet> file_mapt;
67 
68  typedef std::map<std::string, language_modulet> module_mapt;
70 
71  // Contains pointers into file_map!
72  // This is safe-ish as long as this is std::map.
73  typedef std::map<irep_idt, language_filet *> lazy_method_mapt;
75 
76 public:
77  language_filet &add_file(const std::string &filename)
78  {
79  language_filet language_file(filename);
80  return file_map.emplace(filename, std::move(language_file)).first->second;
81  }
82 
83  void remove_file(const std::string &filename)
84  {
85  // Clear relevant entries from lazy_method_map
86  language_filet *language_file = &file_map.at(filename);
87  std::unordered_set<irep_idt> files_methods;
88  for(auto const &method : lazy_method_map)
89  {
90  if(method.second == language_file)
91  files_methods.insert(method.first);
92  }
93  for(const irep_idt &method_name : files_methods)
94  lazy_method_map.erase(method_name);
95 
96  file_map.erase(filename);
97  }
98 
99  void clear_files()
100  {
101  lazy_method_map.clear();
102  file_map.clear();
103  }
104 
105  bool parse(message_handlert &message_handler);
106 
107  void show_parse(std::ostream &out, message_handlert &message_handler);
108 
110  symbol_table_baset &symbol_table,
111  message_handlert &message_handler);
112 
113  bool
114 
115  typecheck(
116  symbol_table_baset &symbol_table,
117  const bool keep_file_local,
118  message_handlert &message_handler);
119  bool
120  typecheck(symbol_table_baset &symbol_table, message_handlert &message_handler)
121  {
122  return typecheck(symbol_table, false, message_handler);
123  }
124 
125  bool final(symbol_table_baset &symbol_table);
126 
127  bool interfaces(
128  symbol_table_baset &symbol_table,
129  message_handlert &message_handler);
130 
131  // The method must have been added to the symbol table and registered
132  // in lazy_method_map (currently always in language_filest::typecheck)
133  // for this to be legal.
135  const irep_idt &id,
136  symbol_table_baset &symbol_table,
137  message_handlert &message_handler)
138  {
139  PRECONDITION(symbol_table.has_symbol(id));
140  lazy_method_mapt::iterator it=lazy_method_map.find(id);
141  if(it!=lazy_method_map.end())
142  it->second->convert_lazy_method(id, symbol_table, message_handler);
143  }
144 
145  bool can_convert_lazy_method(const irep_idt &id) const
146  {
147  return lazy_method_map.count(id) != 0;
148  }
149 
150  void clear()
151  {
152  file_map.clear();
153  module_map.clear();
154  lazy_method_map.clear();
155  }
156 
157 protected:
158  bool typecheck_module(
159  symbol_table_baset &symbol_table,
160  language_modulet &module,
161  const bool keep_file_local,
162  message_handlert &message_handler);
163 
164  bool typecheck_module(
165  symbol_table_baset &symbol_table,
166  const std::string &module,
167  const bool keep_file_local,
168  message_handlert &message_handler);
169 };
170 
171 #endif // CPROVER_UTIL_LANGUAGE_FILE_H
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool interfaces(symbol_table_baset &symbol_table, message_handlert &message_handler)
language_filet & add_file(const std::string &filename)
Definition: language_file.h:77
bool parse(message_handlert &message_handler)
bool typecheck_module(symbol_table_baset &symbol_table, language_modulet &module, const bool keep_file_local, message_handlert &message_handler)
std::map< std::string, language_modulet > module_mapt
Definition: language_file.h:68
void convert_lazy_method(const irep_idt &id, symbol_table_baset &symbol_table, message_handlert &message_handler)
bool typecheck(symbol_table_baset &symbol_table, const bool keep_file_local, message_handlert &message_handler)
file_mapt file_map
Definition: language_file.h:66
std::map< std::string, language_filet > file_mapt
Definition: language_file.h:65
bool generate_support_functions(symbol_table_baset &symbol_table, message_handlert &message_handler)
bool typecheck(symbol_table_baset &symbol_table, message_handlert &message_handler)
lazy_method_mapt lazy_method_map
Definition: language_file.h:74
bool can_convert_lazy_method(const irep_idt &id) const
std::map< irep_idt, language_filet * > lazy_method_mapt
Definition: language_file.h:73
void remove_file(const std::string &filename)
Definition: language_file.h:83
module_mapt module_map
Definition: language_file.h:69
void show_parse(std::ostream &out, message_handlert &message_handler)
language_filet(const std::string &filename)
std::string filename
Definition: language_file.h:47
modulest modules
Definition: language_file.h:44
~language_filet()
To avoid compiler errors, the complete definition of a pointed-to type must be visible at the point a...
void convert_lazy_method(const irep_idt &id, symbol_table_baset &symbol_table, message_handlert &message_handler)
std::set< std::string > modulest
Definition: language_file.h:43
std::unique_ptr< languaget > language
Definition: language_file.h:46
std::string name
Definition: language_file.h:29
language_filet * file
Definition: language_file.h:31
The symbol table base class interface.
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
Author: Diffblue Ltd.