Skip to content

Commit a6740e1

Browse files
author
thk123
committed
Use fieldref_expr to make the parsing more precise
1 parent 63b1946 commit a6740e1

4 files changed

+37
-21
lines changed

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -655,15 +655,17 @@ static irep_idt get_if_cmp_operator(const irep_idt &stmt)
655655
/// \param ns: Global namespace
656656
/// \return A member expression accessing the field, through base class
657657
/// components if necessary.
658-
static member_exprt
659-
to_member(const exprt &pointer, const exprt &field_instruction, const namespacet &ns)
658+
static member_exprt to_member(
659+
const exprt &pointer,
660+
const fieldref_exprt &field_instruction,
661+
const namespacet &ns)
660662
{
661-
struct_tag_typet class_type(field_instruction.get(ID_class));
663+
struct_tag_typet class_type(field_instruction.field_of_class());
662664

663665
const exprt typed_pointer =
664666
typecast_exprt::conditional_cast(pointer, java_reference_type(class_type));
665667

666-
const irep_idt &component_name = field_instruction.get(ID_component_name);
668+
const irep_idt &component_name = field_instruction.component_name();
667669

668670
exprt accessed_object = checked_dereference(typed_pointer, class_type);
669671
const auto type_of = [&](const exprt &object) {
@@ -1560,7 +1562,8 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
15601562
else if(statement=="getfield")
15611563
{
15621564
PRECONDITION(op.size() == 1 && results.size() == 1);
1563-
results[0] = java_bytecode_promotion(to_member(op[0], arg0, ns));
1565+
results[0] = java_bytecode_promotion(
1566+
to_member(op[0], expr_dynamic_cast<fieldref_exprt>(arg0), ns));
15641567
}
15651568
else if(statement=="getstatic")
15661569
{
@@ -1583,7 +1586,7 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
15831586
else if(statement=="putfield")
15841587
{
15851588
PRECONDITION(op.size() == 2 && results.empty());
1586-
c = convert_putfield(arg0, op);
1589+
c = convert_putfield(expr_dynamic_cast<fieldref_exprt>(arg0), op);
15871590
}
15881591
else if(statement=="putstatic")
15891592
{
@@ -2592,15 +2595,12 @@ code_blockt java_bytecode_convert_methodt::convert_putstatic(
25922595
}
25932596

25942597
code_blockt java_bytecode_convert_methodt::convert_putfield(
2595-
const exprt &arg0,
2598+
const fieldref_exprt &arg0,
25962599
const exprt::operandst &op)
25972600
{
25982601
code_blockt block;
25992602
save_stack_entries(
2600-
"stack_field",
2601-
block,
2602-
bytecode_write_typet::FIELD,
2603-
arg0.get(ID_component_name));
2603+
"stack_field", block, bytecode_write_typet::FIELD, arg0.component_name());
26042604
block.add(code_assignt(to_member(op[0], arg0, ns), op[1]));
26052605
return block;
26062606
}

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ class java_bytecode_convert_methodt:public messaget
395395
codet &c,
396396
exprt::operandst &results);
397397

398-
code_blockt convert_putfield(const exprt &arg0, const exprt::operandst &op);
398+
code_blockt
399+
convert_putfield(const fieldref_exprt &arg0, const exprt::operandst &op);
399400

400401
code_blockt convert_putstatic(
401402
const source_locationt &location,

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,17 @@ static void infer_opaque_type_fields(
311311
if(instruction.statement == "getfield" ||
312312
instruction.statement == "putfield")
313313
{
314-
const exprt &fieldref = instruction.args[0];
315-
irep_idt class_symbol_id = fieldref.get(ID_class);
314+
const fieldref_exprt &fieldref =
315+
expr_dynamic_cast<fieldref_exprt>(instruction.args[0]);
316+
irep_idt class_symbol_id = fieldref.field_of_class();
316317
const symbolt *class_symbol = symbol_table.lookup(class_symbol_id);
317318
INVARIANT(
318319
class_symbol != nullptr,
319320
"all types containing fields should have been loaded");
320321

321322
const java_class_typet *class_type =
322323
&to_java_class_type(class_symbol->type);
323-
const irep_idt &component_name = fieldref.get(ID_component_name);
324+
const irep_idt &component_name = fieldref.component_name();
324325
while(!class_type->has_component(component_name))
325326
{
326327
if(class_type->get_is_stub())
@@ -582,12 +583,10 @@ static void create_stub_global_symbols(
582583
INVARIANT(
583584
instruction.args.size() > 0,
584585
"get/putstatic should have at least one argument");
585-
irep_idt component = instruction.args[0].get_string(ID_component_name);
586-
INVARIANT(
587-
!component.empty(), "get/putstatic should specify a component");
588-
irep_idt class_id = instruction.args[0].get_string(ID_class);
589-
INVARIANT(
590-
!class_id.empty(), "get/putstatic should specify a class");
586+
const fieldref_exprt &field_ref =
587+
expr_dynamic_cast<fieldref_exprt>(instruction.args[0]);
588+
irep_idt component = field_ref.component_name();
589+
irep_idt class_id = field_ref.field_of_class();
591590

592591
// The final 'true' parameter here includes interfaces, as they can
593592
// define static fields.

jbmc/src/java_bytecode/java_bytecode_parse_tree.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,22 @@ class fieldref_exprt : public exprt
341341
set(ID_class, class_name);
342342
set(ID_component_name, component_name);
343343
}
344+
345+
irep_idt field_of_class() const
346+
{
347+
return get(ID_class);
348+
}
349+
350+
irep_idt component_name() const
351+
{
352+
return get(ID_component_name);
353+
}
344354
};
345355

356+
template <>
357+
inline bool can_cast_expr<fieldref_exprt>(const exprt &base)
358+
{
359+
return base.get(ID_class) != ID_nil && base.get(ID_component_name) != ID_nil;
360+
}
361+
346362
#endif // CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_PARSE_TREE_H

0 commit comments

Comments
 (0)