Skip to content

Commit fcff35b

Browse files
authored
Merge pull request #3018 from diffblue/merged_type
introduce merged_typet
2 parents 1e40c6c + c2d54a3 commit fcff35b

File tree

6 files changed

+75
-23
lines changed

6 files changed

+75
-23
lines changed

src/ansi-c/ansi_c_declaration.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Author: Daniel Kroening, [email protected]
1818
#include <util/std_types.h>
1919
#include <util/invariant.h>
2020

21+
#include "merged_type.h"
22+
2123
void ansi_c_declaratort::build(irept &src)
2224
{
2325
typet *p=static_cast<typet *>(&src);
@@ -47,8 +49,8 @@ void ansi_c_declaratort::build(irept &src)
4749
else if(t.id()==ID_merged_type)
4850
{
4951
// we always walk down the _last_ member of a merged type
50-
assert(!t.subtypes().empty());
51-
p=&(t.subtypes().back());
52+
auto &merged_type = to_merged_type(t);
53+
p = &merged_type.last_type();
5254
}
5355
else
5456
p=&t.subtype();
@@ -105,8 +107,8 @@ typet ansi_c_declarationt::full_type(
105107
else if(p->id()==ID_merged_type)
106108
{
107109
// we always go down on the right-most subtype
108-
assert(!p->subtypes().empty());
109-
p=&(p->subtypes().back());
110+
auto &merged_type = to_merged_type(*p);
111+
p = &merged_type.last_type();
110112
}
111113
else
112114
UNREACHABLE;

src/ansi-c/merged_type.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*******************************************************************\
2+
3+
Module: A type that holds a combination of types
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_ANSI_C_MERGED_TYPE_H
10+
#define CPROVER_ANSI_C_MERGED_TYPE_H
11+
12+
#include <util/type.h>
13+
14+
/// holds a combination of types
15+
class merged_typet : public type_with_subtypest
16+
{
17+
public:
18+
merged_typet() : type_with_subtypest(ID_merged_type)
19+
{
20+
}
21+
22+
typet &last_type()
23+
{
24+
return subtypes().back();
25+
}
26+
};
27+
28+
/// conversion to merged_typet
29+
inline const merged_typet &to_merged_type(const typet &type)
30+
{
31+
PRECONDITION(type.id() == ID_merged_type);
32+
DATA_INVARIANT(
33+
!static_cast<const type_with_subtypest &>(type).subtypes().empty(),
34+
"merge_typet has at least one subtype");
35+
return static_cast<const merged_typet &>(type);
36+
}
37+
38+
/// conversion to merged_typet
39+
inline merged_typet &to_merged_type(typet &type)
40+
{
41+
PRECONDITION(type.id() == ID_merged_type);
42+
DATA_INVARIANT(
43+
!static_cast<const type_with_subtypest &>(type).subtypes().empty(),
44+
"merge_typet has at least one subtype");
45+
return static_cast<merged_typet &>(type);
46+
}
47+
48+
#endif // CPROVER_ANSI_C_MERGED_TYPE_H

src/ansi-c/parser_static.inc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <util/std_expr.h>
55
#include <util/string2int.h>
66

7+
#include "merged_type.h"
8+
79
#define YYSTYPE unsigned
810
#define YYSTYPE_IS_TRIVIAL 1
911

@@ -115,7 +117,7 @@ static void merge_types(irept &dest, irept &src)
115117
if(dest.id()!=ID_merged_type)
116118
{
117119
source_locationt location=static_cast<const exprt &>(dest).source_location();
118-
typet new_type(ID_merged_type);
120+
merged_typet new_type;
119121
new_type.move_to_subtypes((typet &)(dest));
120122
dest.swap(new_type);
121123
static_cast<exprt &>(dest).add_source_location()=location;
@@ -203,8 +205,8 @@ static void make_subtype(typet &dest, typet &src)
203205
if(p->id()==ID_merged_type)
204206
{
205207
// do last one
206-
assert(!p->subtypes().empty());
207-
sub=&(p->subtypes().back());
208+
auto &merged_type = to_merged_type(*p);
209+
sub = &merged_type.last_type();
208210
}
209211

210212
if(sub->id()==ID_frontend_pointer ||
@@ -243,8 +245,8 @@ static void make_subtype(typet &dest, typet &src)
243245
}
244246
else if(p->id()==ID_merged_type)
245247
{
246-
assert(!p->subtypes().empty());
247-
p=&(p->subtypes().back());
248+
auto &merged_type = to_merged_type(*p);
249+
p=&merged_type.last_type();
248250
}
249251
else if(p->id()==irep_idt())
250252
assert(false);

src/cpp/cpp_declarator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "cpp_declarator.h"
1313

14+
#include <ansi-c/merged_type.h>
15+
1416
#include <ostream>
1517
#include <cassert>
1618

@@ -44,8 +46,8 @@ typet cpp_declaratort::merge_type(const typet &declaration_type) const
4446
else if(t.id()==ID_merged_type)
4547
{
4648
// the chain continues with the last one
47-
assert(!t.subtypes().empty());
48-
p=&t.subtypes().back();
49+
auto &merged_type = to_merged_type(t);
50+
p = &merged_type.last_type();
4951
}
5052
else
5153
{

src/cpp/cpp_typecheck_resolve.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Author: Daniel Kroening, [email protected]
2626
#include <util/string_constant.h>
2727

2828
#include <ansi-c/anonymous_member.h>
29+
#include <ansi-c/merged_type.h>
2930

3031
#include "cpp_typecheck.h"
3132
#include "cpp_template_type.h"
@@ -1892,12 +1893,9 @@ void cpp_typecheck_resolvet::guess_template_args(
18921893
else if(template_type.id()==ID_merged_type)
18931894
{
18941895
// look at subtypes
1895-
for(typet::subtypest::const_iterator
1896-
it=template_type.subtypes().begin();
1897-
it!=template_type.subtypes().end();
1898-
it++)
1896+
for(const auto &t : to_merged_type(template_type).subtypes())
18991897
{
1900-
guess_template_args(*it, desired_type);
1898+
guess_template_args(t, desired_type);
19011899
}
19021900
}
19031901
else if(is_reference(template_type) ||

src/cpp/parse.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ Author: Daniel Kroening, [email protected]
1414
#include <cassert>
1515
#include <map>
1616

17+
#include <util/c_types.h>
1718
#include <util/expr.h>
1819
#include <util/std_code.h>
1920
#include <util/std_expr.h>
2021
#include <util/std_types.h>
2122

2223
#include <ansi-c/ansi_c_y.tab.h>
23-
#include <util/c_types.h>
24+
#include <ansi-c/merged_type.h>
2425

2526
#include "cpp_token_buffer.h"
2627
#include "cpp_member_spec.h"
@@ -401,8 +402,8 @@ class Parser // NOLINT(readability/identifiers)
401402
{
402403
if(p->id()==ID_merged_type)
403404
{
404-
assert(!p->subtypes().empty());
405-
p=&p->subtypes().back();
405+
auto &merged_type = to_merged_type(*p);
406+
p = &merged_type.last_type();
406407
}
407408
else
408409
p=&p->subtype();
@@ -470,7 +471,7 @@ void Parser::merge_types(const typet &src, typet &dest)
470471
if(dest.id()!=ID_merged_type)
471472
{
472473
source_locationt location=dest.source_location();
473-
typet tmp(ID_merged_type);
474+
merged_typet tmp;
474475
tmp.move_to_subtypes(dest);
475476
tmp.add_source_location()=location;
476477
dest=tmp;
@@ -481,7 +482,6 @@ void Parser::merge_types(const typet &src, typet &dest)
481482
// merged_types
482483
typet::subtypest &sub=dest.subtypes();
483484
sub.emplace(sub.begin(), src);
484-
POSTCONDITION(!dest.subtypes().empty());
485485
}
486486
}
487487

@@ -3344,8 +3344,8 @@ bool Parser::optPtrOperator(typet &ptrs)
33443344
{
33453345
if(it->id()==ID_merged_type)
33463346
{
3347-
assert(!it->subtypes().empty());
3348-
it->subtypes().back().subtype().swap(ptrs);
3347+
auto &merged_type = to_merged_type(*it);
3348+
merged_type.last_type().subtype().swap(ptrs);
33493349
}
33503350
else
33513351
{

0 commit comments

Comments
 (0)