declaration.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. namespace Carbon {
  12. struct Value;
  13. template <class K, class V>
  14. struct AssocList;
  15. using Address = unsigned int;
  16. using TypeEnv = AssocList<std::string, Value*>;
  17. using Env = AssocList<std::string, Address>;
  18. /// TODO:explain this. Also name it if necessary. Consult with jsiek.
  19. using ExecutionEnvironment = std::pair<TypeEnv*, Env*>;
  20. struct Declaration {
  21. virtual void Print() const = 0;
  22. virtual auto Name() const -> std::string = 0;
  23. virtual auto TypeChecked(TypeEnv* env, Env* ct_env) const
  24. -> const Declaration* = 0;
  25. virtual void InitGlobals(Env*& globals) const = 0;
  26. virtual auto TopLevel(ExecutionEnvironment&) const -> void = 0;
  27. };
  28. struct FunctionDeclaration : Declaration {
  29. const FunctionDefinition* definition;
  30. explicit FunctionDeclaration(const FunctionDefinition* definition)
  31. : definition(definition) {}
  32. void Print() const;
  33. auto Name() const -> std::string;
  34. auto TypeChecked(TypeEnv* env, Env* ct_env) const -> const Declaration*;
  35. void InitGlobals(Env*& globals) const;
  36. auto TopLevel(ExecutionEnvironment&) const -> void;
  37. };
  38. struct StructDeclaration : Declaration {
  39. StructDefinition definition;
  40. StructDeclaration(int line_num, std::string name, std::list<Member*>* members)
  41. : definition{line_num, new std::string(name), members} {}
  42. void Print() const;
  43. auto Name() const -> std::string;
  44. auto TypeChecked(TypeEnv* env, Env* ct_env) const -> const Declaration*;
  45. void InitGlobals(Env*& globals) const;
  46. auto TopLevel(ExecutionEnvironment&) const -> void;
  47. };
  48. struct ChoiceDeclaration : Declaration {
  49. int line_num;
  50. std::string name;
  51. std::list<std::pair<std::string, Expression*>> alternatives;
  52. ChoiceDeclaration(int line_num, std::string name,
  53. std::list<std::pair<std::string, Expression*>> alternatives)
  54. : line_num(line_num), name(name), alternatives(alternatives) {}
  55. void Print() const;
  56. auto Name() const -> std::string;
  57. auto TypeChecked(TypeEnv* env, Env* ct_env) const -> const Declaration*;
  58. void InitGlobals(Env*& globals) const;
  59. auto TopLevel(ExecutionEnvironment&) const -> void;
  60. };
  61. } // namespace Carbon
  62. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_