binpac: Make 'bool' a discrete type and handle it differently when generating code

This fixes clang-tidy modernize-use-bool-literals findings in the generated code
This commit is contained in:
Tim Wojtulewicz 2025-05-15 17:20:31 -07:00
parent 54a0e01805
commit 3cf68302a2
6 changed files with 15 additions and 7 deletions

View file

@ -284,7 +284,10 @@ void GenCaseStr(ExprList* index_list, Output* out_cc, Env* env, Type* switch_typ
// We're always using "int" for storage, so ok to just
// cast into the type used by the switch statement since
// some unsafe stuff is already checked above.
out_cc->println("case ((%s)%d):", switch_type->DataTypeStr().c_str(), index_const);
if ( ! switch_type->IsBooleanType() )
out_cc->println("case ((%s)%d):", switch_type->DataTypeStr().c_str(), index_const);
else
out_cc->println("case %s:", index_const == 0 ? "false" : "true");
}
}
else {

View file

@ -1,4 +1,4 @@
EXTERNTYPE(bool, bool, NUMBER)
EXTERNTYPE(bool, bool, BOOLEAN)
EXTERNTYPE(int, int, NUMBER)
EXTERNTYPE(double, double, NUMBER)
EXTERNTYPE(string, string, PLAIN)

View file

@ -9,7 +9,8 @@ bool ExternType::DefineValueVar() const { return true; }
string ExternType::DataTypeStr() const {
switch ( ext_type_ ) {
case PLAIN:
case NUMBER: return id_->Name();
case NUMBER:
case BOOLEAN: return id_->Name();
case POINTER: return string(id_->Name()) + " *";
default: ASSERT(0); return "";
}
@ -31,6 +32,8 @@ void ExternType::GenInitCode(Output* out_cc, Env* env) {
out_cc->println("%s = 0;", env->LValue(value_var()));
else if ( IsPointerType() )
out_cc->println("%s = nullptr;", env->LValue(value_var()));
else if ( IsBooleanType() )
out_cc->println("%s = false;", env->LValue(value_var()));
Type::GenInitCode(out_cc, env);
}

View file

@ -10,7 +10,7 @@
class ExternType : public Type {
public:
enum EXTType { PLAIN, NUMBER, POINTER };
enum EXTType { PLAIN, NUMBER, POINTER, BOOLEAN };
ExternType(const ID* id, EXTType ext_type) : Type(EXTERN), id_(id), ext_type_(ext_type) {}
bool DefineValueVar() const override;
@ -21,6 +21,7 @@ public:
string EvalMember(const ID* member_id) const override;
bool IsNumericType() const override { return ext_type_ == NUMBER; }
bool IsPointerType() const override { return ext_type_ == POINTER; }
bool IsBooleanType() const override { return ext_type_ == BOOLEAN; }
void GenInitCode(Output* out_cc, Env* env) override;

View file

@ -887,8 +887,8 @@ bool Type::CompatibleTypes(Type* type1, Type* type2) {
Type* Type::LookUpByID(ID* id) {
// 1. Is it a pre-defined type?
string name = id->Name();
if ( type_map_.find(name) != type_map_.end() ) {
return type_map_[name]->Clone();
if ( auto it = type_map_.find(name); it != type_map_.end() ) {
return it->second->Clone();
}
// 2. Is it a simple declared type?

View file

@ -110,7 +110,7 @@ public:
// is numeric or pointer)
string DataTypeConstRefStr() const {
string data_type = DataTypeStr();
if ( ! IsPointerType() && ! IsNumericType() )
if ( ! IsPointerType() && ! IsNumericType() && ! IsBooleanType() )
data_type += " const&";
return data_type;
}
@ -138,6 +138,7 @@ public:
virtual bool IsPointerType() const = 0;
virtual bool IsNumericType() const { return false; }
virtual bool IsBooleanType() const { return false; }
bool IsEmptyType() const;
////////////////////////////////////////