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