diff --git a/backends/p4tools/common/compiler/compiler_target.cpp b/backends/p4tools/common/compiler/compiler_target.cpp index 5f2f02e6879..d8c99e08b7b 100644 --- a/backends/p4tools/common/compiler/compiler_target.cpp +++ b/backends/p4tools/common/compiler/compiler_target.cpp @@ -1,5 +1,6 @@ #include "backends/p4tools/common/compiler/compiler_target.h" +#include #include #include #include @@ -27,7 +28,7 @@ std::vector *CompilerTarget::initCompiler(int argc, char **argv) { return get().initCompilerImpl(argc, argv); } -std::optional CompilerTarget::runCompiler() { +CompilerResultOrError CompilerTarget::runCompiler() { const auto *program = P4Tools::CompilerTarget::runParser(); if (program == nullptr) { return std::nullopt; @@ -36,7 +37,7 @@ std::optional CompilerTarget::runCompiler() { return runCompiler(program); } -std::optional CompilerTarget::runCompiler(const std::string &source) { +CompilerResultOrError CompilerTarget::runCompiler(const std::string &source) { const auto *program = P4::parseP4String(source, P4CContext::get().options().langVersion); if (program == nullptr) { return std::nullopt; @@ -45,12 +46,11 @@ std::optional CompilerTarget::runCompiler(const std::str return runCompiler(program); } -std::optional CompilerTarget::runCompiler(const IR::P4Program *program) { +CompilerResultOrError CompilerTarget::runCompiler(const IR::P4Program *program) { return get().runCompilerImpl(program); } -std::optional CompilerTarget::runCompilerImpl( - const IR::P4Program *program) const { +CompilerResultOrError CompilerTarget::runCompilerImpl(const IR::P4Program *program) const { const auto &self = get(); program = self.runFrontend(program); @@ -63,7 +63,7 @@ std::optional CompilerTarget::runCompilerImpl( return std::nullopt; } - return new CompilerResult(*program); + return *new CompilerResult(*program); } ICompileContext *CompilerTarget::makeContextImpl() const { diff --git a/backends/p4tools/common/compiler/compiler_target.h b/backends/p4tools/common/compiler/compiler_target.h index c0961f5f0f7..7d88f2cb7d4 100644 --- a/backends/p4tools/common/compiler/compiler_target.h +++ b/backends/p4tools/common/compiler/compiler_target.h @@ -31,6 +31,10 @@ class CompilerResult : public ICastable { [[nodiscard]] const IR::P4Program &getProgram() const; }; +/// P4Tools compilers may return an error instead of a compiler result. +/// This is a convenience definition for the return value. +using CompilerResultOrError = std::optional>; + /// Encapsulates the details of invoking the P4 compiler for a target device and architecture. class CompilerTarget : public Target { public: @@ -46,25 +50,25 @@ class CompilerTarget : public Target { /// program. /// /// @returns std::nullopt if an error occurs during compilation. - static std::optional runCompiler(); + static CompilerResultOrError runCompiler(); /// Runs the P4 compiler to produce an IR and other information for the given source code. /// /// @returns std::nullopt if an error occurs during compilation. - static std::optional runCompiler(const std::string &source); + static CompilerResultOrError runCompiler(const std::string &source); private: /// Runs the front and mid ends on the given parsed program. /// /// @returns std::nullopt if an error occurs during compilation. - static std::optional runCompiler(const IR::P4Program *); + static CompilerResultOrError runCompiler(const IR::P4Program *); protected: /// @see @makeContext. [[nodiscard]] virtual ICompileContext *makeContextImpl() const; /// @see runCompiler. - virtual std::optional runCompilerImpl(const IR::P4Program *) const; + virtual CompilerResultOrError runCompilerImpl(const IR::P4Program *) const; /// This implementation just forwards the given arguments to the compiler. /// diff --git a/backends/p4tools/common/p4ctool.h b/backends/p4tools/common/p4ctool.h index 67a267982f4..a66cacc6a9f 100644 --- a/backends/p4tools/common/p4ctool.h +++ b/backends/p4tools/common/p4ctool.h @@ -44,7 +44,7 @@ class AbstractP4cTool { if (!compilerResult.has_value()) { return EXIT_FAILURE; } - return mainImpl(*compilerResult.value()); + return mainImpl(compilerResult.value()); } }; diff --git a/backends/p4tools/modules/testgen/test/gtest_utils.cpp b/backends/p4tools/modules/testgen/test/gtest_utils.cpp index 1fe82f313da..eb1dd14bd37 100644 --- a/backends/p4tools/modules/testgen/test/gtest_utils.cpp +++ b/backends/p4tools/modules/testgen/test/gtest_utils.cpp @@ -31,7 +31,7 @@ std::optional P4ToolsTestCase::create( if (!compilerResults.has_value()) { return std::nullopt; } - return P4ToolsTestCase{compilerResults.value()->getProgram()}; + return P4ToolsTestCase(compilerResults.value().get().getProgram()); } const IR::P4Program &P4ToolsTestCase::getProgram() const { return program.get(); }