slang_rs_object_ref_count.h revision c202d2d64fe0172bcc3999b515a14d3d88532420
1/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_OBJECT_REF_COUNT_H_  // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_OBJECT_REF_COUNT_H_
19
20#include <list>
21#include <stack>
22
23#include "clang/AST/StmtVisitor.h"
24
25#include "slang_rs_export_type.h"
26
27namespace clang {
28  class Expr;
29  class Stmt;
30}
31
32namespace slang {
33
34class RSObjectRefCount : public clang::StmtVisitor<RSObjectRefCount> {
35 private:
36  class Scope {
37   private:
38    clang::CompoundStmt *mCS;      // Associated compound statement ({ ... })
39    std::list<clang::VarDecl*> mRSO;  // Declared RS objects in this scope
40
41    // RSSetObjectFD and RSClearObjectFD holds FunctionDecl of rsSetObject()
42    // and rsClearObject() in the current ASTContext.
43    static clang::FunctionDecl *RSSetObjectFD[];
44    static clang::FunctionDecl *RSClearObjectFD[];
45
46   public:
47    explicit Scope(clang::CompoundStmt *CS) : mCS(CS) {
48      return;
49    }
50
51    inline void addRSObject(clang::VarDecl* VD) {
52      mRSO.push_back(VD);
53      return;
54    }
55
56    // Initialize RSSetObjectFD and RSClearObjectFD.
57    static void GetRSRefCountingFunctions(clang::ASTContext &C);
58
59    void ReplaceRSObjectAssignment(clang::BinaryOperator *AS);
60
61    void InsertLocalVarDestructors();
62
63    static clang::Stmt *ClearRSObject(clang::VarDecl *VD);
64  };
65
66  std::stack<Scope*> mScopeStack;
67  bool RSInitFD;
68
69  inline Scope *getCurrentScope() {
70    return mScopeStack.top();
71  }
72
73  // TODO(srhines): Composite types and arrays based on RS object types need
74  // to be handled for both zero-initialization + clearing.
75
76  // Return false if the type of variable declared in VD is not an RS object
77  // type.
78  static bool InitializeRSObject(clang::VarDecl *VD);
79
80  // Return a zero-initializer expr of the type DT. This processes both
81  // RS matrix type and RS object type.
82  static clang::Expr *CreateZeroInitializerForRSSpecificType(
83      RSExportPrimitiveType::DataType DT,
84      clang::ASTContext &C,
85      const clang::SourceLocation &Loc);
86
87 public:
88  RSObjectRefCount()
89      : RSInitFD(false) {
90    return;
91  }
92
93  void Init(clang::ASTContext &C) {
94    if (!RSInitFD) {
95      Scope::GetRSRefCountingFunctions(C);
96      RSInitFD = true;
97    }
98    return;
99  }
100
101  void VisitStmt(clang::Stmt *S);
102  void VisitDeclStmt(clang::DeclStmt *DS);
103  void VisitCompoundStmt(clang::CompoundStmt *CS);
104  void VisitBinAssign(clang::BinaryOperator *AS);
105
106  // We believe that RS objects are never involved in CompoundAssignOperator.
107  // I.e., rs_allocation foo; foo += bar;
108};
109
110}  // namespace slang
111
112#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_OBJECT_REF_COUNT_H_  NOLINT
113