declaration.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <string>
  7. #include <vector>
  8. #include "common/ostream.h"
  9. #include "executable_semantics/ast/class_definition.h"
  10. #include "executable_semantics/ast/function_definition.h"
  11. #include "executable_semantics/ast/member.h"
  12. #include "executable_semantics/ast/pattern.h"
  13. #include "executable_semantics/ast/source_location.h"
  14. #include "executable_semantics/common/nonnull.h"
  15. #include "llvm/Support/Compiler.h"
  16. namespace Carbon {
  17. // Abstract base class of all AST nodes representing patterns.
  18. //
  19. // Declaration and its derived classes support LLVM-style RTTI, including
  20. // llvm::isa, llvm::cast, and llvm::dyn_cast. To support this, every
  21. // class derived from Declaration must provide a `classof` operation, and
  22. // every concrete derived class must have a corresponding enumerator
  23. // in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
  24. // details.
  25. class Declaration {
  26. public:
  27. enum class Kind {
  28. FunctionDeclaration,
  29. ClassDeclaration,
  30. ChoiceDeclaration,
  31. VariableDeclaration,
  32. };
  33. Declaration(const Member&) = delete;
  34. Declaration& operator=(const Member&) = delete;
  35. // Returns the enumerator corresponding to the most-derived type of this
  36. // object.
  37. auto Tag() const -> Kind { return tag; }
  38. auto SourceLoc() const -> SourceLocation { return loc; }
  39. void Print(llvm::raw_ostream& out) const;
  40. protected:
  41. // Constructs a Declaration representing syntax at the given line number.
  42. // `tag` must be the enumerator corresponding to the most-derived type being
  43. // constructed.
  44. Declaration(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
  45. private:
  46. const Kind tag;
  47. SourceLocation loc;
  48. };
  49. class FunctionDeclaration : public Declaration {
  50. public:
  51. FunctionDeclaration(Nonnull<const FunctionDefinition*> definition)
  52. : Declaration(Kind::FunctionDeclaration, definition->source_location),
  53. definition(definition) {}
  54. static auto classof(const Declaration* decl) -> bool {
  55. return decl->Tag() == Kind::FunctionDeclaration;
  56. }
  57. auto Definition() const -> const FunctionDefinition& { return *definition; }
  58. private:
  59. Nonnull<const FunctionDefinition*> definition;
  60. };
  61. class ClassDeclaration : public Declaration {
  62. public:
  63. ClassDeclaration(SourceLocation loc, std::string name,
  64. std::vector<Nonnull<Member*>> members)
  65. : Declaration(Kind::ClassDeclaration, loc),
  66. definition({.loc = loc,
  67. .name = std::move(name),
  68. .members = std::move(members)}) {}
  69. static auto classof(const Declaration* decl) -> bool {
  70. return decl->Tag() == Kind::ClassDeclaration;
  71. }
  72. auto Definition() const -> const ClassDefinition& { return definition; }
  73. private:
  74. ClassDefinition definition;
  75. };
  76. class ChoiceDeclaration : public Declaration {
  77. public:
  78. ChoiceDeclaration(
  79. SourceLocation loc, std::string name,
  80. std::vector<std::pair<std::string, Nonnull<const Expression*>>>
  81. alternatives)
  82. : Declaration(Kind::ChoiceDeclaration, loc),
  83. name(std::move(name)),
  84. alternatives(std::move(alternatives)) {}
  85. static auto classof(const Declaration* decl) -> bool {
  86. return decl->Tag() == Kind::ChoiceDeclaration;
  87. }
  88. auto Name() const -> const std::string& { return name; }
  89. auto Alternatives() const -> const
  90. std::vector<std::pair<std::string, Nonnull<const Expression*>>>& {
  91. return alternatives;
  92. }
  93. private:
  94. std::string name;
  95. std::vector<std::pair<std::string, Nonnull<const Expression*>>> alternatives;
  96. };
  97. // Global variable definition implements the Declaration concept.
  98. class VariableDeclaration : public Declaration {
  99. public:
  100. VariableDeclaration(SourceLocation loc,
  101. Nonnull<const BindingPattern*> binding,
  102. Nonnull<const Expression*> initializer)
  103. : Declaration(Kind::VariableDeclaration, loc),
  104. binding(binding),
  105. initializer(initializer) {}
  106. static auto classof(const Declaration* decl) -> bool {
  107. return decl->Tag() == Kind::VariableDeclaration;
  108. }
  109. auto Binding() const -> Nonnull<const BindingPattern*> { return binding; }
  110. auto Initializer() const -> Nonnull<const Expression*> { return initializer; }
  111. private:
  112. // TODO: split this into a non-optional name and a type, initialized by
  113. // a constructor that takes a BindingPattern and handles errors like a
  114. // missing name.
  115. Nonnull<const BindingPattern*> binding;
  116. Nonnull<const Expression*> initializer;
  117. };
  118. } // namespace Carbon
  119. #endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_