slang_rs_object_ref_count.h revision e79fb5ec220e20bd04cd6f6059cbc9748181ce21
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 AppendRSObjectInit(clang::VarDecl *VD,
62                            clang::DeclStmt *DS,
63                            RSExportPrimitiveType::DataType DT,
64                            clang::Expr *InitExpr);
65
66    void InsertLocalVarDestructors();
67
68    static clang::Stmt *ClearRSObject(clang::VarDecl *VD);
69  };
70
71  std::stack<Scope*> mScopeStack;
72  bool RSInitFD;
73
74  inline Scope *getCurrentScope() {
75    return mScopeStack.top();
76  }
77
78  // TODO(srhines): Composite types and arrays based on RS object types need
79  // to be handled for both zero-initialization + clearing.
80
81  // Return false if the type of variable declared in VD is not an RS object
82  // type.
83  static bool InitializeRSObject(clang::VarDecl *VD,
84                                 RSExportPrimitiveType::DataType *DT,
85                                 clang::Expr **InitExpr);
86
87  // Return a zero-initializer expr of the type DT. This processes both
88  // RS matrix type and RS object type.
89  static clang::Expr *CreateZeroInitializerForRSSpecificType(
90      RSExportPrimitiveType::DataType DT,
91      clang::ASTContext &C,
92      const clang::SourceLocation &Loc);
93
94 public:
95  RSObjectRefCount()
96      : RSInitFD(false) {
97    return;
98  }
99
100  void Init(clang::ASTContext &C) {
101    if (!RSInitFD) {
102      Scope::GetRSRefCountingFunctions(C);
103      RSInitFD = true;
104    }
105    return;
106  }
107
108  void VisitStmt(clang::Stmt *S);
109  void VisitDeclStmt(clang::DeclStmt *DS);
110  void VisitCompoundStmt(clang::CompoundStmt *CS);
111  void VisitBinAssign(clang::BinaryOperator *AS);
112
113  // We believe that RS objects are never involved in CompoundAssignOperator.
114  // I.e., rs_allocation foo; foo += bar;
115};
116
117}  // namespace slang
118
119#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_OBJECT_REF_COUNT_H_  NOLINT
120