Skip to content

Commit

Permalink
Allow all boolean convertible types in unary logical not expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Jan 11, 2025
1 parent 2d2d39f commit f04d7a0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
7 changes: 5 additions & 2 deletions source/ast/expressions/OperatorExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ Expression& UnaryExpression::fromSyntax(Compilation& compilation,
result->type = type;
break;
case SyntaxKind::UnaryLogicalNotExpression:
// Supported for both integral and real types. Result is a single bit.
good = type->isNumeric();
// Supported for all boolean convertible types. Result is a single bit.
good = type->isBooleanConvertible();
result->type = type->isFourState() ? &compilation.getLogicType()
: &compilation.getBitType();
selfDetermined(context, result->operand_);
Expand Down Expand Up @@ -491,6 +491,9 @@ ConstantValue UnaryExpression::evalImpl(EvalContext& context) const {
break;
}
}
else if (op == UnaryOperator::LogicalNot) {
return SVInt(cv.isFalse());
}

#undef OP
SLANG_UNREACHABLE;
Expand Down
4 changes: 4 additions & 0 deletions source/numeric/ConstantValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ bool ConstantValue::isTrue() const {
return (bool)arg;
else if constexpr (std::is_same_v<T, ConstantValue::UnboundedPlaceholder>)
return true;
else if constexpr (std::is_same_v<T, std::string>)
return !arg.empty();
else
return false;
},
Expand All @@ -273,6 +275,8 @@ bool ConstantValue::isFalse() const {
return !(bool)arg;
else if constexpr (std::is_same_v<T, ConstantValue::NullPlaceholder>)
return true;
else if constexpr (std::is_same_v<T, std::string>)
return arg.empty();
else
return false;
},
Expand Down
16 changes: 16 additions & 0 deletions tests/unittests/ast/EvalTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2608,3 +2608,19 @@ endmodule
auto& compare = root.lookupName<ParameterSymbol>("m.Compare");
CHECK(compare.getValue().integer() == 1);
}

TEST_CASE("Eval truthiness of strings") {
ScriptSession session(optionsFor(LanguageVersion::v1800_2023));
session.eval(R"(
function automatic bit b;
string s = "SDF";
if (s)
return 1;
return 0;
endfunction
)");

CHECK(session.eval("b()").integer() == 1);

NO_SESSION_ERRORS;
}
17 changes: 17 additions & 0 deletions tests/unittests/ast/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3771,3 +3771,20 @@ endmodule
compilation.addSyntaxTree(tree);
NO_COMPILATION_ERRORS;
}

TEST_CASE("Boolean convertible types can be used with operator! -- GH #1208") {
auto tree = SyntaxTree::fromText(R"(
module m;
chandle handle = null;
always_comb begin
if (!handle) begin
$fatal(1, "handle is null");
end
end
endmodule
)");

Compilation compilation;
compilation.addSyntaxTree(tree);
NO_COMPILATION_ERRORS;
}

0 comments on commit f04d7a0

Please sign in to comment.