Skip to content

Commit d2c1f4b

Browse files
committed
introduce typet::add_subtype()
This introduces a new method typet::add_subtype(), identical in behavior to typet::subtype() nonconst, to allow current users of this method to add a subtype to a given type. The name of the method reflects that the subtype is added.
1 parent 040c93c commit d2c1f4b

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/util/type.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class typet:public irept
5454
return static_cast<const typet &>(get_sub().front());
5555
}
5656

57-
public:
5857
// This method will be protected eventually.
58+
public:
5959
typet &subtype()
6060
{
6161
subt &sub=get_sub();
@@ -64,6 +64,20 @@ class typet:public irept
6464
return static_cast<typet &>(sub.front());
6565
}
6666

67+
public:
68+
// This method allows the construction of a type with a subtype by
69+
// starting from a type without subtype. It avoids copying the contents
70+
// of the type. The primary use-case are parsers, where a copy could be
71+
// too expensive. Consider type_with_subtypet(id, subtype) for other
72+
// use-cases.
73+
typet &add_subtype()
74+
{
75+
subt &sub = get_sub();
76+
if(sub.empty())
77+
sub.resize(1);
78+
return static_cast<typet &>(sub.front());
79+
}
80+
6781
bool has_subtypes() const
6882
{ return !get_sub().empty(); }
6983

0 commit comments

Comments
 (0)