P4C
The P4 Compiler
Loading...
Searching...
No Matches
parseInput.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_COMMON_PARSEINPUT_H_
9#define FRONTENDS_COMMON_PARSEINPUT_H_
10
11#include "frontends/common/options.h"
12#include "frontends/common/parser_options.h"
13#include "frontends/p4-14/fromv1.0/converters.h"
14#include "frontends/parsers/parserDriver.h"
15#include "lib/error.h"
16
17namespace P4::IR {
18class P4Program;
19} // namespace P4::IR
20
21namespace P4 {
22
23template <typename Input, typename C = P4V1::Converter>
24static const IR::P4Program *parseV1Program(Input stream, std::string_view sourceFile,
25 unsigned sourceLine,
26 std::optional<DebugHook> debugHook = std::nullopt) {
27 // We load the model before parsing the input file, so that the SourceInfo
28 // in the model comes first.
29 C converter;
30 if (debugHook) converter.addDebugHook(*debugHook, true);
31 converter.loadModel();
32
33 // Parse.
34 const IR::Node *v1 = V1::V1ParserDriver::parse(stream, sourceFile, sourceLine);
35 if (::P4::errorCount() > 0 || v1 == nullptr) return nullptr;
36
37 // Convert to P4-16.
38 if (Log::verbose()) std::cerr << "Converting to P4-16" << std::endl;
39 v1 = v1->apply(converter);
40 if (::P4::errorCount() > 0 || v1 == nullptr) return nullptr;
41 BUG_CHECK(v1->is<IR::P4Program>(), "Conversion returned %1%", v1);
42 return v1->to<IR::P4Program>();
43}
44
53template <typename C = P4V1::Converter>
54const IR::P4Program *parseP4File(const ParserOptions &options) {
55 BUG_CHECK(&options == &P4CContext::get().options(),
56 "Parsing using options that don't match the current "
57 "compiler context");
58
59 const IR::P4Program *result = nullptr;
60 if (options.doNotPreprocess) {
61 auto *file = fopen(options.file.c_str(), "r");
62 if (file == nullptr) {
63 ::P4::error(ErrorType::ERR_NOT_FOUND, "%1%: No such file or directory.", options.file);
64 return nullptr;
65 }
66 result = options.isv1() ? parseV1Program<FILE *, C>(file, options.file.string(), 1,
67 options.getDebugHook())
68 : P4ParserDriver::parse(file, options.file.string());
69 fclose(file);
70 } else {
71 auto preprocessorResult = options.preprocess();
72 if (!preprocessorResult.has_value()) {
73 return nullptr;
74 }
75 // Need to assign file here because the parser requires an lvalue.
76 result =
77 options.isv1()
78 ? parseV1Program<FILE *, C>(preprocessorResult.value().get(), options.file.string(),
79 1, options.getDebugHook())
80 : P4ParserDriver::parse(preprocessorResult.value().get(), options.file.string());
81 }
82
83 if (::P4::errorCount() > 0) {
84 ::P4::error(ErrorType::ERR_OVERLIMIT, "%1% errors encountered, aborting compilation",
86 return nullptr;
87 }
88 BUG_CHECK(result != nullptr, "Parsing failed, but we didn't report an error");
89 return result;
90}
91
102const IR::P4Program *parseP4String(const char *sourceFile, unsigned sourceLine,
103 const std::string &input,
104 CompilerOptions::FrontendVersion version);
105const IR::P4Program *parseP4String(const std::string &input,
106 CompilerOptions::FrontendVersion version);
107
108} // namespace P4
109
110#endif /* FRONTENDS_COMMON_PARSEINPUT_H_ */
Definition node.h:44
static P4CContext & get()
Definition parser_options.cpp:523
static const IR::P4Program * parse(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:138
Definition parser_options.h:40
static const IR::V1Program * parse(std::istream &in, std::string_view sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:315
Definition constantParsing.h:13
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
const IR::P4Program * parseP4String(const char *sourceFile, unsigned sourceLine, const std::string &input, CompilerOptions::FrontendVersion version)
Definition parseInput.cpp:17
const IR::P4Program * parseP4File(const ParserOptions &options)
Definition parseInput.h:54
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:49
unsigned errorCount()
Definition lib/error.h:25