summaryrefslogtreecommitdiffstats
path: root/include/clang/Analysis/Support/BlkExprDeclBitVector.h
blob: a592be815419e8ab0b1e393e83acb973000800cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// BlkExprDeclBitVector.h - Dataflow types for Bitvector Analysis --*- C++ --*--
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file provides definition of dataflow types used by analyses such
// as LiveVariables and UninitializedValues.  The underlying dataflow values
// are implemented as bitvectors, but the definitions in this file include
// the necessary boilerplate to use with our dataflow framework.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_STMTDECLBVDVAL_H
#define LLVM_CLANG_STMTDECLBVDVAL_H

#include "clang/AST/CFG.h"
#include "clang/AST/Decl.h" // for Decl* -> NamedDecl* conversion
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/DenseMap.h"

namespace clang {
  
  class Stmt;
  class ASTContext;

struct DeclBitVector_Types {
  
  class Idx {
    unsigned I;
  public:
    explicit Idx(unsigned i) : I(i) {}
    Idx() : I(~0U) {}
    
    bool isValid() const {
      return I != ~0U;
    }
    operator unsigned() const {
      assert (isValid());
      return I;
    }
  };    
    
  //===--------------------------------------------------------------------===//
  // AnalysisDataTy - Whole-function meta data.
  //===--------------------------------------------------------------------===//
  
  class AnalysisDataTy {
  public:
    typedef llvm::DenseMap<const NamedDecl*, unsigned > DMapTy;
    typedef DMapTy::const_iterator decl_iterator;
    
  protected:
    DMapTy DMap;    
    unsigned NDecls;
    
  public:
    
    AnalysisDataTy() : NDecls(0) {}
    virtual ~AnalysisDataTy() {}
    
    bool isTracked(const NamedDecl* SD) { return DMap.find(SD) != DMap.end(); }
    
    Idx getIdx(const NamedDecl* SD) const {
      DMapTy::const_iterator I = DMap.find(SD);
      return I == DMap.end() ? Idx() : Idx(I->second);
    }

    unsigned getNumDecls() const { return NDecls; }
    
    void Register(const NamedDecl* SD) {
      if (!isTracked(SD)) DMap[SD] = NDecls++;
    }

    decl_iterator begin_decl() const { return DMap.begin(); }
    decl_iterator end_decl() const { return DMap.end(); }
  };
  
  //===--------------------------------------------------------------------===//
  // ValTy - Dataflow value.
  //===--------------------------------------------------------------------===//
  
  class ValTy {
    llvm::BitVector DeclBV;
  public:
    
    void resetDeclValues(AnalysisDataTy& AD) {
      DeclBV.resize(AD.getNumDecls()); 
      DeclBV.reset();
    }

    void setDeclValues(AnalysisDataTy& AD) {
      DeclBV.resize(AD.getNumDecls()); 
      DeclBV.set();
    }
    
    void resetValues(AnalysisDataTy& AD) {
      resetDeclValues(AD);
    }    
    
    bool operator==(const ValTy& RHS) const { 
      assert (sizesEqual(RHS));
      return DeclBV == RHS.DeclBV;
    }
    
    void copyValues(const ValTy& RHS) { DeclBV = RHS.DeclBV; }
    
    llvm::BitVector::reference getBit(unsigned i) {
      return DeclBV[i];
    }
    
    bool getBit(unsigned i) const {
      return DeclBV[i];
    }
    
    llvm::BitVector::reference
    operator()(const NamedDecl* ND, const AnalysisDataTy& AD) {
      return getBit(AD.getIdx(ND));
    }

    bool operator()(const NamedDecl* ND, const AnalysisDataTy& AD) const {
      return getBit(AD.getIdx(ND));
    }
    
    llvm::BitVector::reference getDeclBit(unsigned i) { return DeclBV[i]; }    
    const llvm::BitVector::reference getDeclBit(unsigned i) const {
      return const_cast<llvm::BitVector&>(DeclBV)[i];
    }
        
    ValTy& operator|=(const ValTy& RHS) {
      assert (sizesEqual(RHS));
      DeclBV |= RHS.DeclBV;
      return *this;
    }
    
    ValTy& operator&=(const ValTy& RHS) {
      assert (sizesEqual(RHS));
      DeclBV &= RHS.DeclBV;
      return *this;
    }
    
    ValTy& OrDeclBits(const ValTy& RHS) {
      return operator|=(RHS);
    }
    
    ValTy& AndDeclBits(const ValTy& RHS) {
      return operator&=(RHS);
    }
        
    bool sizesEqual(const ValTy& RHS) const {
      return DeclBV.size() == RHS.DeclBV.size();
    }
  };
  
  //===--------------------------------------------------------------------===//
  // Some useful merge operations.
  //===--------------------------------------------------------------------===//
    
  struct Union { void operator()(ValTy& Dst, ValTy& Src) { Dst |= Src; } };
  struct Intersect { void operator()(ValTy& Dst, ValTy& Src) { Dst &= Src; } };
};


struct StmtDeclBitVector_Types {
  
  //===--------------------------------------------------------------------===//
  // AnalysisDataTy - Whole-function meta data.
  //===--------------------------------------------------------------------===//

  class AnalysisDataTy : public DeclBitVector_Types::AnalysisDataTy {
    ASTContext* ctx;
    CFG* cfg;
  public:
    AnalysisDataTy() : ctx(0), cfg(0) {}
    virtual ~AnalysisDataTy() {}

    void setContext(ASTContext& c) { ctx = &c; }
    ASTContext& getContext() {
      assert(ctx && "ASTContext should not be NULL."); 
      return *ctx;
    }

    void setCFG(CFG& c) { cfg = &c; }
    CFG& getCFG() { assert(cfg && "CFG should not be NULL."); return *cfg; }
    
    bool isTracked(const Stmt* S) { return cfg->isBlkExpr(S); }
    using DeclBitVector_Types::AnalysisDataTy::isTracked;

    unsigned getIdx(const Stmt* S) const {
      CFG::BlkExprNumTy I = cfg->getBlkExprNum(S);
      assert(I && "Stmtession not tracked for bitvector.");
      return I;
    }
    using DeclBitVector_Types::AnalysisDataTy::getIdx;
    
    unsigned getNumBlkExprs() const { return cfg->getNumBlkExprs(); }
  };

  //===--------------------------------------------------------------------===//
  // ValTy - Dataflow value.
  //===--------------------------------------------------------------------===//

  class ValTy : public DeclBitVector_Types::ValTy {
    llvm::BitVector BlkExprBV;
    typedef DeclBitVector_Types::ValTy ParentTy;
    
    static inline ParentTy& ParentRef(ValTy& X) {
      return static_cast<ParentTy&>(X);
    }
    
    static inline const ParentTy& ParentRef(const ValTy& X) {
      return static_cast<const ParentTy&>(X);
    }
    
  public:

    void resetBlkExprValues(AnalysisDataTy& AD) {
      BlkExprBV.resize(AD.getNumBlkExprs());
      BlkExprBV.reset();
    }
    
    void setBlkExprValues(AnalysisDataTy& AD) {
      BlkExprBV.resize(AD.getNumBlkExprs());
      BlkExprBV.set();
    }
    
    void resetValues(AnalysisDataTy& AD) {
      resetDeclValues(AD);
      resetBlkExprValues(AD);
    }
    
    void setValues(AnalysisDataTy& AD) {
      setDeclValues(AD);
      setBlkExprValues(AD);
    }
    
    bool operator==(const ValTy& RHS) const { 
      return ParentRef(*this) == ParentRef(RHS) 
          && BlkExprBV == RHS.BlkExprBV;
    }
    
    void copyValues(const ValTy& RHS) {
      ParentRef(*this).copyValues(ParentRef(RHS));
      BlkExprBV = RHS.BlkExprBV;
    }
        
    llvm::BitVector::reference
    operator()(const Stmt* S, const AnalysisDataTy& AD) {
      return BlkExprBV[AD.getIdx(S)];      
    }    
    const llvm::BitVector::reference
    operator()(const Stmt* S, const AnalysisDataTy& AD) const {
      return const_cast<ValTy&>(*this)(S,AD);
    }
    
    using DeclBitVector_Types::ValTy::operator();

    
    llvm::BitVector::reference getStmtBit(unsigned i) { return BlkExprBV[i]; }    
    const llvm::BitVector::reference getStmtBit(unsigned i) const {
      return const_cast<llvm::BitVector&>(BlkExprBV)[i];
    }
    
    ValTy& OrBlkExprBits(const ValTy& RHS) {
      BlkExprBV |= RHS.BlkExprBV;
      return *this;
    }
    
    ValTy& AndBlkExprBits(const ValTy& RHS) {
      BlkExprBV &= RHS.BlkExprBV;
      return *this;
    }
    
    ValTy& operator|=(const ValTy& RHS) {
      assert (sizesEqual(RHS));
      ParentRef(*this) |= ParentRef(RHS);
      BlkExprBV |= RHS.BlkExprBV;
      return *this;
    }
    
    ValTy& operator&=(const ValTy& RHS) {
      assert (sizesEqual(RHS));
      ParentRef(*this) &= ParentRef(RHS);
      BlkExprBV &= RHS.BlkExprBV;
      return *this;
    }
    
    bool sizesEqual(const ValTy& RHS) const {
      return ParentRef(*this).sizesEqual(ParentRef(RHS))
          && BlkExprBV.size() == RHS.BlkExprBV.size();
    }
  };
  
  //===--------------------------------------------------------------------===//
  // Some useful merge operations.
  //===--------------------------------------------------------------------===//
  
  struct Union { void operator()(ValTy& Dst, ValTy& Src) { Dst |= Src; } };
  struct Intersect { void operator()(ValTy& Dst, ValTy& Src) { Dst &= Src; } };
  
};
} // end namespace clang

#endif
OpenPOWER on IntegriCloud