cprover
language_util.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 "language_util.h"
10 
11 #include <memory>
12 
13 #include <util/symbol_table.h>
14 #include <util/namespace.h>
15 #include <util/language.h>
16 #include <util/std_expr.h>
17 
18 #include "mode.h"
19 
21  const namespacet &ns,
22  const irep_idt &identifier)
23 {
24  const symbolt *symbol;
25 
26  if(identifier=="" ||
27  ns.lookup(identifier, symbol) ||
28  symbol->mode=="")
29  return get_default_language();
30 
32 
33  if(ptr==nullptr)
34  throw "symbol `"+id2string(symbol->name)+
35  "' has unknown mode '"+id2string(symbol->mode)+"'";
36 
37  return ptr;
38 }
39 
40 std::string from_expr(
41  const namespacet &ns,
42  const irep_idt &identifier,
43  const exprt &expr)
44 {
45  std::unique_ptr<languaget> p(get_language(ns, identifier));
46 
47  std::string result;
48  p->from_expr(expr, result, ns);
49 
50  return result;
51 }
52 
53 std::string from_type(
54  const namespacet &ns,
55  const irep_idt &identifier,
56  const typet &type)
57 {
58  std::unique_ptr<languaget> p(get_language(ns, identifier));
59 
60  std::string result;
61  p->from_type(type, result, ns);
62 
63  return result;
64 }
65 
66 std::string type_to_name(
67  const namespacet &ns,
68  const irep_idt &identifier,
69  const typet &type)
70 {
71  std::unique_ptr<languaget> p(get_language(ns, identifier));
72 
73  std::string result;
74  p->type_to_name(type, result, ns);
75 
76  return result;
77 }
78 
79 std::string from_expr(const exprt &expr)
80 {
81  symbol_tablet symbol_table;
82  return from_expr(namespacet(symbol_table), "", expr);
83 }
84 
85 std::string from_type(const typet &type)
86 {
87  symbol_tablet symbol_table;
88  return from_type(namespacet(symbol_table), "", type);
89 }
90 
92  const namespacet &ns,
93  const irep_idt &identifier,
94  const std::string &src)
95 {
96  std::unique_ptr<languaget> p(get_language(ns, identifier));
97 
98  null_message_handlert null_message_handler;
99  p->set_message_handler(null_message_handler);
100 
101  const symbolt &symbol=ns.lookup(identifier);
102 
103  exprt expr;
104 
105  if(p->to_expr(src, id2string(symbol.module), expr, ns))
106  return nil_exprt();
107 
108  return expr;
109 }
110 
111 std::string type_to_name(const typet &type)
112 {
113  symbol_tablet symbol_table;
114  return type_to_name(namespacet(symbol_table), "", type);
115 }
The type of an expression.
Definition: type.h:20
irep_idt name
The unique identifier.
Definition: symbol.h:46
virtual bool lookup(const irep_idt &name, const symbolt *&symbol) const
Definition: namespace.cpp:139
exprt to_expr(const namespacet &ns, const irep_idt &identifier, const std::string &src)
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
irep_idt mode
Language mode.
Definition: symbol.h:55
std::string from_expr(const namespacet &ns, const irep_idt &identifier, const exprt &expr)
virtual bool type_to_name(const typet &type, std::string &name, const namespacet &ns)
Definition: language.cpp:50
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:49
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:33
std::string type_to_name(const namespacet &ns, const irep_idt &identifier, const typet &type)
The NIL expression.
Definition: std_expr.h:3764
virtual bool to_expr(const std::string &code, const std::string &module, exprt &expr, const namespacet &ns)=0
API to expression classes.
The symbol table.
Definition: symbol_table.h:52
TO_BE_DOCUMENTED.
Definition: namespace.h:62
Abstract interface to support a programming language.
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:122
static languaget * get_language(const namespacet &ns, const irep_idt &identifier)
Symbol table.
languaget * get_default_language()
Definition: mode.cpp:85
std::string from_type(const namespacet &ns, const irep_idt &identifier, const typet &type)
virtual bool from_type(const typet &type, std::string &code, const namespacet &ns)
Definition: language.cpp:41
languaget * get_language_from_mode(const irep_idt &mode)
Definition: mode.cpp:40
Base class for all expressions.
Definition: expr.h:46
virtual bool from_expr(const exprt &expr, std::string &code, const namespacet &ns)
Definition: language.cpp:32