Skip to content

Commit

Permalink
Use a typedef for the compiler result.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Jan 12, 2024
1 parent be3c6bd commit e9a24db
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
12 changes: 6 additions & 6 deletions backends/p4tools/common/compiler/compiler_target.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "backends/p4tools/common/compiler/compiler_target.h"

#include <functional>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -27,7 +28,7 @@ std::vector<const char *> *CompilerTarget::initCompiler(int argc, char **argv) {
return get().initCompilerImpl(argc, argv);
}

std::optional<const CompilerResult *> CompilerTarget::runCompiler() {
CompilerResultOrError CompilerTarget::runCompiler() {
const auto *program = P4Tools::CompilerTarget::runParser();
if (program == nullptr) {
return std::nullopt;
Expand All @@ -36,7 +37,7 @@ std::optional<const CompilerResult *> CompilerTarget::runCompiler() {
return runCompiler(program);
}

std::optional<const CompilerResult *> 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;
Expand All @@ -45,12 +46,11 @@ std::optional<const CompilerResult *> CompilerTarget::runCompiler(const std::str
return runCompiler(program);
}

std::optional<const CompilerResult *> CompilerTarget::runCompiler(const IR::P4Program *program) {
CompilerResultOrError CompilerTarget::runCompiler(const IR::P4Program *program) {
return get().runCompilerImpl(program);
}

std::optional<const CompilerResult *> CompilerTarget::runCompilerImpl(
const IR::P4Program *program) const {
CompilerResultOrError CompilerTarget::runCompilerImpl(const IR::P4Program *program) const {
const auto &self = get();

program = self.runFrontend(program);
Expand All @@ -63,7 +63,7 @@ std::optional<const CompilerResult *> CompilerTarget::runCompilerImpl(
return std::nullopt;
}

return new CompilerResult(*program);
return *new CompilerResult(*program);
}

ICompileContext *CompilerTarget::makeContextImpl() const {
Expand Down
12 changes: 8 additions & 4 deletions backends/p4tools/common/compiler/compiler_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::reference_wrapper<const CompilerResult>>;

/// Encapsulates the details of invoking the P4 compiler for a target device and architecture.
class CompilerTarget : public Target {
public:
Expand All @@ -46,25 +50,25 @@ class CompilerTarget : public Target {
/// program.
///
/// @returns std::nullopt if an error occurs during compilation.
static std::optional<const CompilerResult *> 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<const CompilerResult *> 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<const CompilerResult *> runCompiler(const IR::P4Program *);
static CompilerResultOrError runCompiler(const IR::P4Program *);

protected:
/// @see @makeContext.
[[nodiscard]] virtual ICompileContext *makeContextImpl() const;

/// @see runCompiler.
virtual std::optional<const CompilerResult *> runCompilerImpl(const IR::P4Program *) const;
virtual CompilerResultOrError runCompilerImpl(const IR::P4Program *) const;

/// This implementation just forwards the given arguments to the compiler.
///
Expand Down
2 changes: 1 addition & 1 deletion backends/p4tools/common/p4ctool.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class AbstractP4cTool {
if (!compilerResult.has_value()) {
return EXIT_FAILURE;
}
return mainImpl(*compilerResult.value());
return mainImpl(compilerResult.value());
}
};

Expand Down
2 changes: 1 addition & 1 deletion backends/p4tools/modules/testgen/test/gtest_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::optional<const P4ToolsTestCase> 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(); }
Expand Down

0 comments on commit e9a24db

Please sign in to comment.