mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Use string values instead of string pointers in Expression. (#591)
This commit is contained in:
@@ -96,7 +96,7 @@ auto Expression::MakeFunType(int line_num, const Expression* param,
|
||||
auto Expression::MakeVar(int line_num, std::string var) -> const Expression* {
|
||||
auto* v = new Expression();
|
||||
v->line_num = line_num;
|
||||
v->value = Variable({.name = new std::string(std::move(var))});
|
||||
v->value = Variable({.name = std::move(var)});
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -104,8 +104,7 @@ 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 = new std::string(std::move(var)), .type = type});
|
||||
v->value = PatternVariable({.name = std::move(var), .type = type});
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -163,8 +162,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 = new std::string(std::move(field))});
|
||||
e->value = FieldAccess({.aggregate = exp, .field = std::move(field)});
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -262,7 +260,7 @@ void PrintExp(const Expression* e) {
|
||||
case ExpressionKind::GetField:
|
||||
PrintExp(e->GetFieldAccess().aggregate);
|
||||
std::cout << ".";
|
||||
std::cout << *e->GetFieldAccess().field;
|
||||
std::cout << e->GetFieldAccess().field;
|
||||
break;
|
||||
case ExpressionKind::Tuple:
|
||||
std::cout << "(";
|
||||
@@ -299,12 +297,12 @@ void PrintExp(const Expression* e) {
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::Variable:
|
||||
std::cout << *e->GetVariable().name;
|
||||
std::cout << e->GetVariable().name;
|
||||
break;
|
||||
case ExpressionKind::PatternVariable:
|
||||
PrintExp(e->GetPatternVariable().type);
|
||||
std::cout << ": ";
|
||||
std::cout << *e->GetPatternVariable().name;
|
||||
std::cout << e->GetPatternVariable().name;
|
||||
break;
|
||||
case ExpressionKind::Call:
|
||||
PrintExp(e->GetCall().function);
|
||||
|
||||
@@ -57,13 +57,13 @@ struct Expression;
|
||||
|
||||
struct Variable {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::Variable;
|
||||
std::string* name;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
struct FieldAccess {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::GetField;
|
||||
const Expression* aggregate;
|
||||
std::string* field;
|
||||
std::string field;
|
||||
};
|
||||
|
||||
struct Index {
|
||||
@@ -74,7 +74,7 @@ struct Index {
|
||||
|
||||
struct PatternVariable {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::PatternVariable;
|
||||
std::string* name;
|
||||
std::string name;
|
||||
const Expression* type;
|
||||
};
|
||||
|
||||
|
||||
@@ -624,10 +624,10 @@ void StepLvalue() {
|
||||
// { {x :: C, E, F} :: S, H}
|
||||
// -> { {E(x) :: C, E, F} :: S, H}
|
||||
std::optional<Address> pointer =
|
||||
CurrentEnv(state).Get(*(exp->GetVariable().name));
|
||||
CurrentEnv(state).Get(exp->GetVariable().name);
|
||||
if (!pointer) {
|
||||
std::cerr << exp->line_num << ": could not find `"
|
||||
<< *(exp->GetVariable().name) << "`" << std::endl;
|
||||
<< exp->GetVariable().name << "`" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
const Value* v = Value::MakePtrVal(*pointer);
|
||||
@@ -721,10 +721,10 @@ void StepExp() {
|
||||
case ExpressionKind::Variable: {
|
||||
// { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H}
|
||||
std::optional<Address> pointer =
|
||||
CurrentEnv(state).Get(*(exp->GetVariable().name));
|
||||
CurrentEnv(state).Get(exp->GetVariable().name);
|
||||
if (!pointer) {
|
||||
std::cerr << exp->line_num << ": could not find `"
|
||||
<< *(exp->GetVariable().name) << "`" << std::endl;
|
||||
<< exp->GetVariable().name << "`" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
const Value* pointee = state->heap.Read(*pointer, exp->line_num);
|
||||
@@ -1082,7 +1082,7 @@ void HandleValue() {
|
||||
// -> { { &v.f :: C, E, F} :: S, H }
|
||||
const Value* str = act->results[0];
|
||||
Address a = GetMember(ValToPtr(str, exp->line_num),
|
||||
*exp->GetFieldAccess().field, exp->line_num);
|
||||
exp->GetFieldAccess().field, exp->line_num);
|
||||
frame->todo.Pop(2);
|
||||
frame->todo.Push(MakeValAct(Value::MakePtrVal(a)));
|
||||
break;
|
||||
@@ -1135,7 +1135,7 @@ void HandleValue() {
|
||||
const Expression* exp = act->u.exp;
|
||||
switch (exp->tag()) {
|
||||
case ExpressionKind::PatternVariable: {
|
||||
auto v = Value::MakeVarPatVal(*exp->GetPatternVariable().name,
|
||||
auto v = Value::MakeVarPatVal(exp->GetPatternVariable().name,
|
||||
act->results[0]);
|
||||
frame->todo.Pop(2);
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
@@ -1194,7 +1194,7 @@ void HandleValue() {
|
||||
// { { v :: [].f :: C, E, F} :: S, H}
|
||||
// -> { { v_f :: C, E, F} : S, H}
|
||||
auto a = GetMember(ValToPtr(act->results[0], exp->line_num),
|
||||
*exp->GetFieldAccess().field, exp->line_num);
|
||||
exp->GetFieldAccess().field, exp->line_num);
|
||||
const Value* element = state->heap.Read(a, exp->line_num);
|
||||
frame->todo.Pop(2);
|
||||
frame->todo.Push(MakeValAct(element));
|
||||
|
||||
@@ -157,10 +157,9 @@ 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));
|
||||
types.Set(*e->GetPatternVariable().name, t);
|
||||
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: {
|
||||
@@ -241,30 +240,30 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
case ValKind::StructTV:
|
||||
// Search for a field
|
||||
for (auto& field : *t->GetStructType().fields) {
|
||||
if (*e->GetFieldAccess().field == field.first) {
|
||||
if (e->GetFieldAccess().field == field.first) {
|
||||
const Expression* new_e = Expression::MakeGetField(
|
||||
e->line_num, res.exp, *e->GetFieldAccess().field);
|
||||
e->line_num, res.exp, e->GetFieldAccess().field);
|
||||
return TCResult(new_e, field.second, res.types);
|
||||
}
|
||||
}
|
||||
// Search for a method
|
||||
for (auto& method : *t->GetStructType().methods) {
|
||||
if (*e->GetFieldAccess().field == method.first) {
|
||||
if (e->GetFieldAccess().field == method.first) {
|
||||
const Expression* new_e = Expression::MakeGetField(
|
||||
e->line_num, res.exp, *e->GetFieldAccess().field);
|
||||
e->line_num, res.exp, e->GetFieldAccess().field);
|
||||
return TCResult(new_e, method.second, res.types);
|
||||
}
|
||||
}
|
||||
std::cerr << e->line_num << ": compilation error, struct "
|
||||
<< *t->GetStructType().name
|
||||
<< " does not have a field named "
|
||||
<< *e->GetFieldAccess().field << std::endl;
|
||||
<< e->GetFieldAccess().field << std::endl;
|
||||
exit(-1);
|
||||
case ValKind::TupleV:
|
||||
for (const TupleElement& field : *t->GetTuple().elements) {
|
||||
if (*e->GetFieldAccess().field == field.name) {
|
||||
if (e->GetFieldAccess().field == field.name) {
|
||||
auto new_e = Expression::MakeGetField(e->line_num, res.exp,
|
||||
*e->GetFieldAccess().field);
|
||||
e->GetFieldAccess().field);
|
||||
return TCResult(new_e,
|
||||
state->heap.Read(field.address, e->line_num),
|
||||
res.types);
|
||||
@@ -273,14 +272,14 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
std::cerr << e->line_num << ": compilation error, struct "
|
||||
<< *t->GetStructType().name
|
||||
<< " does not have a field named "
|
||||
<< *e->GetFieldAccess().field << std::endl;
|
||||
<< e->GetFieldAccess().field << std::endl;
|
||||
exit(-1);
|
||||
case ValKind::ChoiceTV:
|
||||
for (auto vt = t->GetChoiceType().alternatives->begin();
|
||||
vt != t->GetChoiceType().alternatives->end(); ++vt) {
|
||||
if (*e->GetFieldAccess().field == vt->first) {
|
||||
if (e->GetFieldAccess().field == vt->first) {
|
||||
const Expression* new_e = Expression::MakeGetField(
|
||||
e->line_num, res.exp, *e->GetFieldAccess().field);
|
||||
e->line_num, res.exp, e->GetFieldAccess().field);
|
||||
auto fun_ty = Value::MakeFunTypeVal(vt->second, t);
|
||||
return TCResult(new_e, fun_ty, res.types);
|
||||
}
|
||||
@@ -288,7 +287,7 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
std::cerr << e->line_num << ": compilation error, struct "
|
||||
<< *t->GetStructType().name
|
||||
<< " does not have a field named "
|
||||
<< *e->GetFieldAccess().field << std::endl;
|
||||
<< e->GetFieldAccess().field << std::endl;
|
||||
exit(-1);
|
||||
|
||||
default:
|
||||
@@ -301,12 +300,12 @@ auto TypeCheckExp(const Expression* e, TypeEnv types, Env values,
|
||||
}
|
||||
}
|
||||
case ExpressionKind::Variable: {
|
||||
std::optional<const Value*> type = types.Get(*(e->GetVariable().name));
|
||||
std::optional<const Value*> type = types.Get(e->GetVariable().name);
|
||||
if (type) {
|
||||
return TCResult(e, *type, types);
|
||||
} else {
|
||||
std::cerr << e->line_num << ": could not find `"
|
||||
<< *(e->GetVariable().name) << "`" << std::endl;
|
||||
<< e->GetVariable().name << "`" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user