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