Skip to content

Commit 3f1fd64

Browse files
committed
Add code_typet::get_parameter_indices
This is simply the opposite of the existing get_parameter_indentifiers, useful for mapping from parameter identifier to its argument or parameter index.
1 parent a22dd1c commit 3f1fd64

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

src/util/std_types.h

+20
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,26 @@ class code_typet:public typet
890890
result.push_back(it->get_identifier());
891891
return result;
892892
}
893+
894+
typedef
895+
std::unordered_map<irep_idt, std::size_t, irep_id_hash> parameter_indicest;
896+
897+
/// Get a map from parameter name to its index
898+
parameter_indicest parameter_indices() const
899+
{
900+
parameter_indicest parameter_indices;
901+
const parameterst &p = parameters();
902+
parameter_indices.reserve(p.size());
903+
std::size_t index = 0;
904+
for(const auto &p : parameters())
905+
{
906+
const irep_idt &id = p.get_identifier();
907+
if(!id.empty())
908+
parameter_indices.insert({ id, index });
909+
++index;
910+
}
911+
return parameter_indices;
912+
}
893913
};
894914

895915
/*! \brief Cast a generic typet to a \ref code_typet

unit/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ SRC += unit_tests.cpp \
3535
util/expr_cast/expr_cast.cpp \
3636
util/expr_iterator.cpp \
3737
util/message.cpp \
38+
util/parameter_indices.cpp \
3839
util/simplify_expr.cpp \
3940
util/symbol_table.cpp \
4041
catch_example.cpp \

unit/util/ParameterIndicesTest.class

358 Bytes
Binary file not shown.

unit/util/ParameterIndicesTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
public class ParameterIndicesTest {
3+
4+
public void f(ParameterIndicesTest param1, int param2) {}
5+
6+
public static void g(float param1, ParameterIndicesTest param2) {}
7+
8+
}

unit/util/parameter_indices.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************************************\
2+
3+
Module: Parameter indices test
4+
5+
Author: Diffblue Limited. All rights reserved.
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/catch.hpp>
10+
#include <testing-utils/load_java_class.h>
11+
#include <util/std_types.h>
12+
13+
void check_consistency(const symbolt &symbol)
14+
{
15+
const auto &code_type = to_code_type(symbol.type);
16+
auto parameter_ids = code_type.parameter_identifiers();
17+
auto parameter_indices = code_type.parameter_indices();
18+
19+
REQUIRE(parameter_ids.size() == parameter_indices.size());
20+
for(std::size_t i = 0; i < parameter_ids.size(); ++i)
21+
REQUIRE(parameter_indices.at(parameter_ids.at(i)) == i);
22+
}
23+
24+
TEST_CASE("Parmeter indices consistency", "[core][util][parameter_indices]")
25+
{
26+
symbol_tablet symbol_table = load_java_class("ParameterIndicesTest", "util/");
27+
check_consistency(
28+
symbol_table.lookup_ref(
29+
"java::ParameterIndicesTest.f:(LParameterIndicesTest;I)V"));
30+
check_consistency(
31+
symbol_table.lookup_ref(
32+
"java::ParameterIndicesTest.g:(FLParameterIndicesTest;)V"));
33+
}

0 commit comments

Comments
 (0)