mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Make NamedEntityInterface non-movable (#946)
This commit is contained in:
@@ -29,8 +29,9 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
case Kind::ChoiceDeclaration: {
|
||||
const auto& choice = cast<ChoiceDeclaration>(*this);
|
||||
out << "choice " << choice.name() << " {\n";
|
||||
for (const auto& alt : choice.alternatives()) {
|
||||
out << "alt " << alt.name() << " " << alt.signature() << ";\n";
|
||||
for (Nonnull<const ChoiceDeclaration::Alternative*> alt :
|
||||
choice.alternatives()) {
|
||||
out << "alt " << alt->name() << " " << alt->signature() << ";\n";
|
||||
}
|
||||
out << "}\n";
|
||||
break;
|
||||
@@ -49,12 +50,12 @@ void FunctionDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
if (!deduced_parameters_.empty()) {
|
||||
out << "[";
|
||||
unsigned int i = 0;
|
||||
for (const auto& deduced : deduced_parameters_) {
|
||||
for (Nonnull<const GenericBinding*> deduced : deduced_parameters_) {
|
||||
if (i != 0) {
|
||||
out << ", ";
|
||||
}
|
||||
out << deduced.name() << ":! ";
|
||||
deduced.type().Print(out);
|
||||
out << deduced->name() << ":! ";
|
||||
deduced->type().Print(out);
|
||||
++i;
|
||||
}
|
||||
out << "]";
|
||||
|
||||
@@ -107,7 +107,7 @@ struct GenericBinding : public NamedEntityInterface {
|
||||
class FunctionDeclaration : public Declaration {
|
||||
public:
|
||||
FunctionDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<GenericBinding> deduced_params,
|
||||
std::vector<Nonnull<GenericBinding*>> deduced_params,
|
||||
Nonnull<TuplePattern*> param_pattern,
|
||||
Nonnull<Pattern*> return_type,
|
||||
bool is_omitted_return_type,
|
||||
@@ -127,7 +127,8 @@ class FunctionDeclaration : public Declaration {
|
||||
void PrintDepth(int depth, llvm::raw_ostream& out) const;
|
||||
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
auto deduced_parameters() const -> llvm::ArrayRef<GenericBinding> {
|
||||
auto deduced_parameters() const
|
||||
-> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
|
||||
return deduced_parameters_;
|
||||
}
|
||||
auto param_pattern() const -> const TuplePattern& { return *param_pattern_; }
|
||||
@@ -146,7 +147,7 @@ class FunctionDeclaration : public Declaration {
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::vector<GenericBinding> deduced_parameters_;
|
||||
std::vector<Nonnull<GenericBinding*>> deduced_parameters_;
|
||||
Nonnull<TuplePattern*> param_pattern_;
|
||||
Nonnull<Pattern*> return_type_;
|
||||
bool is_omitted_return_type_;
|
||||
@@ -204,7 +205,7 @@ class ChoiceDeclaration : public Declaration {
|
||||
};
|
||||
|
||||
ChoiceDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Alternative> alternatives)
|
||||
std::vector<Nonnull<Alternative*>> alternatives)
|
||||
: Declaration(Kind::ChoiceDeclaration, source_loc),
|
||||
name_(std::move(name)),
|
||||
alternatives_(std::move(alternatives)) {}
|
||||
@@ -214,7 +215,7 @@ class ChoiceDeclaration : public Declaration {
|
||||
}
|
||||
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
auto alternatives() const -> llvm::ArrayRef<Alternative> {
|
||||
auto alternatives() const -> llvm::ArrayRef<Nonnull<const Alternative*>> {
|
||||
return alternatives_;
|
||||
}
|
||||
|
||||
@@ -224,7 +225,7 @@ class ChoiceDeclaration : public Declaration {
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::vector<Alternative> alternatives_;
|
||||
std::vector<Nonnull<Alternative*>> alternatives_;
|
||||
StaticScope static_scope_;
|
||||
};
|
||||
|
||||
|
||||
@@ -33,7 +33,12 @@ class NamedEntityInterface {
|
||||
Member,
|
||||
};
|
||||
|
||||
NamedEntityInterface() = default;
|
||||
virtual ~NamedEntityInterface() = default;
|
||||
|
||||
NamedEntityInterface(NamedEntityInterface&&) = delete;
|
||||
auto operator=(NamedEntityInterface&&) -> NamedEntityInterface& = delete;
|
||||
|
||||
// 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;
|
||||
|
||||
@@ -30,7 +30,7 @@ static void AddIntrinsics(Nonnull<Arena*> arena,
|
||||
IntrinsicExpression::Intrinsic::Print),
|
||||
false)}));
|
||||
auto print = arena->New<FunctionDeclaration>(
|
||||
source_loc, "Print", std::vector<GenericBinding>(),
|
||||
source_loc, "Print", std::vector<Nonnull<GenericBinding*>>(),
|
||||
arena->New<TuplePattern>(source_loc, print_params),
|
||||
arena->New<ExpressionPattern>(arena->New<TupleLiteral>(source_loc)),
|
||||
/*is_omitted_return_type=*/false, print_return);
|
||||
|
||||
@@ -115,10 +115,11 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
|
||||
const auto& func_def = cast<FunctionDeclaration>(d);
|
||||
Env new_env = *env;
|
||||
// Bring the deduced parameters into scope.
|
||||
for (const auto& deduced : func_def.deduced_parameters()) {
|
||||
for (Nonnull<const GenericBinding*> deduced :
|
||||
func_def.deduced_parameters()) {
|
||||
AllocationId a =
|
||||
heap_.AllocateValue(arena_->New<VariableType>(deduced.name()));
|
||||
new_env.Set(deduced.name(), a);
|
||||
heap_.AllocateValue(arena_->New<VariableType>(deduced->name()));
|
||||
new_env.Set(deduced->name(), a);
|
||||
}
|
||||
Nonnull<const FunctionValue*> f = arena_->New<FunctionValue>(&func_def);
|
||||
AllocationId a = heap_.AllocateValue(f);
|
||||
@@ -152,9 +153,10 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
|
||||
case Declaration::Kind::ChoiceDeclaration: {
|
||||
const auto& choice = cast<ChoiceDeclaration>(d);
|
||||
std::vector<NamedValue> alts;
|
||||
for (const auto& alternative : choice.alternatives()) {
|
||||
auto t = InterpExp(Env(arena_), &alternative.signature());
|
||||
alts.push_back({.name = alternative.name(), .value = t});
|
||||
for (Nonnull<const ChoiceDeclaration::Alternative*> alternative :
|
||||
choice.alternatives()) {
|
||||
auto t = InterpExp(Env(arena_), &alternative->signature());
|
||||
alts.push_back({.name = alternative->name(), .value = t});
|
||||
}
|
||||
auto ct = arena_->New<ChoiceType>(choice.name(), std::move(alts));
|
||||
AllocationId a = heap_.AllocateValue(ct);
|
||||
@@ -710,9 +712,9 @@ auto Interpreter::StepExp() -> Transition {
|
||||
} else {
|
||||
// { { rt :: fn pt -> [] :: C, E, F} :: S, H}
|
||||
// -> { fn pt -> rt :: {C, E, F} :: S, H}
|
||||
return Done{arena_->New<FunctionType>(std::vector<GenericBinding>(),
|
||||
act->results()[0],
|
||||
act->results()[1])};
|
||||
return Done{arena_->New<FunctionType>(
|
||||
std::vector<Nonnull<const GenericBinding*>>(), act->results()[0],
|
||||
act->results()[1])};
|
||||
}
|
||||
}
|
||||
case Expression::Kind::ContinuationTypeLiteral: {
|
||||
|
||||
@@ -133,8 +133,8 @@ void PopulateNamesInDeclaration(Arena* arena, Declaration& declaration,
|
||||
case Declaration::Kind::FunctionDeclaration: {
|
||||
auto& func = cast<FunctionDeclaration>(declaration);
|
||||
static_scope.Add(func.name(), &declaration);
|
||||
for (const auto& param : func.deduced_parameters()) {
|
||||
func.static_scope().Add(param.name(), ¶m);
|
||||
for (Nonnull<const GenericBinding*> param : func.deduced_parameters()) {
|
||||
func.static_scope().Add(param->name(), param);
|
||||
}
|
||||
PopulateNamesInPattern(func.param_pattern(), func.static_scope());
|
||||
PopulateNamesInStatement(arena, func.body(), static_scope);
|
||||
@@ -151,8 +151,9 @@ void PopulateNamesInDeclaration(Arena* arena, Declaration& declaration,
|
||||
case Declaration::Kind::ChoiceDeclaration: {
|
||||
auto& choice = cast<ChoiceDeclaration>(declaration);
|
||||
static_scope.Add(choice.name(), &declaration);
|
||||
for (auto& alt : choice.alternatives()) {
|
||||
choice.static_scope().Add(alt.name(), &alt);
|
||||
for (Nonnull<const ChoiceDeclaration::Alternative*> alt :
|
||||
choice.alternatives()) {
|
||||
choice.static_scope().Add(alt->name(), alt);
|
||||
}
|
||||
// Populate name into declared_names.
|
||||
// Init the choice's declared_names, and populate it with the
|
||||
|
||||
@@ -390,8 +390,8 @@ auto TypeChecker::Substitute(TypeEnv dict, Nonnull<const Value*> type)
|
||||
const auto& fn_type = cast<FunctionType>(*type);
|
||||
auto param = Substitute(dict, &fn_type.parameters());
|
||||
auto ret = Substitute(dict, &fn_type.return_type());
|
||||
return arena_->New<FunctionType>(std::vector<GenericBinding>(), param,
|
||||
ret);
|
||||
return arena_->New<FunctionType>(
|
||||
std::vector<Nonnull<const GenericBinding*>>(), param, ret);
|
||||
}
|
||||
case Value::Kind::PointerType: {
|
||||
return arena_->New<PointerType>(
|
||||
@@ -545,9 +545,10 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
<< "choice " << choice.name() << " does not have a field named "
|
||||
<< access.field();
|
||||
}
|
||||
SetStaticType(&access, arena_->New<FunctionType>(
|
||||
std::vector<GenericBinding>(),
|
||||
*parameter_types, &aggregate_type));
|
||||
SetStaticType(&access,
|
||||
arena_->New<FunctionType>(
|
||||
std::vector<Nonnull<const GenericBinding*>>(),
|
||||
*parameter_types, &aggregate_type));
|
||||
return TCResult(res.types);
|
||||
}
|
||||
default:
|
||||
@@ -657,13 +658,14 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e, TypeEnv types,
|
||||
auto deduced_args =
|
||||
ArgumentDeduction(e->source_loc(), TypeEnv(arena_), parameters,
|
||||
&call.argument().static_type());
|
||||
for (auto& deduced_param : fun_t.deduced()) {
|
||||
for (Nonnull<const GenericBinding*> deduced_param :
|
||||
fun_t.deduced()) {
|
||||
// TODO: change the following to a CHECK once the real checking
|
||||
// has been added to the type checking of function signatures.
|
||||
if (!deduced_args.Get(deduced_param.name())) {
|
||||
if (!deduced_args.Get(deduced_param->name())) {
|
||||
FATAL_COMPILATION_ERROR(e->source_loc())
|
||||
<< "could not deduce type argument for type parameter "
|
||||
<< deduced_param.name();
|
||||
<< deduced_param->name();
|
||||
}
|
||||
}
|
||||
parameters = Substitute(deduced_args, parameters);
|
||||
@@ -1027,11 +1029,11 @@ void TypeChecker::ExpectReturnOnAllPaths(
|
||||
auto TypeChecker::TypeCheckFunDef(FunctionDeclaration* f, TypeEnv types,
|
||||
Env values) -> TCResult {
|
||||
// Bring the deduced parameters into scope
|
||||
for (const auto& deduced : f->deduced_parameters()) {
|
||||
for (Nonnull<const GenericBinding*> deduced : f->deduced_parameters()) {
|
||||
// auto t = interpreter_.InterpExp(values, deduced.type);
|
||||
types.Set(deduced.name(), arena_->New<VariableType>(deduced.name()));
|
||||
AllocationId a = interpreter_.AllocateValue(*types.Get(deduced.name()));
|
||||
values.Set(deduced.name(), a);
|
||||
types.Set(deduced->name(), arena_->New<VariableType>(deduced->name()));
|
||||
AllocationId a = interpreter_.AllocateValue(*types.Get(deduced->name()));
|
||||
values.Set(deduced->name(), a);
|
||||
}
|
||||
// Type check the parameter pattern
|
||||
auto param_res =
|
||||
@@ -1068,11 +1070,11 @@ auto TypeChecker::TypeOfFunDef(TypeEnv types, Env values,
|
||||
FunctionDeclaration* fun_def)
|
||||
-> Nonnull<const Value*> {
|
||||
// Bring the deduced parameters into scope
|
||||
for (const auto& deduced : fun_def->deduced_parameters()) {
|
||||
for (Nonnull<const GenericBinding*> deduced : fun_def->deduced_parameters()) {
|
||||
// auto t = interpreter_.InterpExp(values, deduced.type);
|
||||
types.Set(deduced.name(), arena_->New<VariableType>(deduced.name()));
|
||||
AllocationId a = interpreter_.AllocateValue(*types.Get(deduced.name()));
|
||||
values.Set(deduced.name(), a);
|
||||
types.Set(deduced->name(), arena_->New<VariableType>(deduced->name()));
|
||||
AllocationId a = interpreter_.AllocateValue(*types.Get(deduced->name()));
|
||||
values.Set(deduced->name(), a);
|
||||
}
|
||||
// Type check the parameter pattern
|
||||
TypeCheckPattern(&fun_def->param_pattern(), types, values, std::nullopt);
|
||||
@@ -1204,9 +1206,11 @@ void TypeChecker::TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops) {
|
||||
case Declaration::Kind::ChoiceDeclaration: {
|
||||
const auto& choice = cast<ChoiceDeclaration>(*d);
|
||||
std::vector<NamedValue> alts;
|
||||
for (const auto& alternative : choice.alternatives()) {
|
||||
auto t = interpreter_.InterpExp(tops->values, &alternative.signature());
|
||||
alts.push_back({.name = alternative.name(), .value = t});
|
||||
for (Nonnull<const ChoiceDeclaration::Alternative*> alternative :
|
||||
choice.alternatives()) {
|
||||
auto t =
|
||||
interpreter_.InterpExp(tops->values, &alternative->signature());
|
||||
alts.push_back({.name = alternative->name(), .value = t});
|
||||
}
|
||||
auto ct = arena_->New<ChoiceType>(choice.name(), std::move(alts));
|
||||
AllocationId a = interpreter_.AllocateValue(ct);
|
||||
|
||||
@@ -213,11 +213,11 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
if (fn_type.deduced().size() > 0) {
|
||||
out << "[";
|
||||
unsigned int i = 0;
|
||||
for (const auto& deduced : fn_type.deduced()) {
|
||||
for (Nonnull<const GenericBinding*> deduced : fn_type.deduced()) {
|
||||
if (i != 0) {
|
||||
out << ", ";
|
||||
}
|
||||
out << deduced.name() << ":! " << deduced.type();
|
||||
out << deduced->name() << ":! " << deduced->type();
|
||||
++i;
|
||||
}
|
||||
out << "]";
|
||||
|
||||
@@ -335,7 +335,7 @@ class TypeType : public Value {
|
||||
// A function type.
|
||||
class FunctionType : public Value {
|
||||
public:
|
||||
FunctionType(std::vector<GenericBinding> deduced,
|
||||
FunctionType(std::vector<Nonnull<const GenericBinding*>> deduced,
|
||||
Nonnull<const Value*> parameters,
|
||||
Nonnull<const Value*> return_type)
|
||||
: Value(Kind::FunctionType),
|
||||
@@ -347,12 +347,14 @@ class FunctionType : public Value {
|
||||
return value->kind() == Kind::FunctionType;
|
||||
}
|
||||
|
||||
auto deduced() const -> llvm::ArrayRef<GenericBinding> { return deduced_; }
|
||||
auto deduced() const -> llvm::ArrayRef<Nonnull<const GenericBinding*>> {
|
||||
return deduced_;
|
||||
}
|
||||
auto parameters() const -> const Value& { return *parameters_; }
|
||||
auto return_type() const -> const Value& { return *return_type_; }
|
||||
|
||||
private:
|
||||
std::vector<GenericBinding> deduced_;
|
||||
std::vector<Nonnull<const GenericBinding*>> deduced_;
|
||||
Nonnull<const Value*> parameters_;
|
||||
Nonnull<const Value*> return_type_;
|
||||
};
|
||||
|
||||
@@ -108,9 +108,9 @@
|
||||
%type <Nonnull<Block*>> block
|
||||
%type <std::vector<Nonnull<Statement*>>> statement_list
|
||||
%type <Nonnull<Expression*>> expression
|
||||
%type <BisonWrap<GenericBinding>> generic_binding
|
||||
%type <std::vector<GenericBinding>> deduced_params
|
||||
%type <std::vector<GenericBinding>> deduced_param_list
|
||||
%type <Nonnull<GenericBinding*>> generic_binding
|
||||
%type <std::vector<Nonnull<GenericBinding*>>> deduced_params
|
||||
%type <std::vector<Nonnull<GenericBinding*>>> deduced_param_list
|
||||
%type <Nonnull<Pattern*>> pattern
|
||||
%type <Nonnull<Pattern*>> non_expression_pattern
|
||||
%type <std::pair<Nonnull<Expression*>, bool>> return_type
|
||||
@@ -131,9 +131,9 @@
|
||||
%type <Nonnull<TuplePattern*>> maybe_empty_tuple_pattern
|
||||
%type <ParenContents<Pattern>> paren_pattern_base
|
||||
%type <ParenContents<Pattern>> paren_pattern_contents
|
||||
%type <BisonWrap<ChoiceDeclaration::Alternative>> alternative
|
||||
%type <std::vector<ChoiceDeclaration::Alternative>> alternative_list
|
||||
%type <std::vector<ChoiceDeclaration::Alternative>> alternative_list_contents
|
||||
%type <Nonnull<ChoiceDeclaration::Alternative*>> alternative
|
||||
%type <std::vector<Nonnull<ChoiceDeclaration::Alternative*>>> alternative_list
|
||||
%type <std::vector<Nonnull<ChoiceDeclaration::Alternative*>>> alternative_list_contents
|
||||
%type <BisonWrap<Match::Clause>> clause
|
||||
%type <std::vector<Match::Clause>> clause_list
|
||||
|
||||
@@ -632,14 +632,16 @@ return_type:
|
||||
;
|
||||
generic_binding:
|
||||
identifier COLON_BANG expression
|
||||
{ $$ = GenericBinding(context.source_loc(), std::move($1), $3); }
|
||||
{
|
||||
$$ = arena->New<GenericBinding>(context.source_loc(), std::move($1), $3);
|
||||
}
|
||||
;
|
||||
deduced_param_list:
|
||||
// Empty
|
||||
{ $$ = std::vector<GenericBinding>(); }
|
||||
{ $$ = std::vector<Nonnull<GenericBinding*>>(); }
|
||||
| generic_binding
|
||||
{
|
||||
$$ = std::vector<GenericBinding>();
|
||||
$$ = std::vector<Nonnull<GenericBinding*>>();
|
||||
$$.push_back($1);
|
||||
}
|
||||
| generic_binding COMMA deduced_param_list
|
||||
@@ -650,7 +652,7 @@ deduced_param_list:
|
||||
;
|
||||
deduced_params:
|
||||
// Empty
|
||||
{ $$ = std::vector<GenericBinding>(); }
|
||||
{ $$ = std::vector<Nonnull<GenericBinding*>>(); }
|
||||
| LEFT_SQUARE_BRACKET deduced_param_list RIGHT_SQUARE_BRACKET
|
||||
{ $$ = $2; }
|
||||
;
|
||||
@@ -696,10 +698,13 @@ member_list:
|
||||
;
|
||||
alternative:
|
||||
identifier tuple
|
||||
{ $$ = ChoiceDeclaration::Alternative(context.source_loc(), $1, $2); }
|
||||
{
|
||||
$$ = arena->New<ChoiceDeclaration::Alternative>(context.source_loc(), $1,
|
||||
$2);
|
||||
}
|
||||
| identifier
|
||||
{
|
||||
$$ = ChoiceDeclaration::Alternative(
|
||||
$$ = arena->New<ChoiceDeclaration::Alternative>(
|
||||
context.source_loc(), $1,
|
||||
arena->New<TupleLiteral>(context.source_loc()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user