Skip to content

Commit bb65b1d

Browse files
author
Daniel Kroening
committed
clean up pointer qualifiers
1 parent 9d628cc commit bb65b1d

File tree

7 files changed

+80
-74
lines changed

7 files changed

+80
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#line 2 "main.ii"
2-
template<typename T> struct member_fkt_ptr { };
32

4-
// the two below are different
5-
//template<typename R, typename A> struct member_fkt_ptr<R (A::*)()> { };
6-
template<typename R, typename A> struct member_fkt_ptr<R (A::*)() const> { };
3+
template <class _Tp> struct remove_pointer;
4+
5+
// the below are different
6+
template <class _Tp> struct remove_pointer {typedef _Tp type;};
7+
template <class _Tp> struct remove_pointer<_Tp*> {typedef _Tp type;};
8+
template <class _Tp> struct remove_pointer<_Tp* const> {typedef _Tp type;};
9+
template <class _Tp> struct remove_pointer<_Tp* volatile> {typedef _Tp type;};
10+
template <class _Tp> struct remove_pointer<_Tp* const volatile> {typedef _Tp type;};
711

812
int main()
913
{
10-
__CPROVER::dump_scopes;
1114
}

src/cpp/cpp_declarator.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ typet cpp_declaratort::merge_type(const typet &declaration_type) const
6262
t=declaration_type;
6363
break;
6464
}
65+
else if(t.id()==ID_merged_type)
66+
{
67+
// the chain continues with the last one
68+
assert(!t.subtypes().empty());
69+
p=&t.subtypes().back();
70+
}
6571
else
6672
{
67-
assert(t.id()!="");
73+
assert(t.id()!=irep_idt());
6874
p=&t.subtype();
6975
}
7076
}

src/cpp/cpp_type2name.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ std::string cpp_type2name(const typet &type)
115115
{
116116
std::string result;
117117

118-
if(type.get_bool(ID_C_constant) ||
119-
type.get(ID_C_qualifier)==ID_const)
118+
if(type.get_bool(ID_C_constant))
120119
result+="const_";
121120

122121
if(type.get_bool(ID_C_restricted))

src/cpp/cpp_typecheck_constructor.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ void cpp_typecheckt::default_cpctor(
270270
parameter_tor.set(ID_name, cpp_parameter);
271271
parameter_tor.type()=reference_typet();
272272
parameter_tor.type().subtype().make_nil();
273-
parameter_tor.type().add(ID_C_qualifier).make_nil();
274273
parameter_tor.add_source_location() = source_location;
275274

276275
// Parameter declaration
@@ -473,7 +472,6 @@ void cpp_typecheckt::default_assignop(
473472

474473
args_decl_declor.type().id(ID_pointer);
475474
args_decl_declor.type().set(ID_C_reference, true);
476-
args_decl_declor.type().add(ID_C_qualifier).make_nil();
477475
args_decl_declor.type().subtype().make_nil();
478476
args_decl_declor.value().make_nil();
479477
}

src/cpp/cpp_typecheck_type.cpp

-11
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,6 @@ void cpp_typecheckt::typecheck_type(typet &type)
120120
}
121121
}
122122
}
123-
124-
// now do qualifier
125-
if(type.find(ID_C_qualifier).is_not_nil())
126-
{
127-
typet &t=static_cast<typet &>(type.add(ID_C_qualifier));
128-
cpp_convert_plain_type(t);
129-
c_qualifierst q(t);
130-
q.write(type);
131-
}
132-
133-
type.remove(ID_C_qualifier);
134123
}
135124
else if(type.id()==ID_array)
136125
{

src/cpp/parse.cpp

+64-52
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class Parser
343343
unsigned number_of_errors;
344344
irep_idt current_function;
345345

346-
void merge_types(typet &src, typet &dest);
346+
void merge_types(const typet &src, typet &dest);
347347

348348
void set_location(irept &dest, const cpp_tokent &token)
349349
{
@@ -355,12 +355,22 @@ class Parser
355355
source_location.set_function(current_function);
356356
}
357357

358-
void make_subtype(typet &src, typet &dest)
358+
void make_subtype(const typet &src, typet &dest)
359359
{
360360
typet *p=&dest;
361+
361362
while(p->id()!=irep_idt() && p->is_not_nil())
362-
p=&p->subtype();
363-
p->swap(src);
363+
{
364+
if(p->id()==ID_merged_type)
365+
{
366+
assert(!p->subtypes().empty());
367+
p=&p->subtypes().back();
368+
}
369+
else
370+
p=&p->subtype();
371+
}
372+
373+
*p=src;
364374
}
365375

366376
unsigned int max_errors;
@@ -452,7 +462,7 @@ void Parser::make_sub_scope(const irep_idt &id, new_scopet::kindt kind)
452462

453463
/*******************************************************************\
454464
455-
Function:
465+
Function: Parser::rString
456466
457467
Inputs:
458468
@@ -472,7 +482,7 @@ bool Parser::rString(cpp_tokent &tk)
472482

473483
/*******************************************************************\
474484
475-
Function:
485+
Function: Parser::merge_types
476486
477487
Inputs:
478488
@@ -482,22 +492,22 @@ bool Parser::rString(cpp_tokent &tk)
482492
483493
\*******************************************************************/
484494

485-
void Parser::merge_types(typet &src, typet &dest)
495+
void Parser::merge_types(const typet &src, typet &dest)
486496
{
487497
if(src.is_nil()) return;
488498

489499
if(dest.is_nil())
490-
dest.swap(src);
500+
dest=src;
491501
else
492502
{
493503
if(dest.id()!=ID_merged_type)
494504
{
495505
typet tmp(ID_merged_type);
496506
tmp.move_to_subtypes(dest);
497-
dest.swap(tmp);
507+
dest=tmp;
498508
}
499509

500-
dest.move_to_subtypes(src);
510+
dest.copy_to_subtypes(src);
501511
}
502512
}
503513

@@ -3428,7 +3438,7 @@ bool Parser::rDeclarator(
34283438

34293439
/*******************************************************************\
34303440
3431-
Function:
3441+
Function: Parser::optPtrOperator
34323442
34333443
Inputs:
34343444
@@ -3461,17 +3471,21 @@ bool Parser::optPtrOperator(typet &ptrs)
34613471

34623472
if(t=='*')
34633473
{
3464-
typet op(ID_pointer);
3474+
pointer_typet op;
34653475
cpp_tokent tk;
34663476
lex.get_token(tk);
34673477
set_location(op, tk);
34683478

34693479
typet cv;
34703480
cv.make_nil();
3471-
optCvQualify(cv);
3472-
op.add(ID_C_qualifier).swap(cv);
3473-
3474-
t_list.push_back(op);
3481+
optCvQualify(cv); // the qualifier is for the pointer
3482+
if(cv.is_not_nil())
3483+
{
3484+
merge_types(op, cv);
3485+
t_list.push_back(cv);
3486+
}
3487+
else
3488+
t_list.push_back(op);
34753489
}
34763490
else if(t=='^')
34773491
{
@@ -3483,10 +3497,14 @@ bool Parser::optPtrOperator(typet &ptrs)
34833497

34843498
typet cv;
34853499
cv.make_nil();
3486-
optCvQualify(cv);
3487-
op.add(ID_C_qualifier).swap(cv);
3488-
3489-
t_list.push_back(op);
3500+
optCvQualify(cv); // the qualifier is for the pointer
3501+
if(cv.is_not_nil())
3502+
{
3503+
merge_types(op, cv);
3504+
t_list.push_back(cv);
3505+
}
3506+
else
3507+
t_list.push_back(op);
34903508
}
34913509
else if(isPtrToMember(0))
34923510
{
@@ -3496,10 +3514,14 @@ bool Parser::optPtrOperator(typet &ptrs)
34963514

34973515
typet cv;
34983516
cv.make_nil();
3499-
optCvQualify(cv);
3500-
op.add(ID_C_qualifier).swap(cv);
3501-
3502-
t_list.push_back(op);
3517+
optCvQualify(cv); // the qualifier is for the pointer
3518+
if(cv.is_not_nil())
3519+
{
3520+
merge_types(op, cv);
3521+
t_list.push_back(cv);
3522+
}
3523+
else
3524+
t_list.push_back(op);
35033525
}
35043526
else
35053527
break;
@@ -3533,7 +3555,17 @@ bool Parser::optPtrOperator(typet &ptrs)
35333555
it!=t_list.rend();
35343556
it++)
35353557
{
3536-
it->subtype().swap(ptrs);
3558+
if(it->id()==ID_merged_type)
3559+
{
3560+
assert(!it->subtypes().empty());
3561+
it->subtypes().back().subtype().swap(ptrs);
3562+
}
3563+
else
3564+
{
3565+
assert(it->is_not_nil());
3566+
it->subtype().swap(ptrs);
3567+
}
3568+
35373569
ptrs.swap(*it);
35383570
}
35393571

@@ -3947,7 +3979,7 @@ bool Parser::rOperatorName(irept &name)
39473979

39483980
/*******************************************************************\
39493981
3950-
Function:
3982+
Function: Parser::rCastOperatorName
39513983
39523984
Inputs:
39533985
@@ -3991,33 +4023,12 @@ bool Parser::rCastOperatorName(irept &name)
39914023

39924024
if(!optPtrOperator(ptr))
39934025
return false;
4026+
4027+
make_subtype(type_name, ptr);
4028+
merge_types(cv2, ptr);
4029+
name = ptr;
39944030

3995-
if(ptr.is_nil())
3996-
{
3997-
name=type_name;
3998-
return true;
3999-
}
4000-
else
4001-
{
4002-
std::list<typet> t_list;
4003-
do
4004-
{
4005-
t_list.push_back(ptr);
4006-
typet tmp = ptr.subtype();
4007-
ptr = tmp;
4008-
}while(ptr.is_not_nil());
4009-
4010-
ptr = type_name;
4011-
while(!t_list.empty())
4012-
{
4013-
t_list.back().subtype() = ptr;
4014-
ptr = t_list.back();
4015-
t_list.pop_back();
4016-
}
4017-
merge_types(cv2,ptr);
4018-
name = ptr;
4019-
return true;
4020-
}
4031+
return true;
40214032
}
40224033

40234034
/*******************************************************************\
@@ -6117,6 +6128,7 @@ bool Parser::rTypeNameOrFunctionType(typet &tname)
61176128

61186129
if(!optPtrOperator(tname))
61196130
return false;
6131+
61206132
return true;
61216133
}
61226134

src/util/irep_ids.txt

-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ C_volatile #volatile
391391
C_restricted #restricted
392392
C_identifier #identifier
393393
C_implicit #implicit
394-
C_qualifier #qualifier
395394
C_ptr32 #ptr32
396395
C_ptr64 #ptr64
397396
C_atomic #atomic

0 commit comments

Comments
 (0)