CBMC
mode.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
9 #include "mode.h"
10 
11 #include <list>
12 #include <memory>
13 #include <set>
14 
15 #ifdef _WIN32
16 #include <cstring>
17 #endif
18 
19 #include "language.h"
20 
21 #include <util/invariant.h>
22 #include <util/namespace.h>
23 #include <util/symbol.h>
24 
26 {
28  std::set<std::string> extensions;
30 };
31 
32 typedef std::list<language_entryt> languagest;
34 
40 {
41  languages.push_back(language_entryt());
42  std::unique_ptr<languaget> l(factory());
43  languages.back().factory=factory;
44  languages.back().extensions=l->extensions();
45  languages.back().mode=l->id();
46 }
47 
51 std::unique_ptr<languaget> get_language_from_mode(const irep_idt &mode)
52 {
53  for(const auto &language : languages)
54  if(mode == language.mode)
55  return language.factory();
56 
57  return nullptr;
58 }
59 
65 const irep_idt &
66 get_mode_from_identifier(const namespacet &ns, const irep_idt &identifier)
67 {
68  if(identifier.empty())
69  return ID_unknown;
70  const symbolt *symbol;
71  if(ns.lookup(identifier, symbol))
72  return ID_unknown;
73  return symbol->mode;
74 }
75 
83 std::unique_ptr<languaget>
84 get_language_from_identifier(const namespacet &ns, const irep_idt &identifier)
85 {
86  const irep_idt &mode = get_mode_from_identifier(ns, identifier);
87  if(mode == ID_unknown)
88  return get_default_language();
89 
90  std::unique_ptr<languaget> language = get_language_from_mode(mode);
91  INVARIANT(
92  language,
93  "symbol '" + id2string(identifier) + "' has unknown mode '" +
94  id2string(mode) + "'");
95  return language;
96 }
97 
102 std::unique_ptr<languaget> get_language_from_filename(
103  const std::string &filename)
104 {
105  std::size_t ext_pos=filename.rfind('.');
106 
107  if(ext_pos==std::string::npos)
108  return nullptr;
109 
110  std::string extension=
111  std::string(filename, ext_pos+1, std::string::npos);
112 
113  if(extension.empty())
114  return nullptr;
115 
116  for(languagest::const_iterator
117  l_it=languages.begin();
118  l_it!=languages.end();
119  l_it++)
120  {
121  #ifdef _WIN32
122  for(std::set<std::string>::const_iterator
123  e_it=l_it->extensions.begin();
124  e_it!=l_it->extensions.end();
125  e_it++)
126  if(_stricmp(extension.c_str(), e_it->c_str())==0)
127  return l_it->factory();
128  #else
129  if(l_it->extensions.find(extension)!=l_it->extensions.end())
130  return l_it->factory();
131  #endif
132  }
133 
134  return nullptr;
135 }
136 
139 std::unique_ptr<languaget> get_default_language()
140 {
141  PRECONDITION(!languages.empty());
142  return languages.front().factory();
143 }
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool empty() const
Definition: dstring.h:89
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:148
Symbol table entry.
Definition: symbol.h:28
irep_idt mode
Language mode.
Definition: symbol.h:49
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
Abstract interface to support a programming language.
std::unique_ptr< languaget > get_language_from_filename(const std::string &filename)
Get the language corresponding to the registered file name extensions.
Definition: mode.cpp:102
void register_language(language_factoryt factory)
Register a language Note: registering a language is required for using the functions in language_util...
Definition: mode.cpp:39
languagest languages
Definition: mode.cpp:33
std::unique_ptr< languaget > get_language_from_identifier(const namespacet &ns, const irep_idt &identifier)
Get the language corresponding to the mode of the given identifier's symbol.
Definition: mode.cpp:84
std::unique_ptr< languaget > get_language_from_mode(const irep_idt &mode)
Get the language corresponding to the given mode.
Definition: mode.cpp:51
std::list< language_entryt > languagest
Definition: mode.cpp:32
std::unique_ptr< languaget > get_default_language()
Returns the default language.
Definition: mode.cpp:139
const irep_idt & get_mode_from_identifier(const namespacet &ns, const irep_idt &identifier)
Get the mode of the given identifier's symbol.
Definition: mode.cpp:66
std::unique_ptr< languaget >(* language_factoryt)()
Definition: mode.h:29
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
std::set< std::string > extensions
Definition: mode.cpp:28
irep_idt mode
Definition: mode.cpp:29
language_factoryt factory
Definition: mode.cpp:27
Symbol table entry.