slang_rs_object_ref_count.h revision 4464d825c11349068f2917f9ebee86b721423f3c
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}
30
31namespace slang {
32
33class RSObjectRefCount : public clang::StmtVisitor<RSObjectRefCount> {
34 private:
35  class Scope {
36   private:
37    clang::CompoundStmt *mCS;      // Associated compound statement ({ ... })
38    std::list<clang::VarDecl*> mRSO;  // Declared RS objects in this scope
39
40    // RSSetObjectFD and RSClearObjectFD holds FunctionDecl of rsSetObject()
41    // and rsClearObject() in the current ASTContext.
42    static clang::FunctionDecl *RSSetObjectFD[];
43    static clang::FunctionDecl *RSClearObjectFD[];
44
45   public:
46    explicit Scope(clang::CompoundStmt *CS) : mCS(CS) {
47      return;
48    }
49
50    inline void addRSObject(clang::VarDecl* VD) {
51      mRSO.push_back(VD);
52      return;
53    }
54
55    // Initialize RSSetObjectFD and RSClearObjectFD.
56    static void GetRSRefCountingFunctions(clang::ASTContext &C);
57
58    void InsertLocalVarDestructors();
59
60    static clang::Expr *ClearRSObject(clang::VarDecl *VD);
61  };
62
63  std::stack<Scope*> mScopeStack;
64  bool RSInitFD;
65
66  inline Scope *getCurrentScope() {
67    return mScopeStack.top();
68  }
69
70  // TODO(srhines): Composite types and arrays based on RS object types need
71  // to be handled for both zero-initialization + clearing.
72
73  // Return false if the type of variable declared in VD is not an RS object
74  // type.
75  static bool InitializeRSObject(clang::VarDecl *VD);
76
77  // Return a zero-initializer expr of the type DT. This processes both
78  // RS matrix type and RS object type.
79  static clang::Expr *CreateZeroInitializerForRSSpecificType(
80      RSExportPrimitiveType::DataType DT,
81      clang::ASTContext &C,
82      const clang::SourceLocation &Loc);
83
84 public:
85  RSObjectRefCount()
86      : RSInitFD(false) {
87    return;
88  }
89
90  void Init(clang::ASTContext &C) {
91    if (!RSInitFD) {
92      Scope::GetRSRefCountingFunctions(C);
93      RSInitFD = true;
94    }
95    return;
96  }
97
98  void VisitStmt(clang::Stmt *S);
99  void VisitDeclStmt(clang::DeclStmt *DS);
100  void VisitCompoundStmt(clang::CompoundStmt *CS);
101  void VisitBinAssign(clang::BinaryOperator *AS);
102
103  // We believe that RS objects are never involved in CompoundAssignOperator.
104  // I.e., rs_allocation foo; foo += bar;
105};
106
107}  // namespace slang
108
109#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_OBJECT_REF_COUNT_H_  NOLINT
110