xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/constexpr-turing.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -verify -std=c++11 %s
2*f4a2713aSLionel Sambuc // expected-no-diagnostics
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc // A direct proof that constexpr is Turing-complete, once DR1454 is implemented.
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc const unsigned halt = (unsigned)-1;
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc enum Dir { L, R };
9*f4a2713aSLionel Sambuc struct Action {
10*f4a2713aSLionel Sambuc   bool tape;
11*f4a2713aSLionel Sambuc   Dir dir;
12*f4a2713aSLionel Sambuc   unsigned next;
13*f4a2713aSLionel Sambuc };
14*f4a2713aSLionel Sambuc using State = Action[2];
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc // An infinite tape!
17*f4a2713aSLionel Sambuc struct Tape {
TapeTape18*f4a2713aSLionel Sambuc   constexpr Tape() : l(0), val(false), r(0) {}
TapeTape19*f4a2713aSLionel Sambuc   constexpr Tape(const Tape &old, bool write) :
20*f4a2713aSLionel Sambuc     l(old.l), val(write), r(old.r) {}
TapeTape21*f4a2713aSLionel Sambuc   constexpr Tape(const Tape &old, Dir dir) :
22*f4a2713aSLionel Sambuc     l(dir == L ? old.l ? old.l->l : 0 : &old),
23*f4a2713aSLionel Sambuc     val(dir == L ? old.l ? old.l->val : false
24*f4a2713aSLionel Sambuc                  : old.r ? old.r->val : false),
25*f4a2713aSLionel Sambuc     r(dir == R ? old.r ? old.r->r : 0 : &old) {}
26*f4a2713aSLionel Sambuc   const Tape *l;
27*f4a2713aSLionel Sambuc   bool val;
28*f4a2713aSLionel Sambuc   const Tape *r;
29*f4a2713aSLionel Sambuc };
update(const Tape & old,bool write)30*f4a2713aSLionel Sambuc constexpr Tape update(const Tape &old, bool write) { return Tape(old, write); }
move(const Tape & old,Dir dir)31*f4a2713aSLionel Sambuc constexpr Tape move(const Tape &old, Dir dir) { return Tape(old, dir); }
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc // Run turing machine 'tm' on tape 'tape' from state 'state'. Return number of
34*f4a2713aSLionel Sambuc // steps taken until halt.
run(const State * tm,const Tape & tape,unsigned state)35*f4a2713aSLionel Sambuc constexpr unsigned run(const State *tm, const Tape &tape, unsigned state) {
36*f4a2713aSLionel Sambuc   return state == halt ? 0 :
37*f4a2713aSLionel Sambuc          run(tm, move(update(tape, tm[state][tape.val].tape),
38*f4a2713aSLionel Sambuc                       tm[state][tape.val].dir),
39*f4a2713aSLionel Sambuc              tm[state][tape.val].next) + 1;
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc // 3-state busy beaver. S(bb3) = 21.
43*f4a2713aSLionel Sambuc constexpr State bb3[] = {
44*f4a2713aSLionel Sambuc   { { true, R, 1 }, { true, R, halt } },
45*f4a2713aSLionel Sambuc   { { true, L, 1 }, { false, R, 2 } },
46*f4a2713aSLionel Sambuc   { { true, L, 2 }, { true, L, 0 } }
47*f4a2713aSLionel Sambuc };
48*f4a2713aSLionel Sambuc static_assert(run(bb3, Tape(), 0) == 21, "");
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc // 4-state busy beaver. S(bb4) = 107.
51*f4a2713aSLionel Sambuc constexpr State bb4[] = {
52*f4a2713aSLionel Sambuc   { { true, R, 1 }, { true, L, 1 } },
53*f4a2713aSLionel Sambuc   { { true, L, 0 }, { false, L, 2 } },
54*f4a2713aSLionel Sambuc   { { true, R, halt }, { true, L, 3 } },
55*f4a2713aSLionel Sambuc   { { true, R, 3 }, { false, R, 0 } } };
56*f4a2713aSLionel Sambuc static_assert(run(bb4, Tape(), 0) == 107, "");
57