frame.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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_FRAME_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_FRAME_H_
  6. #include <list>
  7. #include <string>
  8. #include "common/ostream.h"
  9. #include "executable_semantics/interpreter/action.h"
  10. #include "executable_semantics/interpreter/address.h"
  11. #include "executable_semantics/interpreter/dictionary.h"
  12. #include "executable_semantics/interpreter/stack.h"
  13. #include "llvm/Support/Compiler.h"
  14. namespace Carbon {
  15. using Env = Dictionary<std::string, Address>;
  16. struct Scope {
  17. Scope(Env values, std::list<std::string> l)
  18. : values(values), locals(std::move(l)) {}
  19. Env values;
  20. std::list<std::string> locals;
  21. };
  22. // A frame represents either a function call or a delimited continuation.
  23. struct Frame {
  24. // The name of the function.
  25. std::string name;
  26. // If the frame represents a function call, the bottom scope
  27. // contains the parameter-argument bindings for this function
  28. // call. The rest of the scopes contain local variables defined by
  29. // blocks within the function. The scope at the top of the stack is
  30. // the current scope and its environment is the one used for looking
  31. // up the value associated with a variable.
  32. Stack<Scope*> scopes;
  33. // The actions that need to be executed in the future of the
  34. // current function call. The top of the stack is the action
  35. // that is executed first.
  36. Stack<Action*> todo;
  37. // If this frame is the bottom frame of a continuation, then it stores
  38. // the address of the continuation.
  39. std::optional<Address> continuation;
  40. Frame(const Frame&) = delete;
  41. Frame& operator=(const Frame&) = delete;
  42. Frame(std::string n, Stack<Scope*> s, Stack<Action*> c)
  43. : name(std::move(std::move(n))), scopes(s), todo(c), continuation() {}
  44. void Print(llvm::raw_ostream& out) const;
  45. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  46. };
  47. } // namespace Carbon
  48. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_FRAME_H_