CBMC
jsil_parse_tree.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Jsil Language
4 
5 Author: Michael Tautschnig, tautschn@amazon.com
6 
7 \*******************************************************************/
8 
11 
12 #include "jsil_parse_tree.h"
13 
14 #include <ostream>
15 
16 #include <util/symbol.h>
17 
18 #include "jsil_types.h"
19 
20 static bool insert_at_label(
21  const codet &code,
22  const irep_idt &label,
23  code_blockt &dest)
24 {
25  for(auto &c : dest.statements())
26  {
27  if(c.get_statement()!=ID_label)
28  continue;
29 
31  if(l.get_label()!=label)
32  continue;
33 
34  assert(l.code().get_statement()==ID_skip);
35  l.code()=code;
36 
37  return false;
38  }
39 
40  return true;
41 }
42 
44 {
45  symbol.clear();
46 
48  static_cast<const exprt&>(find(ID_declarator))));
49 
50  code_typet symbol_type=to_code_type(s.type());
51 
52  irep_idt proc_type=s.get("proc_type");
53 
54  if(proc_type=="builtin")
55  symbol_type=jsil_builtin_code_typet(symbol_type);
56  else if(proc_type=="spec")
57  symbol_type=jsil_spec_code_typet(symbol_type);
58 
59  symbol.name=s.get_identifier();
60  symbol.base_name=s.get_identifier();
61  symbol.mode="jsil";
62  symbol.type=symbol_type;
63  symbol.location=s.source_location();
64 
66  static_cast<const codet&>(find(ID_value))));
67 
68  irept returns(find(ID_return));
70 
71  irept throws(find(ID_throw));
73  symbol_exprt::typeless(throws.get(ID_value)), typet(), s.source_location());
74  code_expressiont ct(t);
75 
76  if(insert_at_label(r, returns.get(ID_label), code))
77  throw "return label "+returns.get_string(ID_label)+" not found";
78  if(insert_at_label(ct, throws.get(ID_label), code))
79  throw "throw label "+throws.get_string(ID_label)+" not found";
80 
81  symbol.value.swap(code);
82 }
83 
84 void jsil_declarationt::output(std::ostream &out) const
85 {
86  out << "Declarator: " << find(ID_declarator).pretty() << "\n";
87  out << "Returns: " << find(ID_return).pretty() << "\n";
88  out << "Throws: " << find(ID_throw).pretty() << "\n";
89  out << "Value: " << find(ID_value).pretty() << "\n";
90 }
91 
92 void jsil_parse_treet::output(std::ostream &out) const
93 {
94  for(itemst::const_iterator
95  it=items.begin();
96  it!=items.end();
97  it++)
98  {
99  it->output(out);
100  out << "\n";
101  }
102 }
jsil_parse_treet::items
itemst items
Definition: jsil_parse_tree.h:104
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:129
typet
The type of an expression, extends irept.
Definition: type.h:28
irept::pretty
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:495
irept::find
const irept & find(const irep_idt &name) const
Definition: irep.cpp:106
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
symbol_exprt::typeless
static symbol_exprt typeless(const irep_idt &id)
Generate a symbol_exprt without a proper type.
Definition: std_expr.h:132
exprt
Base class for all expressions.
Definition: expr.h:55
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
jsil_types.h
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
code_labelt::code
codet & code()
Definition: std_code.h:977
irept::get
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:45
code_blockt::statements
code_operandst & statements()
Definition: std_code.h:138
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
jsil_parse_treet::output
void output(std::ostream &out) const
Definition: jsil_parse_tree.cpp:92
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
code_labelt
codet representation of a label for branch targets.
Definition: std_code.h:958
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:142
to_code_label
const code_labelt & to_code_label(const codet &code)
Definition: std_code.h:1004
insert_at_label
static bool insert_at_label(const codet &code, const irep_idt &label, code_blockt &dest)
Definition: jsil_parse_tree.cpp:20
irept::get_string
const std::string & get_string(const irep_idt &name) const
Definition: irep.h:409
jsil_declarationt::to_symbol
void to_symbol(symbolt &symbol) const
Definition: jsil_parse_tree.cpp:43
irept::swap
void swap(irept &irep)
Definition: irep.h:442
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:222
code_typet
Base type of functions.
Definition: std_types.h:538
symbol.h
Symbol table entry.
jsil_declarationt::output
void output(std::ostream &) const
Definition: jsil_parse_tree.cpp:84
code_frontend_returnt
codet representation of a "return from a function" statement.
Definition: std_code.h:892
symbolt::clear
void clear()
Zero initialise a symbol object.
Definition: symbol.h:75
code_labelt::get_label
const irep_idt & get_label() const
Definition: std_code.h:967
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
jsil_builtin_code_typet
Definition: jsil_types.h:37
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:27
jsil_spec_code_typet
Definition: jsil_types.h:60
jsil_parse_tree.h
side_effect_expr_throwt
A side_effect_exprt representation of a side effect that throws an exception.
Definition: std_code.h:1756
to_code_block
const code_blockt & to_code_block(const codet &code)
Definition: std_code.h:203
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:359
r
static int8_t r
Definition: irep_hash.h:60
codet::get_statement
const irep_idt & get_statement() const
Definition: std_code_base.h:65
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:211
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
code_expressiont
codet representation of an expression statement.
Definition: std_code.h:1393
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:28