P4C
The P4 Compiler
Loading...
Searching...
No Matches
id.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 IR_ID_H_
18#define IR_ID_H_
19
20#include "lib/cstring.h"
21#include "lib/exceptions.h"
22#include "lib/hash.h"
23#include "lib/source_file.h"
24
25namespace P4::IR {
26
27// An identifier.
29 Util::SourceInfo srcInfo;
30 cstring name = nullptr;
31 // We save the original name to show the user on error messages.
32 // If originalName is nullptr the symbol has been generated by the compiler.
33 cstring originalName = nullptr;
34 ID() = default;
35 ID(Util::SourceInfo si, cstring n, cstring o) : srcInfo(si), name(n), originalName(o) {
36 if (n.isNullOrEmpty()) BUG("Identifier with no name");
37 }
38 ID(Util::SourceInfo si, cstring n) : srcInfo(si), name(n), originalName(n) {
39 if (n.isNullOrEmpty()) BUG("Identifier with no name");
40 }
41 // FIXME: Replace with single string_view constructor
42 ID(const char *n) : ID(Util::SourceInfo(), cstring(n)) {} // NOLINT(runtime/explicit)
43 ID(cstring n) : ID(Util::SourceInfo(), n) {} // NOLINT(runtime/explicit)
44 ID(const std::string &n) : ID(Util::SourceInfo(), n) {} // NOLINT(runtime/explicit)
45 ID(cstring n, cstring old) : ID(Util::SourceInfo(), n, old) {}
46 void dbprint(std::ostream &out) const override {
47 out << name;
48 if (originalName != nullptr && originalName != name) out << "/" << originalName;
49 }
50 bool operator==(const ID &a) const { return name == a.name; }
51 bool operator!=(const ID &a) const { return name != a.name; }
53 bool operator<(const ID &a) const { return name < a.name; }
54 bool operator==(cstring a) const { return name == a; }
55 bool operator!=(cstring a) const { return name != a; }
57 bool operator<(cstring a) const { return name < a; }
58 bool operator==(const char *a) const { return name == a; }
59 bool operator!=(const char *a) const { return name != a; }
61 bool operator<(const char *a) const { return name < a; }
62 explicit operator bool() const { return name.c_str() != nullptr; }
63 operator cstring() const { return name; }
64 std::string string() const { return name.string(); }
65 std::string_view string_view() const { return name.string_view(); }
66 bool isDontCare() const { return name == "_"; }
67 Util::SourceInfo getSourceInfo() const override { return srcInfo; }
68 cstring toString() const override { return originalName.isNullOrEmpty() ? name : originalName; }
69
72 template <typename Sink>
73 friend void AbslStringify(Sink &sink, const ID &id) {
74 sink.Append(id.string_view());
75 }
76};
77
78} // namespace P4::IR
79
80namespace P4::Util {
81template <>
82struct Hasher<IR::ID> {
83 size_t operator()(const IR::ID &id) const { return Util::Hash{}(id.name); }
84};
85} // namespace P4::Util
86
87#endif /* IR_ID_H_ */
Definition stringify.h:33
Definition source_file.h:227
Definition source_file.h:131
Definition cstring.h:85
The namespace encapsulating IR node classes.
Definition id.h:28
bool operator<(cstring a) const
Defer to cstring's notion of less, which is a lexicographical and not a pointer comparison.
Definition id.h:57
bool operator<(const char *a) const
Defer to cstring's notion of less, which is a lexicographical and not a pointer comparison.
Definition id.h:61
friend void AbslStringify(Sink &sink, const ID &id)
Definition id.h:73
bool operator<(const ID &a) const
Defer to cstring's notion of less, which is a lexicographical and not a pointer comparison.
Definition id.h:53
Definition hash.h:125
Definition hash.h:123