frame.h 2.2 KB

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