18c1e6b3b5fb33cce93852548ee629f161370bd1
[oota-llvm.git] / tools / llvmc2 / CompilationGraph.h
1 //===--- CompilationGraph.h - The LLVM Compiler Driver ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Compilation graph - definition.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
15 #define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
16
17 #include "AutoGenerated.h"
18 #include "Tool.h"
19
20 #include "llvm/ADT/GraphTraits.h"
21 #include "llvm/ADT/IntrusiveRefCntPtr.h"
22 #include "llvm/ADT/iterator"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/System/Path.h"
26
27 #include <string>
28
29 namespace llvmcc {
30
31   // An edge in the graph.
32   class Edge : public llvm::RefCountedBaseVPTR<Edge> {
33   public:
34     Edge(const std::string& T) : ToolName_(T) {}
35     virtual ~Edge() {};
36
37     const std::string& ToolName() const { return ToolName_; }
38     virtual bool isEnabled() const = 0;
39     virtual bool isDefault() const = 0;
40   private:
41     std::string ToolName_;
42   };
43
44   // Edges with no properties are instances of this class.
45   class SimpleEdge : public Edge {
46   public:
47     SimpleEdge(const std::string& T) : Edge(T) {}
48     bool isEnabled() const { return false;}
49     bool isDefault() const { return true;}
50   };
51
52   // A node in the graph.
53   struct Node {
54     typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
55     typedef container_type::iterator iterator;
56     typedef container_type::const_iterator const_iterator;
57
58     Node() {}
59     Node(CompilationGraph* G) : OwningGraph(G) {}
60     Node(CompilationGraph* G, Tool* T) : OwningGraph(G), ToolPtr(T) {}
61
62     bool HasChildren() const { return !OutEdges.empty(); }
63     const std::string Name() const { return ToolPtr->Name(); }
64
65     iterator EdgesBegin() { return OutEdges.begin(); }
66     const_iterator EdgesBegin() const { return OutEdges.begin(); }
67     iterator EdgesEnd() { return OutEdges.end(); }
68     const_iterator EdgesEnd() const { return OutEdges.end(); }
69
70     // Choose one of the edges based on command-line options.
71     const Edge* ChooseEdge() const;
72
73     // Takes ownership of the object.
74     void AddEdge(Edge* E)
75     { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
76
77     // Needed to implement NodeChildIterator/GraphTraits
78     CompilationGraph* OwningGraph;
79     // The corresponding Tool.
80     llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
81     // Links to children.
82     container_type OutEdges;
83   };
84
85   class NodesIterator;
86
87   class CompilationGraph {
88     typedef llvm::SmallVector<std::string, 3> tools_vector_type;
89     typedef llvm::StringMap<tools_vector_type> tools_map_type;
90     typedef llvm::StringMap<Node> nodes_map_type;
91
92     // Map from file extensions to language names.
93     LanguageMap ExtsToLangs;
94     // Map from language names to lists of tool names.
95     tools_map_type ToolsMap;
96     // Map from tool names to Tool objects.
97     nodes_map_type NodesMap;
98
99   public:
100
101     CompilationGraph();
102
103     // insertVertex - insert a new node into the graph. Takes
104     // ownership of the object.
105     void insertNode(Tool* T);
106
107     // insertEdge - Insert a new edge into the graph. Takes ownership
108     // of the object.
109     void insertEdge(const std::string& A, Edge* E);
110
111     // Build - Build target(s) from the input file set. Command-line
112     // options are passed implicitly as global variables.
113     int Build(llvm::sys::Path const& tempDir) const;
114
115     // Return a reference to the node correponding to the given tool
116     // name. Throws std::runtime_error.
117     Node& getNode(const std::string& ToolName);
118     const Node& getNode(const std::string& ToolName) const;
119
120     // viewGraph - This function is meant for use from the debugger.
121     // You can just say 'call G->viewGraph()' and a ghostview window
122     // should pop up from the program, displaying the compilation
123     // graph. This depends on there being a 'dot' and 'gv' program
124     // in your path.
125     void viewGraph();
126
127     // Write a CompilationGraph.dot file.
128     void writeGraph();
129
130     // GraphTraits support
131     friend NodesIterator GraphBegin(CompilationGraph*);
132     friend NodesIterator GraphEnd(CompilationGraph*);
133     friend void PopulateCompilationGraph(CompilationGraph&);
134
135   private:
136     // Helper functions.
137
138     // Find out which language corresponds to the suffix of this file.
139     const std::string& getLanguage(const llvm::sys::Path& File) const;
140
141     // Return a reference to the list of tool names corresponding to
142     // the given language name. Throws std::runtime_error.
143     const tools_vector_type& getToolsVector(const std::string& LangName) const;
144   };
145
146   /// GraphTraits support code.
147
148   // Auxiliary class needed to implement GraphTraits support.  Can be
149   // generalised to something like value_iterator for map-like
150   // containers.
151   class NodesIterator : public llvm::StringMap<Node>::iterator {
152     typedef llvm::StringMap<Node>::iterator super;
153     typedef NodesIterator ThisType;
154     typedef Node* pointer;
155     typedef Node& reference;
156
157   public:
158     NodesIterator(super I) : super(I) {}
159
160     inline reference operator*() const {
161       return super::operator->()->second;
162     }
163     inline pointer operator->() const {
164       return &super::operator->()->second;
165     }
166   };
167
168   inline NodesIterator GraphBegin(CompilationGraph* G) {
169     return NodesIterator(G->NodesMap.begin());
170   }
171
172   inline NodesIterator GraphEnd(CompilationGraph* G) {
173     return NodesIterator(G->NodesMap.end());
174   }
175
176
177   // Another auxiliary class needed by GraphTraits.
178   class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
179     typedef NodeChildIterator ThisType;
180     typedef Node::container_type::iterator iterator;
181
182     CompilationGraph* OwningGraph;
183     iterator EdgeIter;
184   public:
185     typedef Node* pointer;
186     typedef Node& reference;
187
188     NodeChildIterator(Node* N, iterator I) :
189       OwningGraph(N->OwningGraph), EdgeIter(I) {}
190
191     const ThisType& operator=(const ThisType& I) {
192       assert(OwningGraph == I.OwningGraph);
193       EdgeIter = I.EdgeIter;
194       return *this;
195     }
196
197     inline bool operator==(const ThisType& I) const
198     { return EdgeIter == I.EdgeIter; }
199     inline bool operator!=(const ThisType& I) const
200     { return EdgeIter != I.EdgeIter; }
201
202     inline pointer operator*() const {
203       return &OwningGraph->getNode((*EdgeIter)->ToolName());
204     }
205     inline pointer operator->() const {
206       return &OwningGraph->getNode((*EdgeIter)->ToolName());
207     }
208
209     ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
210     ThisType operator++(int) { // Postincrement
211       ThisType tmp = *this;
212       ++*this;
213       return tmp;
214     }
215
216     inline ThisType& operator--() { --EdgeIter; return *this; }  // Predecrement
217     inline ThisType operator--(int) { // Postdecrement
218       ThisType tmp = *this;
219       --*this;
220       return tmp;
221     }
222
223   };
224 }
225
226 namespace llvm {
227   template <>
228   struct GraphTraits<llvmcc::CompilationGraph*> {
229     typedef llvmcc::CompilationGraph GraphType;
230     typedef llvmcc::Node NodeType;
231     typedef llvmcc::NodeChildIterator ChildIteratorType;
232
233     static NodeType* getEntryNode(GraphType* G) {
234       return &G->getNode("root");
235     }
236
237     static ChildIteratorType child_begin(NodeType* N) {
238       return ChildIteratorType(N, N->OutEdges.begin());
239     }
240     static ChildIteratorType child_end(NodeType* N) {
241       return ChildIteratorType(N, N->OutEdges.end());
242     }
243
244     typedef llvmcc::NodesIterator nodes_iterator;
245     static nodes_iterator nodes_begin(GraphType *G) {
246       return GraphBegin(G);
247     }
248     static nodes_iterator nodes_end(GraphType *G) {
249       return GraphEnd(G);
250     }
251   };
252
253 }
254
255 #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H