diff --git a/executable_semantics/ast/expression.cpp b/executable_semantics/ast/expression.cpp index c0b7f48a0df5..32e358e71a09 100644 --- a/executable_semantics/ast/expression.cpp +++ b/executable_semantics/ast/expression.cpp @@ -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* args) - -> const Expression* { + std::vector 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{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{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; diff --git a/executable_semantics/ast/expression.h b/executable_semantics/ast/expression.h index 9802941b91f1..89c4c58b338e 100644 --- a/executable_semantics/ast/expression.h +++ b/executable_semantics/ast/expression.h @@ -98,7 +98,7 @@ struct Tuple { struct PrimitiveOperator { static constexpr ExpressionKind Kind = ExpressionKind::PrimitiveOp; Operator op; - std::vector* arguments; + std::vector 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* 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 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, diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 3c287f4dd5fe..d7ce1665e404 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -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(exp->GetPrimitiveOperator().arguments->size())) { + static_cast(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 { diff --git a/executable_semantics/interpreter/typecheck.cpp b/executable_semantics/interpreter/typecheck.cpp index f6ca4ce6df51..8b3f3ee90963 100644 --- a/executable_semantics/interpreter/typecheck.cpp +++ b/executable_semantics/interpreter/typecheck.cpp @@ -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(); + std::vector es; std::vector 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 = diff --git a/executable_semantics/syntax/parser.ypp b/executable_semantics/syntax/parser.ypp index d3b28d70e454..6ea60414b674 100644 --- a/executable_semantics/syntax/parser.ypp +++ b/executable_semantics/syntax/parser.ypp @@ -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); } ;