diff --git a/backends/p4tools/common/lib/util.cpp b/backends/p4tools/common/lib/util.cpp index a507d26e1f..ad0b16dc4f 100644 --- a/backends/p4tools/common/lib/util.cpp +++ b/backends/p4tools/common/lib/util.cpp @@ -104,4 +104,68 @@ const IR::MethodCallExpression *Utils::generateInternalMethodCall( args); } +std::vector argumentsToTypeDeclarations( + const IR::IGeneralNamespace *ns, const IR::Vector *inputArgs) { + std::vector resultDecls; + for (const auto *arg : *inputArgs) { + const auto *expr = arg->expression; + + const IR::Type_Declaration *declType = nullptr; + + if (const auto *ctorCall = expr->to()) { + const auto *constructedTypeName = ctorCall->constructedType->checkedTo(); + + // Find the corresponding type declaration in the top-level namespace. + declType = + findProgramDecl(ns, constructedTypeName->path)->checkedTo(); + } else if (const auto *pathExpr = expr->to()) { + // Look up the path expression in the top-level namespace and expect to find a + // declaration instance. + const auto *declInstance = + findProgramDecl(ns, pathExpr->path)->checkedTo(); + declType = declInstance->type->checkedTo(); + } else { + BUG("Unexpected main-declaration argument node type: %1%", expr->node_type_name()); + } + + // The constructor's parameter list should be empty, since the compiler should have + // substituted the constructor arguments for us. + if (const auto *iApply = declType->to()) { + const IR::ParameterList *ctorParams = iApply->getConstructorParameters(); + BUG_CHECK(ctorParams->empty(), "Compiler did not eliminate constructor parameters: %1%", + ctorParams); + } else { + BUG("Does not instantiate an IContainer: %1%", expr); + } + + resultDecls.emplace_back(declType); + } + return resultDecls; +} + +const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path) { + auto name = path->name.name; + const auto *decls = ns->getDeclsByName(name)->toVector(); + if (!decls->empty()) { + // TODO: Figure out what to do with multiple results. Maybe return all of them and + // let the caller sort it out? + BUG_CHECK(decls->size() == 1, "Handling of overloaded names not implemented"); + return decls->at(0); + } + BUG("Variable %1% not found in the available namespaces.", path); +} + +const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, + const IR::PathExpression *pathExpr) { + return findProgramDecl(ns, pathExpr->path); +} + +const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns, + const IR::Type_Name *type) { + const auto *path = type->path; + const auto *decl = findProgramDecl(ns, path)->to(); + BUG_CHECK(decl, "Not a type: %1%", path); + return decl; +} + } // namespace P4Tools diff --git a/backends/p4tools/common/lib/util.h b/backends/p4tools/common/lib/util.h index 1105f11ed1..78b2a3d9ee 100644 --- a/backends/p4tools/common/lib/util.h +++ b/backends/p4tools/common/lib/util.h @@ -124,6 +124,23 @@ class Utils { } }; +/// Converts the list of arguments @inputArgs to a list of type declarations. Any names appearing in +/// the arguments are resolved with @ns. +/// This is mainly useful for inspecting package instances. +std::vector argumentsToTypeDeclarations( + const IR::IGeneralNamespace *ns, const IR::Vector *inputArgs); + +/// Looks up a declaration from a path. A BUG occurs if no declaration is found. +const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, const IR::Path *path); + +/// Looks up a declaration from a path expression. A BUG occurs if no declaration is found. +const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, + const IR::PathExpression *pathExpr); + +/// Resolves a Type_Name in the top-level namespace. +const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns, + const IR::Type_Name *type); + } // namespace P4Tools #endif /* BACKENDS_P4TOOLS_COMMON_LIB_UTIL_H_ */ diff --git a/backends/p4tools/modules/testgen/CMakeLists.txt b/backends/p4tools/modules/testgen/CMakeLists.txt index ab3c892fc2..f5eae4284e 100644 --- a/backends/p4tools/modules/testgen/CMakeLists.txt +++ b/backends/p4tools/modules/testgen/CMakeLists.txt @@ -39,7 +39,6 @@ set( lib/test_backend.cpp lib/test_framework.cpp lib/test_spec.cpp - lib/util.cpp ) # GTest source files for p4testgen. diff --git a/backends/p4tools/modules/testgen/core/program_info.cpp b/backends/p4tools/modules/testgen/core/program_info.cpp index 49d1a7b96d..5f60cad09f 100644 --- a/backends/p4tools/modules/testgen/core/program_info.cpp +++ b/backends/p4tools/modules/testgen/core/program_info.cpp @@ -33,36 +33,6 @@ ProgramInfo::ProgramInfo(const IR::P4Program *program) : concolicMethodImpls({}) coverableNodes.insert(coveredNodes.begin(), coveredNodes.end()); } -/* ============================================================================================= - * Namespaces and declarations - * ============================================================================================= */ - -const IR::IDeclaration *ProgramInfo::findProgramDecl(const IR::IGeneralNamespace *ns, - const IR::Path *path) { - auto name = path->name.name; - const auto *decls = ns->getDeclsByName(name)->toVector(); - if (!decls->empty()) { - // TODO: Figure out what to do with multiple results. Maybe return all of them and - // let the caller sort it out? - BUG_CHECK(decls->size() == 1, "Handling of overloaded names not implemented"); - return decls->at(0); - } - BUG("Variable %1% not found in the available namespaces.", path); -} - -const IR::IDeclaration *ProgramInfo::findProgramDecl(const IR::IGeneralNamespace *ns, - const IR::PathExpression *pathExpr) { - return findProgramDecl(ns, pathExpr->path); -} - -const IR::Type_Declaration *ProgramInfo::resolveProgramType(const IR::IGeneralNamespace *ns, - const IR::Type_Name *type) { - const auto *path = type->path; - const auto *decl = findProgramDecl(ns, path)->to(); - BUG_CHECK(decl, "Not a type: %1%", path); - return decl; -} - const IR::Expression *ProgramInfo::createTargetUninitialized(const IR::Type *type, bool forceTaint) const { if (forceTaint) { diff --git a/backends/p4tools/modules/testgen/core/program_info.h b/backends/p4tools/modules/testgen/core/program_info.h index 41e68debb1..3a472f8569 100644 --- a/backends/p4tools/modules/testgen/core/program_info.h +++ b/backends/p4tools/modules/testgen/core/program_info.h @@ -87,18 +87,6 @@ class ProgramInfo : public ICastable { // @returns the width of the parser error for this specific target. [[nodiscard]] virtual const IR::Type_Bits *getParserErrorType() const = 0; - /// Looks up a declaration from a path. A BUG occurs if no declaration is found. - static const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, - const IR::Path *path); - - /// Looks up a declaration from a path expression. A BUG occurs if no declaration is found. - static const IR::IDeclaration *findProgramDecl(const IR::IGeneralNamespace *ns, - const IR::PathExpression *pathExpr); - - /// Resolves a Type_Name in the top-level namespace. - static const IR::Type_Declaration *resolveProgramType(const IR::IGeneralNamespace *ns, - const IR::Type_Name *type); - /// @returns the canonical name of the program block that is passed in. /// Throws a BUG, if the name can not be found. [[nodiscard]] cstring getCanonicalBlockName(cstring programBlockName) const; diff --git a/backends/p4tools/modules/testgen/lib/util.cpp b/backends/p4tools/modules/testgen/lib/util.cpp deleted file mode 100644 index fe01dc3479..0000000000 --- a/backends/p4tools/modules/testgen/lib/util.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "backends/p4tools/modules/testgen/lib/util.h" - -#include "backends/p4tools/modules/testgen/core/program_info.h" - -namespace P4Tools::P4Testgen { - -std::vector argumentsToTypeDeclarations( - const IR::IGeneralNamespace *ns, const IR::Vector *inputArgs) { - std::vector resultDecls; - for (const auto *arg : *inputArgs) { - const auto *expr = arg->expression; - - const IR::Type_Declaration *declType = nullptr; - - if (const auto *ctorCall = expr->to()) { - const auto *constructedTypeName = ctorCall->constructedType->checkedTo(); - - // Find the corresponding type declaration in the top-level namespace. - declType = ProgramInfo::findProgramDecl(ns, constructedTypeName->path) - ->checkedTo(); - } else if (const auto *pathExpr = expr->to()) { - // Look up the path expression in the top-level namespace and expect to find a - // declaration instance. - const auto *declInstance = ProgramInfo::findProgramDecl(ns, pathExpr->path) - ->checkedTo(); - declType = declInstance->type->checkedTo(); - } else { - BUG("Unexpected main-declaration argument node type: %1%", expr->node_type_name()); - } - - // The constructor's parameter list should be empty, since the compiler should have - // substituted the constructor arguments for us. - if (const auto *iApply = declType->to()) { - const IR::ParameterList *ctorParams = iApply->getConstructorParameters(); - BUG_CHECK(ctorParams->empty(), "Compiler did not eliminate constructor parameters: %1%", - ctorParams); - } else { - BUG("Does not instantiate an IContainer: %1%", expr); - } - - resultDecls.emplace_back(declType); - } - return resultDecls; -} - -} // namespace P4Tools::P4Testgen diff --git a/backends/p4tools/modules/testgen/lib/util.h b/backends/p4tools/modules/testgen/lib/util.h deleted file mode 100644 index 3bfbed8f19..0000000000 --- a/backends/p4tools/modules/testgen/lib/util.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_UTIL_H_ -#define BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_UTIL_H_ - -#include - -#include "ir/ir.h" -#include "lib/cstring.h" - -namespace P4Tools::P4Testgen { - -/// Converts the list of arguments @inputArgs to a list of type declarations. Any names appearing in -/// the arguments are resolved with @ns. -/// This is mainly useful for inspecting package instances. -std::vector argumentsToTypeDeclarations( - const IR::IGeneralNamespace *ns, const IR::Vector *inputArgs); - -} // namespace P4Tools::P4Testgen - -#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_UTIL_H_ */ diff --git a/backends/p4tools/modules/testgen/targets/bmv2/target.cpp b/backends/p4tools/modules/testgen/targets/bmv2/target.cpp index c8bc0268fe..cdfcf5e776 100644 --- a/backends/p4tools/modules/testgen/targets/bmv2/target.cpp +++ b/backends/p4tools/modules/testgen/targets/bmv2/target.cpp @@ -10,11 +10,11 @@ #include "lib/exceptions.h" #include "lib/ordered_map.h" +#include "backends/p4tools/common/lib/util.h" #include "backends/p4tools/modules/testgen/core/program_info.h" #include "backends/p4tools/modules/testgen/core/symbolic_executor/symbolic_executor.h" #include "backends/p4tools/modules/testgen/core/target.h" #include "backends/p4tools/modules/testgen/lib/execution_state.h" -#include "backends/p4tools/modules/testgen/lib/util.h" #include "backends/p4tools/modules/testgen/targets/bmv2/cmd_stepper.h" #include "backends/p4tools/modules/testgen/targets/bmv2/constants.h" #include "backends/p4tools/modules/testgen/targets/bmv2/expr_stepper.h" diff --git a/backends/p4tools/modules/testgen/targets/ebpf/target.cpp b/backends/p4tools/modules/testgen/targets/ebpf/target.cpp index af90524971..12aca9156f 100644 --- a/backends/p4tools/modules/testgen/targets/ebpf/target.cpp +++ b/backends/p4tools/modules/testgen/targets/ebpf/target.cpp @@ -11,11 +11,11 @@ #include "lib/exceptions.h" #include "lib/ordered_map.h" +#include "backends/p4tools/common/lib/util.h" #include "backends/p4tools/modules/testgen/core/program_info.h" #include "backends/p4tools/modules/testgen/core/symbolic_executor/symbolic_executor.h" #include "backends/p4tools/modules/testgen/core/target.h" #include "backends/p4tools/modules/testgen/lib/execution_state.h" -#include "backends/p4tools/modules/testgen/lib/util.h" #include "backends/p4tools/modules/testgen/options.h" #include "backends/p4tools/modules/testgen/targets/ebpf/cmd_stepper.h" #include "backends/p4tools/modules/testgen/targets/ebpf/expr_stepper.h" diff --git a/backends/p4tools/modules/testgen/targets/pna/shared_program_info.cpp b/backends/p4tools/modules/testgen/targets/pna/shared_program_info.cpp index 50cca28146..cc5f671f96 100644 --- a/backends/p4tools/modules/testgen/targets/pna/shared_program_info.cpp +++ b/backends/p4tools/modules/testgen/targets/pna/shared_program_info.cpp @@ -9,6 +9,7 @@ #include "lib/cstring.h" #include "lib/null.h" +#include "backends/p4tools/common/lib/util.h" #include "backends/p4tools/modules/testgen/core/program_info.h" #include "backends/p4tools/modules/testgen/core/target.h" #include "backends/p4tools/modules/testgen/lib/concolic.h" diff --git a/backends/p4tools/modules/testgen/targets/pna/target.cpp b/backends/p4tools/modules/testgen/targets/pna/target.cpp index dda108c16a..e3574b6c61 100644 --- a/backends/p4tools/modules/testgen/targets/pna/target.cpp +++ b/backends/p4tools/modules/testgen/targets/pna/target.cpp @@ -9,11 +9,11 @@ #include "lib/exceptions.h" #include "lib/ordered_map.h" +#include "backends/p4tools/common/lib/util.h" #include "backends/p4tools/modules/testgen/core/program_info.h" #include "backends/p4tools/modules/testgen/core/symbolic_executor/symbolic_executor.h" #include "backends/p4tools/modules/testgen/core/target.h" #include "backends/p4tools/modules/testgen/lib/execution_state.h" -#include "backends/p4tools/modules/testgen/lib/util.h" #include "backends/p4tools/modules/testgen/targets/pna/dpdk/cmd_stepper.h" #include "backends/p4tools/modules/testgen/targets/pna/dpdk/expr_stepper.h" #include "backends/p4tools/modules/testgen/targets/pna/dpdk/program_info.h"