typecheck.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_TYPECHECK_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_TYPECHECK_H_
  6. #include <set>
  7. #include "common/ostream.h"
  8. #include "executable_semantics/ast/expression.h"
  9. #include "executable_semantics/ast/statement.h"
  10. #include "executable_semantics/interpreter/dictionary.h"
  11. #include "executable_semantics/interpreter/interpreter.h"
  12. namespace Carbon {
  13. using TypeEnv = Dictionary<std::string, const Value*>;
  14. struct TCExpression {
  15. TCExpression(const Expression* e, const Value* t, TypeEnv types)
  16. : exp(e), type(t), types(types) {}
  17. const Expression* exp;
  18. const Value* type;
  19. TypeEnv types;
  20. };
  21. struct TCPattern {
  22. const Pattern* pattern;
  23. const Value* type;
  24. TypeEnv types;
  25. };
  26. struct TCStatement {
  27. TCStatement(const Statement* s, TypeEnv types) : stmt(s), types(types) {}
  28. const Statement* stmt;
  29. TypeEnv types;
  30. };
  31. struct TypeCheckContext {
  32. // Symbol table mapping names of runtime entities to their type.
  33. TypeEnv types;
  34. // Symbol table mapping names of compile time entities to their value.
  35. Env values;
  36. };
  37. auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
  38. -> TCExpression;
  39. auto TypeCheckPattern(const Pattern* p, TypeEnv types, Env values,
  40. const Value* expected) -> TCPattern;
  41. auto TypeCheckStmt(const Statement* s, TypeEnv types, Env values,
  42. const Value*& ret_type, bool is_omitted_ret_type)
  43. -> TCStatement;
  44. auto MakeTypeChecked(const Declaration& decl, const TypeEnv& types,
  45. const Env& values) -> const Declaration*;
  46. auto TopLevel(const std::list<const Declaration*>& fs) -> TypeCheckContext;
  47. } // namespace Carbon
  48. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_TYPECHECK_H_