Skip to content

Commit cc02062

Browse files
author
thk123
committed
Extract out method for converting array types
The method allows for the caller to exclude the size of the array.
1 parent 82ab0ec commit cc02062

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

src/ansi-c/expr2c.cpp

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -495,18 +495,7 @@ std::string expr2ct::convert_rec(
495495
}
496496
else if(src.id()==ID_array)
497497
{
498-
// The [...] gets attached to the declarator.
499-
std::string array_suffix;
500-
501-
if(to_array_type(src).size().is_nil())
502-
array_suffix="[]";
503-
else
504-
array_suffix="["+convert(to_array_type(src).size())+"]";
505-
506-
// This won't really parse without declarator.
507-
// Note that qualifiers are passed down.
508-
return convert_rec(
509-
src.subtype(), qualifiers, declarator+array_suffix);
498+
return convert_array_type(src, qualifiers, declarator);
510499
}
511500
else if(src.id()==ID_incomplete_array)
512501
{
@@ -777,6 +766,68 @@ std::string expr2ct::convert_struct_type(
777766

778767
/*******************************************************************\
779768
769+
Function: expr2ct::convert_array_type
770+
771+
Inputs:
772+
src - The array type to convert
773+
qualifier
774+
declarator_str
775+
776+
Outputs: A C-like type declaration of an array
777+
778+
Purpose: To generate a C-like type declaration of an array. Includes
779+
the size of the array in the []
780+
781+
\*******************************************************************/
782+
783+
std::string expr2ct::convert_array_type(
784+
const typet &src,
785+
const c_qualifierst &qualifiers,
786+
const std::string &declarator_str)
787+
{
788+
return convert_array_type(src, qualifiers, declarator_str, true);
789+
}
790+
791+
/*******************************************************************\
792+
793+
Function: expr2ct::convert_array_type
794+
795+
Inputs:
796+
src - The array type to convert
797+
qualifier
798+
declarator_str
799+
inc_size_if_possible - Should the generated string include
800+
the size of the array (if it is known).
801+
802+
Outputs: A C-like type declaration of an array
803+
804+
Purpose: To generate a C-like type declaration of an array. Optionally
805+
can include or exclude the size of the array in the []
806+
807+
\*******************************************************************/
808+
809+
std::string expr2ct::convert_array_type(
810+
const typet &src,
811+
const c_qualifierst &qualifiers,
812+
const std::string &declarator_str,
813+
bool inc_size_if_possible)
814+
{
815+
// The [...] gets attached to the declarator.
816+
std::string array_suffix;
817+
818+
if(to_array_type(src).size().is_nil() || !inc_size_if_possible)
819+
array_suffix="[]";
820+
else
821+
array_suffix="["+convert(to_array_type(src).size())+"]";
822+
823+
// This won't really parse without declarator.
824+
// Note that qualifiers are passed down.
825+
return convert_rec(
826+
src.subtype(), qualifiers, declarator_str+array_suffix);
827+
}
828+
829+
/*******************************************************************\
830+
780831
Function: expr2ct::convert_typecast
781832
782833
Inputs:

src/ansi-c/expr2c_class.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ class expr2ct
5050
bool inc_struct_body,
5151
bool inc_padding_parameters);
5252

53+
virtual std::string convert_array_type(
54+
const typet &src,
55+
const c_qualifierst &qualifiers,
56+
const std::string &declarator_str);
57+
58+
std::string convert_array_type(
59+
const typet &src,
60+
const c_qualifierst &qualifiers,
61+
const std::string &declarator_str,
62+
bool inc_size_if_possible);
63+
5364
static std::string indent_str(unsigned indent);
5465

5566
std::unordered_map<irep_idt,

0 commit comments

Comments
 (0)