declaration.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_AST_DECLARATION_H_
  5. #define EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
  6. #include <list>
  7. #include <string>
  8. #include "executable_semantics/ast/function_definition.h"
  9. #include "executable_semantics/ast/member.h"
  10. #include "executable_semantics/ast/struct_definition.h"
  11. #include "executable_semantics/interpreter/dictionary.h"
  12. namespace Carbon {
  13. struct Value;
  14. using Address = unsigned int;
  15. using TypeEnv = Dictionary<std::string, Value*>;
  16. using Env = Dictionary<std::string, Address>;
  17. /// TODO:explain this. Also name it if necessary. Consult with jsiek.
  18. using ExecutionEnvironment = std::pair<TypeEnv, Env>;
  19. /// An existential AST declaration satisfying the Declaration concept.
  20. class Declaration {
  21. public: // ValueSemantic concept API.
  22. Declaration(const Declaration& other) = default;
  23. Declaration& operator=(const Declaration& other) = default;
  24. /// Constructs an instance equivalent to `d`, where `Model` satisfies the
  25. /// Declaration concept.
  26. template <class Model>
  27. Declaration(Model d) : box(std::make_shared<Boxed<Model>>(d)) {}
  28. public: // Declaration concept API, in addition to ValueSemantic.
  29. void Print() const { box->Print(); }
  30. auto Name() const -> std::string { return box->Name(); }
  31. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration {
  32. return box->TypeChecked(env, ct_env);
  33. }
  34. void InitGlobals(Env& globals) const { return box->InitGlobals(globals); }
  35. auto TopLevel(ExecutionEnvironment& e) const -> void {
  36. return box->TopLevel(e);
  37. }
  38. private: // types
  39. /// A base class that erases the type of a `Boxed<Content>`, where `Content`
  40. /// satisfies the Declaration concept.
  41. struct Box {
  42. protected:
  43. Box() {}
  44. public:
  45. Box(const Box& other) = delete;
  46. Box& operator=(const Box& other) = delete;
  47. virtual ~Box() {}
  48. virtual auto Print() const -> void = 0;
  49. virtual auto Name() const -> std::string = 0;
  50. virtual auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration = 0;
  51. virtual auto InitGlobals(Env& globals) const -> void = 0;
  52. virtual auto TopLevel(ExecutionEnvironment&) const -> void = 0;
  53. };
  54. /// The derived class that holds an instance of `Content` satisfying the
  55. /// Declaration concept.
  56. template <class Content>
  57. struct Boxed final : Box {
  58. const Content content;
  59. explicit Boxed(Content content) : Box(), content(content) {}
  60. auto Print() const -> void override { return content.Print(); }
  61. auto Name() const -> std::string override { return content.Name(); }
  62. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration override {
  63. return content.TypeChecked(env, ct_env);
  64. }
  65. auto InitGlobals(Env& globals) const -> void override {
  66. content.InitGlobals(globals);
  67. }
  68. auto TopLevel(ExecutionEnvironment& e) const -> void override {
  69. content.TopLevel(e);
  70. }
  71. };
  72. private: // data members
  73. // Note: the pointee is const as long as we have no mutating methods. When
  74. std::shared_ptr<const Box> box;
  75. };
  76. struct FunctionDeclaration {
  77. const FunctionDefinition* definition;
  78. explicit FunctionDeclaration(const FunctionDefinition* definition)
  79. : definition(definition) {}
  80. auto Print() const -> void;
  81. auto Name() const -> std::string;
  82. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
  83. auto InitGlobals(Env& globals) const -> void;
  84. auto TopLevel(ExecutionEnvironment&) const -> void;
  85. };
  86. struct StructDeclaration {
  87. StructDefinition definition;
  88. StructDeclaration(int line_num, std::string name, std::list<Member*>* members)
  89. : definition{line_num, new std::string(name), members} {}
  90. void Print() const;
  91. auto Name() const -> std::string;
  92. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
  93. void InitGlobals(Env& globals) const;
  94. auto TopLevel(ExecutionEnvironment&) const -> void;
  95. };
  96. struct ChoiceDeclaration {
  97. int line_num;
  98. std::string name;
  99. std::list<std::pair<std::string, Expression*>> alternatives;
  100. ChoiceDeclaration(int line_num, std::string name,
  101. std::list<std::pair<std::string, Expression*>> alternatives)
  102. : line_num(line_num), name(name), alternatives(alternatives) {}
  103. void Print() const;
  104. auto Name() const -> std::string;
  105. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
  106. void InitGlobals(Env& globals) const;
  107. auto TopLevel(ExecutionEnvironment&) const -> void;
  108. };
  109. } // namespace Carbon
  110. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_