mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 06:44:46 +01:00
Switch Expression to use inheritance+cast (#712)
Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Geoff Romer
parent
4e0307efbc
commit
dbcd6ad20d
@@ -243,12 +243,12 @@ void DeallocateLocals(int line_num, Frame* frame) {
|
||||
void CreateTuple(Frame* frame, Action* act, const Expression* exp) {
|
||||
// { { (v1,...,vn) :: C, E, F} :: S, H}
|
||||
// -> { { `(v1,...,vn) :: C, E, F} :: S, H}
|
||||
const auto& tup_lit = exp->GetTupleLiteral();
|
||||
CHECK(act->Results().size() == tup_lit.fields.size());
|
||||
const auto& tup_lit = cast<TupleLiteral>(*exp);
|
||||
CHECK(act->Results().size() == tup_lit.Fields().size());
|
||||
std::vector<TupleElement> elements;
|
||||
for (size_t i = 0; i < act->Results().size(); ++i) {
|
||||
elements.push_back(
|
||||
{.name = tup_lit.fields[i].name, .value = act->Results()[i]});
|
||||
{.name = tup_lit.Fields()[i].name, .value = act->Results()[i]});
|
||||
}
|
||||
|
||||
const Value* tv = global_arena->New<TupleValue>(std::move(elements));
|
||||
@@ -407,50 +407,51 @@ void StepLvalue() {
|
||||
if (tracing_output) {
|
||||
llvm::outs() << "--- step lvalue " << *exp << " --->\n";
|
||||
}
|
||||
switch (exp->tag()) {
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
switch (exp->Tag()) {
|
||||
case Expression::Kind::IdentifierExpression: {
|
||||
// { {x :: C, E, F} :: S, H}
|
||||
// -> { {E(x) :: C, E, F} :: S, H}
|
||||
std::optional<Address> pointer =
|
||||
CurrentEnv(state).Get(exp->GetIdentifierExpression().name);
|
||||
CurrentEnv(state).Get(cast<IdentifierExpression>(*exp).Name());
|
||||
if (!pointer) {
|
||||
FATAL_RUNTIME_ERROR(exp->line_num)
|
||||
<< "could not find `" << exp->GetIdentifierExpression().name << "`";
|
||||
FATAL_RUNTIME_ERROR(exp->LineNumber())
|
||||
<< "could not find `" << cast<IdentifierExpression>(*exp).Name()
|
||||
<< "`";
|
||||
}
|
||||
const Value* v = global_arena->New<PointerValue>(*pointer);
|
||||
frame->todo.Pop();
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::FieldAccessExpression: {
|
||||
case Expression::Kind::FieldAccessExpression: {
|
||||
if (act->Pos() == 0) {
|
||||
// { {e.f :: C, E, F} :: S, H}
|
||||
// -> { e :: [].f :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<LValAction>(
|
||||
exp->GetFieldAccessExpression().aggregate));
|
||||
cast<FieldAccessExpression>(*exp).Aggregate()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
// { v :: [].f :: C, E, F} :: S, H}
|
||||
// -> { { &v.f :: C, E, F} :: S, H }
|
||||
Address aggregate = cast<PointerValue>(*act->Results()[0]).Val();
|
||||
Address field =
|
||||
aggregate.SubobjectAddress(exp->GetFieldAccessExpression().field);
|
||||
Address field = aggregate.SubobjectAddress(
|
||||
cast<FieldAccessExpression>(*exp).Field());
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(
|
||||
global_arena->New<PointerValue>(field)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::IndexExpression: {
|
||||
case Expression::Kind::IndexExpression: {
|
||||
if (act->Pos() == 0) {
|
||||
// { {e[i] :: C, E, F} :: S, H}
|
||||
// -> { e :: [][i] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
global_arena->New<LValAction>(exp->GetIndexExpression().aggregate));
|
||||
frame->todo.Push(global_arena->New<LValAction>(
|
||||
cast<IndexExpression>(*exp).Aggregate()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
exp->GetIndexExpression().offset));
|
||||
cast<IndexExpression>(*exp).Offset()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
// { v :: [][i] :: C, E, F} :: S, H}
|
||||
@@ -465,21 +466,21 @@ void StepLvalue() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::TupleLiteral: {
|
||||
case Expression::Kind::TupleLiteral: {
|
||||
if (act->Pos() == 0) {
|
||||
// { {(f1=e1,...) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H}
|
||||
const Expression* e1 = exp->GetTupleLiteral().fields[0].expression;
|
||||
const Expression* e1 = cast<TupleLiteral>(*exp).Fields()[0].expression;
|
||||
frame->todo.Push(global_arena->New<LValAction>(e1));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() !=
|
||||
static_cast<int>(exp->GetTupleLiteral().fields.size())) {
|
||||
static_cast<int>(cast<TupleLiteral>(*exp).Fields().size())) {
|
||||
// { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S,
|
||||
// H}
|
||||
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
|
||||
// H}
|
||||
const Expression* elt =
|
||||
exp->GetTupleLiteral().fields[act->Pos()].expression;
|
||||
cast<TupleLiteral>(*exp).Fields()[act->Pos()].expression;
|
||||
frame->todo.Push(global_arena->New<LValAction>(elt));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
@@ -487,15 +488,15 @@ void StepLvalue() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::IntLiteral:
|
||||
case ExpressionKind::BoolLiteral:
|
||||
case ExpressionKind::CallExpression:
|
||||
case ExpressionKind::PrimitiveOperatorExpression:
|
||||
case ExpressionKind::IntTypeLiteral:
|
||||
case ExpressionKind::BoolTypeLiteral:
|
||||
case ExpressionKind::TypeTypeLiteral:
|
||||
case ExpressionKind::FunctionTypeLiteral:
|
||||
case ExpressionKind::ContinuationTypeLiteral: {
|
||||
case Expression::Kind::IntLiteral:
|
||||
case Expression::Kind::BoolLiteral:
|
||||
case Expression::Kind::CallExpression:
|
||||
case Expression::Kind::PrimitiveOperatorExpression:
|
||||
case Expression::Kind::IntTypeLiteral:
|
||||
case Expression::Kind::BoolTypeLiteral:
|
||||
case Expression::Kind::TypeTypeLiteral:
|
||||
case Expression::Kind::FunctionTypeLiteral:
|
||||
case Expression::Kind::ContinuationTypeLiteral: {
|
||||
FATAL_RUNTIME_ERROR_NO_LINE()
|
||||
<< "Can't treat expression as lvalue: " << *exp;
|
||||
}
|
||||
@@ -511,17 +512,17 @@ void StepExp() {
|
||||
if (tracing_output) {
|
||||
llvm::outs() << "--- step exp " << *exp << " --->\n";
|
||||
}
|
||||
switch (exp->tag()) {
|
||||
case ExpressionKind::IndexExpression: {
|
||||
switch (exp->Tag()) {
|
||||
case Expression::Kind::IndexExpression: {
|
||||
if (act->Pos() == 0) {
|
||||
// { { e[i] :: C, E, F} :: S, H}
|
||||
// -> { { e :: [][i] :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
exp->GetIndexExpression().aggregate));
|
||||
cast<IndexExpression>(*exp).Aggregate()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
exp->GetIndexExpression().offset));
|
||||
cast<IndexExpression>(*exp).Offset()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
auto tuple = act->Results()[0];
|
||||
@@ -547,25 +548,26 @@ void StepExp() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::TupleLiteral: {
|
||||
case Expression::Kind::TupleLiteral: {
|
||||
if (act->Pos() == 0) {
|
||||
if (exp->GetTupleLiteral().fields.size() > 0) {
|
||||
if (cast<TupleLiteral>(*exp).Fields().size() > 0) {
|
||||
// { {(f1=e1,...) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H}
|
||||
const Expression* e1 = exp->GetTupleLiteral().fields[0].expression;
|
||||
const Expression* e1 =
|
||||
cast<TupleLiteral>(*exp).Fields()[0].expression;
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(e1));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
CreateTuple(frame, act, exp);
|
||||
}
|
||||
} else if (act->Pos() !=
|
||||
static_cast<int>(exp->GetTupleLiteral().fields.size())) {
|
||||
static_cast<int>(cast<TupleLiteral>(*exp).Fields().size())) {
|
||||
// { { vk :: (f1=v1,..., fk=[],fk+1=ek+1,...) :: C, E, F} :: S,
|
||||
// H}
|
||||
// -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S,
|
||||
// H}
|
||||
const Expression* elt =
|
||||
exp->GetTupleLiteral().fields[act->Pos()].expression;
|
||||
cast<TupleLiteral>(*exp).Fields()[act->Pos()].expression;
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(elt));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
@@ -573,123 +575,122 @@ void StepExp() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::FieldAccessExpression: {
|
||||
case Expression::Kind::FieldAccessExpression: {
|
||||
const auto& access = cast<FieldAccessExpression>(*exp);
|
||||
if (act->Pos() == 0) {
|
||||
// { { e.f :: C, E, F} :: S, H}
|
||||
// -> { { e :: [].f :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
exp->GetFieldAccessExpression().aggregate));
|
||||
frame->todo.Push(
|
||||
global_arena->New<ExpressionAction>(access.Aggregate()));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
// { { v :: [].f :: C, E, F} :: S, H}
|
||||
// -> { { v_f :: C, E, F} : S, H}
|
||||
const Value* element = act->Results()[0]->GetField(
|
||||
FieldPath(exp->GetFieldAccessExpression().field), exp->line_num);
|
||||
FieldPath(access.Field()), exp->LineNumber());
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(element));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
case Expression::Kind::IdentifierExpression: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const auto& ident = cast<IdentifierExpression>(*exp);
|
||||
// { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H}
|
||||
std::optional<Address> pointer =
|
||||
CurrentEnv(state).Get(exp->GetIdentifierExpression().name);
|
||||
std::optional<Address> pointer = CurrentEnv(state).Get(ident.Name());
|
||||
if (!pointer) {
|
||||
FATAL_RUNTIME_ERROR(exp->line_num)
|
||||
<< "could not find `" << exp->GetIdentifierExpression().name << "`";
|
||||
FATAL_RUNTIME_ERROR(exp->LineNumber())
|
||||
<< "could not find `" << ident.Name() << "`";
|
||||
}
|
||||
const Value* pointee = state->heap.Read(*pointer, exp->line_num);
|
||||
const Value* pointee = state->heap.Read(*pointer, exp->LineNumber());
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(pointee));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::IntLiteral:
|
||||
case Expression::Kind::IntLiteral:
|
||||
CHECK(act->Pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(
|
||||
global_arena->New<IntValue>(exp->GetIntLiteral())));
|
||||
global_arena->New<IntValue>(cast<IntLiteral>(*exp).Val())));
|
||||
break;
|
||||
case ExpressionKind::BoolLiteral:
|
||||
case Expression::Kind::BoolLiteral:
|
||||
CHECK(act->Pos() == 0);
|
||||
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(
|
||||
global_arena->New<BoolValue>(exp->GetBoolLiteral())));
|
||||
global_arena->New<BoolValue>(cast<BoolLiteral>(*exp).Val())));
|
||||
break;
|
||||
case ExpressionKind::PrimitiveOperatorExpression:
|
||||
if (act->Pos() !=
|
||||
static_cast<int>(
|
||||
exp->GetPrimitiveOperatorExpression().arguments.size())) {
|
||||
case Expression::Kind::PrimitiveOperatorExpression: {
|
||||
const auto& op = cast<PrimitiveOperatorExpression>(*exp);
|
||||
if (act->Pos() != static_cast<int>(op.Arguments().size())) {
|
||||
// { {v :: op(vs,[],e,es) :: C, E, F} :: S, H}
|
||||
// -> { {e :: op(vs,v,[],es) :: C, E, F} :: S, H}
|
||||
const Expression* arg =
|
||||
exp->GetPrimitiveOperatorExpression().arguments[act->Pos()];
|
||||
const Expression* arg = op.Arguments()[act->Pos()];
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(arg));
|
||||
act->IncrementPos();
|
||||
} else {
|
||||
// { {v :: op(vs,[]) :: C, E, F} :: S, H}
|
||||
// -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H}
|
||||
const Value* v = EvalPrim(exp->GetPrimitiveOperatorExpression().op,
|
||||
act->Results(), exp->line_num);
|
||||
const Value* v = EvalPrim(op.Op(), act->Results(), exp->LineNumber());
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
}
|
||||
break;
|
||||
case ExpressionKind::CallExpression:
|
||||
}
|
||||
case Expression::Kind::CallExpression:
|
||||
if (act->Pos() == 0) {
|
||||
// { {e1(e2) :: C, E, F} :: S, H}
|
||||
// -> { {e1 :: [](e2) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
exp->GetCallExpression().function));
|
||||
cast<CallExpression>(*exp).Function()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
// { { v :: [](e) :: C, E, F} :: S, H}
|
||||
// -> { { e :: v([]) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
exp->GetCallExpression().argument));
|
||||
cast<CallExpression>(*exp).Argument()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
// { { v2 :: v1([]) :: C, E, F} :: S, H}
|
||||
// -> { {C',E',F'} :: {C, E, F} :: S, H}
|
||||
frame->todo.Pop(1);
|
||||
CallFunction(exp->line_num, act->Results(), state);
|
||||
CallFunction(exp->LineNumber(), act->Results(), state);
|
||||
} else {
|
||||
FATAL() << "in handle_value with Call pos " << act->Pos();
|
||||
}
|
||||
break;
|
||||
case ExpressionKind::IntTypeLiteral: {
|
||||
case Expression::Kind::IntTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<IntType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::BoolTypeLiteral: {
|
||||
case Expression::Kind::BoolTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<BoolType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::TypeTypeLiteral: {
|
||||
case Expression::Kind::TypeTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<TypeType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::FunctionTypeLiteral: {
|
||||
case Expression::Kind::FunctionTypeLiteral: {
|
||||
if (act->Pos() == 0) {
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
exp->GetFunctionTypeLiteral().parameter));
|
||||
cast<FunctionTypeLiteral>(*exp).Parameter()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 1) {
|
||||
// { { pt :: fn [] -> e :: C, E, F} :: S, H}
|
||||
// -> { { e :: fn pt -> []) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(global_arena->New<ExpressionAction>(
|
||||
exp->GetFunctionTypeLiteral().return_type));
|
||||
cast<FunctionTypeLiteral>(*exp).ReturnType()));
|
||||
act->IncrementPos();
|
||||
} else if (act->Pos() == 2) {
|
||||
// { { rt :: fn pt -> [] :: C, E, F} :: S, H}
|
||||
@@ -702,14 +703,14 @@ void StepExp() {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::ContinuationTypeLiteral: {
|
||||
case Expression::Kind::ContinuationTypeLiteral: {
|
||||
CHECK(act->Pos() == 0);
|
||||
const Value* v = global_arena->New<ContinuationType>();
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(global_arena->New<ValAction>(v));
|
||||
break;
|
||||
}
|
||||
} // switch (exp->tag)
|
||||
} // switch (exp->Tag)
|
||||
}
|
||||
|
||||
void StepPattern() {
|
||||
@@ -1124,7 +1125,7 @@ void StepStmt() {
|
||||
Action* ignore_result = global_arena->New<StatementAction>(
|
||||
Statement::MakeExpressionStatement(
|
||||
stmt->line_num,
|
||||
Expression::MakeTupleLiteral(stmt->line_num, {})));
|
||||
global_arena->New<TupleLiteral>(stmt->line_num)));
|
||||
frame->todo.Push(ignore_result);
|
||||
// Push the continuation onto the current stack.
|
||||
const std::vector<Frame*>& continuation_vector =
|
||||
@@ -1190,9 +1191,9 @@ auto InterpProgram(const std::list<const Declaration*>& fs) -> int {
|
||||
}
|
||||
InitGlobals(fs);
|
||||
|
||||
const Expression* arg = Expression::MakeTupleLiteral(0, {});
|
||||
const Expression* call_main = Expression::MakeCallExpression(
|
||||
0, Expression::MakeIdentifierExpression(0, "main"), arg);
|
||||
const Expression* arg = global_arena->New<TupleLiteral>(0);
|
||||
const Expression* call_main = global_arena->New<CallExpression>(
|
||||
0, global_arena->New<IdentifierExpression>(0, "main"), arg);
|
||||
auto todo = Stack<Action*>(global_arena->New<ExpressionAction>(call_main));
|
||||
auto* scope = global_arena->New<Scope>(globals, std::list<std::string>());
|
||||
auto* frame = global_arena->New<Frame>("top", Stack(scope), todo);
|
||||
|
||||
@@ -46,16 +46,16 @@ static void ExpectPointerType(int line_num, const std::string& context,
|
||||
static auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
switch (t->Tag()) {
|
||||
case Value::Kind::IntType:
|
||||
return Expression::MakeIntTypeLiteral(0);
|
||||
return global_arena->New<IntTypeLiteral>(0);
|
||||
case Value::Kind::BoolType:
|
||||
return Expression::MakeBoolTypeLiteral(0);
|
||||
return global_arena->New<BoolTypeLiteral>(0);
|
||||
case Value::Kind::TypeType:
|
||||
return Expression::MakeTypeTypeLiteral(0);
|
||||
return global_arena->New<TypeTypeLiteral>(0);
|
||||
case Value::Kind::ContinuationType:
|
||||
return Expression::MakeContinuationTypeLiteral(0);
|
||||
return global_arena->New<ContinuationTypeLiteral>(0);
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fn_type = cast<FunctionType>(*t);
|
||||
return Expression::MakeFunctionTypeLiteral(
|
||||
return global_arena->New<FunctionTypeLiteral>(
|
||||
0, ReifyType(fn_type.Param(), line_num),
|
||||
ReifyType(fn_type.Ret(), line_num),
|
||||
/*is_omitted_return_type=*/false);
|
||||
@@ -66,20 +66,21 @@ static auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
args.push_back(
|
||||
FieldInitializer(field.name, ReifyType(field.value, line_num)));
|
||||
}
|
||||
return Expression::MakeTupleLiteral(0, args);
|
||||
return global_arena->New<TupleLiteral>(0, args);
|
||||
}
|
||||
case Value::Kind::StructType:
|
||||
return Expression::MakeIdentifierExpression(0,
|
||||
cast<StructType>(*t).Name());
|
||||
return global_arena->New<IdentifierExpression>(
|
||||
0, cast<StructType>(*t).Name());
|
||||
case Value::Kind::ChoiceType:
|
||||
return Expression::MakeIdentifierExpression(0,
|
||||
cast<ChoiceType>(*t).Name());
|
||||
return global_arena->New<IdentifierExpression>(
|
||||
0, cast<ChoiceType>(*t).Name());
|
||||
case Value::Kind::PointerType:
|
||||
return Expression::MakePrimitiveOperatorExpression(
|
||||
return global_arena->New<PrimitiveOperatorExpression>(
|
||||
0, Operator::Ptr,
|
||||
{ReifyType(cast<PointerType>(*t).Type(), line_num)});
|
||||
std::vector<const Expression*>(
|
||||
{ReifyType(cast<PointerType>(*t).Type(), line_num)}));
|
||||
case Value::Kind::VariableType:
|
||||
return Expression::MakeIdentifierExpression(
|
||||
return global_arena->New<IdentifierExpression>(
|
||||
0, cast<VariableType>(*t).Name());
|
||||
default:
|
||||
FATAL() << "expected a type, not " << *t;
|
||||
@@ -245,202 +246,204 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
if (tracing_output) {
|
||||
llvm::outs() << "checking expression " << *e << "\n";
|
||||
}
|
||||
switch (e->tag()) {
|
||||
case ExpressionKind::IndexExpression: {
|
||||
auto res = TypeCheckExp(e->GetIndexExpression().aggregate, types, values);
|
||||
switch (e->Tag()) {
|
||||
case Expression::Kind::IndexExpression: {
|
||||
const auto& index = cast<IndexExpression>(*e);
|
||||
auto res = TypeCheckExp(index.Aggregate(), types, values);
|
||||
auto t = res.type;
|
||||
switch (t->Tag()) {
|
||||
case Value::Kind::TupleValue: {
|
||||
auto i =
|
||||
cast<IntValue>(*InterpExp(values, e->GetIndexExpression().offset))
|
||||
.Val();
|
||||
auto i = cast<IntValue>(*InterpExp(values, index.Offset())).Val();
|
||||
std::string f = std::to_string(i);
|
||||
const Value* field_t = cast<TupleValue>(*t).FindField(f);
|
||||
if (field_t == nullptr) {
|
||||
FATAL_COMPILATION_ERROR(e->line_num)
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "field " << f << " is not in the tuple " << *t;
|
||||
}
|
||||
auto new_e = Expression::MakeIndexExpression(
|
||||
e->line_num, res.exp, Expression::MakeIntLiteral(e->line_num, i));
|
||||
auto new_e = global_arena->New<IndexExpression>(
|
||||
e->LineNumber(), res.exp,
|
||||
global_arena->New<IntLiteral>(e->LineNumber(), i));
|
||||
return TCExpression(new_e, field_t, res.types);
|
||||
}
|
||||
default:
|
||||
FATAL_COMPILATION_ERROR(e->line_num) << "expected a tuple";
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber()) << "expected a tuple";
|
||||
}
|
||||
}
|
||||
case ExpressionKind::TupleLiteral: {
|
||||
case Expression::Kind::TupleLiteral: {
|
||||
std::vector<FieldInitializer> new_args;
|
||||
std::vector<TupleElement> arg_types;
|
||||
auto new_types = types;
|
||||
int i = 0;
|
||||
for (auto arg = e->GetTupleLiteral().fields.begin();
|
||||
arg != e->GetTupleLiteral().fields.end(); ++arg, ++i) {
|
||||
auto arg_res = TypeCheckExp(arg->expression, new_types, values);
|
||||
for (const auto& arg : cast<TupleLiteral>(*e).Fields()) {
|
||||
auto arg_res = TypeCheckExp(arg.expression, new_types, values);
|
||||
new_types = arg_res.types;
|
||||
new_args.push_back(FieldInitializer(arg->name, arg_res.exp));
|
||||
arg_types.push_back({.name = arg->name, .value = arg_res.type});
|
||||
new_args.push_back(FieldInitializer(arg.name, arg_res.exp));
|
||||
arg_types.push_back({.name = arg.name, .value = arg_res.type});
|
||||
}
|
||||
auto tuple_e = Expression::MakeTupleLiteral(e->line_num, new_args);
|
||||
auto tuple_e = global_arena->New<TupleLiteral>(e->LineNumber(), new_args);
|
||||
auto tuple_t = global_arena->New<TupleValue>(std::move(arg_types));
|
||||
return TCExpression(tuple_e, tuple_t, new_types);
|
||||
}
|
||||
case ExpressionKind::FieldAccessExpression: {
|
||||
auto res =
|
||||
TypeCheckExp(e->GetFieldAccessExpression().aggregate, types, values);
|
||||
case Expression::Kind::FieldAccessExpression: {
|
||||
const auto& access = cast<FieldAccessExpression>(*e);
|
||||
auto res = TypeCheckExp(access.Aggregate(), types, values);
|
||||
auto t = res.type;
|
||||
switch (t->Tag()) {
|
||||
case Value::Kind::StructType: {
|
||||
const auto& t_struct = cast<StructType>(*t);
|
||||
// Search for a field
|
||||
for (auto& field : t_struct.Fields()) {
|
||||
if (e->GetFieldAccessExpression().field == field.first) {
|
||||
const Expression* new_e = Expression::MakeFieldAccessExpression(
|
||||
e->line_num, res.exp, e->GetFieldAccessExpression().field);
|
||||
if (access.Field() == field.first) {
|
||||
const Expression* new_e =
|
||||
global_arena->New<FieldAccessExpression>(
|
||||
e->LineNumber(), res.exp, access.Field());
|
||||
return TCExpression(new_e, field.second, res.types);
|
||||
}
|
||||
}
|
||||
// Search for a method
|
||||
for (auto& method : t_struct.Methods()) {
|
||||
if (e->GetFieldAccessExpression().field == method.first) {
|
||||
const Expression* new_e = Expression::MakeFieldAccessExpression(
|
||||
e->line_num, res.exp, e->GetFieldAccessExpression().field);
|
||||
if (access.Field() == method.first) {
|
||||
const Expression* new_e =
|
||||
global_arena->New<FieldAccessExpression>(
|
||||
e->LineNumber(), res.exp, access.Field());
|
||||
return TCExpression(new_e, method.second, res.types);
|
||||
}
|
||||
}
|
||||
FATAL_COMPILATION_ERROR(e->line_num)
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "struct " << t_struct.Name() << " does not have a field named "
|
||||
<< e->GetFieldAccessExpression().field;
|
||||
<< access.Field();
|
||||
}
|
||||
case Value::Kind::TupleValue: {
|
||||
const auto& tup = cast<TupleValue>(*t);
|
||||
for (const TupleElement& field : tup.Elements()) {
|
||||
if (e->GetFieldAccessExpression().field == field.name) {
|
||||
auto new_e = Expression::MakeFieldAccessExpression(
|
||||
e->line_num, res.exp, e->GetFieldAccessExpression().field);
|
||||
if (access.Field() == field.name) {
|
||||
auto new_e = global_arena->New<FieldAccessExpression>(
|
||||
e->LineNumber(), res.exp, access.Field());
|
||||
return TCExpression(new_e, field.value, res.types);
|
||||
}
|
||||
}
|
||||
FATAL_COMPILATION_ERROR(e->line_num)
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "tuple " << tup << " does not have a field named "
|
||||
<< e->GetFieldAccessExpression().field;
|
||||
<< access.Field();
|
||||
}
|
||||
case Value::Kind::ChoiceType: {
|
||||
const auto& choice = cast<ChoiceType>(*t);
|
||||
for (const auto& vt : choice.Alternatives()) {
|
||||
if (e->GetFieldAccessExpression().field == vt.first) {
|
||||
const Expression* new_e = Expression::MakeFieldAccessExpression(
|
||||
e->line_num, res.exp, e->GetFieldAccessExpression().field);
|
||||
if (access.Field() == vt.first) {
|
||||
const Expression* new_e =
|
||||
global_arena->New<FieldAccessExpression>(
|
||||
e->LineNumber(), res.exp, access.Field());
|
||||
auto fun_ty = global_arena->New<FunctionType>(
|
||||
std::vector<GenericBinding>(), vt.second, t);
|
||||
return TCExpression(new_e, fun_ty, res.types);
|
||||
}
|
||||
}
|
||||
FATAL_COMPILATION_ERROR(e->line_num)
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "choice " << choice.Name() << " does not have a field named "
|
||||
<< e->GetFieldAccessExpression().field;
|
||||
<< access.Field();
|
||||
}
|
||||
default:
|
||||
FATAL_COMPILATION_ERROR(e->line_num)
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "field access, expected a struct\n"
|
||||
<< *e;
|
||||
}
|
||||
}
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
std::optional<const Value*> type =
|
||||
types.Get(e->GetIdentifierExpression().name);
|
||||
case Expression::Kind::IdentifierExpression: {
|
||||
const auto& ident = cast<IdentifierExpression>(*e);
|
||||
std::optional<const Value*> type = types.Get(ident.Name());
|
||||
if (type) {
|
||||
return TCExpression(e, *type, types);
|
||||
} else {
|
||||
FATAL_COMPILATION_ERROR(e->line_num)
|
||||
<< "could not find `" << e->GetIdentifierExpression().name << "`";
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "could not find `" << ident.Name() << "`";
|
||||
}
|
||||
}
|
||||
case ExpressionKind::IntLiteral:
|
||||
case Expression::Kind::IntLiteral:
|
||||
return TCExpression(e, global_arena->New<IntType>(), types);
|
||||
case ExpressionKind::BoolLiteral:
|
||||
case Expression::Kind::BoolLiteral:
|
||||
return TCExpression(e, global_arena->New<BoolType>(), types);
|
||||
case ExpressionKind::PrimitiveOperatorExpression: {
|
||||
case Expression::Kind::PrimitiveOperatorExpression: {
|
||||
const auto& op = cast<PrimitiveOperatorExpression>(*e);
|
||||
std::vector<const Expression*> es;
|
||||
std::vector<const Value*> ts;
|
||||
auto new_types = types;
|
||||
for (const Expression* argument :
|
||||
e->GetPrimitiveOperatorExpression().arguments) {
|
||||
for (const Expression* argument : op.Arguments()) {
|
||||
auto res = TypeCheckExp(argument, types, values);
|
||||
new_types = res.types;
|
||||
es.push_back(res.exp);
|
||||
ts.push_back(res.type);
|
||||
}
|
||||
auto new_e = Expression::MakePrimitiveOperatorExpression(
|
||||
e->line_num, e->GetPrimitiveOperatorExpression().op, es);
|
||||
switch (e->GetPrimitiveOperatorExpression().op) {
|
||||
auto new_e = global_arena->New<PrimitiveOperatorExpression>(
|
||||
e->LineNumber(), op.Op(), es);
|
||||
switch (op.Op()) {
|
||||
case Operator::Neg:
|
||||
ExpectType(e->line_num, "negation", global_arena->New<IntType>(),
|
||||
ExpectType(e->LineNumber(), "negation", global_arena->New<IntType>(),
|
||||
ts[0]);
|
||||
return TCExpression(new_e, global_arena->New<IntType>(), new_types);
|
||||
case Operator::Add:
|
||||
ExpectType(e->line_num, "addition(1)", global_arena->New<IntType>(),
|
||||
ts[0]);
|
||||
ExpectType(e->line_num, "addition(2)", global_arena->New<IntType>(),
|
||||
ts[1]);
|
||||
ExpectType(e->LineNumber(), "addition(1)",
|
||||
global_arena->New<IntType>(), ts[0]);
|
||||
ExpectType(e->LineNumber(), "addition(2)",
|
||||
global_arena->New<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<IntType>(), new_types);
|
||||
case Operator::Sub:
|
||||
ExpectType(e->line_num, "subtraction(1)",
|
||||
ExpectType(e->LineNumber(), "subtraction(1)",
|
||||
global_arena->New<IntType>(), ts[0]);
|
||||
ExpectType(e->line_num, "subtraction(2)",
|
||||
ExpectType(e->LineNumber(), "subtraction(2)",
|
||||
global_arena->New<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<IntType>(), new_types);
|
||||
case Operator::Mul:
|
||||
ExpectType(e->line_num, "multiplication(1)",
|
||||
ExpectType(e->LineNumber(), "multiplication(1)",
|
||||
global_arena->New<IntType>(), ts[0]);
|
||||
ExpectType(e->line_num, "multiplication(2)",
|
||||
ExpectType(e->LineNumber(), "multiplication(2)",
|
||||
global_arena->New<IntType>(), ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<IntType>(), new_types);
|
||||
case Operator::And:
|
||||
ExpectType(e->line_num, "&&(1)", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "&&(1)", global_arena->New<BoolType>(),
|
||||
ts[0]);
|
||||
ExpectType(e->line_num, "&&(2)", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "&&(2)", global_arena->New<BoolType>(),
|
||||
ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<BoolType>(), new_types);
|
||||
case Operator::Or:
|
||||
ExpectType(e->line_num, "||(1)", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "||(1)", global_arena->New<BoolType>(),
|
||||
ts[0]);
|
||||
ExpectType(e->line_num, "||(2)", global_arena->New<BoolType>(),
|
||||
ExpectType(e->LineNumber(), "||(2)", global_arena->New<BoolType>(),
|
||||
ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<BoolType>(), new_types);
|
||||
case Operator::Not:
|
||||
ExpectType(e->line_num, "!", global_arena->New<BoolType>(), ts[0]);
|
||||
ExpectType(e->LineNumber(), "!", global_arena->New<BoolType>(),
|
||||
ts[0]);
|
||||
return TCExpression(new_e, global_arena->New<BoolType>(), new_types);
|
||||
case Operator::Eq:
|
||||
ExpectType(e->line_num, "==", ts[0], ts[1]);
|
||||
ExpectType(e->LineNumber(), "==", ts[0], ts[1]);
|
||||
return TCExpression(new_e, global_arena->New<BoolType>(), new_types);
|
||||
case Operator::Deref:
|
||||
ExpectPointerType(e->line_num, "*", ts[0]);
|
||||
ExpectPointerType(e->LineNumber(), "*", ts[0]);
|
||||
return TCExpression(new_e, cast<PointerType>(*ts[0]).Type(),
|
||||
new_types);
|
||||
case Operator::Ptr:
|
||||
ExpectType(e->line_num, "*", global_arena->New<TypeType>(), ts[0]);
|
||||
ExpectType(e->LineNumber(), "*", global_arena->New<TypeType>(),
|
||||
ts[0]);
|
||||
return TCExpression(new_e, global_arena->New<TypeType>(), new_types);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::CallExpression: {
|
||||
auto fun_res =
|
||||
TypeCheckExp(e->GetCallExpression().function, types, values);
|
||||
case Expression::Kind::CallExpression: {
|
||||
const auto& call = cast<CallExpression>(*e);
|
||||
auto fun_res = TypeCheckExp(call.Function(), types, values);
|
||||
switch (fun_res.type->Tag()) {
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fun_t = cast<FunctionType>(*fun_res.type);
|
||||
auto arg_res = TypeCheckExp(e->GetCallExpression().argument,
|
||||
fun_res.types, values);
|
||||
auto arg_res = TypeCheckExp(call.Argument(), fun_res.types, values);
|
||||
auto parameter_type = fun_t.Param();
|
||||
auto return_type = fun_t.Ret();
|
||||
if (!fun_t.Deduced().empty()) {
|
||||
auto deduced_args = ArgumentDeduction(e->line_num, TypeEnv(),
|
||||
auto deduced_args = ArgumentDeduction(e->LineNumber(), TypeEnv(),
|
||||
parameter_type, arg_res.type);
|
||||
for (auto& 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)) {
|
||||
FATAL_COMPILATION_ERROR(e->line_num)
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "could not deduce type argument for type parameter "
|
||||
<< deduced_param.name;
|
||||
}
|
||||
@@ -448,35 +451,37 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values)
|
||||
parameter_type = Substitute(deduced_args, parameter_type);
|
||||
return_type = Substitute(deduced_args, return_type);
|
||||
} else {
|
||||
ExpectType(e->line_num, "call", parameter_type, arg_res.type);
|
||||
ExpectType(e->LineNumber(), "call", parameter_type, arg_res.type);
|
||||
}
|
||||
auto new_e = Expression::MakeCallExpression(e->line_num, fun_res.exp,
|
||||
arg_res.exp);
|
||||
auto new_e = global_arena->New<CallExpression>(
|
||||
e->LineNumber(), fun_res.exp, arg_res.exp);
|
||||
return TCExpression(new_e, return_type, arg_res.types);
|
||||
}
|
||||
default: {
|
||||
FATAL_COMPILATION_ERROR(e->line_num)
|
||||
FATAL_COMPILATION_ERROR(e->LineNumber())
|
||||
<< "in call, expected a function\n"
|
||||
<< *e;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::FunctionTypeLiteral: {
|
||||
auto pt = InterpExp(values, e->GetFunctionTypeLiteral().parameter);
|
||||
auto rt = InterpExp(values, e->GetFunctionTypeLiteral().return_type);
|
||||
auto new_e = Expression::MakeFunctionTypeLiteral(
|
||||
e->line_num, ReifyType(pt, e->line_num), ReifyType(rt, e->line_num),
|
||||
case Expression::Kind::FunctionTypeLiteral: {
|
||||
const auto& fn = cast<FunctionTypeLiteral>(*e);
|
||||
auto pt = InterpExp(values, fn.Parameter());
|
||||
auto rt = InterpExp(values, fn.ReturnType());
|
||||
auto new_e = global_arena->New<FunctionTypeLiteral>(
|
||||
e->LineNumber(), ReifyType(pt, e->LineNumber()),
|
||||
ReifyType(rt, e->LineNumber()),
|
||||
/*is_omitted_return_type=*/false);
|
||||
return TCExpression(new_e, global_arena->New<TypeType>(), types);
|
||||
}
|
||||
case ExpressionKind::IntTypeLiteral:
|
||||
case Expression::Kind::IntTypeLiteral:
|
||||
return TCExpression(e, global_arena->New<TypeType>(), types);
|
||||
case ExpressionKind::BoolTypeLiteral:
|
||||
case Expression::Kind::BoolTypeLiteral:
|
||||
return TCExpression(e, global_arena->New<TypeType>(), types);
|
||||
case ExpressionKind::TypeTypeLiteral:
|
||||
case Expression::Kind::TypeTypeLiteral:
|
||||
return TCExpression(e, global_arena->New<TypeType>(), types);
|
||||
case ExpressionKind::ContinuationTypeLiteral:
|
||||
case Expression::Kind::ContinuationTypeLiteral:
|
||||
return TCExpression(e, global_arena->New<TypeType>(), types);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user