• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 NAMES.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   Name operator()(const Special&);
62   Name operator()(const PointerReference&);
63   Name operator()(const PointerToMember&);
64   Name operator()(const Typedef&);
65   Name operator()(const Qualified&);
66   Name operator()(const Primitive&);
67   Name operator()(const Array&);
68   Name operator()(const BaseClass&);
69   Name operator()(const Method&);
70   Name operator()(const Member&);
71   Name operator()(const VariantMember&);
72   Name operator()(const StructUnion&);
73   Name operator()(const Enumeration&);
74   Name operator()(const Variant&);
75   Name operator()(const Function&);
76   Name operator()(const ElfSymbol&);
77   Name operator()(const Interface&);
78   const Graph& graph;
79   NameCache& names;
80 };
81 
82 struct DescribeKind {
DescribeKindDescribeKind83   explicit DescribeKind(const Graph& graph) : graph(graph) {}
84   std::string operator()(Id id);
85   std::string operator()(const BaseClass&);
86   std::string operator()(const Method&);
87   std::string operator()(const Member&);
88   std::string operator()(const ElfSymbol&);
89   std::string operator()(const Interface&);
90   template <typename Node>
91   std::string operator()(const Node&);
92   const Graph& graph;
93 };
94 
95 struct DescribeExtra {
DescribeExtraDescribeExtra96   explicit DescribeExtra(const Graph& graph) : graph(graph) {}
97   std::string operator()(Id id);
98   std::string operator()(const ElfSymbol&);
99   template <typename Node>
100   std::string operator()(const Node&);
101   const Graph& graph;
102 };
103 
104 }  // namespace stg
105 
106 #endif  // STG_NAMING_H_
107