mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Define a base class for all AST nodes. (#947)
Also implement code-generation to manage the resulting boilerplate.
This commit is contained in:
@@ -10,50 +10,33 @@
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "executable_semantics/ast/ast_node.h"
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/common/nonnull.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
class NamedEntityInterface {
|
||||
class NamedEntity : public virtual AstNode {
|
||||
public:
|
||||
enum class NamedEntityKind {
|
||||
// Includes variable definitions and matching contexts.
|
||||
BindingPattern,
|
||||
// Used by entries in choices.
|
||||
ChoiceDeclarationAlternative,
|
||||
// Used by continuations.
|
||||
Continuation,
|
||||
// Includes choices, classes, and functions. Variables are handled through
|
||||
// BindingPattern.
|
||||
Declaration,
|
||||
// Used by functions.
|
||||
GenericBinding,
|
||||
// Used by entries in classes.
|
||||
Member,
|
||||
};
|
||||
virtual ~NamedEntity() = 0;
|
||||
|
||||
NamedEntityInterface() = default;
|
||||
virtual ~NamedEntityInterface() = default;
|
||||
|
||||
NamedEntityInterface(NamedEntityInterface&&) = delete;
|
||||
auto operator=(NamedEntityInterface&&) -> NamedEntityInterface& = delete;
|
||||
NamedEntity() = default;
|
||||
|
||||
// TODO: This is unused, but is intended for casts after lookup.
|
||||
virtual auto named_entity_kind() const -> NamedEntityKind = 0;
|
||||
virtual auto source_loc() const -> SourceLocation = 0;
|
||||
auto kind() const -> NamedEntityKind {
|
||||
return static_cast<NamedEntityKind>(root_kind());
|
||||
}
|
||||
};
|
||||
|
||||
// The set of declared names in a scope. This is not aware of child scopes, but
|
||||
// does include directions to parent or related scopes for lookup purposes.
|
||||
class StaticScope {
|
||||
public:
|
||||
void Add(std::string name, Nonnull<const NamedEntityInterface*> entity);
|
||||
void Add(std::string name, Nonnull<const NamedEntity*> entity);
|
||||
|
||||
private:
|
||||
// Maps locally declared names to their entities.
|
||||
std::unordered_map<std::string, Nonnull<const NamedEntityInterface*>>
|
||||
declared_names_;
|
||||
std::unordered_map<std::string, Nonnull<const NamedEntity*>> declared_names_;
|
||||
|
||||
// A list of scopes used for name lookup within this scope.
|
||||
// TODO: This is unused, but is intended for name lookup cross-scope.
|
||||
|
||||
Reference in New Issue
Block a user