blob: 06c377db008aa724f636ed8dfd8a131d357528f5 [file] [log] [blame]
Corentin Wallez71d147f2015-02-11 11:15:24 -08001//
2// Copyright (c) 2002-2015 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// CallDAG.h: Defines a call graph DAG of functions to be re-used accross
8// analyses, allows to efficiently traverse the functions in topological
9// order.
10
11#ifndef COMPILER_TRANSLATOR_CALLDAG_H_
12#define COMPILER_TRANSLATOR_CALLDAG_H_
13
14#include <map>
15
16#include "compiler/translator/IntermNode.h"
17#include "compiler/translator/VariableInfo.h"
18
19
20// The translator needs to analyze the the graph of the function calls
21// to run checks and analyses; since in GLSL recursion is not allowed
22// that graph is a DAG.
23// This class is used to precompute that function call DAG so that it
24// can be reused by multiple analyses.
25//
26// It stores a vector of function records, with one record per function.
27// Records are accessed by index but a mangled function name can be converted
28// to the index of the corresponding record. The records mostly contain the
29// AST node of the function and the indices of the function's callees.
30//
31// In addition, records are in reverse topological order: a function F being
32// called by a function G will have index index(F) < index(G), that way
33// depth-first analysis becomes analysis in the order of indices.
34
35class CallDAG : angle::NonCopyable
36{
37 public:
38 CallDAG();
39 ~CallDAG();
40
41 struct Record
42 {
43 std::string name;
44 TIntermAggregate *node;
45 std::vector<int> callees;
46 };
47
48 enum InitResult
49 {
50 INITDAG_SUCCESS,
51 INITDAG_RECURSION,
52 INITDAG_UNDEFINED,
53 };
54
55 // Returns INITDAG_SUCCESS if it was able to create the DAG, otherwise prints
56 // the initialization error in info, if present.
57 InitResult init(TIntermNode *root, TInfoSinkBase *info);
58
59 // Returns InvalidIndex if the function wasn't found
60 size_t findIndex(const TIntermAggregate *function) const;
61
62 const Record &getRecordFromIndex(size_t index) const;
63 const Record &getRecord(const TIntermAggregate *function) const;
64 size_t size() const;
65 void clear();
66
67 const static size_t InvalidIndex;
68 private:
69 std::vector<Record> mRecords;
70 std::map<int, int> mFunctionIdToIndex;
71
72 class CallDAGCreator;
73};
74
75#endif // COMPILER_TRANSLATOR_CALLDAG_H_