From 0016e99c0e8092b0689669acced1d55adafaccaf Mon Sep 17 00:00:00 2001 From: Remi Delmas Date: Tue, 29 Mar 2022 12:00:01 -0400 Subject: [PATCH] C front-end: factor out adding parameters to symbol table Upcoming code contracts related changes will want to use the same code. --- src/ansi-c/c_typecheck_base.cpp | 66 +++++++++++++++++++-------------- src/ansi-c/c_typecheck_base.h | 3 ++ 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/ansi-c/c_typecheck_base.cpp b/src/ansi-c/c_typecheck_base.cpp index 104da3a02a0..efc5cbd787f 100644 --- a/src/ansi-c/c_typecheck_base.cpp +++ b/src/ansi-c/c_typecheck_base.cpp @@ -521,34 +521,8 @@ void c_typecheck_baset::typecheck_function_body(symbolt &symbol) // set return type return_type=code_type.return_type(); - unsigned anon_counter=0; - - // Add the parameter declarations into the symbol table. - for(auto &p : code_type.parameters()) - { - // may be anonymous - if(p.get_base_name().empty()) - { - irep_idt base_name="#anon"+std::to_string(anon_counter++); - p.set_base_name(base_name); - } - - // produce identifier - irep_idt base_name = p.get_base_name(); - irep_idt identifier=id2string(symbol.name)+"::"+id2string(base_name); - - p.set_identifier(identifier); - - parameter_symbolt p_symbol; - - p_symbol.type = p.type(); - p_symbol.name=identifier; - p_symbol.base_name=base_name; - p_symbol.location = p.source_location(); - - symbolt *new_p_symbol; - move_symbol(p_symbol, new_p_symbol); - } + // Add the parameter declarations into the symbol table + add_parameters_to_symbol_table(symbol); // typecheck the body code typecheck_code(to_code(symbol.value)); @@ -774,3 +748,39 @@ void c_typecheck_baset::typecheck_declaration( } } } + +void c_typecheck_baset::add_parameters_to_symbol_table(symbolt &symbol) +{ + PRECONDITION(can_cast_type(symbol.type)); + + code_typet &code_type = to_code_type(symbol.type); + + unsigned anon_counter = 0; + + // Add the parameter declarations into the symbol table. + for(auto &p : code_type.parameters()) + { + // may be anonymous + if(p.get_base_name().empty()) + { + irep_idt base_name = "#anon" + std::to_string(anon_counter++); + p.set_base_name(base_name); + } + + // produce identifier + irep_idt base_name = p.get_base_name(); + irep_idt identifier = id2string(symbol.name) + "::" + id2string(base_name); + + p.set_identifier(identifier); + + parameter_symbolt p_symbol; + + p_symbol.type = p.type(); + p_symbol.name = identifier; + p_symbol.base_name = base_name; + p_symbol.location = p.source_location(); + + symbolt *new_p_symbol; + move_symbol(p_symbol, new_p_symbol); + } +} diff --git a/src/ansi-c/c_typecheck_base.h b/src/ansi-c/c_typecheck_base.h index 4ee55c2cde9..bc13f60b943 100644 --- a/src/ansi-c/c_typecheck_base.h +++ b/src/ansi-c/c_typecheck_base.h @@ -268,6 +268,9 @@ class c_typecheck_baset: symbolt &old_symbol, symbolt &new_symbol); void typecheck_function_body(symbolt &symbol); + /// Create symbols for parameter of the code-typed symbol \p symbol. + void add_parameters_to_symbol_table(symbolt &symbol); + virtual void do_initializer(symbolt &symbol); static bool is_numeric_type(const typet &src)