Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e11fef

Browse files
author
Daniel Kroening
committedMay 25, 2017
the c_types now have stronger C++ types
1 parent d27ffe5 commit 2e11fef

File tree

4 files changed

+216
-167
lines changed

4 files changed

+216
-167
lines changed
 

‎src/ansi-c/ansi_c_convert_type.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,25 @@ void ansi_c_convert_typet::write(typet &type)
464464
}
465465

466466
if(int8_cnt)
467-
type=is_signed?signed_char_type():unsigned_char_type();
467+
if(is_signed)
468+
type=signed_char_type();
469+
else
470+
type=unsigned_char_type();
468471
else if(int16_cnt)
469-
type=is_signed?signed_short_int_type():unsigned_short_int_type();
472+
if(is_signed)
473+
type=signed_short_int_type();
474+
else
475+
type=unsigned_short_int_type();
470476
else if(int32_cnt)
471-
type=is_signed?signed_int_type():unsigned_int_type();
477+
if(is_signed)
478+
type=signed_int_type();
479+
else
480+
type=unsigned_int_type();
472481
else if(int64_cnt) // Visual Studio: equivalent to long long int
473-
type=
474-
is_signed?signed_long_long_int_type():unsigned_long_long_int_type();
482+
if(is_signed)
483+
type=signed_long_long_int_type();
484+
else
485+
type=unsigned_long_long_int_type();
475486
else
476487
assert(false);
477488
}
@@ -509,19 +520,31 @@ void ansi_c_convert_typet::write(typet &type)
509520
throw 0;
510521
}
511522

512-
type=is_signed?signed_short_int_type():unsigned_short_int_type();
523+
if(is_signed)
524+
type=signed_short_int_type();
525+
else
526+
type=unsigned_short_int_type();
513527
}
514528
else if(long_cnt==0)
515529
{
516-
type=is_signed?signed_int_type():unsigned_int_type();
530+
if(is_signed)
531+
type=signed_int_type();
532+
else
533+
type=unsigned_int_type();
517534
}
518535
else if(long_cnt==1)
519536
{
520-
type=is_signed?signed_long_int_type():unsigned_long_int_type();
537+
if(is_signed)
538+
type=signed_long_int_type();
539+
else
540+
type=unsigned_long_int_type();
521541
}
522542
else if(long_cnt==2)
523543
{
524-
type=is_signed?signed_long_long_int_type():unsigned_long_long_int_type();
544+
if(is_signed)
545+
type=signed_long_long_int_type();
546+
else
547+
type=unsigned_long_long_int_type();
525548
}
526549
else
527550
{

‎src/ansi-c/c_typecheck_type.cpp

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,39 +134,74 @@ void c_typecheck_baset::typecheck_type(typet &type)
134134
typet result;
135135

136136
if(mode=="__QI__") // 8 bits
137-
result=is_signed?signed_char_type():unsigned_char_type();
137+
if(is_signed)
138+
result=signed_char_type();
139+
else
140+
result=unsigned_char_type();
138141
else if(mode=="__byte__") // 8 bits
139-
result=is_signed?signed_char_type():unsigned_char_type();
142+
if(is_signed)
143+
result=signed_char_type();
144+
else
145+
result=unsigned_char_type();
140146
else if(mode=="__HI__") // 16 bits
141-
result=is_signed?signed_short_int_type():unsigned_short_int_type();
147+
if(is_signed)
148+
result=signed_short_int_type();
149+
else
150+
result=unsigned_short_int_type();
142151
else if(mode=="__SI__") // 32 bits
143-
result=is_signed?signed_int_type():unsigned_int_type();
152+
if(is_signed)
153+
result=signed_int_type();
154+
else
155+
result=unsigned_int_type();
144156
else if(mode=="__word__") // long int, we think
145-
result=is_signed?signed_long_int_type():unsigned_long_int_type();
157+
if(is_signed)
158+
result=signed_long_int_type();
159+
else
160+
result=unsigned_long_int_type();
146161
else if(mode=="__pointer__") // we think this is size_t/ssize_t
147-
result=is_signed?signed_size_type():size_type();
162+
if(is_signed)
163+
result=signed_size_type();
164+
else
165+
result=size_type();
148166
else if(mode=="__DI__") // 64 bits
149167
{
150168
if(config.ansi_c.long_int_width==64)
151-
result=is_signed?signed_long_int_type():unsigned_long_int_type();
169+
if(is_signed)
170+
result=signed_long_int_type();
171+
else
172+
result=unsigned_long_int_type();
152173
else
153174
{
154175
assert(config.ansi_c.long_long_int_width==64);
155-
result=
156-
is_signed?signed_long_long_int_type():unsigned_long_long_int_type();
176+
177+
if(is_signed)
178+
result=signed_long_long_int_type();
179+
else
180+
result=unsigned_long_long_int_type();
157181
}
158182
}
159183
else if(mode=="__TI__") // 128 bits
160-
result=is_signed?gcc_signed_int128_type():gcc_unsigned_int128_type();
184+
if(is_signed)
185+
result=gcc_signed_int128_type();
186+
else
187+
result=gcc_unsigned_int128_type();
161188
else if(mode=="__V2SI__") // vector of 2 ints, deprecated by gcc
162-
result=
163-
vector_typet(
164-
is_signed?signed_int_type():unsigned_int_type(),
189+
if(is_signed)
190+
result=vector_typet(
191+
signed_int_type(),
192+
from_integer(2, size_type()));
193+
else
194+
result=vector_typet(
195+
unsigned_int_type(),
165196
from_integer(2, size_type()));
166197
else if(mode=="__V4SI__") // vector of 4 ints, deprecated by gcc
167-
result=
168-
vector_typet(
169-
is_signed?signed_int_type():unsigned_int_type(),
198+
if(is_signed)
199+
result=vector_typet(
200+
signed_int_type(),
201+
from_integer(4, size_type()));
202+
else
203+
result=vector_typet(
204+
unsigned_int_type(),
170205
from_integer(4, size_type()));
171206
else // give up, just use subtype
172207
result=type.subtype();

‎src/util/c_types.cpp

Lines changed: 104 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Function: index_type
2323
2424
\*******************************************************************/
2525

26-
typet index_type()
26+
bitvector_typet index_type()
2727
{
2828
// same as signed size type
2929
return signed_size_type();
@@ -41,7 +41,7 @@ Function: enum_constant_type
4141
4242
\*******************************************************************/
4343

44-
typet enum_constant_type()
44+
bitvector_typet enum_constant_type()
4545
{
4646
// usually same as 'int',
4747
// but might be unsigned, or shorter than 'int'
@@ -60,9 +60,9 @@ Function: signed_int_type
6060
6161
\*******************************************************************/
6262

63-
typet signed_int_type()
63+
signedbv_typet signed_int_type()
6464
{
65-
typet result=signedbv_typet(config.ansi_c.int_width);
65+
signedbv_typet result(config.ansi_c.int_width);
6666
result.set(ID_C_c_type, ID_signed_int);
6767
return result;
6868
}
@@ -79,9 +79,9 @@ Function: signed_short_int_type
7979
8080
\*******************************************************************/
8181

82-
typet signed_short_int_type()
82+
signedbv_typet signed_short_int_type()
8383
{
84-
typet result=signedbv_typet(config.ansi_c.short_int_width);
84+
signedbv_typet result(config.ansi_c.short_int_width);
8585
result.set(ID_C_c_type, ID_signed_short_int);
8686
return result;
8787
}
@@ -98,9 +98,9 @@ Function: unsigned_int_type
9898
9999
\*******************************************************************/
100100

101-
typet unsigned_int_type()
101+
unsignedbv_typet unsigned_int_type()
102102
{
103-
typet result=unsignedbv_typet(config.ansi_c.int_width);
103+
unsignedbv_typet result(config.ansi_c.int_width);
104104
result.set(ID_C_c_type, ID_unsigned_int);
105105
return result;
106106
}
@@ -117,9 +117,9 @@ Function: unsigned_short_int_type
117117
118118
\*******************************************************************/
119119

120-
typet unsigned_short_int_type()
120+
unsignedbv_typet unsigned_short_int_type()
121121
{
122-
typet result=unsignedbv_typet(config.ansi_c.short_int_width);
122+
unsignedbv_typet result(config.ansi_c.short_int_width);
123123
result.set(ID_C_c_type, ID_unsigned_short_int);
124124
return result;
125125
}
@@ -136,7 +136,7 @@ Function: size_type
136136
137137
\*******************************************************************/
138138

139-
typet size_type()
139+
unsignedbv_typet size_type()
140140
{
141141
// The size type varies. This is unsigned int on some systems,
142142
// and unsigned long int on others,
@@ -164,7 +164,7 @@ Function: signed_size_type
164164
165165
\*******************************************************************/
166166

167-
typet signed_size_type()
167+
signedbv_typet signed_size_type()
168168
{
169169
// we presume this is the same as pointer difference
170170
return pointer_diff_type();
@@ -182,9 +182,9 @@ Function: signed_long_int_type
182182
183183
\*******************************************************************/
184184

185-
typet signed_long_int_type()
185+
signedbv_typet signed_long_int_type()
186186
{
187-
typet result=signedbv_typet(config.ansi_c.long_int_width);
187+
signedbv_typet result(config.ansi_c.long_int_width);
188188
result.set(ID_C_c_type, ID_signed_long_int);
189189
return result;
190190
}
@@ -201,9 +201,9 @@ Function: signed_long_long_int_type
201201
202202
\*******************************************************************/
203203

204-
typet signed_long_long_int_type()
204+
signedbv_typet signed_long_long_int_type()
205205
{
206-
typet result=signedbv_typet(config.ansi_c.long_long_int_width);
206+
signedbv_typet result(config.ansi_c.long_long_int_width);
207207
result.set(ID_C_c_type, ID_signed_long_long_int);
208208
return result;
209209
}
@@ -220,9 +220,9 @@ Function: unsigned_long_int_type
220220
221221
\*******************************************************************/
222222

223-
typet unsigned_long_int_type()
223+
unsignedbv_typet unsigned_long_int_type()
224224
{
225-
typet result=unsignedbv_typet(config.ansi_c.long_int_width);
225+
unsignedbv_typet result(config.ansi_c.long_int_width);
226226
result.set(ID_C_c_type, ID_unsigned_long_int);
227227
return result;
228228
}
@@ -239,9 +239,9 @@ Function: unsigned_long_long_int_type
239239
240240
\*******************************************************************/
241241

242-
typet unsigned_long_long_int_type()
242+
unsignedbv_typet unsigned_long_long_int_type()
243243
{
244-
typet result=unsignedbv_typet(config.ansi_c.long_long_int_width);
244+
unsignedbv_typet result(config.ansi_c.long_long_int_width);
245245
result.set(ID_C_c_type, ID_unsigned_long_long_int);
246246
return result;
247247
}
@@ -276,23 +276,25 @@ Function: char_type
276276
277277
\*******************************************************************/
278278

279-
typet char_type()
279+
bitvector_typet char_type()
280280
{
281-
typet result;
282-
283281
// this can be signed or unsigned, depending on the architecture
284282

285-
if(config.ansi_c.char_is_unsigned)
286-
result=unsignedbv_typet(config.ansi_c.char_width);
287-
else
288-
result=signedbv_typet(config.ansi_c.char_width);
289-
290283
// There are 3 char types, i.e., this one is
291284
// different from either signed char or unsigned char!
292285

293-
result.set(ID_C_c_type, ID_char);
294-
295-
return result;
286+
if(config.ansi_c.char_is_unsigned)
287+
{
288+
unsignedbv_typet result(config.ansi_c.char_width);
289+
result.set(ID_C_c_type, ID_char);
290+
return result;
291+
}
292+
else
293+
{
294+
signedbv_typet result(config.ansi_c.char_width);
295+
result.set(ID_C_c_type, ID_char);
296+
return result;
297+
}
296298
}
297299

298300
/*******************************************************************\
@@ -307,12 +309,10 @@ Function: unsigned_char_type
307309
308310
\*******************************************************************/
309311

310-
typet unsigned_char_type()
312+
unsignedbv_typet unsigned_char_type()
311313
{
312-
typet result=unsignedbv_typet(config.ansi_c.char_width);
313-
314+
unsignedbv_typet result(config.ansi_c.char_width);
314315
result.set(ID_C_c_type, ID_unsigned_char);
315-
316316
return result;
317317
}
318318

@@ -328,12 +328,10 @@ Function: signed_char_type
328328
329329
\*******************************************************************/
330330

331-
typet signed_char_type()
331+
signedbv_typet signed_char_type()
332332
{
333-
typet result=signedbv_typet(config.ansi_c.char_width);
334-
333+
signedbv_typet result(config.ansi_c.char_width);
335334
result.set(ID_C_c_type, ID_signed_char);
336-
337335
return result;
338336
}
339337

@@ -349,18 +347,20 @@ Function: wchar_t_type
349347
350348
\*******************************************************************/
351349

352-
typet wchar_t_type()
350+
bitvector_typet wchar_t_type()
353351
{
354-
typet result;
355-
356352
if(config.ansi_c.wchar_t_is_unsigned)
357-
result=unsignedbv_typet(config.ansi_c.wchar_t_width);
353+
{
354+
unsignedbv_typet result(config.ansi_c.wchar_t_width);
355+
result.set(ID_C_c_type, ID_wchar_t);
356+
return result;
357+
}
358358
else
359-
result=signedbv_typet(config.ansi_c.wchar_t_width);
360-
361-
result.set(ID_C_c_type, ID_wchar_t);
362-
363-
return result;
359+
{
360+
signedbv_typet result(config.ansi_c.wchar_t_width);
361+
result.set(ID_C_c_type, ID_wchar_t);
362+
return result;
363+
}
364364
}
365365

366366
/*******************************************************************\
@@ -375,17 +375,13 @@ Function: char16_t_type
375375
376376
\*******************************************************************/
377377

378-
typet char16_t_type()
378+
unsignedbv_typet char16_t_type()
379379
{
380-
typet result;
381-
382380
// Types char16_t and char32_t denote distinct types with the same size,
383381
// signedness, and alignment as uint_least16_t and uint_least32_t,
384382
// respectively, in <stdint.h>, called the underlying types.
385-
result=unsignedbv_typet(16);
386-
383+
unsignedbv_typet result(16);
387384
result.set(ID_C_c_type, ID_char16_t);
388-
389385
return result;
390386
}
391387

@@ -401,17 +397,13 @@ Function: char32_t_type
401397
402398
\*******************************************************************/
403399

404-
typet char32_t_type()
400+
unsignedbv_typet char32_t_type()
405401
{
406-
typet result;
407-
408402
// Types char16_t and char32_t denote distinct types with the same size,
409403
// signedness, and alignment as uint_least16_t and uint_least32_t,
410404
// respectively, in <stdint.h>, called the underlying types.
411-
result=unsignedbv_typet(32);
412-
405+
unsignedbv_typet result(32);
413406
result.set(ID_C_c_type, ID_char32_t);
414-
415407
return result;
416408
}
417409

@@ -427,23 +419,23 @@ Function: float_type
427419
428420
\*******************************************************************/
429421

430-
typet float_type()
422+
bitvector_typet float_type()
431423
{
432-
typet result;
433-
434424
if(config.ansi_c.use_fixed_for_float)
435425
{
436-
fixedbv_typet tmp;
437-
tmp.set_width(config.ansi_c.single_width);
438-
tmp.set_integer_bits(config.ansi_c.single_width/2);
439-
result=tmp;
426+
fixedbv_typet result;
427+
result.set_width(config.ansi_c.single_width);
428+
result.set_integer_bits(config.ansi_c.single_width/2);
429+
result.set(ID_C_c_type, ID_float);
430+
return result;
440431
}
441432
else
442-
result=ieee_float_spect::single_precision().to_type();
443-
444-
result.set(ID_C_c_type, ID_float);
445-
446-
return result;
433+
{
434+
floatbv_typet result=
435+
ieee_float_spect::single_precision().to_type();
436+
result.set(ID_C_c_type, ID_float);
437+
return result;
438+
}
447439
}
448440

449441
/*******************************************************************\
@@ -458,23 +450,23 @@ Function: double_type
458450
459451
\*******************************************************************/
460452

461-
typet double_type()
453+
bitvector_typet double_type()
462454
{
463-
typet result;
464-
465455
if(config.ansi_c.use_fixed_for_float)
466456
{
467-
fixedbv_typet tmp;
468-
tmp.set_width(config.ansi_c.double_width);
469-
tmp.set_integer_bits(config.ansi_c.double_width/2);
470-
result=tmp;
457+
fixedbv_typet result;
458+
result.set_width(config.ansi_c.double_width);
459+
result.set_integer_bits(config.ansi_c.double_width/2);
460+
result.set(ID_C_c_type, ID_double);
461+
return result;
471462
}
472463
else
473-
result=ieee_float_spect::double_precision().to_type();
474-
475-
result.set(ID_C_c_type, ID_double);
476-
477-
return result;
464+
{
465+
floatbv_typet result=
466+
ieee_float_spect::double_precision().to_type();
467+
result.set(ID_C_c_type, ID_double);
468+
return result;
469+
}
478470
}
479471

480472
/*******************************************************************\
@@ -489,19 +481,19 @@ Function: long_double_type
489481
490482
\*******************************************************************/
491483

492-
typet long_double_type()
484+
bitvector_typet long_double_type()
493485
{
494-
typet result;
495-
496486
if(config.ansi_c.use_fixed_for_float)
497487
{
498-
fixedbv_typet tmp;
499-
tmp.set_width(config.ansi_c.long_double_width);
500-
tmp.set_integer_bits(config.ansi_c.long_double_width/2);
501-
result=tmp;
488+
fixedbv_typet result;
489+
result.set_width(config.ansi_c.long_double_width);
490+
result.set_integer_bits(config.ansi_c.long_double_width/2);
491+
result.set(ID_C_c_type, ID_long_double);
492+
return result;
502493
}
503494
else
504495
{
496+
floatbv_typet result;
505497
if(config.ansi_c.long_double_width==128)
506498
result=ieee_float_spect::quadruple_precision().to_type();
507499
else if(config.ansi_c.long_double_width==64)
@@ -520,11 +512,11 @@ typet long_double_type()
520512
}
521513
else
522514
assert(false);
523-
}
524515

525-
result.set(ID_C_c_type, ID_long_double);
516+
result.set(ID_C_c_type, ID_long_double);
526517

527-
return result;
518+
return result;
519+
}
528520
}
529521

530522
/*******************************************************************\
@@ -539,26 +531,25 @@ Function: gcc_float128_type
539531
540532
\*******************************************************************/
541533

542-
typet gcc_float128_type()
534+
bitvector_typet gcc_float128_type()
543535
{
544-
typet result;
536+
// not same as long double!
545537

546538
if(config.ansi_c.use_fixed_for_float)
547539
{
548-
fixedbv_typet tmp;
549-
tmp.set_width(128);
550-
tmp.set_integer_bits(128/2);
551-
result=tmp;
540+
fixedbv_typet result;
541+
result.set_width(128);
542+
result.set_integer_bits(128/2);
543+
result.set(ID_C_c_type, ID_gcc_float128);
544+
return result;
552545
}
553546
else
554547
{
555-
result=ieee_float_spect::quadruple_precision().to_type();
548+
floatbv_typet result=
549+
ieee_float_spect::quadruple_precision().to_type();
550+
result.set(ID_C_c_type, ID_gcc_float128);
551+
return result;
556552
}
557-
558-
// not same as long double!
559-
result.set(ID_C_c_type, ID_gcc_float128);
560-
561-
return result;
562553
}
563554

564555
/*******************************************************************\
@@ -573,7 +564,7 @@ Function: pointer_diff_type
573564
574565
\*******************************************************************/
575566

576-
typet pointer_diff_type()
567+
signedbv_typet pointer_diff_type()
577568
{
578569
// The pointer-diff type varies. This is signed int on some systems,
579570
// and signed long int on others, and signed long long on say Windows.
@@ -600,7 +591,7 @@ Function: pointer_type
600591
601592
\*******************************************************************/
602593

603-
typet pointer_type(const typet &subtype)
594+
pointer_typet pointer_type(const typet &subtype)
604595
{
605596
return pointer_typet(subtype);
606597
}
@@ -617,7 +608,7 @@ Function: reference_type
617608
618609
\*******************************************************************/
619610

620-
typet reference_type(const typet &subtype)
611+
reference_typet reference_type(const typet &subtype)
621612
{
622613
return reference_typet(subtype);
623614
}
@@ -651,9 +642,9 @@ Function: gcc_unsigned_int128_type
651642
652643
\*******************************************************************/
653644

654-
typet gcc_unsigned_int128_type()
645+
unsignedbv_typet gcc_unsigned_int128_type()
655646
{
656-
typet result=unsignedbv_typet(128);
647+
unsignedbv_typet result(128);
657648
result.set(ID_C_c_type, ID_unsigned_int128);
658649
return result;
659650
}
@@ -670,9 +661,9 @@ Function: gcc_signed_int128_type
670661
671662
\*******************************************************************/
672663

673-
typet gcc_signed_int128_type()
664+
signedbv_typet gcc_signed_int128_type()
674665
{
675-
typet result=signedbv_typet(128);
666+
signedbv_typet result(128);
676667
result.set(ID_C_c_type, ID_signed_int128);
677668
return result;
678669
}

‎src/util/c_types.h

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,39 @@ Author: Daniel Kroening, kroening@kroening.com
99
#ifndef CPROVER_UTIL_C_TYPES_H
1010
#define CPROVER_UTIL_C_TYPES_H
1111

12-
#include "type.h"
13-
14-
typet index_type();
15-
typet enum_constant_type();
16-
typet signed_int_type();
17-
typet unsigned_int_type();
18-
typet signed_long_int_type();
19-
typet signed_short_int_type();
20-
typet unsigned_short_int_type();
21-
typet signed_long_long_int_type();
22-
typet unsigned_long_int_type();
23-
typet unsigned_long_long_int_type();
12+
#include "std_types.h"
13+
14+
bitvector_typet index_type();
15+
bitvector_typet enum_constant_type();
16+
signedbv_typet signed_int_type();
17+
unsignedbv_typet unsigned_int_type();
18+
signedbv_typet signed_long_int_type();
19+
signedbv_typet signed_short_int_type();
20+
unsignedbv_typet unsigned_short_int_type();
21+
signedbv_typet signed_long_long_int_type();
22+
unsignedbv_typet unsigned_long_int_type();
23+
unsignedbv_typet unsigned_long_long_int_type();
2424
typet c_bool_type();
25-
typet char_type();
26-
typet unsigned_char_type();
27-
typet signed_char_type();
28-
typet wchar_t_type();
29-
typet char16_t_type();
30-
typet char32_t_type();
31-
typet float_type();
32-
typet double_type();
33-
typet long_double_type();
34-
typet gcc_float128_type();
35-
typet gcc_unsigned_int128_type();
36-
typet gcc_signed_int128_type();
37-
typet size_type();
38-
typet signed_size_type();
39-
typet pointer_diff_type();
40-
typet pointer_type(const typet &);
25+
bitvector_typet char_type();
26+
unsignedbv_typet unsigned_char_type();
27+
signedbv_typet signed_char_type();
28+
bitvector_typet wchar_t_type();
29+
unsignedbv_typet char16_t_type();
30+
unsignedbv_typet char32_t_type();
31+
bitvector_typet float_type();
32+
bitvector_typet double_type();
33+
bitvector_typet long_double_type();
34+
bitvector_typet gcc_float128_type();
35+
unsignedbv_typet gcc_unsigned_int128_type();
36+
signedbv_typet gcc_signed_int128_type();
37+
unsignedbv_typet size_type();
38+
signedbv_typet signed_size_type();
39+
signedbv_typet pointer_diff_type();
40+
pointer_typet pointer_type(const typet &);
4141
typet void_type();
4242

4343
// This is for Java and C++
44-
typet reference_type(const typet &);
44+
reference_typet reference_type(const typet &);
4545

4646
// Turns an ID_C_c_type into a string, e.g.,
4747
// ID_signed_int gets "signed int".

0 commit comments

Comments
 (0)
Please sign in to comment.