declaration.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, const Value*>;
  16. using Env = Dictionary<std::string, Address>;
  17. struct TypeCheckContext {
  18. // Symbol table mapping names of runtime entities to their type.
  19. TypeEnv types;
  20. // Symbol table mapping names of compile time entities to their value.
  21. Env values;
  22. };
  23. // An existential AST declaration satisfying the Declaration concept.
  24. class Declaration {
  25. public: // ValueSemantic concept API.
  26. Declaration(const Declaration& other) = default;
  27. Declaration& operator=(const Declaration& other) = default;
  28. // Constructs an instance equivalent to `d`, where `Model` satisfies the
  29. // Declaration concept.
  30. template <class Model>
  31. Declaration(Model d) : box(std::make_shared<Boxed<Model>>(d)) {}
  32. public: // Declaration concept API, in addition to ValueSemantic.
  33. void Print() const { box->Print(); }
  34. auto Name() const -> std::string { return box->Name(); }
  35. // Signals a type error if the declaration is not well typed,
  36. // otherwise returns this declaration with annotated types.
  37. //
  38. // - Parameter env: types of run-time names.
  39. // - Paraemter ct_env: values of compile-time names.
  40. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration {
  41. return box->TypeChecked(env, ct_env);
  42. }
  43. // Add an entry in the runtime global symbol table for this declaration.
  44. void InitGlobals(Env& globals) const { return box->InitGlobals(globals); }
  45. // Add an entry in the compile time global symbol tables for this declaration.
  46. auto TopLevel(TypeCheckContext& e) const -> void { return box->TopLevel(e); }
  47. private: // types
  48. // A base class that erases the type of a `Boxed<Content>`, where `Content`
  49. // satisfies the Declaration concept.
  50. struct Box {
  51. protected:
  52. Box() {}
  53. public:
  54. Box(const Box& other) = delete;
  55. Box& operator=(const Box& other) = delete;
  56. virtual ~Box() {}
  57. virtual auto Print() const -> void = 0;
  58. virtual auto Name() const -> std::string = 0;
  59. virtual auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration = 0;
  60. virtual auto InitGlobals(Env& globals) const -> void = 0;
  61. virtual auto TopLevel(TypeCheckContext&) const -> void = 0;
  62. };
  63. // The derived class that holds an instance of `Content` satisfying the
  64. // Declaration concept.
  65. template <class Content>
  66. struct Boxed final : Box {
  67. const Content content;
  68. explicit Boxed(Content content) : Box(), content(content) {}
  69. auto Print() const -> void override { return content.Print(); }
  70. auto Name() const -> std::string override { return content.Name(); }
  71. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration override {
  72. return content.TypeChecked(env, ct_env);
  73. }
  74. auto InitGlobals(Env& globals) const -> void override {
  75. content.InitGlobals(globals);
  76. }
  77. auto TopLevel(TypeCheckContext& e) const -> void override {
  78. content.TopLevel(e);
  79. }
  80. };
  81. private: // data members
  82. // Note: the pointee is const as long as we have no mutating methods. When
  83. std::shared_ptr<const Box> box;
  84. };
  85. struct FunctionDeclaration {
  86. const FunctionDefinition* definition;
  87. explicit FunctionDeclaration(const FunctionDefinition* definition)
  88. : definition(definition) {}
  89. auto Print() const -> void;
  90. auto Name() const -> std::string;
  91. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
  92. auto InitGlobals(Env& globals) const -> void;
  93. auto TopLevel(TypeCheckContext&) const -> void;
  94. };
  95. struct StructDeclaration {
  96. StructDefinition definition;
  97. StructDeclaration(int line_num, std::string name, std::list<Member*>* members)
  98. : definition{line_num, new std::string(name), members} {}
  99. void Print() const;
  100. auto Name() const -> std::string;
  101. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
  102. void InitGlobals(Env& globals) const;
  103. auto TopLevel(TypeCheckContext&) const -> void;
  104. };
  105. struct ChoiceDeclaration {
  106. int line_num;
  107. std::string name;
  108. std::list<std::pair<std::string, const Expression*>> alternatives;
  109. ChoiceDeclaration(
  110. int line_num, std::string name,
  111. std::list<std::pair<std::string, const Expression*>> alternatives)
  112. : line_num(line_num), name(name), alternatives(alternatives) {}
  113. void Print() const;
  114. auto Name() const -> std::string;
  115. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
  116. void InitGlobals(Env& globals) const;
  117. auto TopLevel(TypeCheckContext&) const -> void;
  118. };
  119. // Global variable definition implements the Declaration concept.
  120. class VariableDeclaration {
  121. public:
  122. VariableDeclaration(int source_location, std::string name,
  123. const Expression* type, const Expression* initializer)
  124. : source_location(source_location),
  125. name(name),
  126. type(type),
  127. initializer(initializer) {}
  128. void Print() const;
  129. auto Name() const -> std::string;
  130. auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
  131. void InitGlobals(Env& globals) const;
  132. auto TopLevel(TypeCheckContext&) const -> void;
  133. private:
  134. int source_location;
  135. std::string name;
  136. const Expression* type;
  137. const Expression* initializer;
  138. };
  139. } // namespace Carbon
  140. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_