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:
Jon Meow
2021-11-03 08:04:13 -07:00
committed by GitHub
co-authored by Geoff Romer
parent 82a870cfcd
commit 27e084d37a
25 changed files with 586 additions and 30 deletions
+18 -1
View File
@@ -13,6 +13,7 @@ cc_library(
deps = [
":declaration",
":library_name",
":static_scope",
],
)
@@ -39,6 +40,7 @@ cc_library(
":pattern",
":source_location",
":statement",
":static_scope",
"//common:ostream",
"//executable_semantics/common:nonnull",
"@llvm-project//llvm:Support",
@@ -101,6 +103,7 @@ cc_library(
deps = [
":expression",
":source_location",
":static_scope",
"//common:ostream",
"//executable_semantics/common:arena",
"//executable_semantics/common:error",
@@ -119,10 +122,24 @@ cc_test(
],
)
cc_library(
name = "static_scope",
srcs = ["static_scope.cpp"],
hdrs = ["static_scope.h"],
deps = [
":source_location",
"//executable_semantics/common:arena",
"//executable_semantics/common:error",
],
)
cc_library(
name = "source_location",
hdrs = ["source_location.h"],
deps = ["//common:ostream"],
deps = [
"//common:ostream",
"//executable_semantics/common:nonnull",
],
)
cc_library(
+3
View File
@@ -9,6 +9,7 @@
#include "executable_semantics/ast/declaration.h"
#include "executable_semantics/ast/library_name.h"
#include "executable_semantics/ast/static_scope.h"
#include "executable_semantics/common/nonnull.h"
namespace Carbon {
@@ -23,6 +24,8 @@ struct AST {
std::vector<LibraryName> imports;
// The file's ordered declarations.
std::vector<Nonnull<Declaration*>> declarations;
// Names declared at the top level of the file.
StaticScope static_scope;
};
} // namespace Carbon
@@ -10,9 +10,12 @@
#include "executable_semantics/ast/member.h"
#include "executable_semantics/ast/source_location.h"
#include "executable_semantics/ast/static_scope.h"
namespace Carbon {
class StaticScope;
class ClassDefinition {
public:
ClassDefinition(SourceLocation source_loc, std::string name,
@@ -25,10 +28,15 @@ class ClassDefinition {
auto name() const -> const std::string& { return name_; }
auto members() const -> llvm::ArrayRef<Nonnull<Member*>> { return members_; }
// Contains class members.
auto static_scope() const -> const StaticScope& { return static_scope_; }
auto static_scope() -> StaticScope& { return static_scope_; }
private:
SourceLocation source_loc_;
std::string name_;
std::vector<Nonnull<Member*>> members_;
StaticScope static_scope_;
};
} // namespace Carbon
+2 -2
View File
@@ -54,8 +54,8 @@ void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
if (i != 0) {
out << ", ";
}
out << deduced.name << ":! ";
deduced.type->Print(out);
out << deduced.name() << ":! ";
deduced.type().Print(out);
++i;
}
out << "]";
+49 -8
View File
@@ -15,12 +15,15 @@
#include "executable_semantics/ast/pattern.h"
#include "executable_semantics/ast/source_location.h"
#include "executable_semantics/ast/statement.h"
#include "executable_semantics/ast/static_scope.h"
#include "executable_semantics/common/nonnull.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compiler.h"
namespace Carbon {
class StaticScope;
// Abstract base class of all AST nodes representing patterns.
//
// Declaration and its derived classes support LLVM-style RTTI, including
@@ -29,7 +32,7 @@ namespace Carbon {
// every concrete derived class must have a corresponding enumerator
// in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
// details.
class Declaration {
class Declaration : public NamedEntityInterface {
public:
enum class Kind {
FunctionDeclaration,
@@ -48,7 +51,11 @@ class Declaration {
// object.
auto kind() const -> Kind { return kind_; }
auto source_loc() const -> SourceLocation { return source_loc_; }
auto named_entity_kind() const -> NamedEntityKind override {
return NamedEntityKind::Declaration;
}
auto source_loc() const -> SourceLocation override { return source_loc_; }
// The static type of the declared entity. Cannot be called before
// typechecking.
@@ -78,9 +85,24 @@ class Declaration {
// TODO: expand the kinds of things that can be deduced parameters.
// For now, only generic parameters are supported.
struct GenericBinding {
std::string name;
Nonnull<const Expression*> type;
struct GenericBinding : public NamedEntityInterface {
public:
GenericBinding(SourceLocation source_loc, std::string name,
Nonnull<Expression*> type)
: source_loc_(source_loc), name_(std::move(name)), type_(type) {}
auto named_entity_kind() const -> NamedEntityKind override {
return NamedEntityKind::GenericBinding;
}
auto source_loc() const -> SourceLocation override { return source_loc_; }
auto name() const -> const std::string& { return name_; }
auto type() const -> const Expression& { return *type_; }
private:
SourceLocation source_loc_;
std::string name_;
Nonnull<Expression*> type_;
};
class FunctionDeclaration : public Declaration {
@@ -119,6 +141,10 @@ class FunctionDeclaration : public Declaration {
auto body() const -> std::optional<Nonnull<const Block*>> { return body_; }
auto body() -> std::optional<Nonnull<Block*>> { return body_; }
// Only contains function parameters. Scoped variables are in the body.
auto static_scope() const -> const StaticScope& { return static_scope_; }
auto static_scope() -> StaticScope& { return static_scope_; }
private:
std::string name_;
std::vector<GenericBinding> deduced_parameters_;
@@ -126,6 +152,7 @@ class FunctionDeclaration : public Declaration {
Nonnull<Pattern*> return_type_;
bool is_omitted_return_type_;
std::optional<Nonnull<Block*>> body_;
StaticScope static_scope_;
};
class ClassDeclaration : public Declaration {
@@ -148,15 +175,24 @@ class ClassDeclaration : public Declaration {
class ChoiceDeclaration : public Declaration {
public:
class Alternative {
class Alternative : public NamedEntityInterface {
public:
Alternative(std::string name, Nonnull<Expression*> signature)
: name_(std::move(name)), signature_(signature) {}
Alternative(SourceLocation source_loc, std::string name,
Nonnull<Expression*> signature)
: source_loc_(source_loc),
name_(std::move(name)),
signature_(signature) {}
auto named_entity_kind() const -> NamedEntityKind override {
return NamedEntityKind::ChoiceDeclarationAlternative;
}
auto source_loc() const -> SourceLocation override { return source_loc_; }
auto name() const -> const std::string& { return name_; }
auto signature() const -> const Expression& { return *signature_; }
private:
SourceLocation source_loc_;
std::string name_;
Nonnull<Expression*> signature_;
};
@@ -176,9 +212,14 @@ class ChoiceDeclaration : public Declaration {
return alternatives_;
}
// Contains the alternatives.
auto static_scope() const -> const StaticScope& { return static_scope_; }
auto static_scope() -> StaticScope& { return static_scope_; }
private:
std::string name_;
std::vector<Alternative> alternatives_;
StaticScope static_scope_;
};
// Global variable definition implements the Declaration concept.
+6 -2
View File
@@ -23,7 +23,7 @@ namespace Carbon {
// every concrete derived class must have a corresponding enumerator
// in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
// details.
class Member {
class Member : public NamedEntityInterface {
public:
enum class Kind { FieldMember };
@@ -37,7 +37,11 @@ class Member {
// object.
auto kind() const -> Kind { return kind_; }
auto source_loc() const -> SourceLocation { return source_loc_; }
auto named_entity_kind() const -> NamedEntityKind override {
return NamedEntityKind::Member;
}
auto source_loc() const -> SourceLocation override { return source_loc_; }
protected:
// Constructs a Member representing syntax at the given line number.
+10 -1
View File
@@ -12,6 +12,7 @@
#include "common/ostream.h"
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/source_location.h"
#include "executable_semantics/ast/static_scope.h"
#include "llvm/ADT/ArrayRef.h"
namespace Carbon {
@@ -100,7 +101,7 @@ class AutoPattern : public Pattern {
// A pattern that matches a value of a specified type, and optionally binds
// a name to it.
class BindingPattern : public Pattern {
class BindingPattern : public Pattern, public NamedEntityInterface {
public:
BindingPattern(SourceLocation source_loc, std::optional<std::string> name,
Nonnull<Pattern*> type)
@@ -108,6 +109,14 @@ class BindingPattern : public Pattern {
name_(std::move(name)),
type_(type) {}
auto named_entity_kind() const -> NamedEntityKind override {
return NamedEntityKind::BindingPattern;
}
auto source_loc() const -> SourceLocation override {
return Pattern::source_loc();
}
static auto classof(const Pattern* pattern) -> bool {
return pattern->kind() == Kind::BindingPattern;
}
+21 -1
View File
@@ -11,6 +11,7 @@
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/ast/pattern.h"
#include "executable_semantics/ast/source_location.h"
#include "executable_semantics/ast/static_scope.h"
#include "executable_semantics/common/arena.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Compiler.h"
@@ -18,6 +19,7 @@
namespace Carbon {
class FunctionDeclaration;
class StaticScope;
class Statement {
public:
@@ -75,8 +77,12 @@ class Block : public Statement {
return statements_;
}
auto static_scope() const -> const StaticScope& { return static_scope_; }
auto static_scope() -> StaticScope& { return static_scope_; }
private:
std::vector<Nonnull<Statement*>> statements_;
StaticScope static_scope_;
};
class ExpressionStatement : public Statement {
@@ -293,9 +299,15 @@ class Match : public Statement {
auto statement() const -> const Statement& { return *statement_; }
auto statement() -> Statement& { return *statement_; }
// Contains names for the pattern and statement. Note that when the
// statement is a block, it gains its own scope.
auto static_scope() const -> const StaticScope& { return static_scope_; }
auto static_scope() -> StaticScope& { return static_scope_; }
private:
Nonnull<Pattern*> pattern_;
Nonnull<Statement*> statement_;
StaticScope static_scope_;
};
Match(SourceLocation source_loc, Nonnull<Expression*> expression,
@@ -323,7 +335,7 @@ class Match : public Statement {
// __continuation <continuation_variable> {
// <body>
// }
class Continuation : public Statement {
class Continuation : public Statement, public NamedEntityInterface {
public:
Continuation(SourceLocation source_loc, std::string continuation_variable,
Nonnull<Block*> body)
@@ -331,6 +343,14 @@ class Continuation : public Statement {
continuation_variable_(std::move(continuation_variable)),
body_(body) {}
auto named_entity_kind() const -> NamedEntityKind override {
return NamedEntityKind::Continuation;
}
auto source_loc() const -> SourceLocation override {
return Statement::source_loc();
}
static auto classof(const Statement* stmt) -> bool {
return stmt->kind() == Kind::Continuation;
}
+20
View File
@@ -0,0 +1,20 @@
// 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
#include "executable_semantics/ast/static_scope.h"
#include "executable_semantics/common/error.h"
namespace Carbon {
void StaticScope::Add(std::string name,
Nonnull<const NamedEntityInterface*> entity) {
if (!declared_names_.insert({name, entity}).second) {
FATAL_COMPILATION_ERROR(entity->source_loc())
<< "Duplicate name `" << name << "` also found at "
<< declared_names_[name]->source_loc();
}
}
} // namespace Carbon
+60
View File
@@ -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_