Skip to content

Commit

Permalink
Start using scoped enums in many places for readability.
Browse files Browse the repository at this point in the history
Instead of foo use UHDM_OBJECT_TYPE::foo if something is of
that type. This does not cover all places yet, but it will allow
us to make UHDM_OBJECT_TYPE an enum class at some point and improve
type-safety.
  • Loading branch information
hzeller committed Aug 30, 2023
1 parent 6aa3bd5 commit eb02596
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 493 deletions.
2 changes: 1 addition & 1 deletion scripts/UhdmListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def generate(models):
for classname in sorted(classnames):
Classname_ = classname[:1].upper() + classname[1:]

any_implementation.append(f' case uhdm{classname}: listen{Classname_}(static_cast<const {classname} *>(object)); break;')
any_implementation.append(f' case UHDM_OBJECT_TYPE::uhdm{classname}: listen{Classname_}(static_cast<const {classname} *>(object)); break;')

uhdm_enter_leave_declarations.append(f' virtual void enter{Classname_}(const {classname}* const object) {{}}')
uhdm_enter_leave_declarations.append(f' virtual void leave{Classname_}(const {classname}* const object) {{}}')
Expand Down
2 changes: 1 addition & 1 deletion scripts/VpiListener.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def generate(models):
for classname in sorted(classnames):
Classname_ = classname[:1].upper() + classname[1:]

any_implementation.append(f' case uhdm{classname}: listen{Classname_}(handle); break;')
any_implementation.append(f' case UHDM_OBJECT_TYPE::uhdm{classname}: listen{Classname_}(handle); break;')

enter_leave_declarations.append(f' virtual void enter{Classname_}(const {classname}* object, vpiHandle handle) {{}}')
enter_leave_declarations.append(f' virtual void leave{Classname_}(const {classname}* object, vpiHandle handle) {{}}')
Expand Down
12 changes: 6 additions & 6 deletions scripts/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def generate(models):
factory_data_members.append(f' {classname}Factory {classname}Maker;')
factory_function_declarations.append(f' {classname}* Make{Classname_}();')
factory_function_implementations.append(f'{classname}* Serializer::Make{Classname_}() {{ return Make<{classname}>(&{classname}Maker); }}')
factory_get_object.append(f' case uhdm{classname} /* = {type_map["uhdm" + classname]} */: return {classname}Maker.objects_[index];')
factory_erase_object.append(f' case uhdm{classname} /* = {type_map["uhdm" + classname]} */: return {classname}Maker.Erase(static_cast<const {classname}*>(p));')
factory_get_object.append(f' case UHDM_OBJECT_TYPE::uhdm{classname} /* = {type_map["uhdm" + classname]} */: return {classname}Maker.objects_[index];')
factory_erase_object.append(f' case UHDM_OBJECT_TYPE::uhdm{classname} /* = {type_map["uhdm" + classname]} */: return {classname}Maker.Erase(static_cast<const {classname}*>(p));')

save_ids.append(f' {classname}Maker.MapToIndex(idMap);')
save_objects.append(f' adapter.template operator()<{classname}, {Classname}>({classname}Maker, this, idMap, cap_root.initFactory{Classname}({classname}Maker.objects_.size()));')
Expand Down Expand Up @@ -84,7 +84,7 @@ def generate(models):
else:
saves_adapters.append(f' builder.set{Vpi}(obj->{Vpi_}());')
restore_adapters.append(f' obj->{Vpi_}(reader.get{Vpi}());')

elif key in ['class', 'obj_ref', 'class_ref', 'group_ref']:
name = value.get('name')
type = value.get('type')
Expand All @@ -109,7 +109,7 @@ def generate(models):
saves_adapters.append(f' if (obj->{Name_}() != nullptr) {{')
saves_adapters.append(f' ::ObjIndexType::Builder tmp = builder.get{Name}();')
saves_adapters.append(f' tmp.setIndex(GetId(obj->{Name_}(), idMap));')
saves_adapters.append(f' tmp.setType((obj->{Name_}())->UhdmType());')
saves_adapters.append(f' tmp.setType(static_cast<uint32_t>((obj->{Name_}())->UhdmType()));')
saves_adapters.append( ' }')

restore_adapters.append(f' obj->{Name_}(({type}*)serializer->GetObject(reader.get{Name}().getType(), reader.get{Name}().getIndex() - 1));')
Expand All @@ -135,7 +135,7 @@ def generate(models):
if key in ['class_ref', 'group_ref']:
saves_adapters.append(f' ::ObjIndexType::Builder tmp = {Name}s[i];')
saves_adapters.append(f' tmp.setIndex(GetId((*obj->{Name_}())[i], idMap));')
saves_adapters.append(f' tmp.setType(((BaseClass*)((*obj->{Name_}())[i]))->UhdmType());')
saves_adapters.append(f' tmp.setType(static_cast<uint32_t>(((BaseClass*)((*obj->{Name_}())[i]))->UhdmType()));')

restore_adapters.append(f' vect->emplace_back(({type}*)serializer->GetObject(reader.get{Name}()[i].getType(), reader.get{Name}()[i].getIndex() - 1));')
else:
Expand All @@ -156,7 +156,7 @@ def generate(models):
restore_adapters.append(' }')
restore_adapters.append('')

uhdm_name_map = [ f' case {name} /* = {id} */: return "{name[4:]}";' for name, id in uhdm_types_h.get_type_map(models).items() ]
uhdm_name_map = [ f' case UHDM_OBJECT_TYPE::{name} /* = {id} */: return "{name[4:]}";' for name, id in uhdm_types_h.get_type_map(models).items() ]

# Serializer.h
with open(config.get_template_filepath('Serializer.h'), 'rt') as strm:
Expand Down
32 changes: 16 additions & 16 deletions templates/BaseClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ void BaseClass::DeepCopy(BaseClass* clone, BaseClass* parent,
}

std::string BaseClass::ComputeFullName() const {
if ((UhdmType() == uhdmmodule_inst) && (VpiParent() != nullptr) &&
(VpiParent()->UhdmType() == uhdmmodule_inst)) {
if ((UhdmType() == UHDM_OBJECT_TYPE::uhdmmodule_inst) && (VpiParent() != nullptr) &&
(VpiParent()->UhdmType() == UHDM_OBJECT_TYPE::uhdmmodule_inst)) {
return std::string(VpiDefName());
}
std::vector<std::string_view> names;
Expand All @@ -113,31 +113,31 @@ std::string BaseClass::ComputeFullName() const {
UHDM_OBJECT_TYPE actual_parent_type = (actual_parent != nullptr)
? actual_parent->UhdmType()
: uhdmunsupported_stmt;
if (parent_type == uhdmdesign) break;
if ((parent_type == uhdmpackage) || (parent_type == uhdmclass_defn))
if (parent_type == UHDM_OBJECT_TYPE::uhdmdesign) break;
if ((parent_type == UHDM_OBJECT_TYPE::uhdmpackage) || (parent_type == UHDM_OBJECT_TYPE::uhdmclass_defn))
column = true;
std::string_view name =
parent->VpiName().empty() ? parent->VpiDefName() : parent->VpiName();
bool skip_name = (actual_parent_type == uhdmref_obj) ||
(parent_type == uhdmmethod_func_call) ||
(parent_type == uhdmmethod_task_call) ||
(parent_type == uhdmfunc_call) ||
(parent_type == uhdmtask_call) ||
(parent_type == uhdmsys_func_call) ||
(parent_type == uhdmsys_task_call);
bool skip_name = (actual_parent_type == UHDM_OBJECT_TYPE::uhdmref_obj) ||
(parent_type == UHDM_OBJECT_TYPE::uhdmmethod_func_call) ||
(parent_type == UHDM_OBJECT_TYPE::uhdmmethod_task_call) ||
(parent_type == UHDM_OBJECT_TYPE::uhdmfunc_call) ||
(parent_type == UHDM_OBJECT_TYPE::uhdmtask_call) ||
(parent_type == UHDM_OBJECT_TYPE::uhdmsys_func_call) ||
(parent_type == UHDM_OBJECT_TYPE::uhdmsys_task_call);
if (child != nullptr) {
UHDM_OBJECT_TYPE child_type = child->UhdmType();
if ((child_type == uhdmbit_select) && (parent_type == uhdmport)) {
if ((child_type == UHDM_OBJECT_TYPE::uhdmbit_select) && (parent_type == UHDM_OBJECT_TYPE::uhdmport)) {
skip_name = true;
}
if ((child_type == uhdmref_obj) && (parent_type == uhdmbit_select)) {
if ((child_type == UHDM_OBJECT_TYPE::uhdmref_obj) && (parent_type == UHDM_OBJECT_TYPE::uhdmbit_select)) {
skip_name = true;
}
if ((child_type == uhdmref_obj) &&
(parent_type == uhdmindexed_part_select)) {
if ((child_type == UHDM_OBJECT_TYPE::uhdmref_obj) &&
(parent_type == UHDM_OBJECT_TYPE::uhdmindexed_part_select)) {
skip_name = true;
}
if ((child_type == uhdmref_obj) && (parent_type == uhdmhier_path)) {
if ((child_type == UHDM_OBJECT_TYPE::uhdmref_obj) && (parent_type == UHDM_OBJECT_TYPE::uhdmhier_path)) {
skip_name = true;
}
}
Expand Down
52 changes: 26 additions & 26 deletions templates/ElaboratorListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void propagateParamAssign(param_assign* pass, const any* target) {
UHDM_OBJECT_TYPE targetType = target->UhdmType();
Serializer& s = *pass->GetSerializer();
switch (targetType) {
case uhdmclass_defn: {
case UHDM_OBJECT_TYPE::uhdmclass_defn: {
class_defn* defn = (class_defn*)target;
const any* lhs = pass->Lhs();
const std::string_view name = lhs->VpiName();
Expand Down Expand Up @@ -65,12 +65,12 @@ static void propagateParamAssign(param_assign* pass, const any* target) {
}
break;
}
case uhdmclass_var: {
case UHDM_OBJECT_TYPE::uhdmclass_var: {
class_var* var = (class_var*)target;
propagateParamAssign(pass, var->Typespec());
break;
}
case uhdmclass_typespec: {
case UHDM_OBJECT_TYPE::uhdmclass_typespec: {
class_typespec* defn = (class_typespec*)target;
const any* lhs = pass->Lhs();
const std::string_view name = lhs->VpiName();
Expand Down Expand Up @@ -102,7 +102,7 @@ static void propagateParamAssign(param_assign* pass, const any* target) {

void ElaboratorListener::enterVariables(const variables* object,
vpiHandle handle) {
if (object->UhdmType() == uhdmclass_var) {
if (object->UhdmType() == UHDM_OBJECT_TYPE::uhdmclass_var) {
if (!inHierarchy_)
return; // Only do class var propagation while in elaboration
const class_var* cv = (class_var*)object;
Expand Down Expand Up @@ -170,7 +170,7 @@ void ElaboratorListener::enterModule_inst(const module_inst* object,
if (object->Variables()) {
for (variables* var : *object->Variables()) {
netMap.emplace(var->VpiName(), var);
if (var->UhdmType() == uhdmenum_var) {
if (var->UhdmType() == UHDM_OBJECT_TYPE::uhdmenum_var) {
enum_var* evar = (enum_var*)var;
enum_typespec* etps = evar->Typespec<enum_typespec>();
for (auto c : *etps->Enum_consts()) {
Expand Down Expand Up @@ -244,7 +244,7 @@ void ElaboratorListener::enterModule_inst(const module_inst* object,

if (object->Typespecs()) {
for (typespec* tps : *object->Typespecs()) {
if (tps->UhdmType() == uhdmenum_typespec) {
if (tps->UhdmType() == UHDM_OBJECT_TYPE::uhdmenum_typespec) {
enum_typespec* etps = (enum_typespec*)tps;
for (auto c : *etps->Enum_consts()) {
paramMap.emplace(c->VpiName(), c);
Expand Down Expand Up @@ -283,7 +283,7 @@ void ElaboratorListener::enterModule_inst(const module_inst* object,
module_inst* defMod = (module_inst*)comp;
if (defMod->Typespecs()) {
for (typespec* tps : *defMod->Typespecs()) {
if (tps->UhdmType() == uhdmenum_typespec) {
if (tps->UhdmType() == UHDM_OBJECT_TYPE::uhdmenum_typespec) {
enum_typespec* etps = (enum_typespec*)tps;
for (enum_const* econst : *etps->Enum_consts()) {
paramMap.emplace(econst->VpiName(), econst);
Expand Down Expand Up @@ -405,7 +405,7 @@ void ElaboratorListener::enterPackage(const package* object, vpiHandle handle) {
if (object->Variables()) {
for (variables* var : *object->Variables()) {
netMap.emplace(var->VpiName(), var);
if (var->UhdmType() == uhdmenum_var) {
if (var->UhdmType() == UHDM_OBJECT_TYPE::uhdmenum_var) {
enum_var* evar = (enum_var*)var;
enum_typespec* etps = (enum_typespec*)evar->Typespec();
for (auto c : *etps->Enum_consts()) {
Expand Down Expand Up @@ -474,7 +474,7 @@ void ElaboratorListener::enterClass_defn(const class_defn* object,
if (defn->Variables()) {
for (variables* var : *defn->Variables()) {
varMap.emplace(var->VpiName(), var);
if (var->UhdmType() == uhdmenum_var) {
if (var->UhdmType() == UHDM_OBJECT_TYPE::uhdmenum_var) {
enum_var* evar = (enum_var*)var;
enum_typespec* etps = (enum_typespec*)evar->Typespec();
for (auto c : *etps->Enum_consts()) {
Expand Down Expand Up @@ -537,21 +537,21 @@ void ElaboratorListener::bindScheduledTaskFunc() {
for (auto& call_prefix : scheduledTfCallBinding_) {
tf_call* call = call_prefix.first;
const class_var* prefix = call_prefix.second;
if (call->UhdmType() == uhdmfunc_call) {
if (call->UhdmType() == UHDM_OBJECT_TYPE::uhdmfunc_call) {
if (function* f =
any_cast<function*>(bindTaskFunc(call->VpiName(), prefix))) {
((func_call*)call)->Function(f);
}
} else if (call->UhdmType() == uhdmtask_call) {
} else if (call->UhdmType() == UHDM_OBJECT_TYPE::uhdmtask_call) {
if (task* f = any_cast<task*>(bindTaskFunc(call->VpiName(), prefix))) {
((task_call*)call)->Task(f);
}
} else if (call->UhdmType() == uhdmmethod_func_call) {
} else if (call->UhdmType() == UHDM_OBJECT_TYPE::uhdmmethod_func_call) {
if (function* f =
any_cast<function*>(bindTaskFunc(call->VpiName(), prefix))) {
((method_func_call*)call)->Function(f);
}
} else if (call->UhdmType() == uhdmmethod_task_call) {
} else if (call->UhdmType() == UHDM_OBJECT_TYPE::uhdmmethod_task_call) {
if (task* f = any_cast<task*>(bindTaskFunc(call->VpiName(), prefix))) {
((method_task_call*)call)->Task(f);
}
Expand Down Expand Up @@ -604,7 +604,7 @@ void ElaboratorListener::enterInterface_inst(const interface_inst* object,
if (object->Variables()) {
for (variables* var : *object->Variables()) {
netMap.emplace(var->VpiName(), var);
if (var->UhdmType() == uhdmenum_var) {
if (var->UhdmType() == UHDM_OBJECT_TYPE::uhdmenum_var) {
enum_var* evar = (enum_var*)var;
enum_typespec* etps = (enum_typespec*)evar->Typespec();
for (auto c : *etps->Enum_consts()) {
Expand Down Expand Up @@ -653,9 +653,9 @@ void ElaboratorListener::enterInterface_inst(const interface_inst* object,
for (ports* port : *object->Ports()) {
if (const ref_obj* ro = port->Low_conn<ref_obj>()) {
if (const any* actual = ro->Actual_group()) {
if (actual->UhdmType() == uhdminterface_inst) {
if (actual->UhdmType() == UHDM_OBJECT_TYPE::uhdminterface_inst) {
netMap.emplace(port->VpiName(), actual);
} else if (actual->UhdmType() == uhdmmodport) {
} else if (actual->UhdmType() == UHDM_OBJECT_TYPE::uhdmmodport) {
// If the interface of the modport is not yet in the map
netMap.emplace(port->VpiName(), actual);
}
Expand All @@ -682,7 +682,7 @@ void ElaboratorListener::enterInterface_inst(const interface_inst* object,
module_inst* defMod = (module_inst*)comp;
if (defMod->Typespecs()) {
for (typespec* tps : *defMod->Typespecs()) {
if (tps->UhdmType() == uhdmenum_typespec) {
if (tps->UhdmType() == UHDM_OBJECT_TYPE::uhdmenum_typespec) {
enum_typespec* etps = (enum_typespec*)tps;
for (enum_const* econst : *etps->Enum_consts()) {
paramMap.emplace(econst->VpiName(), econst);
Expand Down Expand Up @@ -852,7 +852,7 @@ any* ElaboratorListener::bindTaskFunc(std::string_view name,
}
if (prefix) {
const typespec* tps = prefix->Typespec();
if (tps && tps->UhdmType() == uhdmclass_typespec) {
if (tps && tps->UhdmType() == UHDM_OBJECT_TYPE::uhdmclass_typespec) {
const class_defn* defn = ((const class_typespec*)tps)->Class_defn();
while (defn) {
if (defn->Task_funcs()) {
Expand Down Expand Up @@ -881,14 +881,14 @@ bool ElaboratorListener::isFunctionCall(std::string_view name,
const ComponentMap& funcMap = std::get<3>(*i);
ComponentMap::const_iterator funcItr = funcMap.find(name);
if (funcItr != funcMap.end()) {
return (funcItr->second->UhdmType() == uhdmfunction);
return (funcItr->second->UhdmType() == UHDM_OBJECT_TYPE::uhdmfunction);
}
}
if (prefix) {
if (const ref_obj* ref = any_cast<const ref_obj*>(prefix)) {
if (const class_var* vprefix = ref->Actual_group<class_var>()) {
if (const any* func = bindTaskFunc(name, vprefix)) {
return (func->UhdmType() == uhdmfunction);
return (func->UhdmType() == UHDM_OBJECT_TYPE::uhdmfunction);
}
}
}
Expand All @@ -903,14 +903,14 @@ bool ElaboratorListener::isTaskCall(std::string_view name,
const ComponentMap& funcMap = std::get<3>(*i);
ComponentMap::const_iterator funcItr = funcMap.find(name);
if (funcItr != funcMap.end()) {
return (funcItr->second->UhdmType() == uhdmtask);
return (funcItr->second->UhdmType() == UHDM_OBJECT_TYPE::uhdmtask);
}
}
if (prefix) {
if (const ref_obj* ref = any_cast<const ref_obj*>(prefix)) {
if (const class_var* vprefix = ref->Actual_group<class_var>()) {
if (const any* task = bindTaskFunc(name, vprefix)) {
return (task->UhdmType() == uhdmtask);
return (task->UhdmType() == UHDM_OBJECT_TYPE::uhdmtask);
}
}
}
Expand All @@ -935,7 +935,7 @@ void ElaboratorListener::enterTask_func(const task_func* object,
varMap.emplace(object->VpiName(), object->Return());

if (const any* parent = object->VpiParent()) {
if (parent->UhdmType() == uhdmclass_defn) {
if (parent->UhdmType() == UHDM_OBJECT_TYPE::uhdmclass_defn) {
const class_defn* defn = (const class_defn*)parent;
while (defn) {
if (defn->Variables()) {
Expand Down Expand Up @@ -983,10 +983,10 @@ void ElaboratorListener::enterFor_stmt(const for_stmt* object,
}
if (object->VpiForInitStmts()) {
for (any* stmt : *object->VpiForInitStmts()) {
if (stmt->UhdmType() == uhdmassign_stmt) {
if (stmt->UhdmType() == UHDM_OBJECT_TYPE::uhdmassign_stmt) {
assign_stmt* astmt = (assign_stmt*)stmt;
const any* lhs = astmt->Lhs();
if (lhs->UhdmType() != uhdmref_var) {
if (lhs->UhdmType() != UHDM_OBJECT_TYPE::uhdmref_var) {
varMap.emplace(lhs->VpiName(), lhs);
}
}
Expand Down Expand Up @@ -1186,7 +1186,7 @@ void ElaboratorListener::enterGen_scope(const gen_scope* object,
if (object->Variables()) {
for (variables* var : *object->Variables()) {
netMap.emplace(var->VpiName(), var);
if (var->UhdmType() == uhdmenum_var) {
if (var->UhdmType() == UHDM_OBJECT_TYPE::uhdmenum_var) {
enum_var* evar = (enum_var*)var;
enum_typespec* etps = (enum_typespec*)evar->Typespec();
for (auto c : *etps->Enum_consts()) {
Expand Down
Loading

0 comments on commit eb02596

Please sign in to comment.