slang_rs_object_ref_count.h revision 688e64b2d56e4218c680b9d6523c5de672f55757
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_assert.h"
26#include "slang_rs_export_type.h"
27
28namespace clang {
29  class Expr;
30  class Stmt;
31}
32
33namespace slang {
34
35// This class provides the overall reference counting mechanism for handling
36// local variables of RS object types (rs_font, rs_allocation, ...). This
37// class ensures that appropriate functions (rsSetObject, rsClearObject) are
38// called at proper points in the object's lifetime.
39// 1) Each local object of appropriate type must be zero-initialized (to
40// prevent corruption) during subsequent rsSetObject()/rsClearObject() calls.
41// 2) Assignments using these types must also be converted into the
42// appropriate (possibly a series of) rsSetObject() calls.
43// 3) Finally, each local object must call rsClearObject() when it goes out
44// of scope.
45class RSObjectRefCount : public clang::StmtVisitor<RSObjectRefCount> {
46 private:
47  class Scope {
48   private:
49    clang::CompoundStmt *mCS;      // Associated compound statement ({ ... })
50    std::list<clang::VarDecl*> mRSO;  // Declared RS objects in this scope
51
52   public:
53    explicit Scope(clang::CompoundStmt *CS) : mCS(CS) {
54      return;
55    }
56
57    inline void addRSObject(clang::VarDecl* VD) {
58      mRSO.push_back(VD);
59      return;
60    }
61
62    void ReplaceRSObjectAssignment(clang::BinaryOperator *AS);
63
64    void AppendRSObjectInit(clang::VarDecl *VD,
65                            clang::DeclStmt *DS,
66                            RSExportPrimitiveType::DataType DT,
67                            clang::Expr *InitExpr);
68
69    void InsertLocalVarDestructors();
70
71    static clang::Stmt *ClearRSObject(clang::VarDecl *VD);
72  };
73
74  clang::ASTContext &mCtx;
75  std::stack<Scope*> mScopeStack;
76  bool RSInitFD;
77
78  // RSSetObjectFD and RSClearObjectFD holds FunctionDecl of rsSetObject()
79  // and rsClearObject() in the current ASTContext.
80  static clang::FunctionDecl *RSSetObjectFD[];
81  static clang::FunctionDecl *RSClearObjectFD[];
82
83  inline Scope *getCurrentScope() {
84    return mScopeStack.top();
85  }
86
87  // Initialize RSSetObjectFD and RSClearObjectFD.
88  static void GetRSRefCountingFunctions(clang::ASTContext &C);
89
90  // Return false if the type of variable declared in VD does not contain
91  // an RS object type.
92  static bool InitializeRSObject(clang::VarDecl *VD,
93                                 RSExportPrimitiveType::DataType *DT,
94                                 clang::Expr **InitExpr);
95
96  // Return a zero-initializer expr of the type DT. This processes both
97  // RS matrix type and RS object type.
98  static clang::Expr *CreateZeroInitializerForRSSpecificType(
99      RSExportPrimitiveType::DataType DT,
100      clang::ASTContext &C,
101      const clang::SourceLocation &Loc);
102
103 public:
104  explicit RSObjectRefCount(clang::ASTContext &C)
105      : mCtx(C),
106        RSInitFD(false) {
107    return;
108  }
109
110  void Init() {
111    if (!RSInitFD) {
112      GetRSRefCountingFunctions(mCtx);
113      RSInitFD = true;
114    }
115    return;
116  }
117
118  static clang::FunctionDecl *GetRSSetObjectFD(
119      RSExportPrimitiveType::DataType DT) {
120    slangAssert(RSExportPrimitiveType::IsRSObjectType(DT));
121    return RSSetObjectFD[(DT - RSExportPrimitiveType::FirstRSObjectType)];
122  }
123
124  static clang::FunctionDecl *GetRSSetObjectFD(const clang::Type *T) {
125    return GetRSSetObjectFD(RSExportPrimitiveType::GetRSSpecificType(T));
126  }
127
128  static clang::FunctionDecl *GetRSClearObjectFD(
129      RSExportPrimitiveType::DataType DT) {
130    slangAssert(RSExportPrimitiveType::IsRSObjectType(DT));
131    return RSClearObjectFD[(DT - RSExportPrimitiveType::FirstRSObjectType)];
132  }
133
134  static clang::FunctionDecl *GetRSClearObjectFD(const clang::Type *T) {
135    return GetRSClearObjectFD(RSExportPrimitiveType::GetRSSpecificType(T));
136  }
137
138  void VisitStmt(clang::Stmt *S);
139  void VisitDeclStmt(clang::DeclStmt *DS);
140  void VisitCompoundStmt(clang::CompoundStmt *CS);
141  void VisitBinAssign(clang::BinaryOperator *AS);
142  // We believe that RS objects are never involved in CompoundAssignOperator.
143  // I.e., rs_allocation foo; foo += bar;
144
145  // Emit a global destructor to clean up RS objects.
146  clang::FunctionDecl *CreateStaticGlobalDtor();
147};
148
149}  // namespace slang
150
151#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_OBJECT_REF_COUNT_H_  NOLINT
152