Prefix most macro names with CARBON_ (#1232)

I'm doing this to avoid macro name conflicts, following https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros: "If you do export a macro from a header, it must have a globally unique name. To achieve this, it must be named with a prefix consisting of your project's namespace name (but upper case)."

Commands run:

```
sed -i 's/\(DCHECK\|CHECK\|FATAL\|MAKE_UNIQUE_NAME\|MAKE_UNIQUE_NAME_IMPL\|RAW_EXITING_STREAM\|RETURN_IF_ERROR\|RETURN_IF_ERROR_IMPL\|ASSIGN_OR_RETURN\|ASSIGN_OR_RETURN_IMPL\|DIAGNOSTIC_KIND\|RETURN_IF_STACK_LIMITED\)(/CARBON_\1(/g' $(git ls-files *.cpp *.h *.lpp *.ypp *.def ':!third_party')
sed -i 's/#undef DIAGNOSTIC_KIND/#undef CARBON_DIAGNOSTIC_KIND/' toolchain/diagnostics/diagnostic_registry.def
```

Note this isn't *quite* everything, but it's intended to be a large pass at everything:

```
╚╡git grep '#define ' *.cpp *.h *.lpp *.ypp *.def ':!third_party' | grep -v '#define CARBON' | grep -v _H_
explorer/syntax/lexer.lpp:  #define YY_USER_ACTION                                             \
explorer/syntax/lexer.lpp:  #define SIMPLE_TOKEN(name) \
explorer/syntax/lexer.lpp:  #define ARG_TOKEN(name, arg) \
explorer/syntax/parse_and_lex_context.h:#define YY_DECL                                                         \
migrate_cpp/cpp_refactoring/var_decl.cpp:#define ABSTRACT_TYPE(Class, Base)
migrate_cpp/cpp_refactoring/var_decl.cpp:#define TYPE(Class, Base)     \
```

We may in particular want to do a pass to clean up #ifdef guards and make them be CARBON_ rooted.
This commit is contained in:
Jon Meow
2022-05-06 15:30:25 -07:00
committed by GitHub
parent c4fecf720f
commit af694b97cb
57 changed files with 869 additions and 779 deletions
+132 -115
View File
@@ -196,7 +196,7 @@ auto Interpreter::EvalPrim(Operator op,
auto Interpreter::CreateStruct(const std::vector<FieldInitializer>& fields,
const std::vector<Nonnull<const Value*>>& values)
-> Nonnull<const Value*> {
CHECK(fields.size() == values.size());
CARBON_CHECK(fields.size() == values.size());
std::vector<NamedValue> elements;
for (size_t i = 0; i < fields.size(); ++i) {
elements.push_back({.name = fields[i].name(), .value = values[i]});
@@ -216,7 +216,7 @@ auto PatternMatch(Nonnull<const Value*> p, Nonnull<const Value*> v,
}
switch (p->kind()) {
case Value::Kind::BindingPlaceholderValue: {
CHECK(bindings.has_value());
CARBON_CHECK(bindings.has_value());
const auto& placeholder = cast<BindingPlaceholderValue>(*p);
if (placeholder.value_node().has_value()) {
(*bindings)->Initialize(*placeholder.value_node(), v);
@@ -233,7 +233,7 @@ auto PatternMatch(Nonnull<const Value*> p, Nonnull<const Value*> v,
case Value::Kind::TupleValue: {
const auto& p_tup = cast<TupleValue>(*p);
const auto& v_tup = cast<TupleValue>(*v);
CHECK(p_tup.elements().size() == v_tup.elements().size());
CARBON_CHECK(p_tup.elements().size() == v_tup.elements().size());
for (size_t i = 0; i < p_tup.elements().size(); ++i) {
if (!PatternMatch(p_tup.elements()[i], v_tup.elements()[i],
source_loc, bindings, generic_args,
@@ -244,14 +244,15 @@ auto PatternMatch(Nonnull<const Value*> p, Nonnull<const Value*> v,
return true;
}
default:
FATAL() << "expected a tuple value in pattern, not " << *v;
CARBON_FATAL() << "expected a tuple value in pattern, not " << *v;
}
case Value::Kind::StructValue: {
const auto& p_struct = cast<StructValue>(*p);
const auto& v_struct = cast<StructValue>(*v);
CHECK(p_struct.elements().size() == v_struct.elements().size());
CARBON_CHECK(p_struct.elements().size() == v_struct.elements().size());
for (size_t i = 0; i < p_struct.elements().size(); ++i) {
CHECK(p_struct.elements()[i].name == v_struct.elements()[i].name);
CARBON_CHECK(p_struct.elements()[i].name ==
v_struct.elements()[i].name);
if (!PatternMatch(p_struct.elements()[i].value,
v_struct.elements()[i].value, source_loc, bindings,
generic_args, trace_stream)) {
@@ -273,7 +274,8 @@ auto PatternMatch(Nonnull<const Value*> p, Nonnull<const Value*> v,
bindings, generic_args, trace_stream);
}
default:
FATAL() << "expected a choice alternative in pattern, not " << *v;
CARBON_FATAL() << "expected a choice alternative in pattern, not "
<< *v;
}
case Value::Kind::FunctionType:
switch (v->kind()) {
@@ -313,11 +315,11 @@ auto Interpreter::StepLvalue() -> ErrorOr<Success> {
case ExpressionKind::IdentifierExpression: {
// { {x :: C, E, F} :: S, H}
// -> { {E(x) :: C, E, F} :: S, H}
ASSIGN_OR_RETURN(
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> value,
todo_.ValueOfNode(cast<IdentifierExpression>(exp).value_node(),
exp.source_loc()));
CHECK(isa<LValue>(value)) << *value;
CARBON_CHECK(isa<LValue>(value)) << *value;
return todo_.FinishAction(value);
}
case ExpressionKind::FieldAccessExpression: {
@@ -358,8 +360,8 @@ auto Interpreter::StepLvalue() -> ErrorOr<Success> {
case ExpressionKind::PrimitiveOperatorExpression: {
const auto& op = cast<PrimitiveOperatorExpression>(exp);
if (op.op() != Operator::Deref) {
FATAL() << "Can't treat primitive operator expression as lvalue: "
<< exp;
CARBON_FATAL()
<< "Can't treat primitive operator expression as lvalue: " << exp;
}
if (act.pos() == 0) {
return todo_.Spawn(
@@ -387,9 +389,9 @@ auto Interpreter::StepLvalue() -> ErrorOr<Success> {
case ExpressionKind::IfExpression:
case ExpressionKind::ArrayTypeLiteral:
case ExpressionKind::InstantiateImpl:
FATAL() << "Can't treat expression as lvalue: " << exp;
CARBON_FATAL() << "Can't treat expression as lvalue: " << exp;
case ExpressionKind::UnimplementedExpression:
FATAL() << "Unimplemented: " << exp;
CARBON_FATAL() << "Unimplemented: " << exp;
}
}
@@ -398,28 +400,28 @@ auto Interpreter::EvalImplExp(Nonnull<const Expression*> exp) const
switch (exp->kind()) {
case ExpressionKind::InstantiateImpl: {
const InstantiateImpl& inst_impl = cast<InstantiateImpl>(*exp);
ASSIGN_OR_RETURN(Nonnull<const Witness*> gen_impl,
EvalImplExp(inst_impl.generic_impl()));
CARBON_ASSIGN_OR_RETURN(Nonnull<const Witness*> gen_impl,
EvalImplExp(inst_impl.generic_impl()));
ImplWitnessMap witnesses;
for (auto& [bind, impl_exp] : inst_impl.impls()) {
ASSIGN_OR_RETURN(witnesses[bind], EvalImplExp(impl_exp));
CARBON_ASSIGN_OR_RETURN(witnesses[bind], EvalImplExp(impl_exp));
}
return arena_->New<Witness>(&gen_impl->declaration(),
inst_impl.type_args(), witnesses);
}
case ExpressionKind::IdentifierExpression: {
const auto& ident = cast<IdentifierExpression>(*exp);
ASSIGN_OR_RETURN(
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> value,
todo_.ValueOfNode(ident.value_node(), ident.source_loc()));
if (const auto* lvalue = dyn_cast<LValue>(value)) {
ASSIGN_OR_RETURN(value,
heap_.Read(lvalue->address(), exp->source_loc()));
CARBON_ASSIGN_OR_RETURN(
value, heap_.Read(lvalue->address(), exp->source_loc()));
}
return cast<Witness>(value);
}
default: {
FATAL() << "EvalImplExp, unexpected expression: " << *exp;
CARBON_FATAL() << "EvalImplExp, unexpected expression: " << *exp;
}
}
}
@@ -429,11 +431,12 @@ auto Interpreter::InstantiateType(Nonnull<const Value*> type,
-> ErrorOr<Nonnull<const Value*>> {
switch (type->kind()) {
case Value::Kind::VariableType: {
ASSIGN_OR_RETURN(
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> value,
todo_.ValueOfNode(&cast<VariableType>(*type).binding(), source_loc));
if (const auto* lvalue = dyn_cast<LValue>(value)) {
ASSIGN_OR_RETURN(value, heap_.Read(lvalue->address(), source_loc));
CARBON_ASSIGN_OR_RETURN(value,
heap_.Read(lvalue->address(), source_loc));
}
return value;
}
@@ -441,12 +444,12 @@ auto Interpreter::InstantiateType(Nonnull<const Value*> type,
const auto& class_type = cast<NominalClassType>(*type);
BindingMap inst_type_args;
for (const auto& [ty_var, ty_arg] : class_type.type_args()) {
ASSIGN_OR_RETURN(inst_type_args[ty_var],
InstantiateType(ty_arg, source_loc));
CARBON_ASSIGN_OR_RETURN(inst_type_args[ty_var],
InstantiateType(ty_arg, source_loc));
}
std::map<Nonnull<const ImplBinding*>, Nonnull<const Witness*>> witnesses;
for (const auto& [bind, impl_exp] : class_type.impls()) {
ASSIGN_OR_RETURN(witnesses[bind], EvalImplExp(impl_exp));
CARBON_ASSIGN_OR_RETURN(witnesses[bind], EvalImplExp(impl_exp));
}
return arena_->New<NominalClassType>(&class_type.declaration(),
inst_type_args, witnesses);
@@ -493,8 +496,8 @@ auto Interpreter::Convert(Nonnull<const Value*> value,
case Value::Kind::TypeOfChoiceType:
case Value::Kind::TypeOfParameterizedEntityName:
case Value::Kind::StaticArrayType:
// TODO: add `CHECK(TypeEqual(type, value->dynamic_type()))`, once we
// have Value::dynamic_type.
// TODO: add `CARBON_CHECK(TypeEqual(type, value->dynamic_type()))`, once
// we have Value::dynamic_type.
return value;
case Value::Kind::StructValue: {
const auto& struct_val = cast<StructValue>(*value);
@@ -507,8 +510,9 @@ auto Interpreter::Convert(Nonnull<const Value*> value,
destination_struct_type.fields()) {
std::optional<Nonnull<const Value*>> old_value =
struct_val.FindField(field_name);
ASSIGN_OR_RETURN(Nonnull<const Value*> val,
Convert(*old_value, field_type, source_loc));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> val,
Convert(*old_value, field_type, source_loc));
new_elements.push_back({.name = field_name, .value = val});
}
return arena_->New<StructValue>(std::move(new_elements));
@@ -516,13 +520,14 @@ auto Interpreter::Convert(Nonnull<const Value*> value,
case Value::Kind::NominalClassType: {
// Instantiate the `destintation_type` to obtain the runtime
// type of the object.
ASSIGN_OR_RETURN(Nonnull<const Value*> inst_dest,
InstantiateType(destination_type, source_loc));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> inst_dest,
InstantiateType(destination_type, source_loc));
return arena_->New<NominalClassValue>(inst_dest, value);
}
default:
FATAL() << "Can't convert value " << *value << " to type "
<< *destination_type;
CARBON_FATAL() << "Can't convert value " << *value << " to type "
<< *destination_type;
}
}
case Value::Kind::TupleValue: {
@@ -540,15 +545,17 @@ auto Interpreter::Convert(Nonnull<const Value*> value,
break;
}
default:
FATAL() << "Can't convert value " << *value << " to type "
<< *destination_type;
CARBON_FATAL() << "Can't convert value " << *value << " to type "
<< *destination_type;
}
CHECK(tuple->elements().size() == destination_element_types.size());
CARBON_CHECK(tuple->elements().size() ==
destination_element_types.size());
std::vector<Nonnull<const Value*>> new_elements;
for (size_t i = 0; i < tuple->elements().size(); ++i) {
ASSIGN_OR_RETURN(Nonnull<const Value*> val,
Convert(tuple->elements()[i],
destination_element_types[i], source_loc));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> val,
Convert(tuple->elements()[i], destination_element_types[i],
source_loc));
new_elements.push_back(val);
}
return arena_->New<TupleValue>(std::move(new_elements));
@@ -573,9 +580,10 @@ auto Interpreter::CallFunction(const CallExpression& call,
case Value::Kind::FunctionValue: {
const FunctionValue& fun_val = cast<FunctionValue>(*fun);
const FunctionDeclaration& function = fun_val.declaration();
ASSIGN_OR_RETURN(Nonnull<const Value*> converted_args,
Convert(arg, &function.param_pattern().static_type(),
call.source_loc()));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> converted_args,
Convert(arg, &function.param_pattern().static_type(),
call.source_loc()));
RuntimeScope function_scope(&heap_);
// Bring the class type arguments into scope.
for (const auto& [bind, val] : fun_val.type_args()) {
@@ -593,10 +601,10 @@ auto Interpreter::CallFunction(const CallExpression& call,
function_scope.Initialize(impl_bind, witness);
}
BindingMap generic_args;
CHECK(PatternMatch(&function.param_pattern().value(), converted_args,
call.source_loc(), &function_scope, generic_args,
trace_stream_));
CHECK(function.body().has_value())
CARBON_CHECK(PatternMatch(&function.param_pattern().value(),
converted_args, call.source_loc(),
&function_scope, generic_args, trace_stream_));
CARBON_CHECK(function.body().has_value())
<< "Calling a function that's missing a body";
return todo_.Spawn(std::make_unique<StatementAction>(*function.body()),
std::move(function_scope));
@@ -604,18 +612,19 @@ auto Interpreter::CallFunction(const CallExpression& call,
case Value::Kind::BoundMethodValue: {
const auto& m = cast<BoundMethodValue>(*fun);
const FunctionDeclaration& method = m.declaration();
CHECK(method.is_method());
ASSIGN_OR_RETURN(Nonnull<const Value*> converted_args,
Convert(arg, &method.param_pattern().static_type(),
call.source_loc()));
CARBON_CHECK(method.is_method());
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> converted_args,
Convert(arg, &method.param_pattern().static_type(),
call.source_loc()));
RuntimeScope method_scope(&heap_);
BindingMap generic_args;
CHECK(PatternMatch(&method.me_pattern().value(), m.receiver(),
call.source_loc(), &method_scope, generic_args,
trace_stream_));
CHECK(PatternMatch(&method.param_pattern().value(), converted_args,
call.source_loc(), &method_scope, generic_args,
trace_stream_));
CARBON_CHECK(PatternMatch(&method.me_pattern().value(), m.receiver(),
call.source_loc(), &method_scope, generic_args,
trace_stream_));
CARBON_CHECK(PatternMatch(&method.param_pattern().value(), converted_args,
call.source_loc(), &method_scope, generic_args,
trace_stream_));
// Bring the class type arguments into scope.
for (const auto& [bind, val] : m.type_args()) {
method_scope.Initialize(bind, val);
@@ -625,7 +634,7 @@ auto Interpreter::CallFunction(const CallExpression& call,
for (const auto& [impl_bind, witness] : m.witnesses()) {
method_scope.Initialize(impl_bind, witness);
}
CHECK(method.body().has_value())
CARBON_CHECK(method.body().has_value())
<< "Calling a method that's missing a body";
return todo_.Spawn(std::make_unique<StatementAction>(*method.body()),
std::move(method_scope));
@@ -635,8 +644,8 @@ auto Interpreter::CallFunction(const CallExpression& call,
const Declaration& decl = name.declaration();
RuntimeScope params_scope(&heap_);
BindingMap generic_args;
CHECK(PatternMatch(&name.params().value(), arg, call.source_loc(),
&params_scope, generic_args, trace_stream_));
CARBON_CHECK(PatternMatch(&name.params().value(), arg, call.source_loc(),
&params_scope, generic_args, trace_stream_));
switch (decl.kind()) {
case DeclarationKind::ClassDeclaration: {
switch (phase()) {
@@ -660,7 +669,7 @@ auto Interpreter::CallFunction(const CallExpression& call,
}
}
default:
FATAL() << "unknown kind of ParameterizedEntityName " << decl;
CARBON_FATAL() << "unknown kind of ParameterizedEntityName " << decl;
}
}
default:
@@ -768,42 +777,43 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
// -> { { v_f :: C, E, F} : S, H}
std::optional<Nonnull<const Witness*>> witness = std::nullopt;
if (access.impl().has_value()) {
ASSIGN_OR_RETURN(
CARBON_ASSIGN_OR_RETURN(
auto witness_addr,
todo_.ValueOfNode(*access.impl(), access.source_loc()));
ASSIGN_OR_RETURN(
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> witness_value,
heap_.Read(llvm::cast<LValue>(witness_addr)->address(),
access.source_loc()));
witness = cast<Witness>(witness_value);
}
FieldPath::Component field(access.field(), witness);
ASSIGN_OR_RETURN(Nonnull<const Value*> member,
act.results()[0]->GetField(arena_, FieldPath(field),
exp.source_loc()));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> member,
act.results()[0]->GetField(arena_, FieldPath(field),
exp.source_loc()));
return todo_.FinishAction(member);
}
}
case ExpressionKind::IdentifierExpression: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
const auto& ident = cast<IdentifierExpression>(exp);
// { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H}
ASSIGN_OR_RETURN(
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> value,
todo_.ValueOfNode(ident.value_node(), ident.source_loc()));
if (const auto* lvalue = dyn_cast<LValue>(value)) {
ASSIGN_OR_RETURN(value,
heap_.Read(lvalue->address(), exp.source_loc()));
CARBON_ASSIGN_OR_RETURN(
value, heap_.Read(lvalue->address(), exp.source_loc()));
}
return todo_.FinishAction(value);
}
case ExpressionKind::IntLiteral:
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
return todo_.FinishAction(
arena_->New<IntValue>(cast<IntLiteral>(exp).value()));
case ExpressionKind::BoolLiteral:
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
return todo_.FinishAction(
arena_->New<BoolValue>(cast<BoolLiteral>(exp).value()));
@@ -821,8 +831,9 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
} else {
// { {v :: op(vs,[]) :: C, E, F} :: S, H}
// -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H}
ASSIGN_OR_RETURN(Nonnull<const Value*> value,
EvalPrim(op.op(), act.results(), exp.source_loc()));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> value,
EvalPrim(op.op(), act.results(), exp.source_loc()));
return todo_.FinishAction(value);
}
}
@@ -866,7 +877,7 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
return todo_.FinishAction(act.results()[2 + int(num_impls)]);
}
} else {
FATAL() << "in StepExp with Call pos " << act.pos();
CARBON_FATAL() << "in StepExp with Call pos " << act.pos();
}
}
case ExpressionKind::IntrinsicExpression: {
@@ -886,15 +897,15 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
}
}
case ExpressionKind::IntTypeLiteral: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
return todo_.FinishAction(arena_->New<IntType>());
}
case ExpressionKind::BoolTypeLiteral: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
return todo_.FinishAction(arena_->New<BoolType>());
}
case ExpressionKind::TypeTypeLiteral: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
return todo_.FinishAction(arena_->New<TypeType>());
}
case ExpressionKind::FunctionTypeLiteral: {
@@ -915,16 +926,16 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
}
}
case ExpressionKind::ContinuationTypeLiteral: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
return todo_.FinishAction(arena_->New<ContinuationType>());
}
case ExpressionKind::StringLiteral:
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
// { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H}
return todo_.FinishAction(
arena_->New<StringValue>(cast<StringLiteral>(exp).value()));
case ExpressionKind::StringTypeLiteral: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
return todo_.FinishAction(arena_->New<StringType>());
}
case ExpressionKind::IfExpression: {
@@ -943,7 +954,7 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
break;
}
case ExpressionKind::UnimplementedExpression:
FATAL() << "Unimplemented: " << exp;
CARBON_FATAL() << "Unimplemented: " << exp;
case ExpressionKind::ArrayTypeLiteral: {
const auto& array_literal = cast<ArrayTypeLiteral>(exp);
if (act.pos() == 0) {
@@ -969,7 +980,7 @@ auto Interpreter::StepPattern() -> ErrorOr<Success> {
}
switch (pattern.kind()) {
case PatternKind::AutoPattern: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
return todo_.FinishAction(arena_->New<AutoType>());
}
case PatternKind::BindingPattern: {
@@ -1007,7 +1018,7 @@ auto Interpreter::StepPattern() -> ErrorOr<Success> {
return todo_.Spawn(
std::make_unique<PatternAction>(&alternative.arguments()));
} else {
CHECK(act.pos() == 2);
CARBON_CHECK(act.pos() == 2);
const auto& choice_type = cast<ChoiceType>(*act.results()[0]);
return todo_.FinishAction(arena_->New<AlternativeValue>(
alternative.alternative_name(), choice_type.name(),
@@ -1056,9 +1067,10 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
auto c = match_stmt.clauses()[clause_num];
RuntimeScope matches(&heap_);
BindingMap generic_args;
ASSIGN_OR_RETURN(Nonnull<const Value*> val,
Convert(act.results()[0], &c.pattern().static_type(),
stmt.source_loc()));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> val,
Convert(act.results()[0], &c.pattern().static_type(),
stmt.source_loc()));
if (PatternMatch(&c.pattern().value(), val, stmt.source_loc(), &matches,
generic_args, trace_stream_)) {
// Ensure we don't process any more clauses.
@@ -1078,9 +1090,10 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
return todo_.Spawn(
std::make_unique<ExpressionAction>(&cast<While>(stmt).condition()));
} else {
ASSIGN_OR_RETURN(Nonnull<const Value*> condition,
Convert(act.results().back(), arena_->New<BoolType>(),
stmt.source_loc()));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> condition,
Convert(act.results().back(), arena_->New<BoolType>(),
stmt.source_loc()));
if (cast<BoolValue>(*condition).value()) {
// { {true :: (while ([]) s) :: C, E, F} :: S, H}
// -> { { s :: (while (e) s) :: C, E, F } :: S, H}
@@ -1093,13 +1106,13 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
}
}
case StatementKind::Break: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
// { { break; :: ... :: (while (e) s) :: C, E, F} :: S, H}
// -> { { C, E', F} :: S, H}
return todo_.UnwindPast(&cast<Break>(stmt).loop());
}
case StatementKind::Continue: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
// { { continue; :: ... :: (while (e) s) :: C, E, F} :: S, H}
// -> { { (while (e) s) :: C, E', F} :: S, H}
return todo_.UnwindTo(&cast<Continue>(stmt).loop());
@@ -1130,7 +1143,7 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
} else {
// { { v :: (x = []) :: C, E, F} :: S, H}
// -> { { C, E(x := a), F} :: S, H(a := copy(v))}
ASSIGN_OR_RETURN(
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> v,
Convert(act.results()[0], &definition.pattern().static_type(),
stmt.source_loc()));
@@ -1139,8 +1152,8 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
RuntimeScope matches(&heap_);
BindingMap generic_args;
CHECK(PatternMatch(p, v, stmt.source_loc(), &matches, generic_args,
trace_stream_))
CARBON_CHECK(PatternMatch(p, v, stmt.source_loc(), &matches,
generic_args, trace_stream_))
<< stmt.source_loc()
<< ": internal error in variable definition, match failed";
todo_.MergeScope(std::move(matches));
@@ -1170,10 +1183,12 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
// { { v :: (a = []) :: C, E, F} :: S, H}
// -> { { C, E, F} :: S, H(a := v)}
const auto& lval = cast<LValue>(*act.results()[0]);
ASSIGN_OR_RETURN(Nonnull<const Value*> rval,
Convert(act.results()[1], &assign.lhs().static_type(),
stmt.source_loc()));
RETURN_IF_ERROR(heap_.Write(lval.address(), rval, stmt.source_loc()));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> rval,
Convert(act.results()[1], &assign.lhs().static_type(),
stmt.source_loc()));
CARBON_RETURN_IF_ERROR(
heap_.Write(lval.address(), rval, stmt.source_loc()));
return todo_.FinishAction();
}
}
@@ -1184,9 +1199,10 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
return todo_.Spawn(
std::make_unique<ExpressionAction>(&cast<If>(stmt).condition()));
} else if (act.pos() == 1) {
ASSIGN_OR_RETURN(Nonnull<const Value*> condition,
Convert(act.results()[0], arena_->New<BoolType>(),
stmt.source_loc()));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> condition,
Convert(act.results()[0], arena_->New<BoolType>(),
stmt.source_loc()));
if (cast<BoolValue>(*condition).value()) {
// { {true :: if ([]) then_stmt else else_stmt :: C, E, F} ::
// S, H}
@@ -1215,14 +1231,14 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
// { {v :: return [] :: C, E, F} :: {C', E', F'} :: S, H}
// -> { {v :: C', E', F'} :: S, H}
const FunctionDeclaration& function = cast<Return>(stmt).function();
ASSIGN_OR_RETURN(
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> return_value,
Convert(act.results()[0], &function.return_term().static_type(),
stmt.source_loc()));
return todo_.UnwindPast(*function.body(), return_value);
}
case StatementKind::Continuation: {
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
const auto& continuation = cast<Continuation>(stmt);
// Create a continuation object by creating a frame similar the
// way one is created in a function call.
@@ -1247,7 +1263,7 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
}
}
case StatementKind::Await:
CHECK(act.pos() == 0);
CARBON_CHECK(act.pos() == 0);
return todo_.Suspend();
}
}
@@ -1290,22 +1306,22 @@ auto Interpreter::Step() -> ErrorOr<Success> {
Action& act = todo_.CurrentAction();
switch (act.kind()) {
case Action::Kind::LValAction:
RETURN_IF_ERROR(StepLvalue());
CARBON_RETURN_IF_ERROR(StepLvalue());
break;
case Action::Kind::ExpressionAction:
RETURN_IF_ERROR(StepExp());
CARBON_RETURN_IF_ERROR(StepExp());
break;
case Action::Kind::PatternAction:
RETURN_IF_ERROR(StepPattern());
CARBON_RETURN_IF_ERROR(StepPattern());
break;
case Action::Kind::StatementAction:
RETURN_IF_ERROR(StepStmt());
CARBON_RETURN_IF_ERROR(StepStmt());
break;
case Action::Kind::DeclarationAction:
RETURN_IF_ERROR(StepDeclaration());
CARBON_RETURN_IF_ERROR(StepDeclaration());
break;
case Action::Kind::ScopeAction:
FATAL() << "ScopeAction escaped ActionStack";
CARBON_FATAL() << "ScopeAction escaped ActionStack";
} // switch
return Success();
}
@@ -1317,7 +1333,7 @@ auto Interpreter::RunAllSteps(std::unique_ptr<Action> action)
}
todo_.Start(std::move(action));
while (!todo_.IsEmpty()) {
RETURN_IF_ERROR(Step());
CARBON_RETURN_IF_ERROR(Step());
if (trace_stream_) {
PrintState(**trace_stream_);
}
@@ -1334,7 +1350,7 @@ auto InterpProgram(const AST& ast, Nonnull<Arena*> arena,
}
for (Nonnull<Declaration*> declaration : ast.declarations) {
RETURN_IF_ERROR(interpreter.RunAllSteps(
CARBON_RETURN_IF_ERROR(interpreter.RunAllSteps(
std::make_unique<DeclarationAction>(declaration)));
}
@@ -1342,7 +1358,7 @@ auto InterpProgram(const AST& ast, Nonnull<Arena*> arena,
**trace_stream << "********** calling main function **********\n";
}
RETURN_IF_ERROR(interpreter.RunAllSteps(
CARBON_RETURN_IF_ERROR(interpreter.RunAllSteps(
std::make_unique<ExpressionAction>(*ast.main_call)));
return cast<IntValue>(*interpreter.result()).value();
@@ -1352,7 +1368,7 @@ auto InterpExp(Nonnull<const Expression*> e, Nonnull<Arena*> arena,
std::optional<Nonnull<llvm::raw_ostream*>> trace_stream)
-> ErrorOr<Nonnull<const Value*>> {
Interpreter interpreter(Phase::CompileTime, arena, trace_stream);
RETURN_IF_ERROR(
CARBON_RETURN_IF_ERROR(
interpreter.RunAllSteps(std::make_unique<ExpressionAction>(e)));
return interpreter.result();
}
@@ -1361,7 +1377,8 @@ auto InterpPattern(Nonnull<const Pattern*> p, Nonnull<Arena*> arena,
std::optional<Nonnull<llvm::raw_ostream*>> trace_stream)
-> ErrorOr<Nonnull<const Value*>> {
Interpreter interpreter(Phase::CompileTime, arena, trace_stream);
RETURN_IF_ERROR(interpreter.RunAllSteps(std::make_unique<PatternAction>(p)));
CARBON_RETURN_IF_ERROR(
interpreter.RunAllSteps(std::make_unique<PatternAction>(p)));
return interpreter.result();
}