mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 10:01:06 +01:00
Migrate remaining Expression alternatives to IndirectValue (#607)
This commit is contained in:
@@ -84,11 +84,12 @@ auto Expression::MakeContinuationType(int line_num) -> const Expression* {
|
||||
return type;
|
||||
}
|
||||
|
||||
auto Expression::MakeFunType(int line_num, const Expression* param,
|
||||
const Expression* ret) -> const Expression* {
|
||||
auto Expression::MakeFunType(int line_num, Expression param, Expression ret)
|
||||
-> const Expression* {
|
||||
auto* t = new Expression();
|
||||
t->line_num = line_num;
|
||||
t->value = FunctionType({.parameter = param, .return_type = ret});
|
||||
t->value = FunctionType(
|
||||
{.parameter = std::move(param), .return_type = std::move(ret)});
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -99,11 +100,11 @@ auto Expression::MakeVar(int line_num, std::string var) -> const Expression* {
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Expression::MakeVarPat(int line_num, std::string var,
|
||||
const Expression* type) -> const Expression* {
|
||||
auto Expression::MakeVarPat(int line_num, std::string var, Expression type)
|
||||
-> const Expression* {
|
||||
auto* v = new Expression();
|
||||
v->line_num = line_num;
|
||||
v->value = PatternVariable({.name = std::move(var), .type = type});
|
||||
v->value = PatternVariable({.name = std::move(var), .type = std::move(type)});
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -146,11 +147,11 @@ auto Expression::MakeBinOp(int line_num, enum Operator op, Expression arg1,
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeCall(int line_num, const Expression* fun,
|
||||
const Expression* arg) -> const Expression* {
|
||||
auto Expression::MakeCall(int line_num, Expression fun, Expression arg)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = Call({.function = fun, .argument = arg});
|
||||
e->value = Call({.function = std::move(fun), .argument = std::move(arg)});
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -186,11 +187,11 @@ auto Expression::MakeTuple(int line_num, std::vector<FieldInitializer> args)
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeIndex(int line_num, const Expression* exp,
|
||||
const Expression* i) -> const Expression* {
|
||||
auto Expression::MakeIndex(int line_num, Expression exp, Expression i)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = Index({.aggregate = exp, .offset = i});
|
||||
e->value = Index({.aggregate = std::move(exp), .offset = std::move(i)});
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -237,9 +238,9 @@ static void PrintFields(const std::vector<FieldInitializer>& fields) {
|
||||
void PrintExp(const Expression* e) {
|
||||
switch (e->tag()) {
|
||||
case ExpressionKind::Index:
|
||||
PrintExp(e->GetIndex().aggregate);
|
||||
PrintExp(e->GetIndex().aggregate.GetPointer());
|
||||
std::cout << "[";
|
||||
PrintExp(e->GetIndex().offset);
|
||||
PrintExp(e->GetIndex().offset.GetPointer());
|
||||
std::cout << "]";
|
||||
break;
|
||||
case ExpressionKind::GetField:
|
||||
@@ -285,17 +286,17 @@ void PrintExp(const Expression* e) {
|
||||
std::cout << e->GetVariable().name;
|
||||
break;
|
||||
case ExpressionKind::PatternVariable:
|
||||
PrintExp(e->GetPatternVariable().type);
|
||||
PrintExp(e->GetPatternVariable().type.GetPointer());
|
||||
std::cout << ": ";
|
||||
std::cout << e->GetPatternVariable().name;
|
||||
break;
|
||||
case ExpressionKind::Call:
|
||||
PrintExp(e->GetCall().function);
|
||||
PrintExp(e->GetCall().function.GetPointer());
|
||||
if (e->GetCall().argument->tag() == ExpressionKind::Tuple) {
|
||||
PrintExp(e->GetCall().argument);
|
||||
PrintExp(e->GetCall().argument.GetPointer());
|
||||
} else {
|
||||
std::cout << "(";
|
||||
PrintExp(e->GetCall().argument);
|
||||
PrintExp(e->GetCall().argument.GetPointer());
|
||||
std::cout << ")";
|
||||
}
|
||||
break;
|
||||
@@ -316,9 +317,9 @@ void PrintExp(const Expression* e) {
|
||||
break;
|
||||
case ExpressionKind::FunctionT:
|
||||
std::cout << "fn ";
|
||||
PrintExp(e->GetFunctionType().parameter);
|
||||
PrintExp(e->GetFunctionType().parameter.GetPointer());
|
||||
std::cout << " -> ";
|
||||
PrintExp(e->GetFunctionType().return_type);
|
||||
PrintExp(e->GetFunctionType().return_type.GetPointer());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,14 +70,14 @@ struct FieldAccess {
|
||||
|
||||
struct Index {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Index;
|
||||
const Expression* aggregate;
|
||||
const Expression* offset;
|
||||
IndirectValue<Expression> aggregate;
|
||||
IndirectValue<Expression> offset;
|
||||
};
|
||||
|
||||
struct PatternVariable {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::PatternVariable;
|
||||
std::string name;
|
||||
const Expression* type;
|
||||
IndirectValue<Expression> type;
|
||||
};
|
||||
|
||||
struct IntLiteral {
|
||||
@@ -103,14 +103,14 @@ struct PrimitiveOperator {
|
||||
|
||||
struct Call {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Call;
|
||||
const Expression* function;
|
||||
const Expression* argument;
|
||||
IndirectValue<Expression> function;
|
||||
IndirectValue<Expression> argument;
|
||||
};
|
||||
|
||||
struct FunctionType {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::FunctionT;
|
||||
const Expression* parameter;
|
||||
const Expression* return_type;
|
||||
IndirectValue<Expression> parameter;
|
||||
IndirectValue<Expression> return_type;
|
||||
};
|
||||
|
||||
struct AutoT {
|
||||
@@ -138,7 +138,7 @@ struct Expression {
|
||||
inline auto tag() const -> ExpressionKind;
|
||||
|
||||
static auto MakeVar(int line_num, std::string var) -> const Expression*;
|
||||
static auto MakeVarPat(int line_num, std::string var, const Expression* type)
|
||||
static auto MakeVarPat(int line_num, std::string var, Expression type)
|
||||
-> const Expression*;
|
||||
static auto MakeInt(int line_num, int i) -> const Expression*;
|
||||
static auto MakeBool(int line_num, bool b) -> const Expression*;
|
||||
@@ -148,19 +148,19 @@ struct Expression {
|
||||
-> const Expression*;
|
||||
static auto MakeBinOp(int line_num, enum Operator op, Expression arg1,
|
||||
Expression arg2) -> const Expression*;
|
||||
static auto MakeCall(int line_num, const Expression* fun,
|
||||
const Expression* arg) -> const Expression*;
|
||||
static auto MakeCall(int line_num, Expression fun, Expression arg)
|
||||
-> const Expression*;
|
||||
static auto MakeGetField(int line_num, const Expression* exp,
|
||||
std::string field) -> const Expression*;
|
||||
static auto MakeTuple(int line_num, std::vector<FieldInitializer> args)
|
||||
-> const Expression*;
|
||||
static auto MakeIndex(int line_num, const Expression* exp,
|
||||
const Expression* i) -> const Expression*;
|
||||
static auto MakeIndex(int line_num, Expression exp, Expression i)
|
||||
-> const Expression*;
|
||||
static auto MakeTypeType(int line_num) -> const Expression*;
|
||||
static auto MakeIntType(int line_num) -> const Expression*;
|
||||
static auto MakeBoolType(int line_num) -> const Expression*;
|
||||
static auto MakeFunType(int line_num, const Expression* param,
|
||||
const Expression* ret) -> const Expression*;
|
||||
static auto MakeFunType(int line_num, Expression param, Expression ret)
|
||||
-> const Expression*;
|
||||
static auto MakeAutoType(int line_num) -> const Expression*;
|
||||
static auto MakeContinuationType(int line_num) -> const Expression*;
|
||||
|
||||
|
||||
@@ -646,7 +646,7 @@ void StepLvalue() {
|
||||
case ExpressionKind::Index: {
|
||||
// { {e[i] :: C, E, F} :: S, H}
|
||||
// -> { e :: [][i] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().aggregate));
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().aggregate.GetPointer()));
|
||||
act->pos++;
|
||||
break;
|
||||
}
|
||||
@@ -689,14 +689,14 @@ void StepExp() {
|
||||
}
|
||||
switch (exp->tag()) {
|
||||
case ExpressionKind::PatternVariable: {
|
||||
frame->todo.Push(MakeExpAct(exp->GetPatternVariable().type));
|
||||
frame->todo.Push(MakeExpAct(exp->GetPatternVariable().type.GetPointer()));
|
||||
act->pos++;
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Index: {
|
||||
// { { e[i] :: C, E, F} :: S, H}
|
||||
// -> { { e :: [][i] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().aggregate));
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().aggregate.GetPointer()));
|
||||
act->pos++;
|
||||
break;
|
||||
}
|
||||
@@ -764,7 +764,7 @@ void StepExp() {
|
||||
case ExpressionKind::Call:
|
||||
// { {e1(e2) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: [](e2) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(MakeExpAct(exp->GetCall().function));
|
||||
frame->todo.Push(MakeExpAct(exp->GetCall().function.GetPointer()));
|
||||
act->pos++;
|
||||
break;
|
||||
case ExpressionKind::IntT: {
|
||||
@@ -792,7 +792,8 @@ void StepExp() {
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::FunctionT: {
|
||||
frame->todo.Push(MakeExpAct(exp->GetFunctionType().parameter));
|
||||
frame->todo.Push(
|
||||
MakeExpAct(exp->GetFunctionType().parameter.GetPointer()));
|
||||
act->pos++;
|
||||
break;
|
||||
}
|
||||
@@ -1093,7 +1094,7 @@ void HandleValue() {
|
||||
case ExpressionKind::Index: {
|
||||
if (act->pos == 1) {
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().offset));
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().offset.GetPointer()));
|
||||
} else if (act->pos == 2) {
|
||||
// { v :: [][i] :: C, E, F} :: S, H}
|
||||
// -> { { &v[i] :: C, E, F} :: S, H }
|
||||
@@ -1163,7 +1164,7 @@ void HandleValue() {
|
||||
case ExpressionKind::Index: {
|
||||
if (act->pos == 1) {
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().offset));
|
||||
frame->todo.Push(MakeExpAct(exp->GetIndex().offset.GetPointer()));
|
||||
} else if (act->pos == 2) {
|
||||
auto tuple = act->results[0];
|
||||
switch (tuple->tag) {
|
||||
@@ -1227,7 +1228,7 @@ void HandleValue() {
|
||||
// { { v :: [](e) :: C, E, F} :: S, H}
|
||||
// -> { { e :: v([]) :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeExpAct(exp->GetCall().argument));
|
||||
frame->todo.Push(MakeExpAct(exp->GetCall().argument.GetPointer()));
|
||||
} else if (act->pos == 2) {
|
||||
// { { v2 :: v1([]) :: C, E, F} :: S, H}
|
||||
// -> { {C',E',F'} :: {C, E, F} :: S, H}
|
||||
@@ -1252,7 +1253,8 @@ void HandleValue() {
|
||||
// { { pt :: fn [] -> e :: C, E, F} :: S, H}
|
||||
// -> { { e :: fn pt -> []) :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeExpAct(exp->GetFunctionType().return_type));
|
||||
frame->todo.Push(
|
||||
MakeExpAct(exp->GetFunctionType().return_type.GetPointer()));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -1500,7 +1502,7 @@ auto InterpProgram(std::list<Declaration>* fs) -> int {
|
||||
|
||||
const Expression* arg = Expression::MakeTuple(0, {});
|
||||
const Expression* call_main =
|
||||
Expression::MakeCall(0, Expression::MakeVar(0, "main"), arg);
|
||||
Expression::MakeCall(0, *Expression::MakeVar(0, "main"), *arg);
|
||||
auto todo = Stack(MakeExpAct(call_main));
|
||||
auto* scope = new Scope(globals, std::list<std::string>());
|
||||
auto* frame = new Frame("top", Stack(scope), todo);
|
||||
|
||||
@@ -67,8 +67,8 @@ auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
return Expression::MakeContinuationType(0);
|
||||
case ValKind::FunctionTV:
|
||||
return Expression::MakeFunType(
|
||||
0, ReifyType(t->GetFunctionType().param, line_num),
|
||||
ReifyType(t->GetFunctionType().ret, line_num));
|
||||
0, *ReifyType(t->GetFunctionType().param, line_num),
|
||||
*ReifyType(t->GetFunctionType().ret, line_num));
|
||||
case ValKind::TupleV: {
|
||||
std::vector<FieldInitializer> args;
|
||||
for (const TupleElement& field : *t->GetTuple().elements) {
|
||||
@@ -144,7 +144,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
auto t = InterpExp(values, e->GetPatternVariable().type);
|
||||
auto t = InterpExp(values, e->GetPatternVariable().type.GetPointer());
|
||||
if (t->tag == ValKind::AutoTV) {
|
||||
if (expected == nullptr) {
|
||||
std::cerr << e->line_num
|
||||
@@ -157,18 +157,20 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
} else if (expected) {
|
||||
ExpectType(e->line_num, "pattern variable", t, expected);
|
||||
}
|
||||
auto new_e = Expression::MakeVarPat(
|
||||
e->line_num, e->GetPatternVariable().name, ReifyType(t, e->line_num));
|
||||
auto new_e =
|
||||
Expression::MakeVarPat(e->line_num, e->GetPatternVariable().name,
|
||||
*ReifyType(t, e->line_num));
|
||||
types.Set(e->GetPatternVariable().name, t);
|
||||
return TCResult(new_e, t, types);
|
||||
}
|
||||
case ExpressionKind::Index: {
|
||||
auto res = TypeCheckExp(e->GetIndex().aggregate, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
auto res = TypeCheckExp(e->GetIndex().aggregate.GetPointer(), types,
|
||||
values, nullptr, TCContext::ValueContext);
|
||||
auto t = res.type;
|
||||
switch (t->tag) {
|
||||
case ValKind::TupleV: {
|
||||
auto i = ToInteger(InterpExp(values, e->GetIndex().offset));
|
||||
auto i =
|
||||
ToInteger(InterpExp(values, e->GetIndex().offset.GetPointer()));
|
||||
std::string f = std::to_string(i);
|
||||
std::optional<Address> field_address = FindTupleField(f, t);
|
||||
if (field_address == std::nullopt) {
|
||||
@@ -180,7 +182,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
}
|
||||
auto field_t = state->heap.Read(*field_address, e->line_num);
|
||||
auto new_e = Expression::MakeIndex(
|
||||
e->line_num, res.exp, Expression::MakeInt(e->line_num, i));
|
||||
e->line_num, *res.exp, *Expression::MakeInt(e->line_num, i));
|
||||
return TCResult(new_e, field_t, res.types);
|
||||
}
|
||||
default:
|
||||
@@ -372,18 +374,18 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Call: {
|
||||
auto fun_res = TypeCheckExp(e->GetCall().function, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
auto fun_res = TypeCheckExp(e->GetCall().function.GetPointer(), types,
|
||||
values, nullptr, TCContext::ValueContext);
|
||||
switch (fun_res.type->tag) {
|
||||
case ValKind::FunctionTV: {
|
||||
auto fun_t = fun_res.type;
|
||||
auto arg_res =
|
||||
TypeCheckExp(e->GetCall().argument, fun_res.types, values,
|
||||
fun_t->GetFunctionType().param, context);
|
||||
TypeCheckExp(e->GetCall().argument.GetPointer(), fun_res.types,
|
||||
values, fun_t->GetFunctionType().param, context);
|
||||
ExpectType(e->line_num, "call", fun_t->GetFunctionType().param,
|
||||
arg_res.type);
|
||||
auto new_e =
|
||||
Expression::MakeCall(e->line_num, fun_res.exp, arg_res.exp);
|
||||
Expression::MakeCall(e->line_num, *fun_res.exp, *arg_res.exp);
|
||||
return TCResult(new_e, fun_t->GetFunctionType().ret, arg_res.types);
|
||||
}
|
||||
default: {
|
||||
@@ -401,22 +403,25 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
switch (context) {
|
||||
case TCContext::ValueContext:
|
||||
case TCContext::TypeContext: {
|
||||
auto pt = InterpExp(values, e->GetFunctionType().parameter);
|
||||
auto rt = InterpExp(values, e->GetFunctionType().return_type);
|
||||
auto pt =
|
||||
InterpExp(values, e->GetFunctionType().parameter.GetPointer());
|
||||
auto rt =
|
||||
InterpExp(values, e->GetFunctionType().return_type.GetPointer());
|
||||
auto new_e =
|
||||
Expression::MakeFunType(e->line_num, ReifyType(pt, e->line_num),
|
||||
ReifyType(rt, e->line_num));
|
||||
Expression::MakeFunType(e->line_num, *ReifyType(pt, e->line_num),
|
||||
*ReifyType(rt, e->line_num));
|
||||
return TCResult(new_e, Value::MakeTypeTypeVal(), types);
|
||||
}
|
||||
case TCContext::PatternContext: {
|
||||
auto param_res = TypeCheckExp(e->GetFunctionType().parameter, types,
|
||||
values, nullptr, context);
|
||||
auto ret_res =
|
||||
TypeCheckExp(e->GetFunctionType().return_type, param_res.types,
|
||||
auto param_res =
|
||||
TypeCheckExp(e->GetFunctionType().parameter.GetPointer(), types,
|
||||
values, nullptr, context);
|
||||
auto ret_res =
|
||||
TypeCheckExp(e->GetFunctionType().return_type.GetPointer(),
|
||||
param_res.types, values, nullptr, context);
|
||||
auto new_e = Expression::MakeFunType(
|
||||
e->line_num, ReifyType(param_res.type, e->line_num),
|
||||
ReifyType(ret_res.type, e->line_num));
|
||||
e->line_num, *ReifyType(param_res.type, e->line_num),
|
||||
*ReifyType(ret_res.type, e->line_num));
|
||||
return TCResult(new_e, Value::MakeTypeTypeVal(), ret_res.types);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,9 +205,9 @@ expression:
|
||||
| expression designator
|
||||
{ $$ = Carbon::Expression::MakeGetField(yylineno, $1, $2); }
|
||||
| expression "[" expression "]"
|
||||
{ $$ = Carbon::Expression::MakeIndex(yylineno, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeIndex(yylineno, *$1, *$3); }
|
||||
| identifier ":" expression
|
||||
{ $$ = Carbon::Expression::MakeVarPat(yylineno, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeVarPat(yylineno, $1, *$3); }
|
||||
| integer_literal
|
||||
{ $$ = Carbon::Expression::MakeInt(yylineno, $1); }
|
||||
| TRUE
|
||||
@@ -246,13 +246,13 @@ expression:
|
||||
| UNARY_STAR expression %prec PREFIX_STAR
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Deref, *$2); }
|
||||
| expression tuple
|
||||
{ $$ = Carbon::Expression::MakeCall(yylineno, $1, $2); }
|
||||
{ $$ = Carbon::Expression::MakeCall(yylineno, *$1, *$2); }
|
||||
| expression POSTFIX_STAR
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Ptr, *$1); }
|
||||
| expression UNARY_STAR
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Ptr, *$1); }
|
||||
| FNTY tuple return_type
|
||||
{ $$ = Carbon::Expression::MakeFunType(yylineno, $2, $3); }
|
||||
{ $$ = Carbon::Expression::MakeFunType(yylineno, *$2, *$3); }
|
||||
;
|
||||
designator: "." identifier { $$ = $2; }
|
||||
;
|
||||
@@ -297,7 +297,7 @@ clause:
|
||||
| DEFAULT DBLARROW statement
|
||||
{
|
||||
auto vp = Carbon::Expression::MakeVarPat(yylineno, "_",
|
||||
Carbon::Expression::MakeAutoType(yylineno));
|
||||
*Carbon::Expression::MakeAutoType(yylineno));
|
||||
$$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>(vp, $3);
|
||||
}
|
||||
;
|
||||
|
||||
Reference in New Issue
Block a user