P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
parseInput.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef FRONTENDS_COMMON_PARSEINPUT_H_
18#define FRONTENDS_COMMON_PARSEINPUT_H_
19
20#include "frontends/common/options.h"
21#include "frontends/p4/fromv1.0/converters.h"
22#include "frontends/p4/frontend.h"
23#include "frontends/parsers/parserDriver.h"
24#include "lib/error.h"
25#include "lib/source_file.h"
26
27namespace IR {
28class P4Program;
29} // namespace IR
30
31namespace P4 {
32
33template <typename Input, typename C = P4V1::Converter>
34static const IR::P4Program *parseV1Program(Input &stream, const char *sourceFile,
35 unsigned sourceLine,
36 std::optional<DebugHook> debugHook = std::nullopt) {
37 // We load the model before parsing the input file, so that the SourceInfo
38 // in the model comes first.
39 C converter;
40 if (debugHook) converter.addDebugHook(*debugHook, true);
41 converter.loadModel();
42
43 // Parse.
44 const IR::Node *v1 = V1::V1ParserDriver::parse(stream, sourceFile, sourceLine);
45 if (::errorCount() > 0 || v1 == nullptr) return nullptr;
46
47 // Convert to P4-16.
48 if (Log::verbose()) std::cerr << "Converting to P4-16" << std::endl;
49 v1 = v1->apply(converter);
50 if (::errorCount() > 0 || v1 == nullptr) return nullptr;
51 BUG_CHECK(v1->is<IR::P4Program>(), "Conversion returned %1%", v1);
52 return v1->to<IR::P4Program>();
53}
54
63template <typename C = P4V1::Converter>
64const IR::P4Program *parseP4File(ParserOptions &options) {
65 BUG_CHECK(&options == &P4CContext::get().options(),
66 "Parsing using options that don't match the current "
67 "compiler context");
68 FILE *in = nullptr;
69 if (options.doNotPreprocess) {
70 in = fopen(options.file, "r");
71 if (in == nullptr) {
72 ::error(ErrorType::ERR_NOT_FOUND, "%1%: No such file or directory.", options.file);
73 return nullptr;
74 }
75 } else {
76 in = options.preprocess();
77 if (::errorCount() > 0 || in == nullptr) return nullptr;
78 }
79
80 auto result = options.isv1()
81 ? parseV1Program<FILE *, C>(in, options.file, 1, options.getDebugHook())
82 : P4ParserDriver::parse(in, options.file);
83 if (options.doNotPreprocess) {
84 fclose(in);
85 } else {
86 options.closePreprocessedInput(in);
87 }
88
89 if (::errorCount() > 0) {
90 ::error(ErrorType::ERR_OVERLIMIT, "%1% errors encountered, aborting compilation",
91 ::errorCount());
92 return nullptr;
93 }
94 BUG_CHECK(result != nullptr, "Parsing failed, but we didn't report an error");
95 return result;
96}
97
108const IR::P4Program *parseP4String(const char *sourceFile, unsigned sourceLine,
109 const std::string &input,
110 CompilerOptions::FrontendVersion version);
111const IR::P4Program *parseP4String(const std::string &input,
112 CompilerOptions::FrontendVersion version);
113
114} // namespace P4
115
116#endif /* FRONTENDS_COMMON_PARSEINPUT_H_ */
static const IR::P4Program * parse(std::istream &in, const char *sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:135
static P4CContext & get()
Definition parser_options.cpp:522
Definition parser_options.h:39
static const IR::V1Program * parse(std::istream &in, const char *sourceFile, unsigned sourceLine=1)
Definition parserDriver.cpp:286
Definition applyOptionsPragmas.cpp:24
const IR::P4Program * parseP4String(const char *sourceFile, unsigned sourceLine, const std::string &input, CompilerOptions::FrontendVersion version)
Definition parseInput.cpp:32
const IR::P4Program * parseP4File(ParserOptions &options)
Definition parseInput.h:64