mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 13:31:06 +01:00
Handle use-before-declare in static name lookup (#967)
Also remove inheritance from NamedEntity for some classes that don't need it.
This commit is contained in:
@@ -10,7 +10,6 @@ cc_library(
|
||||
deps = [
|
||||
":declaration",
|
||||
":library_name",
|
||||
":static_scope",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
#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 {
|
||||
@@ -24,8 +23,6 @@ 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;
|
||||
// Synthesized call to `Main`. Injected after parsing.
|
||||
std::optional<Nonnull<CallExpression*>> main_call;
|
||||
};
|
||||
|
||||
@@ -10,13 +10,13 @@ abstract class Pattern : AstNode;
|
||||
class TuplePattern : Pattern;
|
||||
class AlternativePattern : Pattern;
|
||||
class ExpressionPattern : Pattern;
|
||||
abstract class Declaration : AstNode, NamedEntity;
|
||||
class FunctionDeclaration : Declaration;
|
||||
class ClassDeclaration : Declaration;
|
||||
class ChoiceDeclaration : Declaration;
|
||||
abstract class Declaration : AstNode;
|
||||
class FunctionDeclaration : Declaration, NamedEntity;
|
||||
class ClassDeclaration : Declaration, NamedEntity;
|
||||
class ChoiceDeclaration : Declaration, NamedEntity;
|
||||
class VariableDeclaration : Declaration;
|
||||
class GenericBinding : AstNode, NamedEntity;
|
||||
class AlternativeSignature : AstNode, NamedEntity;
|
||||
class AlternativeSignature : AstNode;
|
||||
abstract class Statement : AstNode;
|
||||
class ExpressionStatement : Statement;
|
||||
class Assign : Statement;
|
||||
@@ -52,4 +52,4 @@ abstract class Expression : AstNode;
|
||||
class IntrinsicExpression : Expression;
|
||||
class UnimplementedExpression : Expression;
|
||||
abstract class Member : AstNode;
|
||||
class FieldMember : Member, NamedEntity;
|
||||
class FieldMember : Member;
|
||||
|
||||
@@ -21,8 +21,6 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
class StaticScope;
|
||||
|
||||
// Abstract base class of all AST nodes representing patterns.
|
||||
//
|
||||
// Declaration and its derived classes support LLVM-style RTTI, including
|
||||
@@ -31,7 +29,7 @@ class StaticScope;
|
||||
// every concrete derived class must have a corresponding enumerator
|
||||
// in `Kind`; see https://llvm.org/docs/HowToSetUpLLVMStyleRTTI.html for
|
||||
// details.
|
||||
class Declaration : public virtual AstNode, public NamedEntity {
|
||||
class Declaration : public virtual AstNode {
|
||||
public:
|
||||
~Declaration() override = 0;
|
||||
|
||||
@@ -177,7 +175,7 @@ class ReturnTerm {
|
||||
SourceLocation source_loc_;
|
||||
};
|
||||
|
||||
class FunctionDeclaration : public Declaration {
|
||||
class FunctionDeclaration : public Declaration, public NamedEntity {
|
||||
public:
|
||||
FunctionDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Nonnull<GenericBinding*>> deduced_params,
|
||||
@@ -212,20 +210,15 @@ 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<Nonnull<GenericBinding*>> deduced_parameters_;
|
||||
Nonnull<TuplePattern*> param_pattern_;
|
||||
ReturnTerm return_term_;
|
||||
std::optional<Nonnull<Block*>> body_;
|
||||
StaticScope static_scope_;
|
||||
};
|
||||
|
||||
class ClassDeclaration : public Declaration {
|
||||
class ClassDeclaration : public Declaration, public NamedEntity {
|
||||
public:
|
||||
ClassDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Nonnull<Member*>> members)
|
||||
@@ -240,17 +233,12 @@ class ClassDeclaration : public Declaration {
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
auto members() const -> llvm::ArrayRef<Nonnull<Member*>> { return members_; }
|
||||
|
||||
// Contains class members. 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<Nonnull<Member*>> members_;
|
||||
StaticScope static_scope_;
|
||||
};
|
||||
|
||||
class AlternativeSignature : public virtual AstNode, public NamedEntity {
|
||||
class AlternativeSignature : public virtual AstNode {
|
||||
public:
|
||||
AlternativeSignature(SourceLocation source_loc, std::string name,
|
||||
Nonnull<Expression*> signature)
|
||||
@@ -273,7 +261,7 @@ class AlternativeSignature : public virtual AstNode, public NamedEntity {
|
||||
Nonnull<Expression*> signature_;
|
||||
};
|
||||
|
||||
class ChoiceDeclaration : public Declaration {
|
||||
class ChoiceDeclaration : public Declaration, public NamedEntity {
|
||||
public:
|
||||
ChoiceDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Nonnull<AlternativeSignature*>> alternatives)
|
||||
@@ -294,20 +282,12 @@ 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<Nonnull<AlternativeSignature*>> alternatives_;
|
||||
StaticScope static_scope_;
|
||||
};
|
||||
|
||||
// Global variable definition implements the Declaration concept.
|
||||
//
|
||||
// TODO: this should not inherit from NamedEntity, because names should
|
||||
// always resolve to the underlying binding, not the VariableDeclaration.
|
||||
class VariableDeclaration : public Declaration {
|
||||
public:
|
||||
VariableDeclaration(SourceLocation source_loc,
|
||||
|
||||
@@ -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 : public virtual AstNode, public NamedEntity {
|
||||
class Member : public virtual AstNode {
|
||||
public:
|
||||
~Member() override = 0;
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
namespace Carbon {
|
||||
|
||||
class FunctionDeclaration;
|
||||
class StaticScope;
|
||||
|
||||
class Statement : public virtual AstNode {
|
||||
public:
|
||||
@@ -60,12 +59,8 @@ 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 {
|
||||
@@ -283,15 +278,9 @@ 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,
|
||||
|
||||
@@ -11,10 +11,11 @@ namespace Carbon {
|
||||
NamedEntity::~NamedEntity() = default;
|
||||
|
||||
void StaticScope::Add(std::string name, Nonnull<const NamedEntity*> entity) {
|
||||
if (!declared_names_.insert({name, entity}).second) {
|
||||
auto [it, success] = declared_names_.insert({name, entity});
|
||||
if (!success && it->second != entity) {
|
||||
FATAL_COMPILATION_ERROR(entity->source_loc())
|
||||
<< "Duplicate name `" << name << "` also found at "
|
||||
<< declared_names_[name]->source_loc();
|
||||
<< it->second->source_loc();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,8 +25,7 @@ auto StaticScope::Resolve(const std::string& name,
|
||||
std::optional<Nonnull<const NamedEntity*>> result =
|
||||
TryResolve(name, source_loc);
|
||||
if (!result.has_value()) {
|
||||
FATAL_COMPILATION_ERROR(source_loc)
|
||||
<< "'" << name << "' is not declared in this scope";
|
||||
FATAL_COMPILATION_ERROR(source_loc) << "could not resolve '" << name << "'";
|
||||
}
|
||||
return *result;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ class NamedEntity : public virtual AstNode {
|
||||
class StaticScope {
|
||||
public:
|
||||
// Defines `name` to be `entity` in this scope, or reports a compilation error
|
||||
// if `name` is already defined in this scope.
|
||||
// if `name` is already defined to be a different entity in this scope.
|
||||
void Add(std::string name, Nonnull<const NamedEntity*> entity);
|
||||
|
||||
// Make `parent` a parent of this scope.
|
||||
|
||||
Reference in New Issue
Block a user