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