Skip to content

C front-end: factor out adding parameters to symbol table #6796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 38 additions & 28 deletions src/ansi-c/c_typecheck_base.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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