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 ID(const char *n) : ID(Util::SourceInfo(), cstring(n)) {} // NOLINT(runtime/explicit)
42 ID(cstring n) : ID(Util::SourceInfo(), n) {} // NOLINT(runtime/explicit)
43 ID(std::string n) : ID(Util::SourceInfo(), n) {} // NOLINT(runtime/explicit)
44 ID(cstring n, cstring old) : ID(Util::SourceInfo(), n, old) {}
45 void dbprint(std::ostream &out) const override {
46 out << name;
47 if (originalName != nullptr && originalName != name) out << "/" << originalName;
48 }
49 bool operator==(const ID &a) const { return name == a.name; }
50 bool operator!=(const ID &a) const { return name != a.name; }
52 bool operator<(const ID &a) const { return name < a.name; }
53 bool operator==(cstring a) const { return name == a; }
54 bool operator!=(cstring a) const { return name != a; }
56 bool operator<(cstring a) const { return name < a; }
57 bool operator==(const char *a) const { return name == a; }
58 bool operator!=(const char *a) const { return name != a; }
60 bool operator<(const char *a) const { return name < a; }
61 explicit operator bool() const { return name; }
62 operator cstring() const { return name; }
63 std::string string() const { return name.string(); }
64 std::string_view string_view() const { return name.string_view(); }
65 bool isDontCare() const { return name == "_"; }
66 Util::SourceInfo getSourceInfo() const override { return srcInfo; }
67 cstring toString() const override { return originalName.isNullOrEmpty() ? name : originalName; }
68};
69
70} // namespace P4::IR
71
72namespace P4::Util {
73template <>
74struct Hasher<IR::ID> {
75 size_t operator()(const IR::ID &id) const { return Util::Hash{}(id.name); }
76};
77} // namespace P4::Util
78
79#endif /* IR_ID_H_ */
Definition stringify.h:33
Definition source_file.h:221
Definition source_file.h:125
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:56
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:60
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:52
Definition hash.h:125
Definition hash.h:123