P4C
The P4 Compiler
Loading...
Searching...
No Matches
id.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef IR_ID_H_
9#define IR_ID_H_
10
11#include "lib/cstring.h"
12#include "lib/exceptions.h"
13#include "lib/hash.h"
14#include "lib/source_file.h"
15
16namespace P4::IR {
17
18// An identifier.
19struct ID : Util::IHasSourceInfo, public IHasDbPrint {
20 Util::SourceInfo srcInfo;
21 cstring name = nullptr;
22 // We save the original name to show the user on error messages.
23 // If originalName is nullptr the symbol has been generated by the compiler.
24 cstring originalName = nullptr;
25 ID() = default;
26 ID(Util::SourceInfo si, cstring n, cstring o) : srcInfo(si), name(n), originalName(o) {
27 if (n.isNullOrEmpty()) BUG("Identifier with no name");
28 }
29 ID(Util::SourceInfo si, cstring n) : srcInfo(si), name(n), originalName(n) {
30 if (n.isNullOrEmpty()) BUG("Identifier with no name");
31 }
32 // FIXME: Replace with single string_view constructor
33 ID(const char *n) : ID(Util::SourceInfo(), cstring(n)) {} // NOLINT(runtime/explicit)
34 ID(cstring n) : ID(Util::SourceInfo(), n) {} // NOLINT(runtime/explicit)
35 ID(const std::string &n) : ID(Util::SourceInfo(), n) {} // NOLINT(runtime/explicit)
36 ID(cstring n, cstring old) : ID(Util::SourceInfo(), n, old) {}
37 void dbprint(std::ostream &out) const override {
38 out << name;
39 if (originalName != nullptr && originalName != name) out << "/" << originalName;
40 }
41 bool operator==(const ID &a) const { return name == a.name; }
42 bool operator!=(const ID &a) const { return name != a.name; }
44 bool operator<(const ID &a) const { return name < a.name; }
45 bool operator==(cstring a) const { return name == a; }
46 bool operator!=(cstring a) const { return name != a; }
48 bool operator<(cstring a) const { return name < a; }
49 bool operator==(const char *a) const { return name == a; }
50 bool operator!=(const char *a) const { return name != a; }
52 bool operator<(const char *a) const { return name < a; }
53 explicit operator bool() const { return name.c_str() != nullptr; }
54 operator cstring() const { return name; }
55 std::string string() const { return name.string(); }
56 std::string_view string_view() const { return name.string_view(); }
57 bool isDontCare() const { return name == "_"; }
58 Util::SourceInfo getSourceInfo() const override { return srcInfo; }
59 cstring toString() const override { return originalName.isNullOrEmpty() ? name : originalName; }
60
63 template <typename Sink>
64 friend void AbslStringify(Sink &sink, const ID &id) {
65 sink.Append(id.string_view());
66 }
67};
68
69} // namespace P4::IR
70
71namespace P4::Util {
72template <>
73struct Hasher<IR::ID> {
74 size_t operator()(const IR::ID &id) const { return Util::Hash{}(id.name); }
75};
76} // namespace P4::Util
77
78#endif /* IR_ID_H_ */
Definition stringify.h:24
Definition source_file.h:214
Definition source_file.h:123
Definition cstring.h:76
The namespace encapsulating IR node classes.
Definition constantParsing.h:13
Definition id.h:19
bool operator<(cstring a) const
Defer to cstring's notion of less, which is a lexicographical and not a pointer comparison.
Definition id.h:48
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:52
friend void AbslStringify(Sink &sink, const ID &id)
Definition id.h:64
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:44
Definition hash.h:125
Definition hash.h:123