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