Skip to content

Commit 14cf4a9

Browse files
committed
introduce __CPROVER_map type
This introduces C syntax for a type __CPROVER_map domain -> codomain where domain is a list of types and codomain is a type. The type is internal, for modeling purposes. It replaces arrays with size __CPROVER_infinity, which are now deprecated.
1 parent ed19cf1 commit 14cf4a9

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

src/ansi-c/c_typecheck_base.h

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Author: Daniel Kroening, [email protected]
2121

2222
class ansi_c_declarationt;
2323
class c_bit_field_typet;
24+
class mathematical_function_typet;
2425
class shift_exprt;
2526

2627
class c_typecheck_baset:
@@ -237,6 +238,7 @@ class c_typecheck_baset:
237238
virtual void typecheck_code_type(code_typet &type);
238239
virtual void typecheck_typedef_type(typet &type);
239240
virtual void typecheck_c_bit_field_type(c_bit_field_typet &type);
241+
virtual void typecheck_map_type(mathematical_function_typet &);
240242
virtual void typecheck_typeof_type(typet &type);
241243
virtual void typecheck_array_type(array_typet &type);
242244
virtual void typecheck_vector_type(typet &type);

src/ansi-c/c_typecheck_type.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ void c_typecheck_baset::typecheck_type(typet &type)
114114
{
115115
typecheck_vector_type(type);
116116
}
117+
else if(type.id() == ID_mathematical_function)
118+
{
119+
typecheck_map_type(to_mathematical_function_type(type));
120+
}
117121
else if(type.id()==ID_custom_unsignedbv ||
118122
type.id()==ID_custom_signedbv ||
119123
type.id()==ID_custom_floatbv ||
@@ -728,7 +732,6 @@ void c_typecheck_baset::typecheck_vector_type(typet &type)
728732
error() << "type had size 0: '" << to_string(subtype) << "'" << eom;
729733
throw 0;
730734
}
731-
732735
// adjust by width of base type
733736
if(s % *sub_size != 0)
734737
{
@@ -748,6 +751,14 @@ void c_typecheck_baset::typecheck_vector_type(typet &type)
748751
type = new_type;
749752
}
750753

754+
void c_typecheck_baset::typecheck_map_type(mathematical_function_typet &type)
755+
{
756+
for(auto &t : type.domain())
757+
typecheck_type(t);
758+
759+
typecheck_type(type.codomain());
760+
}
761+
751762
void c_typecheck_baset::typecheck_compound_type(struct_union_typet &type)
752763
{
753764
// These get replaced by symbol types later.

src/ansi-c/expr2c.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,27 @@ std::string expr2ct::convert_rec(
613613

614614
return q+dest+d;
615615
}
616+
else if(src.id() == ID_mathematical_function)
617+
{
618+
const mathematical_function_typet &function_type =
619+
to_mathematical_function_type(src);
620+
std::string dest = CPROVER_PREFIX "map ";
621+
bool first = true;
622+
for(auto &t : function_type.domain())
623+
{
624+
if(first)
625+
first = false;
626+
else
627+
dest += ", ";
628+
629+
dest += convert(t);
630+
}
631+
632+
dest += " -> ";
633+
dest += convert(function_type.codomain());
634+
635+
return q + dest + d;
636+
}
616637
else if(src.id()==ID_constructor ||
617638
src.id()==ID_destructor)
618639
{

src/ansi-c/parser.y

+14
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ extern char *yyansi_ctext;
195195
%token TOK_CPROVER_BITVECTOR "__CPROVER_bitvector"
196196
%token TOK_CPROVER_FLOATBV "__CPROVER_floatbv"
197197
%token TOK_CPROVER_FIXEDBV "__CPROVER_fixedbv"
198+
%token TOK_CPROVER_MAP "__CPROVER_map"
198199
%token TOK_CPROVER_ATOMIC "__CPROVER_atomic"
199200
%token TOK_CPROVER_BOOL "__CPROVER_bool"
200201
%token TOK_CPROVER_THROW "__CPROVER_throw"
@@ -1090,6 +1091,7 @@ type_specifier:
10901091
| typedef_type_specifier
10911092
| typeof_type_specifier
10921093
| atomic_type_specifier
1094+
| map_type_specifier
10931095
;
10941096

10951097
declaration_qualifier_list:
@@ -1533,6 +1535,18 @@ array_of_construct:
15331535
{ $$=$1; stack_type($$).subtype().swap(parser_stack($2)); }
15341536
;
15351537

1538+
map_type_specifier:
1539+
TOK_CPROVER_MAP type_specifier TOK_ARROW type_specifier
1540+
{
1541+
$$=$1;
1542+
parser_stack($$).id(ID_mathematical_function);
1543+
irept domain;
1544+
domain.move_to_sub(parser_stack($2));
1545+
parser_stack($$).move_to_sub(domain);
1546+
mts($$, $4);
1547+
}
1548+
;
1549+
15361550
pragma_packed:
15371551
{
15381552
init($$);

src/ansi-c/scanner.l

+1
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ __decltype { if(PARSER.cpp98 &&
12811281
{CPROVER_PREFIX}"bitvector" { loc(); return TOK_CPROVER_BITVECTOR; }
12821282
{CPROVER_PREFIX}"floatbv" { loc(); return TOK_CPROVER_FLOATBV; }
12831283
{CPROVER_PREFIX}"fixedbv" { loc(); return TOK_CPROVER_FIXEDBV; }
1284+
{CPROVER_PREFIX}"map" { loc(); return TOK_CPROVER_MAP; }
12841285
{CPROVER_PREFIX}"bool" { loc(); return TOK_CPROVER_BOOL; }
12851286
{CPROVER_PREFIX}"throw" { loc(); return TOK_CPROVER_THROW; }
12861287
{CPROVER_PREFIX}"catch" { loc(); return TOK_CPROVER_CATCH; }

0 commit comments

Comments
 (0)