cons_list.h 895 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_
  6. namespace Carbon {
  7. template <class T>
  8. struct Stack;
  9. template <class T>
  10. struct Cons {
  11. friend struct Stack<T>;
  12. private:
  13. Cons(T e, Cons* n) : curr(e), next(n) {}
  14. const T curr;
  15. Cons* const next;
  16. // Cons cells are part of a "persistent data structure" and are thus
  17. // immutable.
  18. Cons& operator=(const Cons&) = delete;
  19. Cons& operator=(Cons&&) = delete;
  20. };
  21. template <class T>
  22. auto Length(Cons<T>* ls) -> unsigned int {
  23. if (ls) {
  24. return 1 + Length(ls->next);
  25. } else {
  26. return 0;
  27. }
  28. }
  29. } // namespace Carbon
  30. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_