mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Revert to handling Expression by const pointer (#616)
This reverts the bulk of #606, #607, and #611, as well as parts of #588, #605, and #614.
This commit is contained in:
@@ -84,12 +84,11 @@ auto Expression::MakeContinuationType(int line_num) -> const Expression* {
|
||||
return type;
|
||||
}
|
||||
|
||||
auto Expression::MakeFunType(int line_num, Expression param, Expression ret)
|
||||
-> const Expression* {
|
||||
auto Expression::MakeFunType(int line_num, const Expression* param,
|
||||
const Expression* ret) -> const Expression* {
|
||||
auto* t = new Expression();
|
||||
t->line_num = line_num;
|
||||
t->value = FunctionType(
|
||||
{.parameter = std::move(param), .return_type = std::move(ret)});
|
||||
t->value = FunctionType({.parameter = param, .return_type = ret});
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -100,11 +99,11 @@ auto Expression::MakeVar(int line_num, std::string var) -> const Expression* {
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Expression::MakeVarPat(int line_num, std::string var, Expression type)
|
||||
-> const Expression* {
|
||||
auto Expression::MakeVarPat(int line_num, std::string var,
|
||||
const Expression* type) -> const Expression* {
|
||||
auto* v = new Expression();
|
||||
v->line_num = line_num;
|
||||
v->value = PatternVariable({.name = std::move(var), .type = std::move(type)});
|
||||
v->value = PatternVariable({.name = std::move(var), .type = type});
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -123,35 +122,36 @@ auto Expression::MakeBool(int line_num, bool b) -> const Expression* {
|
||||
}
|
||||
|
||||
auto Expression::MakeOp(int line_num, enum Operator op,
|
||||
std::vector<Expression> args) -> const Expression* {
|
||||
std::vector<const Expression*> args)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = PrimitiveOperator({.op = op, .arguments = std::move(args)});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeUnOp(int line_num, enum Operator op, Expression arg)
|
||||
auto Expression::MakeUnOp(int line_num, enum Operator op, const Expression* arg)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = PrimitiveOperator({.op = op, .arguments = {std::move(arg)}});
|
||||
e->value = PrimitiveOperator({.op = op, .arguments = {arg}});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeBinOp(int line_num, enum Operator op, Expression arg1,
|
||||
Expression arg2) -> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = PrimitiveOperator(
|
||||
{.op = op, .arguments = {std::move(arg1), std::move(arg2)}});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeCall(int line_num, Expression fun, Expression arg)
|
||||
auto Expression::MakeBinOp(int line_num, enum Operator op,
|
||||
const Expression* arg1, const Expression* arg2)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = Call({.function = std::move(fun), .argument = std::move(arg)});
|
||||
e->value = PrimitiveOperator({.op = op, .arguments = {arg1, arg2}});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeCall(int line_num, const Expression* fun,
|
||||
const Expression* arg) -> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = Call({.function = fun, .argument = arg});
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ auto Expression::MakeGetField(int line_num, const Expression* exp,
|
||||
std::string field) -> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = FieldAccess({.aggregate = *exp, .field = std::move(field)});
|
||||
e->value = FieldAccess({.aggregate = exp, .field = std::move(field)});
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -187,11 +187,11 @@ auto Expression::MakeTuple(int line_num, std::vector<FieldInitializer> args)
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeIndex(int line_num, Expression exp, Expression i)
|
||||
-> const Expression* {
|
||||
auto Expression::MakeIndex(int line_num, const Expression* exp,
|
||||
const Expression* i) -> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = Index({.aggregate = std::move(exp), .offset = std::move(i)});
|
||||
e->value = Index({.aggregate = exp, .offset = i});
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -231,20 +231,20 @@ static void PrintFields(const std::vector<FieldInitializer>& fields) {
|
||||
std::cout << ", ";
|
||||
}
|
||||
std::cout << iter->name << " = ";
|
||||
PrintExp(iter->expression.GetPointer());
|
||||
PrintExp(iter->expression);
|
||||
}
|
||||
}
|
||||
|
||||
void PrintExp(const Expression* e) {
|
||||
switch (e->tag()) {
|
||||
case ExpressionKind::Index:
|
||||
PrintExp(e->GetIndex().aggregate.GetPointer());
|
||||
PrintExp(e->GetIndex().aggregate);
|
||||
std::cout << "[";
|
||||
PrintExp(e->GetIndex().offset.GetPointer());
|
||||
PrintExp(e->GetIndex().offset);
|
||||
std::cout << "]";
|
||||
break;
|
||||
case ExpressionKind::GetField:
|
||||
PrintExp(e->GetFieldAccess().aggregate.GetPointer());
|
||||
PrintExp(e->GetFieldAccess().aggregate);
|
||||
std::cout << ".";
|
||||
std::cout << e->GetFieldAccess().field;
|
||||
break;
|
||||
@@ -269,15 +269,15 @@ void PrintExp(const Expression* e) {
|
||||
PrintOp(op.op);
|
||||
std::cout << " ";
|
||||
auto iter = op.arguments.begin();
|
||||
PrintExp(&*iter);
|
||||
PrintExp(*iter);
|
||||
} else if (op.arguments.size() == 2) {
|
||||
auto iter = op.arguments.begin();
|
||||
PrintExp(&*iter);
|
||||
PrintExp(*iter);
|
||||
std::cout << " ";
|
||||
PrintOp(op.op);
|
||||
std::cout << " ";
|
||||
++iter;
|
||||
PrintExp(&*iter);
|
||||
PrintExp(*iter);
|
||||
}
|
||||
std::cout << ")";
|
||||
break;
|
||||
@@ -286,17 +286,17 @@ void PrintExp(const Expression* e) {
|
||||
std::cout << e->GetVariable().name;
|
||||
break;
|
||||
case ExpressionKind::PatternVariable:
|
||||
PrintExp(e->GetPatternVariable().type.GetPointer());
|
||||
PrintExp(e->GetPatternVariable().type);
|
||||
std::cout << ": ";
|
||||
std::cout << e->GetPatternVariable().name;
|
||||
break;
|
||||
case ExpressionKind::Call:
|
||||
PrintExp(e->GetCall().function.GetPointer());
|
||||
PrintExp(e->GetCall().function);
|
||||
if (e->GetCall().argument->tag() == ExpressionKind::Tuple) {
|
||||
PrintExp(e->GetCall().argument.GetPointer());
|
||||
PrintExp(e->GetCall().argument);
|
||||
} else {
|
||||
std::cout << "(";
|
||||
PrintExp(e->GetCall().argument.GetPointer());
|
||||
PrintExp(e->GetCall().argument);
|
||||
std::cout << ")";
|
||||
}
|
||||
break;
|
||||
@@ -317,9 +317,9 @@ void PrintExp(const Expression* e) {
|
||||
break;
|
||||
case ExpressionKind::FunctionT:
|
||||
std::cout << "fn ";
|
||||
PrintExp(e->GetFunctionType().parameter.GetPointer());
|
||||
PrintExp(e->GetFunctionType().parameter);
|
||||
std::cout << " -> ";
|
||||
PrintExp(e->GetFunctionType().return_type.GetPointer());
|
||||
PrintExp(e->GetFunctionType().return_type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user