Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 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 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 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> |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 21 | #include <stack> |
| 22 | |
| 23 | #include "clang/AST/StmtVisitor.h" |
| 24 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 25 | #include "slang_assert.h" |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 26 | #include "slang_rs_export_type.h" |
| 27 | |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 28 | namespace clang { |
| 29 | class Expr; |
Stephen Hines | d5f9d6c | 2010-12-15 16:11:29 -0800 | [diff] [blame] | 30 | class Stmt; |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 33 | namespace slang { |
| 34 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 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. |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 45 | class RSObjectRefCount : public clang::StmtVisitor<RSObjectRefCount> { |
| 46 | private: |
| 47 | class Scope { |
| 48 | private: |
| 49 | clang::CompoundStmt *mCS; // Associated compound statement ({ ... }) |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 50 | std::list<clang::VarDecl*> mRSO; // Declared RS objects in this scope |
| 51 | |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 52 | public: |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 53 | explicit Scope(clang::CompoundStmt *CS) : mCS(CS) { |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 57 | inline void addRSObject(clang::VarDecl* VD) { |
| 58 | mRSO.push_back(VD); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 59 | return; |
| 60 | } |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 61 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 62 | void ReplaceRSObjectAssignment(clang::BinaryOperator *AS); |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 63 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 64 | void AppendRSObjectInit(clang::VarDecl *VD, |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 65 | clang::DeclStmt *DS, |
| 66 | RSExportPrimitiveType::DataType DT, |
| 67 | clang::Expr *InitExpr); |
| 68 | |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 69 | void InsertLocalVarDestructors(); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 70 | |
Stephen Hines | 3f175af | 2011-09-16 16:26:29 -0700 | [diff] [blame] | 71 | static clang::Stmt *ClearRSObject(clang::VarDecl *VD, |
| 72 | clang::DeclContext *DC); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 75 | clang::ASTContext &mCtx; |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 76 | std::stack<Scope*> mScopeStack; |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 77 | bool RSInitFD; |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 78 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 79 | // RSSetObjectFD and RSClearObjectFD holds FunctionDecl of rsSetObject() |
| 80 | // and rsClearObject() in the current ASTContext. |
| 81 | static clang::FunctionDecl *RSSetObjectFD[]; |
| 82 | static clang::FunctionDecl *RSClearObjectFD[]; |
| 83 | |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 84 | inline Scope *getCurrentScope() { |
| 85 | return mScopeStack.top(); |
| 86 | } |
| 87 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 88 | // Initialize RSSetObjectFD and RSClearObjectFD. |
| 89 | static void GetRSRefCountingFunctions(clang::ASTContext &C); |
| 90 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 91 | // Return false if the type of variable declared in VD does not contain |
| 92 | // an RS object type. |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 93 | static bool InitializeRSObject(clang::VarDecl *VD, |
| 94 | RSExportPrimitiveType::DataType *DT, |
| 95 | clang::Expr **InitExpr); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 96 | |
| 97 | // Return a zero-initializer expr of the type DT. This processes both |
| 98 | // RS matrix type and RS object type. |
| 99 | static clang::Expr *CreateZeroInitializerForRSSpecificType( |
| 100 | RSExportPrimitiveType::DataType DT, |
| 101 | clang::ASTContext &C, |
| 102 | const clang::SourceLocation &Loc); |
| 103 | |
| 104 | public: |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 105 | explicit RSObjectRefCount(clang::ASTContext &C) |
| 106 | : mCtx(C), |
| 107 | RSInitFD(false) { |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 111 | void Init() { |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 112 | if (!RSInitFD) { |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 113 | GetRSRefCountingFunctions(mCtx); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 114 | RSInitFD = true; |
| 115 | } |
| 116 | return; |
| 117 | } |
| 118 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 119 | static clang::FunctionDecl *GetRSSetObjectFD( |
| 120 | RSExportPrimitiveType::DataType DT) { |
| 121 | slangAssert(RSExportPrimitiveType::IsRSObjectType(DT)); |
| 122 | return RSSetObjectFD[(DT - RSExportPrimitiveType::FirstRSObjectType)]; |
| 123 | } |
| 124 | |
| 125 | static clang::FunctionDecl *GetRSSetObjectFD(const clang::Type *T) { |
| 126 | return GetRSSetObjectFD(RSExportPrimitiveType::GetRSSpecificType(T)); |
| 127 | } |
| 128 | |
| 129 | static clang::FunctionDecl *GetRSClearObjectFD( |
| 130 | RSExportPrimitiveType::DataType DT) { |
| 131 | slangAssert(RSExportPrimitiveType::IsRSObjectType(DT)); |
| 132 | return RSClearObjectFD[(DT - RSExportPrimitiveType::FirstRSObjectType)]; |
| 133 | } |
| 134 | |
| 135 | static clang::FunctionDecl *GetRSClearObjectFD(const clang::Type *T) { |
| 136 | return GetRSClearObjectFD(RSExportPrimitiveType::GetRSSpecificType(T)); |
| 137 | } |
| 138 | |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 139 | void VisitStmt(clang::Stmt *S); |
| 140 | void VisitDeclStmt(clang::DeclStmt *DS); |
| 141 | void VisitCompoundStmt(clang::CompoundStmt *CS); |
| 142 | void VisitBinAssign(clang::BinaryOperator *AS); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 143 | // We believe that RS objects are never involved in CompoundAssignOperator. |
| 144 | // I.e., rs_allocation foo; foo += bar; |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 145 | |
| 146 | // Emit a global destructor to clean up RS objects. |
| 147 | clang::FunctionDecl *CreateStaticGlobalDtor(); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 150 | } // namespace slang |
| 151 | |
| 152 | #endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_OBJECT_REF_COUNT_H_ NOLINT |