54ced15321d1a6144fcaf30b44dec4008f0e9793
[oota-llvm.git] / include / llvm / Support / DOTGraphTraits.h
1 //===-- llvm/Support/DotGraphTraits.h - Customize .dot output ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a template class that can be used to customize dot output
11 // graphs generated by the GraphWriter.h file.  The default implementation of
12 // this file will produce a simple, but not very polished graph.  By
13 // specializing this template, lots of customization opportunities are possible.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_SUPPORT_DOTGRAPHTRAITS_H
18 #define LLVM_SUPPORT_DOTGRAPHTRAITS_H
19
20 #include <string>
21
22 namespace llvm {
23
24 /// DefaultDOTGraphTraits - This class provides the default implementations of
25 /// all of the DOTGraphTraits methods.  If a specialization does not need to
26 /// override all methods here it should inherit so that it can get the default
27 /// implementations.
28 ///
29 struct DefaultDOTGraphTraits {
30 private:
31   bool IsSimple;
32
33 protected:
34   bool isSimple() {
35     return IsSimple;
36   }
37
38 public:
39   DefaultDOTGraphTraits (bool simple=false) : IsSimple (simple) {}
40
41   /// getGraphName - Return the label for the graph as a whole.  Printed at the
42   /// top of the graph.
43   ///
44   template<typename GraphType>
45   static std::string getGraphName(const GraphType& Graph) { return ""; }
46
47   /// getGraphProperties - Return any custom properties that should be included
48   /// in the top level graph structure for dot.
49   ///
50   template<typename GraphType>
51   static std::string getGraphProperties(const GraphType& Graph) {
52     return "";
53   }
54
55   /// renderGraphFromBottomUp - If this function returns true, the graph is
56   /// emitted bottom-up instead of top-down.  This requires graphviz 2.0 to work
57   /// though.
58   static bool renderGraphFromBottomUp() {
59     return false;
60   }
61
62   /// getNodeLabel - Given a node and a pointer to the top level graph, return
63   /// the label to print in the node.
64   template<typename GraphType>
65   std::string getNodeLabel(const void *Node, const GraphType& Graph) {
66     return "";
67   }
68
69   /// hasNodeAddressLabel - If this method returns true, the address of the node
70   /// is added to the label of the node.
71   template<typename GraphType>
72   static bool hasNodeAddressLabel(const void *Node, const GraphType& Graph) {
73     return false;
74   }
75
76   /// If you want to specify custom node attributes, this is the place to do so
77   ///
78   template<typename GraphType>
79   static std::string getNodeAttributes(const void *Node,
80                                        const GraphType& Graph) {
81     return "";
82   }
83
84   /// If you want to override the dot attributes printed for a particular edge,
85   /// override this method.
86   template<typename EdgeIter>
87   static std::string getEdgeAttributes(const void *Node, EdgeIter EI) {
88     return "";
89   }
90
91   /// getEdgeSourceLabel - If you want to label the edge source itself,
92   /// implement this method.
93   template<typename EdgeIter>
94   static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
95     return "";
96   }
97
98   /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
99   /// should actually target another edge source, not a node.  If this method is
100   /// implemented, getEdgeTarget should be implemented.
101   template<typename EdgeIter>
102   static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
103     return false;
104   }
105
106   /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
107   /// called to determine which outgoing edge of Node is the target of this
108   /// edge.
109   template<typename EdgeIter>
110   static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
111     return I;
112   }
113
114   /// hasEdgeDestLabels - If this function returns true, the graph is able
115   /// to provide labels for edge destinations.
116   static bool hasEdgeDestLabels() {
117     return false;
118   }
119
120   /// numEdgeDestLabels - If hasEdgeDestLabels, this function returns the
121   /// number of incoming edge labels the given node has.
122   static unsigned numEdgeDestLabels(const void *Node) {
123     return 0;
124   }
125
126   /// getEdgeDestLabel - If hasEdgeDestLabels, this function returns the
127   /// incoming edge label with the given index in the given node.
128   static std::string getEdgeDestLabel(const void *Node, unsigned i) {
129     return "";
130   }
131
132   /// addCustomGraphFeatures - If a graph is made up of more than just
133   /// straight-forward nodes and edges, this is the place to put all of the
134   /// custom stuff necessary.  The GraphWriter object, instantiated with your
135   /// GraphType is passed in as an argument.  You may call arbitrary methods on
136   /// it to add things to the output graph.
137   ///
138   template<typename GraphType, typename GraphWriter>
139   static void addCustomGraphFeatures(const GraphType& Graph, GraphWriter &GW) {}
140 };
141
142
143 /// DOTGraphTraits - Template class that can be specialized to customize how
144 /// graphs are converted to 'dot' graphs.  When specializing, you may inherit
145 /// from DefaultDOTGraphTraits if you don't need to override everything.
146 ///
147 template <typename Ty>
148 struct DOTGraphTraits : public DefaultDOTGraphTraits {
149   DOTGraphTraits (bool simple=false) : DefaultDOTGraphTraits (simple) {}
150 };
151
152 } // End llvm namespace
153
154 #endif