Files
revng-revng/tests/unit/SimpleRecursiveCoroutine.h
Pietro Fezzardi 8ffcd49dd2 RecursiveCoroutine: fix reference arguments
Before this commit, recursive coroutines did not work properly if they
had out arguments with reference type.
The reason is that `rc_run` was inferring the type of its arguments from
the arguments themselves, not from the prototype of the recursive
coroutine.

Hence, code snippets like the following did not work properly, because
`rc_run` was taking x by value, not by reference.

```
RecursiveCoroutine<void> accumulate_on_i(int &i) {
  // ...
}

int f() {
  int x = 0;
  rc_run(accumulate_on_i, x);
  return x;
}
```

This commit fixes the problem. Now the arguments of `rc_run` are
properly forwarded to the recursive coroutine.
2021-02-17 11:37:00 +01:00

45 lines
850 B
C++

#pragma once
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include <iostream>
#include <vector>
#include "revng/ADT/RecursiveCoroutine.h"
struct MyState {
int Depth;
};
inline RecursiveCoroutine<int> get10() {
rc_return 10;
}
inline RecursiveCoroutine<> my_coroutine(std::vector<MyState> &RCS, int x) {
int X = 1;
std::cerr << "Pre " << x << ':';
RCS.back().Depth = x;
for (const MyState &S : RCS) {
std::cerr << ' ' << S.Depth;
}
std::cerr << std::endl;
if (x < (rc_recur get10())) {
RCS.emplace_back();
rc_recur my_coroutine(RCS, x + 1);
}
std::cerr << "Post " << x << ' ' << X << std::endl;
RCS.pop_back();
rc_return;
}
inline RecursiveCoroutine<void> accumulateSums(int I, int &Result) {
Result += I;
if (I)
rc_recur accumulateSums(I - 1, Result);
}