P4C
The P4 Compiler
Loading...
Searching...
No Matches
pragma.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_COMMON_PRAGMA_H_
20#define BACKENDS_TOFINO_BF_P4C_COMMON_PRAGMA_H_
21#include <algorithm>
22#include <cstring>
23#include <iomanip>
24#include <iostream>
25#include <set>
26#include <string>
27
28namespace BFN {
29
32class Pragma {
33 public:
35 virtual const char *name() const { return "undefined"; }
36
38 virtual const char *description() const = 0;
39
41 virtual const char *help() const = 0;
42
44 static std::ostream &printHelp(std::ostream &o) {
45 // format a string to width
46 auto format = [](const char *str, size_t width, size_t indent = 0) -> const char * {
47 std::string res = "";
48 do {
49 std::string chunk = std::string(str).substr(0, width);
50 auto newline = chunk.find('\n');
51 if (newline != std::string::npos) {
52 res += chunk.substr(0, newline + 1);
53 str += newline + 1;
54 } else if (strlen(str) < width) {
55 res += str;
56 break; // no need to go further
57 } else {
58 // split at the last space before the desired width
59 auto lastspace = chunk.rfind(' ');
60 if (lastspace != std::string::npos) {
61 res += chunk.substr(0, lastspace) + "\n";
62 str += lastspace + 1;
63 } else {
64 // no space? unlikely, so then we just print the string
65 res += chunk + "\n";
66 str += chunk.size();
67 }
68 }
69 } while (strlen(str) > 0);
70 return res.c_str();
71 };
72
73 auto formatPragma = [format](const Pragma *p, std::ostream &o) {
74 o << std::endl;
75 o << p->name() << ": " << format(p->description(), 80 - (strlen(p->name()) + 3))
76 << std::endl
77 << std::endl;
78 o << format(p->help(), 80) << std::endl;
79 };
80
81 o << "Supported pragmas:" << std::endl;
82 for (auto &p : _supported_pragmas) formatPragma(p, o);
83#if BAREFOOT_INTERNAL
84 for (auto &p : _internal_pragmas) formatPragma(p, o);
85#endif
86 return o;
87 }
88
89 protected:
92 static bool registerPragma(const Pragma *p, bool internal = false) {
93 std::cerr << "registering " << p->name() << std::endl;
94 if (!internal)
95 _supported_pragmas.insert(p);
96 else
97 _internal_pragmas.insert(p);
98 return true;
99 }
100
101 private:
102 struct PragmaCmp {
103 bool operator()(const Pragma *a, const Pragma *b) const {
104 return std::string(a->name()) < std::string(b->name());
105 }
106 };
107
109 static std::set<const Pragma *, PragmaCmp> _supported_pragmas;
111 static std::set<const Pragma *, PragmaCmp> _internal_pragmas;
112};
113
114} // end namespace BFN
115
116#endif // BACKENDS_TOFINO_BF_P4C_COMMON_PRAGMA_H_
virtual const char * help() const =0
detailed description that will be printed as help
static bool registerPragma(const Pragma *p, bool internal=false)
Definition pragma.h:92
virtual const char * description() const =0
short description that will be printed as help
virtual const char * name() const
the pragma name – should be overriden
Definition pragma.h:35
static std::ostream & printHelp(std::ostream &o)
print the currently supported pragmas
Definition pragma.h:44
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
Definition pretty_print.h:25