CBMC
java_bytecode_parse_tree.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
10 
11 #include <algorithm>
12 #include <ostream>
13 
14 #include <util/symbol_table.h>
15 #include <util/namespace.h>
16 
17 #include "expr2java.h"
18 
19 void java_bytecode_parse_treet::output(std::ostream &out) const
20 {
21  parsed_class.output(out);
22 
23  out << "Class references:\n";
24  for(const auto &class_ref : class_refs)
25  out << " " << class_ref << '\n';
26 }
27 
28 void java_bytecode_parse_treet::classt::output(std::ostream &out) const
29 {
30  for(const auto &annotation : annotations)
31  {
32  annotation.output(out);
33  out << '\n';
34  }
35 
36  out << "class " << name;
37  if(!super_class.empty())
38  out << " extends " << super_class;
39  out << " {" << '\n';
40 
41  for(const auto &field : fields)
42  field.output(out);
43 
44  out << '\n';
45 
46  for(const auto &method : methods)
47  method.output(out);
48 
49  out << '}' << '\n';
50  out << '\n';
51 }
52 
54 {
55  symbol_tablet symbol_table;
56  namespacet ns(symbol_table);
57 
58  out << '@' << type2java(type, ns);
59 
60  if(!element_value_pairs.empty())
61  {
62  out << '(';
63 
64  bool first=true;
65  for(const auto &element_value_pair : element_value_pairs)
66  {
67  if(first)
68  first=false;
69  else
70  out << ", ";
71  element_value_pair.output(out);
72  }
73 
74  out << ')';
75  }
76 }
77 
79  std::ostream &out) const
80 {
81  symbol_tablet symbol_table;
82  namespacet ns(symbol_table);
83 
84  out << '"' << element_name << '"' << '=';
85  out << expr2java(value, ns);
86 }
87 
94 std::optional<java_bytecode_parse_treet::annotationt>
96  const annotationst &annotations,
97  const irep_idt &annotation_type_name)
98 {
99  const auto annotation_it = std::find_if(
100  annotations.begin(),
101  annotations.end(),
102  [&annotation_type_name](const annotationt &annotation) {
103  if(annotation.type.id() != ID_pointer)
104  return false;
105  const typet &type = to_pointer_type(annotation.type).base_type();
106  return type.id() == ID_struct_tag &&
107  to_struct_tag_type(type).get_identifier() == annotation_type_name;
108  });
109  if(annotation_it == annotations.end())
110  return {};
111  return *annotation_it;
112 }
113 
114 void java_bytecode_parse_treet::methodt::output(std::ostream &out) const
115 {
116  symbol_tablet symbol_table;
117  namespacet ns(symbol_table);
118 
119  for(const auto &annotation : annotations)
120  {
121  out << " ";
122  annotation.output(out);
123  out << '\n';
124  }
125 
126  out << " ";
127 
128  if(is_public)
129  out << "public ";
130 
131  if(is_protected)
132  out << "protected ";
133 
134  if(is_private)
135  out << "private ";
136 
137  if(is_static)
138  out << "static ";
139 
140  if(is_final)
141  out << "final ";
142 
143  if(is_native)
144  out << "native ";
145 
146  if(is_synchronized)
147  out << "synchronized ";
148 
149  out << name;
150  out << " : " << descriptor;
151 
152  out << '\n';
153 
154  out << " {" << '\n';
155 
156  for(const auto &i : instructions)
157  {
158  if(!i.source_location.get_line().empty())
159  out << " // " << i.source_location << '\n';
160 
161  out << " " << i.address << ": ";
162  out << bytecode_info[i.bytecode].mnemonic;
163 
164  bool first = true;
165  for(const auto &arg : i.args)
166  {
167  if(first)
168  first = false;
169  else
170  out << ',';
171 #if 0
172  out << ' ' << from_expr(arg);
173 #else
174  out << ' ' << expr2java(arg, ns);
175 #endif
176  }
177 
178  out << '\n';
179  }
180 
181  out << " }" << '\n';
182 
183  out << '\n';
184 
185  out << " Locals:\n";
186  for(const auto &v : local_variable_table)
187  {
188  out << " " << v.index << ": " << v.name << ' '
189  << v.descriptor << '\n';
190  }
191 
192  out << '\n';
193 }
194 
195 void java_bytecode_parse_treet::fieldt::output(std::ostream &out) const
196 {
197  for(const auto &annotation : annotations)
198  {
199  out << " ";
200  annotation.output(out);
201  out << '\n';
202  }
203 
204  out << " ";
205 
206  if(is_public)
207  out << "public ";
208 
209  if(is_static)
210  out << "static ";
211 
212  out << name;
213  out << " : " << descriptor << ';';
214 
215  out << '\n';
216 }
struct bytecode_infot const bytecode_info[]
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
The symbol table.
Definition: symbol_table.h:14
std::string type2java(const typet &type, const namespacet &ns)
Definition: expr2java.cpp:461
std::string expr2java(const exprt &expr, const namespacet &ns)
Definition: expr2java.cpp:454
std::string from_expr(const namespacet &ns, const irep_idt &identifier, const exprt &expr)
const char * mnemonic
Definition: bytecode_info.h:46
void output(std::ostream &out) const
std::vector< annotationt > annotationst
static std::optional< annotationt > find_annotation(const annotationst &annotations, const irep_idt &annotation_type_name)
Find an annotation given its name.
void output(std::ostream &out) const
Author: Diffblue Ltd.