typecheck.h 2.0 KB

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