-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the json loader and parser independent from the generated ir files.
Signed-off-by: fruffy <[email protected]>
- Loading branch information
Showing
9 changed files
with
185 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2013-present Barefoot Networks, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#ifndef IR_INODE_H_ | ||
#define IR_INODE_H_ | ||
|
||
#include "lib/castable.h" | ||
#include "lib/cstring.h" | ||
#include "lib/exceptions.h" | ||
#include "lib/rtti.h" | ||
#include "lib/source_file.h" | ||
|
||
namespace P4 { | ||
class JSONGenerator; | ||
class JSONLoader; | ||
} // namespace P4 | ||
|
||
namespace P4::IR { | ||
|
||
class Node; | ||
|
||
/// SFINAE helper to check if given class has a `static_type_name` | ||
/// method. Definite node classes have them while interfaces do not | ||
template <class, class = void> | ||
struct has_static_type_name : std::false_type {}; | ||
|
||
template <class T> | ||
struct has_static_type_name<T, std::void_t<decltype(T::static_type_name())>> : std::true_type {}; | ||
|
||
template <class T> | ||
inline constexpr bool has_static_type_name_v = has_static_type_name<T>::value; | ||
|
||
// node interface | ||
class INode : public Util::IHasSourceInfo, public IHasDbPrint, public ICastable { | ||
public: | ||
virtual ~INode() {} | ||
virtual const Node *getNode() const = 0; | ||
virtual Node *getNode() = 0; | ||
virtual void toJSON(JSONGenerator &) const = 0; | ||
virtual cstring node_type_name() const = 0; | ||
virtual void validate() const {} | ||
|
||
// default checkedTo implementation for nodes: just fallback to generic ICastable method | ||
template <typename T> | ||
std::enable_if_t<!has_static_type_name_v<T>, const T *> checkedTo() const { | ||
return ICastable::checkedTo<T>(); | ||
} | ||
|
||
// alternative checkedTo implementation that produces slightly better error message | ||
// due to node_type_name() / static_type_name() being available | ||
template <typename T> | ||
std::enable_if_t<has_static_type_name_v<T>, const T *> checkedTo() const { | ||
const auto *result = to<T>(); | ||
BUG_CHECK(result, "Cast failed: %1% with type %2% is not a %3%.", this, node_type_name(), | ||
T::static_type_name()); | ||
return result; | ||
} | ||
|
||
DECLARE_TYPEINFO(INode); | ||
}; | ||
|
||
} // namespace P4::IR | ||
|
||
#endif /* IR_INODE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef IR_UNPACKER_TABLE_H_ | ||
#define IR_UNPACKER_TABLE_H_ | ||
|
||
#include "ir/inode.h" | ||
#include "lib/cstring.h" | ||
|
||
namespace P4 { | ||
class JSONLoader; | ||
|
||
using NodeFactoryFn = IR::INode *(*)(JSONLoader &); | ||
namespace IR { | ||
extern std::map<cstring, NodeFactoryFn> unpacker_table; | ||
|
||
} // namespace IR | ||
|
||
} // namespace P4 | ||
|
||
#endif /* IR_UNPACKER_TABLE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright 2013-present Barefoot Networks, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
#ifndef IR_UNPARSED_CONSTANT_H_ | ||
#define IR_UNPARSED_CONSTANT_H_ | ||
|
||
#include "lib/cstring.h" | ||
|
||
namespace P4 { | ||
|
||
/** | ||
* An unparsed numeric constant. We produce these as token values during | ||
* lexing. The parser is responsible for actually interpreting the raw text as a | ||
* numeric value and transforming it into an IR::Constant using parseConstant(). | ||
* | ||
* To illustrate how a numeric constant is represented using this struct, | ||
* here is a breakdown of '16w0x12': | ||
* | ||
* ___ | ||
* / ___ | ||
* | / | ||
* | bitwidth (if @hasWidth) | 16 | ||
* | \___ | ||
* | | ||
* | ___ | ||
* | / | ||
* | separator (if @hasWidth) | w | ||
* | \___ | ||
* @text | | ||
* | ___ | ||
* | / | ||
* | ignored prefix of length @skip | 0x | ||
* | \___ | ||
* | | ||
* | ___ | ||
* | / | ||
* | numeric value in base @base | w | ||
* | \___ | ||
* \___ | ||
* | ||
* Simple numeric constants like '5' are specified by setting @hasWidth to | ||
* false and providing a @skip length of 0. | ||
*/ | ||
struct UnparsedConstant { | ||
cstring text; /// Raw P4 text which was recognized as a numeric constant. | ||
unsigned skip; /// An ignored prefix of the numeric constant (e.g. '0x'). | ||
unsigned base; /// The base in which the constant is expressed. | ||
bool hasWidth; /// If true, a bitwidth and separator are present. | ||
}; | ||
|
||
std::ostream &operator<<(std::ostream &out, const UnparsedConstant &constant); | ||
|
||
bool operator<(const UnparsedConstant &a, const UnparsedConstant &b); | ||
|
||
} // namespace P4 | ||
|
||
#endif /* IR_UNPARSED_CONSTANT_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.