Skip to content

Commit 9670d27

Browse files
author
thk123
committed
Moved out a function for generating C code for a struct type
The code for generating the C-like code for a struct type moved into a sperate function. This allows for the caller to turn off generating the body of the struct (useful for when declaring a variable of the type of the struct).
1 parent 49b0141 commit 9670d27

File tree

2 files changed

+104
-23
lines changed

2 files changed

+104
-23
lines changed

src/ansi-c/expr2c.cpp

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -359,29 +359,7 @@ std::string expr2ct::convert_rec(
359359
}
360360
else if(src.id()==ID_struct)
361361
{
362-
const struct_typet &struct_type=to_struct_type(src);
363-
364-
std::string dest=q+"struct";
365-
366-
const irep_idt &tag=struct_type.get_tag();
367-
if(tag!="") dest+=" "+id2string(tag);
368-
dest+=" {";
369-
370-
for(struct_typet::componentst::const_iterator
371-
it=struct_type.components().begin();
372-
it!=struct_type.components().end();
373-
it++)
374-
{
375-
dest+=' ';
376-
dest+=convert_rec(it->type(), c_qualifierst(), id2string(it->get_name()));
377-
dest+=';';
378-
}
379-
380-
dest+=" }";
381-
382-
dest+=d;
383-
384-
return dest;
362+
return convert_struct_type(src, q, d);
385363
}
386364
else if(src.id()==ID_incomplete_struct)
387365
{
@@ -708,6 +686,97 @@ std::string expr2ct::convert_rec(
708686

709687
/*******************************************************************\
710688
689+
Function: expr2ct::convert_struct_type
690+
691+
Inputs:
692+
src - the struct type being converted
693+
qualifiers - any qualifiers on the type
694+
declarator - the declarator on the type
695+
696+
Outputs: Returns a type declaration for a struct, containing the
697+
body of the struct and in that body the padding parameters.
698+
699+
Purpose: To generate C-like string for defining the given struct
700+
701+
\*******************************************************************/
702+
std::string expr2ct::convert_struct_type(
703+
const typet &src,
704+
const std::string &qualifiers_str,
705+
const std::string &declarator_str)
706+
{
707+
return convert_struct_type(src, qualifiers_str, declarator_str, true, true);
708+
}
709+
710+
/*******************************************************************\
711+
712+
Function: expr2ct::convert_struct_type
713+
714+
Inputs:
715+
src - the struct type being converted
716+
qualifiers - any qualifiers on the type
717+
declarator - the declarator on the type
718+
inc_struct_body - when generating the code, should we include
719+
a complete definition of the struct
720+
inc_padding_parameters - should the padding parameters be included
721+
Note this only makes sense if inc_struct_body
722+
723+
Outputs: Returns a type declaration for a struct, optionally containing the
724+
body of the struct (and in that body, optionally the padding
725+
parameters).
726+
727+
Purpose: To generate C-like string for declaring (or defining) the given struct
728+
729+
\*******************************************************************/
730+
std::string expr2ct::convert_struct_type(
731+
const typet &src,
732+
const std::string &qualifiers,
733+
const std::string &declarator,
734+
bool inc_struct_body,
735+
bool inc_padding_parameters)
736+
{
737+
// Either we are including the body (in which case it makes sense to include
738+
// or exclude the parameters) or there is no body so therefore we definitely
739+
// shouldn't be including the parameters
740+
assert(inc_struct_body || !inc_padding_parameters);
741+
742+
const struct_typet &struct_type=to_struct_type(src);
743+
744+
std::string dest=qualifiers+"struct";
745+
746+
const irep_idt &tag=struct_type.get_tag();
747+
if(tag!="")
748+
dest+=" "+id2string(tag);
749+
750+
if(inc_struct_body)
751+
{
752+
dest+=" {";
753+
754+
for(struct_typet::componentst::const_iterator
755+
it=struct_type.components().begin();
756+
it!=struct_type.components().end();
757+
it++)
758+
{
759+
// Skip padding parameters unless we including them
760+
if(it->get_is_padding() && !inc_padding_parameters)
761+
{
762+
continue;
763+
}
764+
765+
dest+= ' ';
766+
dest+=convert_rec(it->type(), c_qualifierst(), id2string(it->get_name()));
767+
dest+= ';';
768+
}
769+
770+
dest+=" }";
771+
}
772+
773+
dest+=declarator;
774+
775+
return dest;
776+
}
777+
778+
/*******************************************************************\
779+
711780
Function: expr2ct::convert_typecast
712781
713782
Inputs:

src/ansi-c/expr2c_class.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ class expr2ct
3838
const c_qualifierst &qualifiers,
3939
const std::string &declarator);
4040

41+
virtual std::string convert_struct_type(
42+
const typet &src,
43+
const std::string &qualifiers_str,
44+
const std::string &declarator_str);
45+
46+
std::string convert_struct_type(
47+
const typet &src,
48+
const std::string &qualifer_str,
49+
const std::string &declarator_str,
50+
bool inc_struct_body,
51+
bool inc_padding_parameters);
52+
4153
static std::string indent_str(unsigned indent);
4254

4355
std::unordered_map<irep_idt,

0 commit comments

Comments
 (0)