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 | |
| 17 | #include "slang_rs_object_ref_count.h" |
| 18 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 19 | #include <list> |
| 20 | |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 21 | #include "clang/AST/DeclGroup.h" |
| 22 | #include "clang/AST/Expr.h" |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 23 | #include "clang/AST/NestedNameSpecifier.h" |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 24 | #include "clang/AST/OperationKinds.h" |
| 25 | #include "clang/AST/Stmt.h" |
| 26 | #include "clang/AST/StmtVisitor.h" |
| 27 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 28 | #include "slang_assert.h" |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 29 | #include "slang_rs.h" |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 30 | #include "slang_rs_ast_replace.h" |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 31 | #include "slang_rs_export_type.h" |
| 32 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 33 | namespace slang { |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 34 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 35 | clang::FunctionDecl *RSObjectRefCount:: |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 36 | RSSetObjectFD[RSExportPrimitiveType::LastRSObjectType - |
| 37 | RSExportPrimitiveType::FirstRSObjectType + 1]; |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 38 | clang::FunctionDecl *RSObjectRefCount:: |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 39 | RSClearObjectFD[RSExportPrimitiveType::LastRSObjectType - |
| 40 | RSExportPrimitiveType::FirstRSObjectType + 1]; |
| 41 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 42 | void RSObjectRefCount::GetRSRefCountingFunctions(clang::ASTContext &C) { |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 43 | for (unsigned i = 0; |
| 44 | i < (sizeof(RSClearObjectFD) / sizeof(clang::FunctionDecl*)); |
| 45 | i++) { |
| 46 | RSSetObjectFD[i] = NULL; |
| 47 | RSClearObjectFD[i] = NULL; |
| 48 | } |
| 49 | |
| 50 | clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); |
| 51 | |
| 52 | for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(), |
| 53 | E = TUDecl->decls_end(); I != E; I++) { |
| 54 | if ((I->getKind() >= clang::Decl::firstFunction) && |
| 55 | (I->getKind() <= clang::Decl::lastFunction)) { |
| 56 | clang::FunctionDecl *FD = static_cast<clang::FunctionDecl*>(*I); |
| 57 | |
| 58 | // points to RSSetObjectFD or RSClearObjectFD |
| 59 | clang::FunctionDecl **RSObjectFD; |
| 60 | |
| 61 | if (FD->getName() == "rsSetObject") { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 62 | slangAssert((FD->getNumParams() == 2) && |
| 63 | "Invalid rsSetObject function prototype (# params)"); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 64 | RSObjectFD = RSSetObjectFD; |
| 65 | } else if (FD->getName() == "rsClearObject") { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 66 | slangAssert((FD->getNumParams() == 1) && |
| 67 | "Invalid rsClearObject function prototype (# params)"); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 68 | RSObjectFD = RSClearObjectFD; |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 69 | } else { |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 70 | continue; |
| 71 | } |
| 72 | |
| 73 | const clang::ParmVarDecl *PVD = FD->getParamDecl(0); |
| 74 | clang::QualType PVT = PVD->getOriginalType(); |
| 75 | // The first parameter must be a pointer like rs_allocation* |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 76 | slangAssert(PVT->isPointerType() && |
| 77 | "Invalid rs{Set,Clear}Object function prototype (pointer param)"); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 78 | |
| 79 | // The rs object type passed to the FD |
| 80 | clang::QualType RST = PVT->getPointeeType(); |
| 81 | RSExportPrimitiveType::DataType DT = |
| 82 | RSExportPrimitiveType::GetRSSpecificType(RST.getTypePtr()); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 83 | slangAssert(RSExportPrimitiveType::IsRSObjectType(DT) |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 84 | && "must be RS object type"); |
| 85 | |
| 86 | RSObjectFD[(DT - RSExportPrimitiveType::FirstRSObjectType)] = FD; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Stephen Hines | 4464d82 | 2010-11-11 16:45:08 -0800 | [diff] [blame] | 91 | namespace { |
| 92 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 93 | // This function constructs a new CompoundStmt from the input StmtList. |
| 94 | static clang::CompoundStmt* BuildCompoundStmt(clang::ASTContext &C, |
| 95 | std::list<clang::Stmt*> &StmtList, clang::SourceLocation Loc) { |
Stephen Hines | d5f9d6c | 2010-12-15 16:11:29 -0800 | [diff] [blame] | 96 | unsigned NewStmtCount = StmtList.size(); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 97 | unsigned CompoundStmtCount = 0; |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 98 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 99 | clang::Stmt **CompoundStmtList; |
| 100 | CompoundStmtList = new clang::Stmt*[NewStmtCount]; |
| 101 | |
| 102 | std::list<clang::Stmt*>::const_iterator I = StmtList.begin(); |
| 103 | std::list<clang::Stmt*>::const_iterator E = StmtList.end(); |
| 104 | for ( ; I != E; I++) { |
| 105 | CompoundStmtList[CompoundStmtCount++] = *I; |
| 106 | } |
| 107 | slangAssert(CompoundStmtCount == NewStmtCount); |
| 108 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 109 | clang::CompoundStmt *CS = new(C) clang::CompoundStmt( |
| 110 | C, llvm::makeArrayRef(CompoundStmtList, CompoundStmtCount), Loc, Loc); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 111 | |
| 112 | delete [] CompoundStmtList; |
| 113 | |
| 114 | return CS; |
| 115 | } |
| 116 | |
| 117 | static void AppendAfterStmt(clang::ASTContext &C, |
| 118 | clang::CompoundStmt *CS, |
| 119 | clang::Stmt *S, |
| 120 | std::list<clang::Stmt*> &StmtList) { |
| 121 | slangAssert(CS); |
| 122 | clang::CompoundStmt::body_iterator bI = CS->body_begin(); |
| 123 | clang::CompoundStmt::body_iterator bE = CS->body_end(); |
| 124 | clang::Stmt **UpdatedStmtList = |
| 125 | new clang::Stmt*[CS->size() + StmtList.size()]; |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 126 | |
| 127 | unsigned UpdatedStmtCount = 0; |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 128 | unsigned Once = 0; |
| 129 | for ( ; bI != bE; bI++) { |
| 130 | if (!S && ((*bI)->getStmtClass() == clang::Stmt::ReturnStmtClass)) { |
| 131 | // If we come across a return here, we don't have anything we can |
| 132 | // reasonably replace. We should have already inserted our destructor |
| 133 | // code in the proper spot, so we just clean up and return. |
| 134 | delete [] UpdatedStmtList; |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 135 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
| 139 | UpdatedStmtList[UpdatedStmtCount++] = *bI; |
| 140 | |
| 141 | if ((*bI == S) && !Once) { |
| 142 | Once++; |
| 143 | std::list<clang::Stmt*>::const_iterator I = StmtList.begin(); |
| 144 | std::list<clang::Stmt*>::const_iterator E = StmtList.end(); |
| 145 | for ( ; I != E; I++) { |
| 146 | UpdatedStmtList[UpdatedStmtCount++] = *I; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | slangAssert(Once <= 1); |
| 151 | |
| 152 | // When S is NULL, we are appending to the end of the CompoundStmt. |
| 153 | if (!S) { |
| 154 | slangAssert(Once == 0); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 155 | std::list<clang::Stmt*>::const_iterator I = StmtList.begin(); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 156 | std::list<clang::Stmt*>::const_iterator E = StmtList.end(); |
| 157 | for ( ; I != E; I++) { |
Stephen Hines | d5f9d6c | 2010-12-15 16:11:29 -0800 | [diff] [blame] | 158 | UpdatedStmtList[UpdatedStmtCount++] = *I; |
Stephen Hines | 4464d82 | 2010-11-11 16:45:08 -0800 | [diff] [blame] | 159 | } |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Stephen Hines | d5f9d6c | 2010-12-15 16:11:29 -0800 | [diff] [blame] | 162 | CS->setStmts(C, UpdatedStmtList, UpdatedStmtCount); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 163 | |
Stephen Hines | d5f9d6c | 2010-12-15 16:11:29 -0800 | [diff] [blame] | 164 | delete [] UpdatedStmtList; |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 165 | |
| 166 | return; |
| 167 | } |
| 168 | |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 169 | // This class visits a compound statement and inserts DtorStmt |
| 170 | // in proper locations. This includes inserting it before any |
Stephen Hines | 4464d82 | 2010-11-11 16:45:08 -0800 | [diff] [blame] | 171 | // return statement in any sub-block, at the end of the logical enclosing |
| 172 | // scope (compound statement), and/or before any break/continue statement that |
| 173 | // would resume outside the declared scope. We will not handle the case for |
| 174 | // goto statements that leave a local scope. |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 175 | // |
| 176 | // To accomplish these goals, it collects a list of sub-Stmt's that |
| 177 | // correspond to scope exit points. It then uses an RSASTReplace visitor to |
| 178 | // transform the AST, inserting appropriate destructors before each of those |
| 179 | // sub-Stmt's (and also before the exit of the outermost containing Stmt for |
| 180 | // the scope). |
Stephen Hines | 4464d82 | 2010-11-11 16:45:08 -0800 | [diff] [blame] | 181 | class DestructorVisitor : public clang::StmtVisitor<DestructorVisitor> { |
| 182 | private: |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 183 | clang::ASTContext &mCtx; |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 184 | |
| 185 | // The loop depth of the currently visited node. |
| 186 | int mLoopDepth; |
| 187 | |
| 188 | // The switch statement depth of the currently visited node. |
| 189 | // Note that this is tracked separately from the loop depth because |
| 190 | // SwitchStmt-contained ContinueStmt's should have destructors for the |
| 191 | // corresponding loop scope. |
| 192 | int mSwitchDepth; |
| 193 | |
| 194 | // The outermost statement block that we are currently visiting. |
| 195 | // This should always be a CompoundStmt. |
| 196 | clang::Stmt *mOuterStmt; |
| 197 | |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 198 | // The destructor to execute for this scope/variable. |
| 199 | clang::Stmt* mDtorStmt; |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 200 | |
| 201 | // The stack of statements which should be replaced by a compound statement |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 202 | // containing the new destructor call followed by the original Stmt. |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 203 | std::stack<clang::Stmt*> mReplaceStmtStack; |
| 204 | |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 205 | // The source location for the variable declaration that we are trying to |
| 206 | // insert destructors for. Note that InsertDestructors() will not generate |
| 207 | // destructor calls for source locations that occur lexically before this |
| 208 | // location. |
| 209 | clang::SourceLocation mVarLoc; |
| 210 | |
Stephen Hines | 4464d82 | 2010-11-11 16:45:08 -0800 | [diff] [blame] | 211 | public: |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 212 | DestructorVisitor(clang::ASTContext &C, |
| 213 | clang::Stmt* OuterStmt, |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 214 | clang::Stmt* DtorStmt, |
| 215 | clang::SourceLocation VarLoc); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 216 | |
| 217 | // This code walks the collected list of Stmts to replace and actually does |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 218 | // the replacement. It also finishes up by appending the destructor to the |
| 219 | // current outermost CompoundStmt. |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 220 | void InsertDestructors() { |
| 221 | clang::Stmt *S = NULL; |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 222 | clang::SourceManager &SM = mCtx.getSourceManager(); |
| 223 | std::list<clang::Stmt *> StmtList; |
| 224 | StmtList.push_back(mDtorStmt); |
| 225 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 226 | while (!mReplaceStmtStack.empty()) { |
| 227 | S = mReplaceStmtStack.top(); |
| 228 | mReplaceStmtStack.pop(); |
| 229 | |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 230 | // Skip all source locations that occur before the variable's |
| 231 | // declaration, since it won't have been initialized yet. |
| 232 | if (SM.isBeforeInTranslationUnit(S->getLocStart(), mVarLoc)) { |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | StmtList.push_back(S); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 237 | clang::CompoundStmt *CS = |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 238 | BuildCompoundStmt(mCtx, StmtList, S->getLocEnd()); |
| 239 | StmtList.pop_back(); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 240 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 241 | RSASTReplace R(mCtx); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 242 | R.ReplaceStmt(mOuterStmt, S, CS); |
| 243 | } |
Logan Chien | ab992e5 | 2011-07-20 22:06:52 +0800 | [diff] [blame] | 244 | clang::CompoundStmt *CS = |
| 245 | llvm::dyn_cast<clang::CompoundStmt>(mOuterStmt); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 246 | slangAssert(CS); |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 247 | AppendAfterStmt(mCtx, CS, NULL, StmtList); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Stephen Hines | 4464d82 | 2010-11-11 16:45:08 -0800 | [diff] [blame] | 250 | void VisitStmt(clang::Stmt *S); |
| 251 | void VisitCompoundStmt(clang::CompoundStmt *CS); |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 252 | |
| 253 | void VisitBreakStmt(clang::BreakStmt *BS); |
| 254 | void VisitCaseStmt(clang::CaseStmt *CS); |
| 255 | void VisitContinueStmt(clang::ContinueStmt *CS); |
| 256 | void VisitDefaultStmt(clang::DefaultStmt *DS); |
| 257 | void VisitDoStmt(clang::DoStmt *DS); |
| 258 | void VisitForStmt(clang::ForStmt *FS); |
| 259 | void VisitIfStmt(clang::IfStmt *IS); |
| 260 | void VisitReturnStmt(clang::ReturnStmt *RS); |
| 261 | void VisitSwitchCase(clang::SwitchCase *SC); |
| 262 | void VisitSwitchStmt(clang::SwitchStmt *SS); |
| 263 | void VisitWhileStmt(clang::WhileStmt *WS); |
Stephen Hines | 4464d82 | 2010-11-11 16:45:08 -0800 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | DestructorVisitor::DestructorVisitor(clang::ASTContext &C, |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 267 | clang::Stmt *OuterStmt, |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 268 | clang::Stmt *DtorStmt, |
| 269 | clang::SourceLocation VarLoc) |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 270 | : mCtx(C), |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 271 | mLoopDepth(0), |
| 272 | mSwitchDepth(0), |
| 273 | mOuterStmt(OuterStmt), |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 274 | mDtorStmt(DtorStmt), |
| 275 | mVarLoc(VarLoc) { |
Stephen Hines | 4464d82 | 2010-11-11 16:45:08 -0800 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
| 279 | void DestructorVisitor::VisitStmt(clang::Stmt *S) { |
| 280 | for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end(); |
| 281 | I != E; |
| 282 | I++) { |
| 283 | if (clang::Stmt *Child = *I) { |
| 284 | Visit(Child); |
| 285 | } |
| 286 | } |
| 287 | return; |
| 288 | } |
| 289 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 290 | void DestructorVisitor::VisitCompoundStmt(clang::CompoundStmt *CS) { |
| 291 | VisitStmt(CS); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | void DestructorVisitor::VisitBreakStmt(clang::BreakStmt *BS) { |
| 296 | VisitStmt(BS); |
| 297 | if ((mLoopDepth == 0) && (mSwitchDepth == 0)) { |
| 298 | mReplaceStmtStack.push(BS); |
| 299 | } |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | void DestructorVisitor::VisitCaseStmt(clang::CaseStmt *CS) { |
| 304 | VisitStmt(CS); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | void DestructorVisitor::VisitContinueStmt(clang::ContinueStmt *CS) { |
| 309 | VisitStmt(CS); |
| 310 | if (mLoopDepth == 0) { |
| 311 | // Switch statements can have nested continues. |
| 312 | mReplaceStmtStack.push(CS); |
| 313 | } |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | void DestructorVisitor::VisitDefaultStmt(clang::DefaultStmt *DS) { |
| 318 | VisitStmt(DS); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | void DestructorVisitor::VisitDoStmt(clang::DoStmt *DS) { |
| 323 | mLoopDepth++; |
| 324 | VisitStmt(DS); |
| 325 | mLoopDepth--; |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | void DestructorVisitor::VisitForStmt(clang::ForStmt *FS) { |
| 330 | mLoopDepth++; |
| 331 | VisitStmt(FS); |
| 332 | mLoopDepth--; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | void DestructorVisitor::VisitIfStmt(clang::IfStmt *IS) { |
| 337 | VisitStmt(IS); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | void DestructorVisitor::VisitReturnStmt(clang::ReturnStmt *RS) { |
| 342 | mReplaceStmtStack.push(RS); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | void DestructorVisitor::VisitSwitchCase(clang::SwitchCase *SC) { |
| 347 | slangAssert(false && "Both case and default have specialized handlers"); |
| 348 | VisitStmt(SC); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | void DestructorVisitor::VisitSwitchStmt(clang::SwitchStmt *SS) { |
| 353 | mSwitchDepth++; |
| 354 | VisitStmt(SS); |
| 355 | mSwitchDepth--; |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | void DestructorVisitor::VisitWhileStmt(clang::WhileStmt *WS) { |
| 360 | mLoopDepth++; |
| 361 | VisitStmt(WS); |
| 362 | mLoopDepth--; |
| 363 | return; |
| 364 | } |
| 365 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 366 | clang::Expr *ClearSingleRSObject(clang::ASTContext &C, |
| 367 | clang::Expr *RefRSVar, |
| 368 | clang::SourceLocation Loc) { |
| 369 | slangAssert(RefRSVar); |
| 370 | const clang::Type *T = RefRSVar->getType().getTypePtr(); |
| 371 | slangAssert(!T->isArrayType() && |
| 372 | "Should not be destroying arrays with this function"); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 373 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 374 | clang::FunctionDecl *ClearObjectFD = RSObjectRefCount::GetRSClearObjectFD(T); |
| 375 | slangAssert((ClearObjectFD != NULL) && |
| 376 | "rsClearObject doesn't cover all RS object types"); |
| 377 | |
| 378 | clang::QualType ClearObjectFDType = ClearObjectFD->getType(); |
| 379 | clang::QualType ClearObjectFDArgType = |
| 380 | ClearObjectFD->getParamDecl(0)->getOriginalType(); |
| 381 | |
| 382 | // Example destructor for "rs_font localFont;" |
| 383 | // |
| 384 | // (CallExpr 'void' |
| 385 | // (ImplicitCastExpr 'void (*)(rs_font *)' <FunctionToPointerDecay> |
| 386 | // (DeclRefExpr 'void (rs_font *)' FunctionDecl='rsClearObject')) |
| 387 | // (UnaryOperator 'rs_font *' prefix '&' |
| 388 | // (DeclRefExpr 'rs_font':'rs_font' Var='localFont'))) |
| 389 | |
| 390 | // Get address of targeted RS object |
| 391 | clang::Expr *AddrRefRSVar = |
| 392 | new(C) clang::UnaryOperator(RefRSVar, |
| 393 | clang::UO_AddrOf, |
| 394 | ClearObjectFDArgType, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 395 | clang::VK_RValue, |
| 396 | clang::OK_Ordinary, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 397 | Loc); |
| 398 | |
| 399 | clang::Expr *RefRSClearObjectFD = |
| 400 | clang::DeclRefExpr::Create(C, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 401 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 402 | clang::SourceLocation(), |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 403 | ClearObjectFD, |
Shih-wei Liao | e2597ac | 2012-03-24 02:44:33 -0700 | [diff] [blame] | 404 | false, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 405 | ClearObjectFD->getLocation(), |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 406 | ClearObjectFDType, |
| 407 | clang::VK_RValue, |
| 408 | NULL); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 409 | |
| 410 | clang::Expr *RSClearObjectFP = |
| 411 | clang::ImplicitCastExpr::Create(C, |
| 412 | C.getPointerType(ClearObjectFDType), |
| 413 | clang::CK_FunctionToPointerDecay, |
| 414 | RefRSClearObjectFD, |
| 415 | NULL, |
| 416 | clang::VK_RValue); |
| 417 | |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 418 | llvm::SmallVector<clang::Expr*, 1> ArgList; |
| 419 | ArgList.push_back(AddrRefRSVar); |
| 420 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 421 | clang::CallExpr *RSClearObjectCall = |
| 422 | new(C) clang::CallExpr(C, |
| 423 | RSClearObjectFP, |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 424 | ArgList, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 425 | ClearObjectFD->getCallResultType(), |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 426 | clang::VK_RValue, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 427 | Loc); |
| 428 | |
| 429 | return RSClearObjectCall; |
| 430 | } |
| 431 | |
| 432 | static int ArrayDim(const clang::Type *T) { |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 433 | if (!T || !T->isArrayType()) { |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | const clang::ConstantArrayType *CAT = |
| 438 | static_cast<const clang::ConstantArrayType *>(T); |
Stephen Hines | 9d2c0fa | 2011-01-05 14:55:18 -0800 | [diff] [blame] | 439 | return static_cast<int>(CAT->getSize().getSExtValue()); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 442 | static clang::Stmt *ClearStructRSObject( |
| 443 | clang::ASTContext &C, |
| 444 | clang::DeclContext *DC, |
| 445 | clang::Expr *RefRSStruct, |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 446 | clang::SourceLocation StartLoc, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 447 | clang::SourceLocation Loc); |
| 448 | |
| 449 | static clang::Stmt *ClearArrayRSObject( |
| 450 | clang::ASTContext &C, |
| 451 | clang::DeclContext *DC, |
| 452 | clang::Expr *RefRSArr, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 453 | clang::SourceLocation StartLoc, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 454 | clang::SourceLocation Loc) { |
| 455 | const clang::Type *BaseType = RefRSArr->getType().getTypePtr(); |
| 456 | slangAssert(BaseType->isArrayType()); |
| 457 | |
| 458 | int NumArrayElements = ArrayDim(BaseType); |
| 459 | // Actually extract out the base RS object type for use later |
| 460 | BaseType = BaseType->getArrayElementTypeNoTypeQual(); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 461 | |
| 462 | clang::Stmt *StmtArray[2] = {NULL}; |
| 463 | int StmtCtr = 0; |
| 464 | |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 465 | if (NumArrayElements <= 0) { |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | // Example destructor loop for "rs_font fontArr[10];" |
| 470 | // |
| 471 | // (CompoundStmt |
| 472 | // (DeclStmt "int rsIntIter") |
| 473 | // (ForStmt |
| 474 | // (BinaryOperator 'int' '=' |
| 475 | // (DeclRefExpr 'int' Var='rsIntIter') |
| 476 | // (IntegerLiteral 'int' 0)) |
| 477 | // (BinaryOperator 'int' '<' |
| 478 | // (DeclRefExpr 'int' Var='rsIntIter') |
| 479 | // (IntegerLiteral 'int' 10) |
| 480 | // NULL << CondVar >> |
| 481 | // (UnaryOperator 'int' postfix '++' |
| 482 | // (DeclRefExpr 'int' Var='rsIntIter')) |
| 483 | // (CallExpr 'void' |
| 484 | // (ImplicitCastExpr 'void (*)(rs_font *)' <FunctionToPointerDecay> |
| 485 | // (DeclRefExpr 'void (rs_font *)' FunctionDecl='rsClearObject')) |
| 486 | // (UnaryOperator 'rs_font *' prefix '&' |
| 487 | // (ArraySubscriptExpr 'rs_font':'rs_font' |
| 488 | // (ImplicitCastExpr 'rs_font *' <ArrayToPointerDecay> |
| 489 | // (DeclRefExpr 'rs_font [10]' Var='fontArr')) |
| 490 | // (DeclRefExpr 'int' Var='rsIntIter'))))))) |
| 491 | |
| 492 | // Create helper variable for iterating through elements |
| 493 | clang::IdentifierInfo& II = C.Idents.get("rsIntIter"); |
| 494 | clang::VarDecl *IIVD = |
| 495 | clang::VarDecl::Create(C, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 496 | DC, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 497 | StartLoc, |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 498 | Loc, |
| 499 | &II, |
| 500 | C.IntTy, |
| 501 | C.getTrivialTypeSourceInfo(C.IntTy), |
| 502 | clang::SC_None, |
| 503 | clang::SC_None); |
| 504 | clang::Decl *IID = (clang::Decl *)IIVD; |
| 505 | |
| 506 | clang::DeclGroupRef DGR = clang::DeclGroupRef::Create(C, &IID, 1); |
| 507 | StmtArray[StmtCtr++] = new(C) clang::DeclStmt(DGR, Loc, Loc); |
| 508 | |
| 509 | // Form the actual destructor loop |
| 510 | // for (Init; Cond; Inc) |
| 511 | // RSClearObjectCall; |
| 512 | |
| 513 | // Init -> "rsIntIter = 0" |
| 514 | clang::DeclRefExpr *RefrsIntIter = |
| 515 | clang::DeclRefExpr::Create(C, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 516 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 517 | clang::SourceLocation(), |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 518 | IIVD, |
Shih-wei Liao | e2597ac | 2012-03-24 02:44:33 -0700 | [diff] [blame] | 519 | false, |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 520 | Loc, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 521 | C.IntTy, |
| 522 | clang::VK_RValue, |
| 523 | NULL); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 524 | |
| 525 | clang::Expr *Int0 = clang::IntegerLiteral::Create(C, |
| 526 | llvm::APInt(C.getTypeSize(C.IntTy), 0), C.IntTy, Loc); |
| 527 | |
| 528 | clang::BinaryOperator *Init = |
| 529 | new(C) clang::BinaryOperator(RefrsIntIter, |
| 530 | Int0, |
| 531 | clang::BO_Assign, |
| 532 | C.IntTy, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 533 | clang::VK_RValue, |
| 534 | clang::OK_Ordinary, |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 535 | Loc, |
| 536 | false); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 537 | |
| 538 | // Cond -> "rsIntIter < NumArrayElements" |
| 539 | clang::Expr *NumArrayElementsExpr = clang::IntegerLiteral::Create(C, |
| 540 | llvm::APInt(C.getTypeSize(C.IntTy), NumArrayElements), C.IntTy, Loc); |
| 541 | |
| 542 | clang::BinaryOperator *Cond = |
| 543 | new(C) clang::BinaryOperator(RefrsIntIter, |
| 544 | NumArrayElementsExpr, |
| 545 | clang::BO_LT, |
| 546 | C.IntTy, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 547 | clang::VK_RValue, |
| 548 | clang::OK_Ordinary, |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 549 | Loc, |
| 550 | false); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 551 | |
| 552 | // Inc -> "rsIntIter++" |
| 553 | clang::UnaryOperator *Inc = |
| 554 | new(C) clang::UnaryOperator(RefrsIntIter, |
| 555 | clang::UO_PostInc, |
| 556 | C.IntTy, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 557 | clang::VK_RValue, |
| 558 | clang::OK_Ordinary, |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 559 | Loc); |
| 560 | |
| 561 | // Body -> "rsClearObject(&VD[rsIntIter]);" |
| 562 | // Destructor loop operates on individual array elements |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 563 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 564 | clang::Expr *RefRSArrPtr = |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 565 | clang::ImplicitCastExpr::Create(C, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 566 | C.getPointerType(BaseType->getCanonicalTypeInternal()), |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 567 | clang::CK_ArrayToPointerDecay, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 568 | RefRSArr, |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 569 | NULL, |
| 570 | clang::VK_RValue); |
| 571 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 572 | clang::Expr *RefRSArrPtrSubscript = |
| 573 | new(C) clang::ArraySubscriptExpr(RefRSArrPtr, |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 574 | RefrsIntIter, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 575 | BaseType->getCanonicalTypeInternal(), |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 576 | clang::VK_RValue, |
| 577 | clang::OK_Ordinary, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 578 | Loc); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 579 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 580 | RSExportPrimitiveType::DataType DT = |
| 581 | RSExportPrimitiveType::GetRSSpecificType(BaseType); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 582 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 583 | clang::Stmt *RSClearObjectCall = NULL; |
| 584 | if (BaseType->isArrayType()) { |
| 585 | RSClearObjectCall = |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 586 | ClearArrayRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 587 | } else if (DT == RSExportPrimitiveType::DataTypeUnknown) { |
| 588 | RSClearObjectCall = |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 589 | ClearStructRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 590 | } else { |
| 591 | RSClearObjectCall = ClearSingleRSObject(C, RefRSArrPtrSubscript, Loc); |
| 592 | } |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 593 | |
| 594 | clang::ForStmt *DestructorLoop = |
| 595 | new(C) clang::ForStmt(C, |
| 596 | Init, |
| 597 | Cond, |
| 598 | NULL, // no condVar |
| 599 | Inc, |
| 600 | RSClearObjectCall, |
| 601 | Loc, |
| 602 | Loc, |
| 603 | Loc); |
| 604 | |
| 605 | StmtArray[StmtCtr++] = DestructorLoop; |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 606 | slangAssert(StmtCtr == 2); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 607 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 608 | clang::CompoundStmt *CS = new(C) clang::CompoundStmt( |
| 609 | C, llvm::makeArrayRef(StmtArray, StmtCtr), Loc, Loc); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 610 | |
| 611 | return CS; |
| 612 | } |
| 613 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 614 | static unsigned CountRSObjectTypes(clang::ASTContext &C, |
| 615 | const clang::Type *T, |
| 616 | clang::SourceLocation Loc) { |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 617 | slangAssert(T); |
| 618 | unsigned RSObjectCount = 0; |
| 619 | |
| 620 | if (T->isArrayType()) { |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 621 | return CountRSObjectTypes(C, T->getArrayElementTypeNoTypeQual(), Loc); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | RSExportPrimitiveType::DataType DT = |
| 625 | RSExportPrimitiveType::GetRSSpecificType(T); |
| 626 | if (DT != RSExportPrimitiveType::DataTypeUnknown) { |
| 627 | return (RSExportPrimitiveType::IsRSObjectType(DT) ? 1 : 0); |
| 628 | } |
| 629 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 630 | if (T->isUnionType()) { |
| 631 | clang::RecordDecl *RD = T->getAsUnionType()->getDecl(); |
| 632 | RD = RD->getDefinition(); |
| 633 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 634 | FE = RD->field_end(); |
| 635 | FI != FE; |
| 636 | FI++) { |
| 637 | const clang::FieldDecl *FD = *FI; |
| 638 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
| 639 | if (CountRSObjectTypes(C, FT, Loc)) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 640 | slangAssert(false && "can't have unions with RS object types!"); |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 646 | if (!T->isStructureType()) { |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | clang::RecordDecl *RD = T->getAsStructureType()->getDecl(); |
| 651 | RD = RD->getDefinition(); |
| 652 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 653 | FE = RD->field_end(); |
| 654 | FI != FE; |
| 655 | FI++) { |
| 656 | const clang::FieldDecl *FD = *FI; |
| 657 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 658 | if (CountRSObjectTypes(C, FT, Loc)) { |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 659 | // Sub-structs should only count once (as should arrays, etc.) |
| 660 | RSObjectCount++; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return RSObjectCount; |
| 665 | } |
| 666 | |
| 667 | static clang::Stmt *ClearStructRSObject( |
| 668 | clang::ASTContext &C, |
| 669 | clang::DeclContext *DC, |
| 670 | clang::Expr *RefRSStruct, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 671 | clang::SourceLocation StartLoc, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 672 | clang::SourceLocation Loc) { |
| 673 | const clang::Type *BaseType = RefRSStruct->getType().getTypePtr(); |
| 674 | |
| 675 | slangAssert(!BaseType->isArrayType()); |
| 676 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 677 | // Structs should show up as unknown primitive types |
Alex Sakhartchouk | 9be9360 | 2011-03-17 17:03:36 -0700 | [diff] [blame] | 678 | slangAssert(RSExportPrimitiveType::GetRSSpecificType(BaseType) == |
| 679 | RSExportPrimitiveType::DataTypeUnknown); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 680 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 681 | unsigned FieldsToDestroy = CountRSObjectTypes(C, BaseType, Loc); |
Stephen Hines | b0fabe5 | 2013-01-07 19:06:09 -0800 | [diff] [blame] | 682 | slangAssert(FieldsToDestroy != 0); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 683 | |
| 684 | unsigned StmtCount = 0; |
| 685 | clang::Stmt **StmtArray = new clang::Stmt*[FieldsToDestroy]; |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 686 | for (unsigned i = 0; i < FieldsToDestroy; i++) { |
| 687 | StmtArray[i] = NULL; |
| 688 | } |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 689 | |
| 690 | // Populate StmtArray by creating a destructor for each RS object field |
| 691 | clang::RecordDecl *RD = BaseType->getAsStructureType()->getDecl(); |
| 692 | RD = RD->getDefinition(); |
| 693 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 694 | FE = RD->field_end(); |
| 695 | FI != FE; |
| 696 | FI++) { |
| 697 | // We just look through all field declarations to see if we find a |
| 698 | // declaration for an RS object type (or an array of one). |
| 699 | bool IsArrayType = false; |
| 700 | clang::FieldDecl *FD = *FI; |
| 701 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
| 702 | const clang::Type *OrigType = FT; |
| 703 | while (FT && FT->isArrayType()) { |
| 704 | FT = FT->getArrayElementTypeNoTypeQual(); |
| 705 | IsArrayType = true; |
| 706 | } |
| 707 | |
| 708 | if (RSExportPrimitiveType::IsRSObjectType(FT)) { |
| 709 | clang::DeclAccessPair FoundDecl = |
| 710 | clang::DeclAccessPair::make(FD, clang::AS_none); |
| 711 | clang::MemberExpr *RSObjectMember = |
| 712 | clang::MemberExpr::Create(C, |
| 713 | RefRSStruct, |
| 714 | false, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 715 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 716 | clang::SourceLocation(), |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 717 | FD, |
| 718 | FoundDecl, |
| 719 | clang::DeclarationNameInfo(), |
| 720 | NULL, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 721 | OrigType->getCanonicalTypeInternal(), |
| 722 | clang::VK_RValue, |
| 723 | clang::OK_Ordinary); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 724 | |
| 725 | slangAssert(StmtCount < FieldsToDestroy); |
| 726 | |
| 727 | if (IsArrayType) { |
| 728 | StmtArray[StmtCount++] = ClearArrayRSObject(C, |
| 729 | DC, |
| 730 | RSObjectMember, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 731 | StartLoc, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 732 | Loc); |
| 733 | } else { |
| 734 | StmtArray[StmtCount++] = ClearSingleRSObject(C, |
| 735 | RSObjectMember, |
| 736 | Loc); |
| 737 | } |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 738 | } else if (FT->isStructureType() && CountRSObjectTypes(C, FT, Loc)) { |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 739 | // In this case, we have a nested struct. We may not end up filling all |
| 740 | // of the spaces in StmtArray (sub-structs should handle themselves |
| 741 | // with separate compound statements). |
| 742 | clang::DeclAccessPair FoundDecl = |
| 743 | clang::DeclAccessPair::make(FD, clang::AS_none); |
| 744 | clang::MemberExpr *RSObjectMember = |
| 745 | clang::MemberExpr::Create(C, |
| 746 | RefRSStruct, |
| 747 | false, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 748 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 749 | clang::SourceLocation(), |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 750 | FD, |
| 751 | FoundDecl, |
| 752 | clang::DeclarationNameInfo(), |
| 753 | NULL, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 754 | OrigType->getCanonicalTypeInternal(), |
| 755 | clang::VK_RValue, |
| 756 | clang::OK_Ordinary); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 757 | |
| 758 | if (IsArrayType) { |
| 759 | StmtArray[StmtCount++] = ClearArrayRSObject(C, |
| 760 | DC, |
| 761 | RSObjectMember, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 762 | StartLoc, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 763 | Loc); |
| 764 | } else { |
| 765 | StmtArray[StmtCount++] = ClearStructRSObject(C, |
| 766 | DC, |
| 767 | RSObjectMember, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 768 | StartLoc, |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 769 | Loc); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | slangAssert(StmtCount > 0); |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 775 | clang::CompoundStmt *CS = new(C) clang::CompoundStmt( |
| 776 | C, llvm::makeArrayRef(StmtArray, StmtCount), Loc, Loc); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 777 | |
| 778 | delete [] StmtArray; |
| 779 | |
| 780 | return CS; |
| 781 | } |
| 782 | |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 783 | static clang::Stmt *CreateSingleRSSetObject(clang::ASTContext &C, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 784 | clang::Expr *DstExpr, |
| 785 | clang::Expr *SrcExpr, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 786 | clang::SourceLocation StartLoc, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 787 | clang::SourceLocation Loc) { |
| 788 | const clang::Type *T = DstExpr->getType().getTypePtr(); |
| 789 | clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(T); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 790 | slangAssert((SetObjectFD != NULL) && |
| 791 | "rsSetObject doesn't cover all RS object types"); |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 792 | |
| 793 | clang::QualType SetObjectFDType = SetObjectFD->getType(); |
| 794 | clang::QualType SetObjectFDArgType[2]; |
| 795 | SetObjectFDArgType[0] = SetObjectFD->getParamDecl(0)->getOriginalType(); |
| 796 | SetObjectFDArgType[1] = SetObjectFD->getParamDecl(1)->getOriginalType(); |
| 797 | |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 798 | clang::Expr *RefRSSetObjectFD = |
| 799 | clang::DeclRefExpr::Create(C, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 800 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 801 | clang::SourceLocation(), |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 802 | SetObjectFD, |
Shih-wei Liao | e2597ac | 2012-03-24 02:44:33 -0700 | [diff] [blame] | 803 | false, |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 804 | Loc, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 805 | SetObjectFDType, |
| 806 | clang::VK_RValue, |
| 807 | NULL); |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 808 | |
| 809 | clang::Expr *RSSetObjectFP = |
| 810 | clang::ImplicitCastExpr::Create(C, |
| 811 | C.getPointerType(SetObjectFDType), |
| 812 | clang::CK_FunctionToPointerDecay, |
| 813 | RefRSSetObjectFD, |
| 814 | NULL, |
| 815 | clang::VK_RValue); |
| 816 | |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 817 | llvm::SmallVector<clang::Expr*, 2> ArgList; |
| 818 | ArgList.push_back(new(C) clang::UnaryOperator(DstExpr, |
| 819 | clang::UO_AddrOf, |
| 820 | SetObjectFDArgType[0], |
| 821 | clang::VK_RValue, |
| 822 | clang::OK_Ordinary, |
| 823 | Loc)); |
| 824 | ArgList.push_back(SrcExpr); |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 825 | |
| 826 | clang::CallExpr *RSSetObjectCall = |
| 827 | new(C) clang::CallExpr(C, |
| 828 | RSSetObjectFP, |
| 829 | ArgList, |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 830 | SetObjectFD->getCallResultType(), |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 831 | clang::VK_RValue, |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 832 | Loc); |
| 833 | |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 834 | return RSSetObjectCall; |
| 835 | } |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 836 | |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 837 | static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 838 | clang::Expr *LHS, |
| 839 | clang::Expr *RHS, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 840 | clang::SourceLocation StartLoc, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 841 | clang::SourceLocation Loc); |
| 842 | |
Al Sutton | 246fa17 | 2012-02-23 13:09:08 +0000 | [diff] [blame] | 843 | /*static clang::Stmt *CreateArrayRSSetObject(clang::ASTContext &C, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 844 | clang::Expr *DstArr, |
| 845 | clang::Expr *SrcArr, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 846 | clang::SourceLocation StartLoc, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 847 | clang::SourceLocation Loc) { |
| 848 | clang::DeclContext *DC = NULL; |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 849 | const clang::Type *BaseType = DstArr->getType().getTypePtr(); |
| 850 | slangAssert(BaseType->isArrayType()); |
| 851 | |
| 852 | int NumArrayElements = ArrayDim(BaseType); |
| 853 | // Actually extract out the base RS object type for use later |
| 854 | BaseType = BaseType->getArrayElementTypeNoTypeQual(); |
| 855 | |
| 856 | clang::Stmt *StmtArray[2] = {NULL}; |
| 857 | int StmtCtr = 0; |
| 858 | |
| 859 | if (NumArrayElements <= 0) { |
| 860 | return NULL; |
| 861 | } |
| 862 | |
| 863 | // Create helper variable for iterating through elements |
| 864 | clang::IdentifierInfo& II = C.Idents.get("rsIntIter"); |
| 865 | clang::VarDecl *IIVD = |
| 866 | clang::VarDecl::Create(C, |
| 867 | DC, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 868 | StartLoc, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 869 | Loc, |
| 870 | &II, |
| 871 | C.IntTy, |
| 872 | C.getTrivialTypeSourceInfo(C.IntTy), |
| 873 | clang::SC_None, |
| 874 | clang::SC_None); |
| 875 | clang::Decl *IID = (clang::Decl *)IIVD; |
| 876 | |
| 877 | clang::DeclGroupRef DGR = clang::DeclGroupRef::Create(C, &IID, 1); |
| 878 | StmtArray[StmtCtr++] = new(C) clang::DeclStmt(DGR, Loc, Loc); |
| 879 | |
| 880 | // Form the actual loop |
| 881 | // for (Init; Cond; Inc) |
| 882 | // RSSetObjectCall; |
| 883 | |
| 884 | // Init -> "rsIntIter = 0" |
| 885 | clang::DeclRefExpr *RefrsIntIter = |
| 886 | clang::DeclRefExpr::Create(C, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 887 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 888 | IIVD, |
| 889 | Loc, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 890 | C.IntTy, |
| 891 | clang::VK_RValue, |
| 892 | NULL); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 893 | |
| 894 | clang::Expr *Int0 = clang::IntegerLiteral::Create(C, |
| 895 | llvm::APInt(C.getTypeSize(C.IntTy), 0), C.IntTy, Loc); |
| 896 | |
| 897 | clang::BinaryOperator *Init = |
| 898 | new(C) clang::BinaryOperator(RefrsIntIter, |
| 899 | Int0, |
| 900 | clang::BO_Assign, |
| 901 | C.IntTy, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 902 | clang::VK_RValue, |
| 903 | clang::OK_Ordinary, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 904 | Loc); |
| 905 | |
| 906 | // Cond -> "rsIntIter < NumArrayElements" |
| 907 | clang::Expr *NumArrayElementsExpr = clang::IntegerLiteral::Create(C, |
| 908 | llvm::APInt(C.getTypeSize(C.IntTy), NumArrayElements), C.IntTy, Loc); |
| 909 | |
| 910 | clang::BinaryOperator *Cond = |
| 911 | new(C) clang::BinaryOperator(RefrsIntIter, |
| 912 | NumArrayElementsExpr, |
| 913 | clang::BO_LT, |
| 914 | C.IntTy, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 915 | clang::VK_RValue, |
| 916 | clang::OK_Ordinary, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 917 | Loc); |
| 918 | |
| 919 | // Inc -> "rsIntIter++" |
| 920 | clang::UnaryOperator *Inc = |
| 921 | new(C) clang::UnaryOperator(RefrsIntIter, |
| 922 | clang::UO_PostInc, |
| 923 | C.IntTy, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 924 | clang::VK_RValue, |
| 925 | clang::OK_Ordinary, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 926 | Loc); |
| 927 | |
| 928 | // Body -> "rsSetObject(&Dst[rsIntIter], Src[rsIntIter]);" |
| 929 | // Loop operates on individual array elements |
| 930 | |
| 931 | clang::Expr *DstArrPtr = |
| 932 | clang::ImplicitCastExpr::Create(C, |
| 933 | C.getPointerType(BaseType->getCanonicalTypeInternal()), |
| 934 | clang::CK_ArrayToPointerDecay, |
| 935 | DstArr, |
| 936 | NULL, |
| 937 | clang::VK_RValue); |
| 938 | |
| 939 | clang::Expr *DstArrPtrSubscript = |
| 940 | new(C) clang::ArraySubscriptExpr(DstArrPtr, |
| 941 | RefrsIntIter, |
| 942 | BaseType->getCanonicalTypeInternal(), |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 943 | clang::VK_RValue, |
| 944 | clang::OK_Ordinary, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 945 | Loc); |
| 946 | |
| 947 | clang::Expr *SrcArrPtr = |
| 948 | clang::ImplicitCastExpr::Create(C, |
| 949 | C.getPointerType(BaseType->getCanonicalTypeInternal()), |
| 950 | clang::CK_ArrayToPointerDecay, |
| 951 | SrcArr, |
| 952 | NULL, |
| 953 | clang::VK_RValue); |
| 954 | |
| 955 | clang::Expr *SrcArrPtrSubscript = |
| 956 | new(C) clang::ArraySubscriptExpr(SrcArrPtr, |
| 957 | RefrsIntIter, |
| 958 | BaseType->getCanonicalTypeInternal(), |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 959 | clang::VK_RValue, |
| 960 | clang::OK_Ordinary, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 961 | Loc); |
| 962 | |
| 963 | RSExportPrimitiveType::DataType DT = |
| 964 | RSExportPrimitiveType::GetRSSpecificType(BaseType); |
| 965 | |
| 966 | clang::Stmt *RSSetObjectCall = NULL; |
| 967 | if (BaseType->isArrayType()) { |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 968 | RSSetObjectCall = CreateArrayRSSetObject(C, DstArrPtrSubscript, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 969 | SrcArrPtrSubscript, |
| 970 | StartLoc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 971 | } else if (DT == RSExportPrimitiveType::DataTypeUnknown) { |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 972 | RSSetObjectCall = CreateStructRSSetObject(C, DstArrPtrSubscript, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 973 | SrcArrPtrSubscript, |
| 974 | StartLoc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 975 | } else { |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 976 | RSSetObjectCall = CreateSingleRSSetObject(C, DstArrPtrSubscript, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 977 | SrcArrPtrSubscript, |
| 978 | StartLoc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | clang::ForStmt *DestructorLoop = |
| 982 | new(C) clang::ForStmt(C, |
| 983 | Init, |
| 984 | Cond, |
| 985 | NULL, // no condVar |
| 986 | Inc, |
| 987 | RSSetObjectCall, |
| 988 | Loc, |
| 989 | Loc, |
| 990 | Loc); |
| 991 | |
| 992 | StmtArray[StmtCtr++] = DestructorLoop; |
| 993 | slangAssert(StmtCtr == 2); |
| 994 | |
| 995 | clang::CompoundStmt *CS = |
| 996 | new(C) clang::CompoundStmt(C, StmtArray, StmtCtr, Loc, Loc); |
| 997 | |
| 998 | return CS; |
Al Sutton | 246fa17 | 2012-02-23 13:09:08 +0000 | [diff] [blame] | 999 | } */ |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1000 | |
| 1001 | static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1002 | clang::Expr *LHS, |
| 1003 | clang::Expr *RHS, |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 1004 | clang::SourceLocation StartLoc, |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1005 | clang::SourceLocation Loc) { |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1006 | clang::QualType QT = LHS->getType(); |
| 1007 | const clang::Type *T = QT.getTypePtr(); |
| 1008 | slangAssert(T->isStructureType()); |
| 1009 | slangAssert(!RSExportPrimitiveType::IsRSObjectType(T)); |
| 1010 | |
| 1011 | // Keep an extra slot for the original copy (memcpy) |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1012 | unsigned FieldsToSet = CountRSObjectTypes(C, T, Loc) + 1; |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1013 | |
| 1014 | unsigned StmtCount = 0; |
| 1015 | clang::Stmt **StmtArray = new clang::Stmt*[FieldsToSet]; |
| 1016 | for (unsigned i = 0; i < FieldsToSet; i++) { |
| 1017 | StmtArray[i] = NULL; |
| 1018 | } |
| 1019 | |
| 1020 | clang::RecordDecl *RD = T->getAsStructureType()->getDecl(); |
| 1021 | RD = RD->getDefinition(); |
| 1022 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 1023 | FE = RD->field_end(); |
| 1024 | FI != FE; |
| 1025 | FI++) { |
| 1026 | bool IsArrayType = false; |
| 1027 | clang::FieldDecl *FD = *FI; |
| 1028 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
| 1029 | const clang::Type *OrigType = FT; |
| 1030 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1031 | if (!CountRSObjectTypes(C, FT, Loc)) { |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1032 | // Skip to next if we don't have any viable RS object types |
| 1033 | continue; |
| 1034 | } |
| 1035 | |
| 1036 | clang::DeclAccessPair FoundDecl = |
| 1037 | clang::DeclAccessPair::make(FD, clang::AS_none); |
| 1038 | clang::MemberExpr *DstMember = |
| 1039 | clang::MemberExpr::Create(C, |
| 1040 | LHS, |
| 1041 | false, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1042 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 1043 | clang::SourceLocation(), |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1044 | FD, |
| 1045 | FoundDecl, |
| 1046 | clang::DeclarationNameInfo(), |
| 1047 | NULL, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1048 | OrigType->getCanonicalTypeInternal(), |
| 1049 | clang::VK_RValue, |
| 1050 | clang::OK_Ordinary); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1051 | |
| 1052 | clang::MemberExpr *SrcMember = |
| 1053 | clang::MemberExpr::Create(C, |
| 1054 | RHS, |
| 1055 | false, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1056 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 1057 | clang::SourceLocation(), |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1058 | FD, |
| 1059 | FoundDecl, |
| 1060 | clang::DeclarationNameInfo(), |
| 1061 | NULL, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1062 | OrigType->getCanonicalTypeInternal(), |
| 1063 | clang::VK_RValue, |
| 1064 | clang::OK_Ordinary); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1065 | |
| 1066 | if (FT->isArrayType()) { |
| 1067 | FT = FT->getArrayElementTypeNoTypeQual(); |
| 1068 | IsArrayType = true; |
| 1069 | } |
| 1070 | |
| 1071 | RSExportPrimitiveType::DataType DT = |
| 1072 | RSExportPrimitiveType::GetRSSpecificType(FT); |
| 1073 | |
| 1074 | if (IsArrayType) { |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 1075 | clang::DiagnosticsEngine &DiagEngine = C.getDiagnostics(); |
| 1076 | DiagEngine.Report( |
| 1077 | clang::FullSourceLoc(Loc, C.getSourceManager()), |
| 1078 | DiagEngine.getCustomDiagID( |
| 1079 | clang::DiagnosticsEngine::Error, |
| 1080 | "Arrays of RS object types within structures cannot be copied")); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1081 | // TODO(srhines): Support setting arrays of RS objects |
| 1082 | // StmtArray[StmtCount++] = |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1083 | // CreateArrayRSSetObject(C, DstMember, SrcMember, StartLoc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1084 | } else if (DT == RSExportPrimitiveType::DataTypeUnknown) { |
| 1085 | StmtArray[StmtCount++] = |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1086 | CreateStructRSSetObject(C, DstMember, SrcMember, StartLoc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1087 | } else if (RSExportPrimitiveType::IsRSObjectType(DT)) { |
| 1088 | StmtArray[StmtCount++] = |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1089 | CreateSingleRSSetObject(C, DstMember, SrcMember, StartLoc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1090 | } else { |
| 1091 | slangAssert(false); |
| 1092 | } |
| 1093 | } |
| 1094 | |
Stephen Hines | b0fabe5 | 2013-01-07 19:06:09 -0800 | [diff] [blame] | 1095 | slangAssert(StmtCount < FieldsToSet); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1096 | |
| 1097 | // We still need to actually do the overall struct copy. For simplicity, |
| 1098 | // we just do a straight-up assignment (which will still preserve all |
| 1099 | // the proper RS object reference counts). |
| 1100 | clang::BinaryOperator *CopyStruct = |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1101 | new(C) clang::BinaryOperator(LHS, RHS, clang::BO_Assign, QT, |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 1102 | clang::VK_RValue, clang::OK_Ordinary, Loc, |
| 1103 | false); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1104 | StmtArray[StmtCount++] = CopyStruct; |
| 1105 | |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 1106 | clang::CompoundStmt *CS = new(C) clang::CompoundStmt( |
| 1107 | C, llvm::makeArrayRef(StmtArray, StmtCount), Loc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1108 | |
| 1109 | delete [] StmtArray; |
| 1110 | |
| 1111 | return CS; |
| 1112 | } |
| 1113 | |
| 1114 | } // namespace |
| 1115 | |
| 1116 | void RSObjectRefCount::Scope::ReplaceRSObjectAssignment( |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1117 | clang::BinaryOperator *AS) { |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1118 | |
| 1119 | clang::QualType QT = AS->getType(); |
| 1120 | |
| 1121 | clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD( |
| 1122 | RSExportPrimitiveType::DataTypeRSFont)->getASTContext(); |
| 1123 | |
Stephen Hines | 832429f | 2011-02-25 16:05:37 -0800 | [diff] [blame] | 1124 | clang::SourceLocation Loc = AS->getExprLoc(); |
Stephen Hines | 9f1d0aa | 2011-12-18 15:41:00 -0800 | [diff] [blame] | 1125 | clang::SourceLocation StartLoc = AS->getLHS()->getExprLoc(); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1126 | clang::Stmt *UpdatedStmt = NULL; |
| 1127 | |
| 1128 | if (!RSExportPrimitiveType::IsRSObjectType(QT.getTypePtr())) { |
| 1129 | // By definition, this is a struct assignment if we get here |
| 1130 | UpdatedStmt = |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1131 | CreateStructRSSetObject(C, AS->getLHS(), AS->getRHS(), StartLoc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1132 | } else { |
| 1133 | UpdatedStmt = |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1134 | CreateSingleRSSetObject(C, AS->getLHS(), AS->getRHS(), StartLoc, Loc); |
Stephen Hines | 2bb67db | 2011-02-11 01:36:40 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 1137 | RSASTReplace R(C); |
| 1138 | R.ReplaceStmt(mCS, AS, UpdatedStmt); |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | void RSObjectRefCount::Scope::AppendRSObjectInit( |
| 1143 | clang::VarDecl *VD, |
| 1144 | clang::DeclStmt *DS, |
| 1145 | RSExportPrimitiveType::DataType DT, |
| 1146 | clang::Expr *InitExpr) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1147 | slangAssert(VD); |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1148 | |
| 1149 | if (!InitExpr) { |
| 1150 | return; |
| 1151 | } |
| 1152 | |
Stephen Hines | a0611e6 | 2011-02-11 16:35:47 -0800 | [diff] [blame] | 1153 | clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD( |
| 1154 | RSExportPrimitiveType::DataTypeRSFont)->getASTContext(); |
| 1155 | clang::SourceLocation Loc = RSObjectRefCount::GetRSSetObjectFD( |
| 1156 | RSExportPrimitiveType::DataTypeRSFont)->getLocation(); |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 1157 | clang::SourceLocation StartLoc = RSObjectRefCount::GetRSSetObjectFD( |
| 1158 | RSExportPrimitiveType::DataTypeRSFont)->getInnerLocStart(); |
Stephen Hines | a0611e6 | 2011-02-11 16:35:47 -0800 | [diff] [blame] | 1159 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1160 | if (DT == RSExportPrimitiveType::DataTypeIsStruct) { |
Stephen Hines | a0611e6 | 2011-02-11 16:35:47 -0800 | [diff] [blame] | 1161 | const clang::Type *T = RSExportType::GetTypeOfDecl(VD); |
| 1162 | clang::DeclRefExpr *RefRSVar = |
| 1163 | clang::DeclRefExpr::Create(C, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1164 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 1165 | clang::SourceLocation(), |
Stephen Hines | a0611e6 | 2011-02-11 16:35:47 -0800 | [diff] [blame] | 1166 | VD, |
Shih-wei Liao | e2597ac | 2012-03-24 02:44:33 -0700 | [diff] [blame] | 1167 | false, |
Stephen Hines | a0611e6 | 2011-02-11 16:35:47 -0800 | [diff] [blame] | 1168 | Loc, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1169 | T->getCanonicalTypeInternal(), |
| 1170 | clang::VK_RValue, |
| 1171 | NULL); |
Stephen Hines | a0611e6 | 2011-02-11 16:35:47 -0800 | [diff] [blame] | 1172 | |
| 1173 | clang::Stmt *RSSetObjectOps = |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1174 | CreateStructRSSetObject(C, RefRSVar, InitExpr, StartLoc, Loc); |
Stephen Hines | a0611e6 | 2011-02-11 16:35:47 -0800 | [diff] [blame] | 1175 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 1176 | std::list<clang::Stmt*> StmtList; |
| 1177 | StmtList.push_back(RSSetObjectOps); |
| 1178 | AppendAfterStmt(C, mCS, DS, StmtList); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1179 | return; |
| 1180 | } |
| 1181 | |
| 1182 | clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(DT); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1183 | slangAssert((SetObjectFD != NULL) && |
| 1184 | "rsSetObject doesn't cover all RS object types"); |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1185 | |
| 1186 | clang::QualType SetObjectFDType = SetObjectFD->getType(); |
| 1187 | clang::QualType SetObjectFDArgType[2]; |
| 1188 | SetObjectFDArgType[0] = SetObjectFD->getParamDecl(0)->getOriginalType(); |
| 1189 | SetObjectFDArgType[1] = SetObjectFD->getParamDecl(1)->getOriginalType(); |
| 1190 | |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1191 | clang::Expr *RefRSSetObjectFD = |
| 1192 | clang::DeclRefExpr::Create(C, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1193 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 1194 | clang::SourceLocation(), |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1195 | SetObjectFD, |
Shih-wei Liao | e2597ac | 2012-03-24 02:44:33 -0700 | [diff] [blame] | 1196 | false, |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1197 | Loc, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1198 | SetObjectFDType, |
| 1199 | clang::VK_RValue, |
| 1200 | NULL); |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1201 | |
| 1202 | clang::Expr *RSSetObjectFP = |
| 1203 | clang::ImplicitCastExpr::Create(C, |
| 1204 | C.getPointerType(SetObjectFDType), |
| 1205 | clang::CK_FunctionToPointerDecay, |
| 1206 | RefRSSetObjectFD, |
| 1207 | NULL, |
| 1208 | clang::VK_RValue); |
| 1209 | |
| 1210 | const clang::Type *T = RSExportType::GetTypeOfDecl(VD); |
| 1211 | clang::DeclRefExpr *RefRSVar = |
| 1212 | clang::DeclRefExpr::Create(C, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1213 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 1214 | clang::SourceLocation(), |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1215 | VD, |
Shih-wei Liao | e2597ac | 2012-03-24 02:44:33 -0700 | [diff] [blame] | 1216 | false, |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1217 | Loc, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1218 | T->getCanonicalTypeInternal(), |
| 1219 | clang::VK_RValue, |
| 1220 | NULL); |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1221 | |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 1222 | llvm::SmallVector<clang::Expr*, 2> ArgList; |
| 1223 | ArgList.push_back(new(C) clang::UnaryOperator(RefRSVar, |
| 1224 | clang::UO_AddrOf, |
| 1225 | SetObjectFDArgType[0], |
| 1226 | clang::VK_RValue, |
| 1227 | clang::OK_Ordinary, |
| 1228 | Loc)); |
| 1229 | ArgList.push_back(InitExpr); |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1230 | |
| 1231 | clang::CallExpr *RSSetObjectCall = |
| 1232 | new(C) clang::CallExpr(C, |
| 1233 | RSSetObjectFP, |
| 1234 | ArgList, |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1235 | SetObjectFD->getCallResultType(), |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1236 | clang::VK_RValue, |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1237 | Loc); |
| 1238 | |
Stephen Hines | 292e00a | 2011-03-18 19:11:30 -0700 | [diff] [blame] | 1239 | std::list<clang::Stmt*> StmtList; |
| 1240 | StmtList.push_back(RSSetObjectCall); |
| 1241 | AppendAfterStmt(C, mCS, DS, StmtList); |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 1242 | |
| 1243 | return; |
| 1244 | } |
| 1245 | |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1246 | void RSObjectRefCount::Scope::InsertLocalVarDestructors() { |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1247 | for (std::list<clang::VarDecl*>::const_iterator I = mRSO.begin(), |
| 1248 | E = mRSO.end(); |
| 1249 | I != E; |
| 1250 | I++) { |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 1251 | clang::VarDecl *VD = *I; |
Stephen Hines | 3f175af | 2011-09-16 16:26:29 -0700 | [diff] [blame] | 1252 | clang::Stmt *RSClearObjectCall = ClearRSObject(VD, VD->getDeclContext()); |
Stephen Hines | a883ce3 | 2011-08-11 18:52:48 -0700 | [diff] [blame] | 1253 | if (RSClearObjectCall) { |
| 1254 | DestructorVisitor DV((*mRSO.begin())->getASTContext(), |
| 1255 | mCS, |
| 1256 | RSClearObjectCall, |
| 1257 | VD->getSourceRange().getBegin()); |
| 1258 | DV.Visit(mCS); |
| 1259 | DV.InsertDestructors(); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1260 | } |
| 1261 | } |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1262 | return; |
| 1263 | } |
| 1264 | |
Stephen Hines | 3f175af | 2011-09-16 16:26:29 -0700 | [diff] [blame] | 1265 | clang::Stmt *RSObjectRefCount::Scope::ClearRSObject( |
| 1266 | clang::VarDecl *VD, |
| 1267 | clang::DeclContext *DC) { |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1268 | slangAssert(VD); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1269 | clang::ASTContext &C = VD->getASTContext(); |
| 1270 | clang::SourceLocation Loc = VD->getLocation(); |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 1271 | clang::SourceLocation StartLoc = VD->getInnerLocStart(); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1272 | const clang::Type *T = RSExportType::GetTypeOfDecl(VD); |
Stephen Hines | 03981a3 | 2010-12-14 19:45:49 -0800 | [diff] [blame] | 1273 | |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1274 | // Reference expr to target RS object variable |
| 1275 | clang::DeclRefExpr *RefRSVar = |
| 1276 | clang::DeclRefExpr::Create(C, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1277 | clang::NestedNameSpecifierLoc(), |
Stephen Hines | 0444de0 | 2012-03-02 23:19:06 -0800 | [diff] [blame] | 1278 | clang::SourceLocation(), |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1279 | VD, |
Shih-wei Liao | e2597ac | 2012-03-24 02:44:33 -0700 | [diff] [blame] | 1280 | false, |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1281 | Loc, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1282 | T->getCanonicalTypeInternal(), |
| 1283 | clang::VK_RValue, |
| 1284 | NULL); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1285 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1286 | if (T->isArrayType()) { |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 1287 | return ClearArrayRSObject(C, DC, RefRSVar, StartLoc, Loc); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1288 | } |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1289 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1290 | RSExportPrimitiveType::DataType DT = |
| 1291 | RSExportPrimitiveType::GetRSSpecificType(T); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1292 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1293 | if (DT == RSExportPrimitiveType::DataTypeUnknown || |
| 1294 | DT == RSExportPrimitiveType::DataTypeIsStruct) { |
Shih-wei Liao | cc887ba | 2011-04-19 18:36:20 -0700 | [diff] [blame] | 1295 | return ClearStructRSObject(C, DC, RefRSVar, StartLoc, Loc); |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1296 | } |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1297 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1298 | slangAssert((RSExportPrimitiveType::IsRSObjectType(DT)) && |
| 1299 | "Should be RS object"); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1300 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1301 | return ClearSingleRSObject(C, RefRSVar, Loc); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1302 | } |
| 1303 | |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1304 | bool RSObjectRefCount::InitializeRSObject(clang::VarDecl *VD, |
| 1305 | RSExportPrimitiveType::DataType *DT, |
| 1306 | clang::Expr **InitExpr) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1307 | slangAssert(VD && DT && InitExpr); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1308 | const clang::Type *T = RSExportType::GetTypeOfDecl(VD); |
Stephen Hines | 2d09504 | 2010-11-12 18:13:56 -0800 | [diff] [blame] | 1309 | |
| 1310 | // Loop through array types to get to base type |
| 1311 | while (T && T->isArrayType()) { |
| 1312 | T = T->getArrayElementTypeNoTypeQual(); |
Stephen Hines | 2d09504 | 2010-11-12 18:13:56 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1315 | bool DataTypeIsStructWithRSObject = false; |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1316 | *DT = RSExportPrimitiveType::GetRSSpecificType(T); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1317 | |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1318 | if (*DT == RSExportPrimitiveType::DataTypeUnknown) { |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1319 | if (RSExportPrimitiveType::IsStructureTypeWithRSObject(T)) { |
| 1320 | *DT = RSExportPrimitiveType::DataTypeIsStruct; |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1321 | DataTypeIsStructWithRSObject = true; |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1322 | } else { |
| 1323 | return false; |
| 1324 | } |
Stephen Hines | 2d09504 | 2010-11-12 18:13:56 -0800 | [diff] [blame] | 1325 | } |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1326 | |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1327 | bool DataTypeIsRSObject = false; |
| 1328 | if (DataTypeIsStructWithRSObject) { |
| 1329 | DataTypeIsRSObject = true; |
| 1330 | } else { |
| 1331 | DataTypeIsRSObject = RSExportPrimitiveType::IsRSObjectType(*DT); |
| 1332 | } |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1333 | *InitExpr = VD->getInit(); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1334 | |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1335 | if (!DataTypeIsRSObject && *InitExpr) { |
| 1336 | // If we already have an initializer for a matrix type, we are done. |
| 1337 | return DataTypeIsRSObject; |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1340 | clang::Expr *ZeroInitializer = |
| 1341 | CreateZeroInitializerForRSSpecificType(*DT, |
| 1342 | VD->getASTContext(), |
| 1343 | VD->getLocation()); |
| 1344 | |
| 1345 | if (ZeroInitializer) { |
| 1346 | ZeroInitializer->setType(T->getCanonicalTypeInternal()); |
| 1347 | VD->setInit(ZeroInitializer); |
| 1348 | } |
| 1349 | |
| 1350 | return DataTypeIsRSObject; |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | clang::Expr *RSObjectRefCount::CreateZeroInitializerForRSSpecificType( |
| 1354 | RSExportPrimitiveType::DataType DT, |
| 1355 | clang::ASTContext &C, |
| 1356 | const clang::SourceLocation &Loc) { |
| 1357 | clang::Expr *Res = NULL; |
| 1358 | switch (DT) { |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1359 | case RSExportPrimitiveType::DataTypeIsStruct: |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1360 | case RSExportPrimitiveType::DataTypeRSElement: |
| 1361 | case RSExportPrimitiveType::DataTypeRSType: |
| 1362 | case RSExportPrimitiveType::DataTypeRSAllocation: |
| 1363 | case RSExportPrimitiveType::DataTypeRSSampler: |
| 1364 | case RSExportPrimitiveType::DataTypeRSScript: |
| 1365 | case RSExportPrimitiveType::DataTypeRSMesh: |
Jason Sams | db6dfdf | 2011-11-07 18:11:10 -0800 | [diff] [blame] | 1366 | case RSExportPrimitiveType::DataTypeRSPath: |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1367 | case RSExportPrimitiveType::DataTypeRSProgramFragment: |
| 1368 | case RSExportPrimitiveType::DataTypeRSProgramVertex: |
| 1369 | case RSExportPrimitiveType::DataTypeRSProgramRaster: |
| 1370 | case RSExportPrimitiveType::DataTypeRSProgramStore: |
| 1371 | case RSExportPrimitiveType::DataTypeRSFont: { |
| 1372 | // (ImplicitCastExpr 'nullptr_t' |
| 1373 | // (IntegerLiteral 0))) |
| 1374 | llvm::APInt Zero(C.getTypeSize(C.IntTy), 0); |
| 1375 | clang::Expr *Int0 = clang::IntegerLiteral::Create(C, Zero, C.IntTy, Loc); |
| 1376 | clang::Expr *CastToNull = |
| 1377 | clang::ImplicitCastExpr::Create(C, |
| 1378 | C.NullPtrTy, |
| 1379 | clang::CK_IntegralToPointer, |
| 1380 | Int0, |
| 1381 | NULL, |
| 1382 | clang::VK_RValue); |
| 1383 | |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 1384 | llvm::SmallVector<clang::Expr*, 1>InitList; |
| 1385 | InitList.push_back(CastToNull); |
| 1386 | |
| 1387 | Res = new(C) clang::InitListExpr(C, Loc, InitList, Loc); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1388 | break; |
| 1389 | } |
| 1390 | case RSExportPrimitiveType::DataTypeRSMatrix2x2: |
| 1391 | case RSExportPrimitiveType::DataTypeRSMatrix3x3: |
| 1392 | case RSExportPrimitiveType::DataTypeRSMatrix4x4: { |
| 1393 | // RS matrix is not completely an RS object. They hold data by themselves. |
| 1394 | // (InitListExpr rs_matrix2x2 |
| 1395 | // (InitListExpr float[4] |
| 1396 | // (FloatingLiteral 0) |
| 1397 | // (FloatingLiteral 0) |
| 1398 | // (FloatingLiteral 0) |
| 1399 | // (FloatingLiteral 0))) |
| 1400 | clang::QualType FloatTy = C.FloatTy; |
| 1401 | // Constructor sets value to 0.0f by default |
| 1402 | llvm::APFloat Val(C.getFloatTypeSemantics(FloatTy)); |
| 1403 | clang::FloatingLiteral *Float0Val = |
| 1404 | clang::FloatingLiteral::Create(C, |
| 1405 | Val, |
| 1406 | /* isExact = */true, |
| 1407 | FloatTy, |
| 1408 | Loc); |
| 1409 | |
| 1410 | unsigned N = 0; |
| 1411 | if (DT == RSExportPrimitiveType::DataTypeRSMatrix2x2) |
| 1412 | N = 2; |
| 1413 | else if (DT == RSExportPrimitiveType::DataTypeRSMatrix3x3) |
| 1414 | N = 3; |
| 1415 | else if (DT == RSExportPrimitiveType::DataTypeRSMatrix4x4) |
| 1416 | N = 4; |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 1417 | unsigned N_2 = N * N; |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1418 | |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 1419 | // Assume we are going to be allocating 16 elements, since 4x4 is max. |
| 1420 | llvm::SmallVector<clang::Expr*, 16> InitVals; |
| 1421 | for (unsigned i = 0; i < N_2; i++) |
| 1422 | InitVals.push_back(Float0Val); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1423 | clang::Expr *InitExpr = |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 1424 | new(C) clang::InitListExpr(C, Loc, InitVals, Loc); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1425 | InitExpr->setType(C.getConstantArrayType(FloatTy, |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 1426 | llvm::APInt(32, N_2), |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1427 | clang::ArrayType::Normal, |
| 1428 | /* EltTypeQuals = */0)); |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 1429 | llvm::SmallVector<clang::Expr*, 1> InitExprVec; |
| 1430 | InitExprVec.push_back(InitExpr); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1431 | |
Stephen Hines | 1dfc415 | 2012-09-10 20:16:04 -0700 | [diff] [blame] | 1432 | Res = new(C) clang::InitListExpr(C, Loc, InitExprVec, Loc); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1433 | break; |
| 1434 | } |
| 1435 | case RSExportPrimitiveType::DataTypeUnknown: |
| 1436 | case RSExportPrimitiveType::DataTypeFloat16: |
| 1437 | case RSExportPrimitiveType::DataTypeFloat32: |
| 1438 | case RSExportPrimitiveType::DataTypeFloat64: |
| 1439 | case RSExportPrimitiveType::DataTypeSigned8: |
| 1440 | case RSExportPrimitiveType::DataTypeSigned16: |
| 1441 | case RSExportPrimitiveType::DataTypeSigned32: |
| 1442 | case RSExportPrimitiveType::DataTypeSigned64: |
| 1443 | case RSExportPrimitiveType::DataTypeUnsigned8: |
| 1444 | case RSExportPrimitiveType::DataTypeUnsigned16: |
| 1445 | case RSExportPrimitiveType::DataTypeUnsigned32: |
| 1446 | case RSExportPrimitiveType::DataTypeUnsigned64: |
| 1447 | case RSExportPrimitiveType::DataTypeBoolean: |
| 1448 | case RSExportPrimitiveType::DataTypeUnsigned565: |
| 1449 | case RSExportPrimitiveType::DataTypeUnsigned5551: |
| 1450 | case RSExportPrimitiveType::DataTypeUnsigned4444: |
| 1451 | case RSExportPrimitiveType::DataTypeMax: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1452 | slangAssert(false && "Not RS object type!"); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1453 | } |
| 1454 | // No default case will enable compiler detecting the missing cases |
| 1455 | } |
| 1456 | |
| 1457 | return Res; |
| 1458 | } |
| 1459 | |
| 1460 | void RSObjectRefCount::VisitDeclStmt(clang::DeclStmt *DS) { |
| 1461 | for (clang::DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end(); |
| 1462 | I != E; |
| 1463 | I++) { |
| 1464 | clang::Decl *D = *I; |
| 1465 | if (D->getKind() == clang::Decl::Var) { |
| 1466 | clang::VarDecl *VD = static_cast<clang::VarDecl*>(D); |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1467 | RSExportPrimitiveType::DataType DT = |
| 1468 | RSExportPrimitiveType::DataTypeUnknown; |
| 1469 | clang::Expr *InitExpr = NULL; |
| 1470 | if (InitializeRSObject(VD, &DT, &InitExpr)) { |
Stephen Hines | b0fabe5 | 2013-01-07 19:06:09 -0800 | [diff] [blame] | 1471 | // We need to zero-init all RS object types (including matrices), ... |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1472 | getCurrentScope()->AppendRSObjectInit(VD, DS, DT, InitExpr); |
Stephen Hines | b0fabe5 | 2013-01-07 19:06:09 -0800 | [diff] [blame] | 1473 | // ... but, only add to the list of RS objects if we have some |
| 1474 | // non-matrix RS object fields. |
| 1475 | if (CountRSObjectTypes(mCtx, VD->getType().getTypePtr(), |
| 1476 | VD->getLocation())) { |
| 1477 | getCurrentScope()->addRSObject(VD); |
| 1478 | } |
Stephen Hines | e79fb5e | 2011-02-01 19:12:43 -0800 | [diff] [blame] | 1479 | } |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | void RSObjectRefCount::VisitCompoundStmt(clang::CompoundStmt *CS) { |
| 1486 | if (!CS->body_empty()) { |
| 1487 | // Push a new scope |
| 1488 | Scope *S = new Scope(CS); |
| 1489 | mScopeStack.push(S); |
| 1490 | |
| 1491 | VisitStmt(CS); |
| 1492 | |
| 1493 | // Destroy the scope |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1494 | slangAssert((getCurrentScope() == S) && "Corrupted scope stack!"); |
Stephen Hines | 1bdd497 | 2010-11-08 17:35:08 -0800 | [diff] [blame] | 1495 | S->InsertLocalVarDestructors(); |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1496 | mScopeStack.pop(); |
| 1497 | delete S; |
| 1498 | } |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | void RSObjectRefCount::VisitBinAssign(clang::BinaryOperator *AS) { |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 1503 | clang::QualType QT = AS->getType(); |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 1504 | |
Stephen Hines | d0b5edd | 2011-04-18 16:38:03 -0700 | [diff] [blame] | 1505 | if (CountRSObjectTypes(mCtx, QT.getTypePtr(), AS->getExprLoc())) { |
| 1506 | getCurrentScope()->ReplaceRSObjectAssignment(AS); |
Stephen Hines | c202d2d | 2011-01-26 11:57:57 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Stephen Hines | 4b32ffd | 2010-11-05 18:47:11 -0700 | [diff] [blame] | 1509 | return; |
| 1510 | } |
| 1511 | |
| 1512 | void RSObjectRefCount::VisitStmt(clang::Stmt *S) { |
| 1513 | for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end(); |
| 1514 | I != E; |
| 1515 | I++) { |
| 1516 | if (clang::Stmt *Child = *I) { |
| 1517 | Visit(Child); |
| 1518 | } |
| 1519 | } |
| 1520 | return; |
| 1521 | } |
| 1522 | |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 1523 | // This function walks the list of global variables and (potentially) creates |
| 1524 | // a single global static destructor function that properly decrements |
| 1525 | // reference counts on the contained RS object types. |
| 1526 | clang::FunctionDecl *RSObjectRefCount::CreateStaticGlobalDtor() { |
| 1527 | Init(); |
| 1528 | |
| 1529 | clang::DeclContext *DC = mCtx.getTranslationUnitDecl(); |
| 1530 | clang::SourceLocation loc; |
| 1531 | |
Stephen Hines | 3f175af | 2011-09-16 16:26:29 -0700 | [diff] [blame] | 1532 | llvm::StringRef SR(".rs.dtor"); |
| 1533 | clang::IdentifierInfo &II = mCtx.Idents.get(SR); |
| 1534 | clang::DeclarationName N(&II); |
| 1535 | clang::FunctionProtoType::ExtProtoInfo EPI; |
Stephen Hines | 82d7288 | 2013-03-18 18:17:57 -0700 | [diff] [blame^] | 1536 | clang::QualType T = mCtx.getFunctionType(mCtx.VoidTy, |
| 1537 | llvm::ArrayRef<clang::QualType>(), EPI); |
Stephen Hines | 3f175af | 2011-09-16 16:26:29 -0700 | [diff] [blame] | 1538 | clang::FunctionDecl *FD = NULL; |
| 1539 | |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 1540 | // Generate rsClearObject() call chains for every global variable |
| 1541 | // (whether static or extern). |
| 1542 | std::list<clang::Stmt *> StmtList; |
| 1543 | for (clang::DeclContext::decl_iterator I = DC->decls_begin(), |
| 1544 | E = DC->decls_end(); I != E; I++) { |
| 1545 | clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*I); |
| 1546 | if (VD) { |
| 1547 | if (CountRSObjectTypes(mCtx, VD->getType().getTypePtr(), loc)) { |
Stephen Hines | 3f175af | 2011-09-16 16:26:29 -0700 | [diff] [blame] | 1548 | if (!FD) { |
| 1549 | // Only create FD if we are going to use it. |
| 1550 | FD = clang::FunctionDecl::Create(mCtx, DC, loc, loc, N, T, NULL); |
| 1551 | } |
| 1552 | // Make sure to create any helpers within the function's DeclContext, |
| 1553 | // not the one associated with the global translation unit. |
| 1554 | clang::Stmt *RSClearObjectCall = Scope::ClearRSObject(VD, FD); |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 1555 | StmtList.push_back(RSClearObjectCall); |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | // Nothing needs to be destroyed, so don't emit a dtor. |
| 1561 | if (StmtList.empty()) { |
| 1562 | return NULL; |
| 1563 | } |
| 1564 | |
Stephen Hines | 688e64b | 2011-08-23 16:01:25 -0700 | [diff] [blame] | 1565 | clang::CompoundStmt *CS = BuildCompoundStmt(mCtx, StmtList, loc); |
| 1566 | |
| 1567 | FD->setBody(CS); |
| 1568 | |
| 1569 | return FD; |
| 1570 | } |
| 1571 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 1572 | } // namespace slang |