mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Start populating named entities in relevant locations. (#919)
This starts detecting naming collisions as a consequence of being able to determine when the name is declared twice in a given scope. Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Geoff Romer
parent
82a870cfcd
commit
27e084d37a
@@ -0,0 +1,60 @@
|
||||
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "executable_semantics/ast/source_location.h"
|
||||
#include "executable_semantics/common/nonnull.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
class NamedEntityInterface {
|
||||
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 ~NamedEntityInterface() = 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;
|
||||
};
|
||||
|
||||
// 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);
|
||||
|
||||
private:
|
||||
// Maps locally declared names to their entities.
|
||||
std::unordered_map<std::string, Nonnull<const NamedEntityInterface*>>
|
||||
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.
|
||||
std::vector<Nonnull<StaticScope*>> parent_scopes_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_STATIC_SCOPE_H_
|
||||
Reference in New Issue
Block a user