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.
28struct ID : Util::IHasSourceInfo, public IHasDbPrint {
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; }
51 bool operator==(cstring a) const { return name == a; }
52 bool operator!=(cstring a) const { return name != a; }
53 bool operator==(const char *a) const { return name == a; }
54 bool operator!=(const char *a) const { return name != a; }
55 explicit operator bool() const { return name; }
56 operator cstring() const { return name; }
57 std::string string() const { return name.string(); }
58 std::string_view string_view() const { return name.string_view(); }
59 bool isDontCare() const { return name == "_"; }
60 Util::SourceInfo getSourceInfo() const override { return srcInfo; }
61 cstring toString() const override { return originalName.isNullOrEmpty() ? name : originalName; }
62};
63
64} // namespace P4::IR
65
66namespace P4::Util {
67template <>
68struct Hasher<IR::ID> {
69 size_t operator()(const IR::ID &id) const { return Util::Hash{}(id.name); }
70};
71} // namespace P4::Util
72
73#endif /* IR_ID_H_ */
Definition stringify.h:33
Definition source_file.h:213
Definition source_file.h:124
Definition cstring.h:85
Definition id.h:28
Definition hash.h:125
Definition hash.h:123