Skip to content

Commit 9e499d5

Browse files
committed
Create java_string_literal_exprt class
With this new class, we can avoid directly accessing named subexpressions of exprts with id() == ID_java_string_literal.
1 parent c08a6fb commit 9e499d5

8 files changed

+73
-27
lines changed

jbmc/src/java_bytecode/expr2java.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Author: Daniel Kroening, [email protected]
2323
#include <ansi-c/expr2c_class.h>
2424

2525
#include "java_qualifiers.h"
26+
#include "java_string_literal_expr.h"
2627
#include "java_types.h"
2728

2829
std::string expr2javat::convert(const typet &src)
@@ -414,8 +415,11 @@ std::string expr2javat::convert_with_precedence(
414415
id2string(src.get(ID_component_name)) +
415416
")";
416417
}
417-
else if(src.id()==ID_java_string_literal)
418-
return '"'+MetaString(src.get_string(ID_value))+'"';
418+
else if(
419+
const auto &literal = expr_try_dynamic_cast<java_string_literal_exprt>(src))
420+
{
421+
return '"' + MetaString(id2string(literal->value())) + '"';
422+
}
419423
else if(src.id()==ID_constant)
420424
return convert_constant(to_constant_expr(src), precedence=16);
421425
else

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include "java_bytecode_convert_method_class.h"
1919
#include "java_static_initializers.h"
2020
#include "java_string_library_preprocess.h"
21+
#include "java_string_literal_expr.h"
2122
#include "java_types.h"
2223
#include "java_utils.h"
2324
#include "pattern.h"
@@ -1313,7 +1314,7 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
13131314
PRECONDITION(op.empty() && results.size() == 1);
13141315

13151316
INVARIANT(
1316-
arg0.id() != ID_java_string_literal && arg0.id() != ID_type,
1317+
!can_cast_expr<java_string_literal_exprt>(arg0) && arg0.id() != ID_type,
13171318
"String and Class literals should have been lowered in "
13181319
"generate_constant_global_variables");
13191320

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ Author: Daniel Kroening, [email protected]
2626

2727
#include <goto-programs/class_hierarchy.h>
2828

29+
#include "ci_lazy_methods.h"
2930
#include "java_bytecode_concurrency_instrumentation.h"
3031
#include "java_bytecode_convert_class.h"
3132
#include "java_bytecode_convert_method.h"
32-
#include "java_bytecode_internal_additions.h"
3333
#include "java_bytecode_instrument.h"
34-
#include "java_bytecode_typecheck.h"
35-
#include "java_entry_point.h"
34+
#include "java_bytecode_internal_additions.h"
3635
#include "java_bytecode_parser.h"
36+
#include "java_bytecode_typecheck.h"
3737
#include "java_class_loader.h"
38-
#include "java_string_literals.h"
38+
#include "java_entry_point.h"
3939
#include "java_static_initializers.h"
40+
#include "java_string_literal_expr.h"
41+
#include "java_string_literals.h"
4042
#include "java_utils.h"
41-
#include "ci_lazy_methods.h"
4243

4344
#include "expr2java.h"
4445
#include "load_method_by_regex.h"
@@ -417,12 +418,12 @@ static exprt get_ldc_result(
417418
address_of_exprt(
418419
get_or_create_class_literal_symbol(class_id, symbol_table));
419420
}
420-
else if(ldc_arg0.id() == ID_java_string_literal)
421+
else if(
422+
const auto &literal =
423+
expr_try_dynamic_cast<java_string_literal_exprt>(ldc_arg0))
421424
{
422-
return
423-
address_of_exprt(
424-
get_or_create_string_literal_symbol(
425-
ldc_arg0, symbol_table, string_refinement_enabled));
425+
return address_of_exprt(get_or_create_string_literal_symbol(
426+
*literal, symbol_table, string_refinement_enabled));
426427
}
427428
else
428429
{

jbmc/src/java_bytecode/java_bytecode_parser.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ Author: Daniel Kroening, [email protected]
2121
#include <util/string_constant.h>
2222
#include <util/optional.h>
2323

24+
#include "bytecode_info.h"
2425
#include "java_bytecode_parse_tree.h"
26+
#include "java_string_literal_expr.h"
2527
#include "java_types.h"
26-
#include "bytecode_info.h"
2728

2829
#ifdef DEBUG
2930
#include <iostream>
@@ -839,9 +840,7 @@ void java_bytecode_parsert::rconstant_pool()
839840
case CONSTANT_String:
840841
{
841842
// ldc turns these into references to java.lang.String
842-
exprt string_literal(ID_java_string_literal);
843-
string_literal.set(ID_value, pool_entry(it->ref1).s);
844-
it->expr=string_literal;
843+
it->expr = java_string_literal_exprt{pool_entry(it->ref1).s};
845844
}
846845
break;
847846

jbmc/src/java_bytecode/java_bytecode_typecheck_expr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/unicode.h>
1818

1919
#include "java_pointer_casts.h"
20-
#include "java_types.h"
21-
#include "java_utils.h"
2220
#include "java_root_class.h"
2321
#include "java_string_library_preprocess.h"
22+
#include "java_string_literal_expr.h"
23+
#include "java_types.h"
24+
#include "java_utils.h"
2425

2526
void java_bytecode_typecheckt::typecheck_expr(exprt &expr)
2627
{
@@ -39,7 +40,7 @@ void java_bytecode_typecheckt::typecheck_expr(exprt &expr)
3940
typecheck_expr(*it);
4041

4142
INVARIANT(
42-
expr.id() != ID_java_string_literal,
43+
!can_cast_expr<java_string_literal_exprt>(expr),
4344
"String literals should have been converted to constant globals "
4445
"before typecheck_expr");
4546

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*******************************************************************\
2+
3+
Module: Java Bytecode
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Representation of a constant Java string
11+
12+
#ifndef CPROVER_JAVA_BYTECODE_JAVA_STRING_LITERAL_EXPR_H
13+
#define CPROVER_JAVA_BYTECODE_JAVA_STRING_LITERAL_EXPR_H
14+
15+
#include <util/expr.h>
16+
17+
class java_string_literal_exprt : public exprt
18+
{
19+
public:
20+
explicit java_string_literal_exprt(const irep_idt &literal)
21+
: exprt(ID_java_string_literal)
22+
{
23+
set(ID_value, literal);
24+
}
25+
26+
irep_idt value() const
27+
{
28+
return get(ID_value);
29+
}
30+
};
31+
32+
template <>
33+
inline bool can_cast_expr<java_string_literal_exprt>(const exprt &base)
34+
{
35+
return base.id() == ID_java_string_literal;
36+
}
37+
38+
#endif // CPROVER_JAVA_BYTECODE_JAVA_STRING_LITERAL_EXPR_H

jbmc/src/java_bytecode/java_string_literals.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: Chris Smowton, [email protected]
88

99
#include "java_string_literals.h"
1010
#include "java_root_class.h"
11+
#include "java_string_literal_expr.h"
1112
#include "java_types.h"
1213
#include "java_utils.h"
1314

@@ -56,12 +57,11 @@ static array_exprt utf16_to_array(const std::wstring &in)
5657
}
5758

5859
symbol_exprt get_or_create_string_literal_symbol(
59-
const exprt &string_expr,
60+
const java_string_literal_exprt &string_expr,
6061
symbol_table_baset &symbol_table,
6162
bool string_refinement_enabled)
6263
{
63-
PRECONDITION(string_expr.id() == ID_java_string_literal);
64-
const irep_idt value = string_expr.get(ID_value);
64+
const irep_idt value = string_expr.value();
6565
const struct_tag_typet string_type("java::java.lang.String");
6666

6767
const std::string escaped_symbol_name = escape_non_alnum(id2string(value));
@@ -213,8 +213,8 @@ symbol_exprt get_or_create_string_literal_symbol(
213213
symbol_table_baset &symbol_table,
214214
bool string_refinement_enabled)
215215
{
216-
exprt literal{ID_java_string_literal};
217-
literal.set(ID_value, string_value);
218216
return get_or_create_string_literal_symbol(
219-
literal, symbol_table, string_refinement_enabled);
217+
java_string_literal_exprt{string_value},
218+
symbol_table,
219+
string_refinement_enabled);
220220
}

jbmc/src/java_bytecode/java_string_literals.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Author: Chris Smowton, [email protected]
1212
#include <util/symbol_table.h>
1313
#include <util/std_expr.h>
1414

15+
class java_string_literal_exprt;
16+
1517
/// Creates or gets an existing constant global symbol for a given string
1618
/// literal.
1719
/// \param string_expr: string literal expression to convert
@@ -21,7 +23,7 @@ Author: Chris Smowton, [email protected]
2123
/// structure will also be initialised and added to the symbol table.
2224
/// \return a symbol_expr corresponding to the new or existing literal symbol.
2325
symbol_exprt get_or_create_string_literal_symbol(
24-
const exprt &string_expr,
26+
const java_string_literal_exprt &string_expr,
2527
symbol_table_baset &symbol_table,
2628
bool string_refinement_enabled);
2729

0 commit comments

Comments
 (0)