mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:31:15 +01:00
Migrate PrimitiveOperator to value semantics (#606)
* Make PrimitiveOperator.arguments a value. * Make operator factory functions take Expressions by value.
This commit is contained in:
@@ -122,30 +122,27 @@ auto Expression::MakeBool(int line_num, bool b) -> const Expression* {
|
||||
}
|
||||
|
||||
auto Expression::MakeOp(int line_num, enum Operator op,
|
||||
std::vector<const Expression*>* args)
|
||||
-> const Expression* {
|
||||
std::vector<Expression> args) -> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = PrimitiveOperator({.op = op, .arguments = args});
|
||||
e->value = PrimitiveOperator({.op = op, .arguments = std::move(args)});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeUnOp(int line_num, enum Operator op, const Expression* arg)
|
||||
auto Expression::MakeUnOp(int line_num, enum Operator op, Expression arg)
|
||||
-> const Expression* {
|
||||
auto* e = new Expression();
|
||||
e->line_num = line_num;
|
||||
e->value = PrimitiveOperator(
|
||||
{.op = op, .arguments = new std::vector<const Expression*>{arg}});
|
||||
e->value = PrimitiveOperator({.op = op, .arguments = {std::move(arg)}});
|
||||
return e;
|
||||
}
|
||||
|
||||
auto Expression::MakeBinOp(int line_num, enum Operator op,
|
||||
const Expression* arg1, const Expression* arg2)
|
||||
-> const Expression* {
|
||||
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 = new std::vector<const Expression*>{arg1, arg2}});
|
||||
{.op = op, .arguments = {std::move(arg1), std::move(arg2)}});
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -265,21 +262,21 @@ void PrintExp(const Expression* e) {
|
||||
case ExpressionKind::PrimitiveOp: {
|
||||
std::cout << "(";
|
||||
PrimitiveOperator op = e->GetPrimitiveOperator();
|
||||
if (op.arguments->size() == 0) {
|
||||
if (op.arguments.size() == 0) {
|
||||
PrintOp(op.op);
|
||||
} else if (op.arguments->size() == 1) {
|
||||
} else if (op.arguments.size() == 1) {
|
||||
PrintOp(op.op);
|
||||
std::cout << " ";
|
||||
auto iter = op.arguments->begin();
|
||||
PrintExp(*iter);
|
||||
} else if (op.arguments->size() == 2) {
|
||||
auto iter = op.arguments->begin();
|
||||
PrintExp(*iter);
|
||||
auto iter = op.arguments.begin();
|
||||
PrintExp(&*iter);
|
||||
} else if (op.arguments.size() == 2) {
|
||||
auto iter = op.arguments.begin();
|
||||
PrintExp(&*iter);
|
||||
std::cout << " ";
|
||||
PrintOp(op.op);
|
||||
std::cout << " ";
|
||||
++iter;
|
||||
PrintExp(*iter);
|
||||
PrintExp(&*iter);
|
||||
}
|
||||
std::cout << ")";
|
||||
break;
|
||||
|
||||
@@ -98,7 +98,7 @@ struct Tuple {
|
||||
struct PrimitiveOperator {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::PrimitiveOp;
|
||||
Operator op;
|
||||
std::vector<const Expression*>* arguments;
|
||||
std::vector<Expression> arguments;
|
||||
};
|
||||
|
||||
struct Call {
|
||||
@@ -142,12 +142,12 @@ struct Expression {
|
||||
-> const Expression*;
|
||||
static auto MakeInt(int line_num, int i) -> const Expression*;
|
||||
static auto MakeBool(int line_num, bool b) -> const Expression*;
|
||||
static auto MakeOp(int line_num, Operator op,
|
||||
std::vector<const Expression*>* args) -> const Expression*;
|
||||
static auto MakeUnOp(int line_num, enum Operator op, const Expression* arg)
|
||||
static auto MakeOp(int line_num, Operator op, std::vector<Expression> args)
|
||||
-> const Expression*;
|
||||
static auto MakeBinOp(int line_num, enum Operator op, const Expression* arg1,
|
||||
const Expression* arg2) -> const Expression*;
|
||||
static auto MakeUnOp(int line_num, enum Operator op, Expression arg)
|
||||
-> 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 MakeGetField(int line_num, const Expression* exp,
|
||||
|
||||
@@ -746,11 +746,11 @@ void StepExp() {
|
||||
frame->todo.Push(MakeValAct(Value::MakeBoolVal(exp->GetBoolean())));
|
||||
break;
|
||||
case ExpressionKind::PrimitiveOp:
|
||||
if (exp->GetPrimitiveOperator().arguments->size() > 0) {
|
||||
if (exp->GetPrimitiveOperator().arguments.size() > 0) {
|
||||
// { {op(e :: es) :: C, E, F} :: S, H}
|
||||
// -> { e :: op([] :: es) :: C, E, F} :: S, H}
|
||||
frame->todo.Push(
|
||||
MakeExpAct(exp->GetPrimitiveOperator().arguments->front()));
|
||||
MakeExpAct(&exp->GetPrimitiveOperator().arguments.front()));
|
||||
act->pos++;
|
||||
} else {
|
||||
// { {v :: op(]) :: C, E, F} :: S, H}
|
||||
@@ -1205,11 +1205,11 @@ void HandleValue() {
|
||||
}
|
||||
case ExpressionKind::PrimitiveOp: {
|
||||
if (act->pos !=
|
||||
static_cast<int>(exp->GetPrimitiveOperator().arguments->size())) {
|
||||
static_cast<int>(exp->GetPrimitiveOperator().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->GetPrimitiveOperator().arguments)[act->pos];
|
||||
&exp->GetPrimitiveOperator().arguments[act->pos];
|
||||
frame->todo.Pop(1);
|
||||
frame->todo.Push(MakeExpAct(arg));
|
||||
} else {
|
||||
|
||||
@@ -85,7 +85,7 @@ auto ReifyType(const Value* t, int line_num) -> const Expression* {
|
||||
return Expression::MakeVar(0, *t->GetChoiceType().name);
|
||||
case ValKind::PointerTV:
|
||||
return Expression::MakeUnOp(
|
||||
0, Operator::Ptr, ReifyType(t->GetPointerType().type, line_num));
|
||||
0, Operator::Ptr, *ReifyType(t->GetPointerType().type, line_num));
|
||||
default:
|
||||
std::cerr << line_num << ": expected a type, not ";
|
||||
PrintValue(t, std::cerr);
|
||||
@@ -314,14 +314,14 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
case ExpressionKind::Boolean:
|
||||
return TCResult(e, Value::MakeBoolTypeVal(), types);
|
||||
case ExpressionKind::PrimitiveOp: {
|
||||
auto es = new std::vector<const Expression*>();
|
||||
std::vector<Expression> es;
|
||||
std::vector<const Value*> ts;
|
||||
auto new_types = types;
|
||||
for (auto& argument : *e->GetPrimitiveOperator().arguments) {
|
||||
auto res = TypeCheckExp(argument, types, values, nullptr,
|
||||
for (const Expression& argument : e->GetPrimitiveOperator().arguments) {
|
||||
auto res = TypeCheckExp(&argument, types, values, nullptr,
|
||||
TCContext::ValueContext);
|
||||
new_types = res.types;
|
||||
es->push_back(res.exp);
|
||||
es.push_back(*res.exp);
|
||||
ts.push_back(res.type);
|
||||
}
|
||||
auto new_e =
|
||||
|
||||
@@ -226,31 +226,31 @@ expression:
|
||||
{ $$ = Carbon::Expression::MakeContinuationType(yylineno); }
|
||||
| paren_expression { $$ = $1; }
|
||||
| expression EQUAL_EQUAL expression
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Eq, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Eq, *$1, *$3); }
|
||||
| expression "+" expression
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Add, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Add, *$1, *$3); }
|
||||
| expression "-" expression
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Sub, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Sub, *$1, *$3); }
|
||||
| expression BINARY_STAR expression
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Mul, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Mul, *$1, *$3); }
|
||||
| expression AND expression
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::And, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::And, *$1, *$3); }
|
||||
| expression OR expression
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Or, $1, $3); }
|
||||
{ $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Or, *$1, *$3); }
|
||||
| NOT expression
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Not, $2); }
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Not, *$2); }
|
||||
| "-" expression %prec UNARY_MINUS
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Neg, $2); }
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Neg, *$2); }
|
||||
| PREFIX_STAR expression
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Deref, $2); }
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Deref, *$2); }
|
||||
| UNARY_STAR expression %prec PREFIX_STAR
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Deref, $2); }
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Deref, *$2); }
|
||||
| expression tuple
|
||||
{ $$ = Carbon::Expression::MakeCall(yylineno, $1, $2); }
|
||||
| expression POSTFIX_STAR
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Ptr, $1); }
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Ptr, *$1); }
|
||||
| expression UNARY_STAR
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Ptr, $1); }
|
||||
{ $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Ptr, *$1); }
|
||||
| FNTY tuple return_type
|
||||
{ $$ = Carbon::Expression::MakeFunType(yylineno, $2, $3); }
|
||||
;
|
||||
|
||||
Reference in New Issue
Block a user