Skip to content

Commit

Permalink
Merge pull request diffblue#6796 from tautschnig/cleanup/refactor-add…
Browse files Browse the repository at this point in the history
…-param

C front-end: factor out adding parameters to symbol table
  • Loading branch information
kroening committed Apr 11, 2022
2 parents 40d7f29 + 0016e99 commit 799e221
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
66 changes: 38 additions & 28 deletions src/ansi-c/c_typecheck_base.cpp
Expand Up @@ -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));
Expand Down Expand Up @@ -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<code_typet>(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);
}
}
3 changes: 3 additions & 0 deletions src/ansi-c/c_typecheck_base.h
Expand Up @@ -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)
Expand Down

0 comments on commit 799e221

Please sign in to comment.