xref: /aosp_15_r20/external/stg/naming.h (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2020-2024 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Giuliano Procida
19 // Author: Ignes Simeonova
20 
21 #ifndef STG_NAMING_H_
22 #define STG_NAMING_H_
23 
24 #include <ostream>
25 #include <string>
26 #include <unordered_map>
27 
28 #include "graph.h"
29 
30 namespace stg {
31 
32 // See naming.md for conceptual documentation.
33 
34 enum class Precedence { NIL, POINTER, ARRAY_FUNCTION, ATOMIC };
35 enum class Side { LEFT, RIGHT };
36 
37 class Name {
38  public:
Name(const std::string & name)39   explicit Name(const std::string& name)
40       : left_(name), precedence_(Precedence::NIL), right_() {}
Name(const std::string & left,Precedence precedence,const std::string & right)41   Name(const std::string& left, Precedence precedence, const std::string& right)
42       : left_(left), precedence_(precedence), right_(right) {}
43   Name Add(Side side, Precedence precedence, const std::string& text) const;
44   Name Qualify(Qualifier qualifier) const;
45   std::ostream& Print(std::ostream& os) const;
46   std::string ToString() const;
47 
48  private:
49   std::string left_;
50   Precedence precedence_;
51   std::string right_;
52 };
53 
54 std::ostream& operator<<(std::ostream& os, const Name& name);
55 
56 using NameCache = std::unordered_map<Id, Name>;
57 
58 struct Describe {
DescribeDescribe59   Describe(const Graph& graph, NameCache& names) : graph(graph), names(names) {}
60   Name operator()(Id id);
61   const Graph& graph;
62   NameCache& names;
63 };
64 
65 struct DescribeKind {
DescribeKindDescribeKind66   explicit DescribeKind(const Graph& graph) : graph(graph) {}
67   std::string operator()(Id id);
68   const Graph& graph;
69 };
70 
71 struct DescribeExtra {
DescribeExtraDescribeExtra72   explicit DescribeExtra(const Graph& graph) : graph(graph) {}
73   std::string operator()(Id id);
74   const Graph& graph;
75 };
76 
77 }  // namespace stg
78 
79 #endif  // STG_NAMING_H_
80