frame.h 2.2 KB

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