blob: e8a5d2e9f7d086b45efb3a12e34212d8c2ebaf5e [file] [log] [blame]
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001/*
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 Hinese639eb52010-11-08 19:27:20 -080019#include <list>
20
Stephen Hines4b32ffd2010-11-05 18:47:11 -070021#include "clang/AST/DeclGroup.h"
22#include "clang/AST/Expr.h"
Loganbe274822011-02-16 22:02:54 +080023#include "clang/AST/NestedNameSpecifier.h"
Stephen Hines4b32ffd2010-11-05 18:47:11 -070024#include "clang/AST/OperationKinds.h"
25#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtVisitor.h"
27
Stephen Hines6e6578a2011-02-07 18:05:48 -080028#include "slang_assert.h"
Jean-Luc Brouilletc10bc752015-05-04 23:02:25 -070029#include "slang.h"
Stephen Hines292e00a2011-03-18 19:11:30 -070030#include "slang_rs_ast_replace.h"
Stephen Hines4b32ffd2010-11-05 18:47:11 -070031#include "slang_rs_export_type.h"
32
Stephen Hinese639eb52010-11-08 19:27:20 -080033namespace slang {
Stephen Hines4b32ffd2010-11-05 18:47:11 -070034
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070035/* Even though those two arrays are of size DataTypeMax, only entries that
36 * correspond to object types will be set.
37 */
38clang::FunctionDecl *
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070039RSObjectRefCount::RSSetObjectFD[DataTypeMax];
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070040clang::FunctionDecl *
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070041RSObjectRefCount::RSClearObjectFD[DataTypeMax];
Stephen Hines1bdd4972010-11-08 17:35:08 -080042
Stephen Hinesf2174cf2011-02-09 23:21:37 -080043void RSObjectRefCount::GetRSRefCountingFunctions(clang::ASTContext &C) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070044 for (unsigned i = 0; i < DataTypeMax; i++) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070045 RSSetObjectFD[i] = nullptr;
46 RSClearObjectFD[i] = nullptr;
Stephen Hines1bdd4972010-11-08 17:35:08 -080047 }
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 Hines6e6578a2011-02-07 18:05:48 -080061 slangAssert((FD->getNumParams() == 2) &&
62 "Invalid rsSetObject function prototype (# params)");
Stephen Hines1bdd4972010-11-08 17:35:08 -080063 RSObjectFD = RSSetObjectFD;
64 } else if (FD->getName() == "rsClearObject") {
Stephen Hines6e6578a2011-02-07 18:05:48 -080065 slangAssert((FD->getNumParams() == 1) &&
66 "Invalid rsClearObject function prototype (# params)");
Stephen Hines1bdd4972010-11-08 17:35:08 -080067 RSObjectFD = RSClearObjectFD;
Stephen Hinese639eb52010-11-08 19:27:20 -080068 } else {
Stephen Hines1bdd4972010-11-08 17:35:08 -080069 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 Hines6e6578a2011-02-07 18:05:48 -080075 slangAssert(PVT->isPointerType() &&
76 "Invalid rs{Set,Clear}Object function prototype (pointer param)");
Stephen Hines1bdd4972010-11-08 17:35:08 -080077
78 // The rs object type passed to the FD
79 clang::QualType RST = PVT->getPointeeType();
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070080 DataType DT = RSExportPrimitiveType::GetRSSpecificType(RST.getTypePtr());
Stephen Hines6e6578a2011-02-07 18:05:48 -080081 slangAssert(RSExportPrimitiveType::IsRSObjectType(DT)
Stephen Hines1bdd4972010-11-08 17:35:08 -080082 && "must be RS object type");
83
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070084 if (DT >= 0 && DT < DataTypeMax) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070085 RSObjectFD[DT] = FD;
86 } else {
87 slangAssert(false && "incorrect type");
88 }
Stephen Hines1bdd4972010-11-08 17:35:08 -080089 }
90 }
91}
92
Stephen Hines4464d822010-11-11 16:45:08 -080093namespace {
94
Stephen Hines292e00a2011-03-18 19:11:30 -070095// This function constructs a new CompoundStmt from the input StmtList.
96static clang::CompoundStmt* BuildCompoundStmt(clang::ASTContext &C,
97 std::list<clang::Stmt*> &StmtList, clang::SourceLocation Loc) {
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -080098 unsigned NewStmtCount = StmtList.size();
Stephen Hines292e00a2011-03-18 19:11:30 -070099 unsigned CompoundStmtCount = 0;
Stephen Hines1bdd4972010-11-08 17:35:08 -0800100
Stephen Hines292e00a2011-03-18 19:11:30 -0700101 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 Hines23c43582013-01-09 20:02:04 -0800111 clang::CompoundStmt *CS = new(C) clang::CompoundStmt(
112 C, llvm::makeArrayRef(CompoundStmtList, CompoundStmtCount), Loc, Loc);
Stephen Hines292e00a2011-03-18 19:11:30 -0700113
114 delete [] CompoundStmtList;
115
116 return CS;
117}
118
119static 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 Hines1bdd4972010-11-08 17:35:08 -0800128
129 unsigned UpdatedStmtCount = 0;
Stephen Hines292e00a2011-03-18 19:11:30 -0700130 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 Hines1bdd4972010-11-08 17:35:08 -0800137
Stephen Hines292e00a2011-03-18 19:11:30 -0700138 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 Wailes5abbe0e2014-08-12 15:58:29 -0700154 // When S is nullptr, we are appending to the end of the CompoundStmt.
Stephen Hines292e00a2011-03-18 19:11:30 -0700155 if (!S) {
156 slangAssert(Once == 0);
Stephen Hines03981a32010-12-14 19:45:49 -0800157 std::list<clang::Stmt*>::const_iterator I = StmtList.begin();
Stephen Hines292e00a2011-03-18 19:11:30 -0700158 std::list<clang::Stmt*>::const_iterator E = StmtList.end();
159 for ( ; I != E; I++) {
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -0800160 UpdatedStmtList[UpdatedStmtCount++] = *I;
Stephen Hines4464d822010-11-11 16:45:08 -0800161 }
Stephen Hines1bdd4972010-11-08 17:35:08 -0800162 }
163
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -0800164 CS->setStmts(C, UpdatedStmtList, UpdatedStmtCount);
Stephen Hines1bdd4972010-11-08 17:35:08 -0800165
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -0800166 delete [] UpdatedStmtList;
Stephen Hines1bdd4972010-11-08 17:35:08 -0800167}
168
Stephen Hinesa883ce32011-08-11 18:52:48 -0700169// This class visits a compound statement and inserts DtorStmt
170// in proper locations. This includes inserting it before any
Stephen Hines4464d822010-11-11 16:45:08 -0800171// 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 Hines292e00a2011-03-18 19:11:30 -0700175//
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 Hines4464d822010-11-11 16:45:08 -0800181class DestructorVisitor : public clang::StmtVisitor<DestructorVisitor> {
182 private:
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700183 clang::ASTContext &mCtx;
Stephen Hines292e00a2011-03-18 19:11:30 -0700184
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 Hinesa883ce32011-08-11 18:52:48 -0700198 // The destructor to execute for this scope/variable.
199 clang::Stmt* mDtorStmt;
Stephen Hines292e00a2011-03-18 19:11:30 -0700200
201 // The stack of statements which should be replaced by a compound statement
Stephen Hinesa883ce32011-08-11 18:52:48 -0700202 // containing the new destructor call followed by the original Stmt.
Stephen Hines292e00a2011-03-18 19:11:30 -0700203 std::stack<clang::Stmt*> mReplaceStmtStack;
204
Stephen Hinesa883ce32011-08-11 18:52:48 -0700205 // 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 Hines4464d822010-11-11 16:45:08 -0800211 public:
Stephen Hines292e00a2011-03-18 19:11:30 -0700212 DestructorVisitor(clang::ASTContext &C,
213 clang::Stmt* OuterStmt,
Stephen Hinesa883ce32011-08-11 18:52:48 -0700214 clang::Stmt* DtorStmt,
215 clang::SourceLocation VarLoc);
Stephen Hines292e00a2011-03-18 19:11:30 -0700216
217 // This code walks the collected list of Stmts to replace and actually does
Stephen Hinesa883ce32011-08-11 18:52:48 -0700218 // the replacement. It also finishes up by appending the destructor to the
219 // current outermost CompoundStmt.
Stephen Hines292e00a2011-03-18 19:11:30 -0700220 void InsertDestructors() {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700221 clang::Stmt *S = nullptr;
Stephen Hinesa883ce32011-08-11 18:52:48 -0700222 clang::SourceManager &SM = mCtx.getSourceManager();
223 std::list<clang::Stmt *> StmtList;
224 StmtList.push_back(mDtorStmt);
225
Stephen Hines292e00a2011-03-18 19:11:30 -0700226 while (!mReplaceStmtStack.empty()) {
227 S = mReplaceStmtStack.top();
228 mReplaceStmtStack.pop();
229
Stephen Hinesa883ce32011-08-11 18:52:48 -0700230 // 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 Hines292e00a2011-03-18 19:11:30 -0700237 clang::CompoundStmt *CS =
Stephen Hinesa883ce32011-08-11 18:52:48 -0700238 BuildCompoundStmt(mCtx, StmtList, S->getLocEnd());
239 StmtList.pop_back();
Stephen Hines292e00a2011-03-18 19:11:30 -0700240
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700241 RSASTReplace R(mCtx);
Stephen Hines292e00a2011-03-18 19:11:30 -0700242 R.ReplaceStmt(mOuterStmt, S, CS);
243 }
Logan Chienab992e52011-07-20 22:06:52 +0800244 clang::CompoundStmt *CS =
245 llvm::dyn_cast<clang::CompoundStmt>(mOuterStmt);
Stephen Hines292e00a2011-03-18 19:11:30 -0700246 slangAssert(CS);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700247 AppendAfterStmt(mCtx, CS, nullptr, StmtList);
Stephen Hines292e00a2011-03-18 19:11:30 -0700248 }
249
Stephen Hines4464d822010-11-11 16:45:08 -0800250 void VisitStmt(clang::Stmt *S);
251 void VisitCompoundStmt(clang::CompoundStmt *CS);
Stephen Hines292e00a2011-03-18 19:11:30 -0700252
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 Hines4464d822010-11-11 16:45:08 -0800264};
265
266DestructorVisitor::DestructorVisitor(clang::ASTContext &C,
Stephen Hines292e00a2011-03-18 19:11:30 -0700267 clang::Stmt *OuterStmt,
Stephen Hinesa883ce32011-08-11 18:52:48 -0700268 clang::Stmt *DtorStmt,
269 clang::SourceLocation VarLoc)
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700270 : mCtx(C),
Stephen Hines292e00a2011-03-18 19:11:30 -0700271 mLoopDepth(0),
272 mSwitchDepth(0),
273 mOuterStmt(OuterStmt),
Stephen Hinesa883ce32011-08-11 18:52:48 -0700274 mDtorStmt(DtorStmt),
275 mVarLoc(VarLoc) {
Stephen Hines4464d822010-11-11 16:45:08 -0800276}
277
278void 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 Hines4464d822010-11-11 16:45:08 -0800286}
287
Stephen Hines292e00a2011-03-18 19:11:30 -0700288void DestructorVisitor::VisitCompoundStmt(clang::CompoundStmt *CS) {
289 VisitStmt(CS);
Stephen Hines292e00a2011-03-18 19:11:30 -0700290}
291
292void DestructorVisitor::VisitBreakStmt(clang::BreakStmt *BS) {
293 VisitStmt(BS);
294 if ((mLoopDepth == 0) && (mSwitchDepth == 0)) {
295 mReplaceStmtStack.push(BS);
296 }
Stephen Hines292e00a2011-03-18 19:11:30 -0700297}
298
299void DestructorVisitor::VisitCaseStmt(clang::CaseStmt *CS) {
300 VisitStmt(CS);
Stephen Hines292e00a2011-03-18 19:11:30 -0700301}
302
303void 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 Hines292e00a2011-03-18 19:11:30 -0700309}
310
311void DestructorVisitor::VisitDefaultStmt(clang::DefaultStmt *DS) {
312 VisitStmt(DS);
Stephen Hines292e00a2011-03-18 19:11:30 -0700313}
314
315void DestructorVisitor::VisitDoStmt(clang::DoStmt *DS) {
316 mLoopDepth++;
317 VisitStmt(DS);
318 mLoopDepth--;
Stephen Hines292e00a2011-03-18 19:11:30 -0700319}
320
321void DestructorVisitor::VisitForStmt(clang::ForStmt *FS) {
322 mLoopDepth++;
323 VisitStmt(FS);
324 mLoopDepth--;
Stephen Hines292e00a2011-03-18 19:11:30 -0700325}
326
327void DestructorVisitor::VisitIfStmt(clang::IfStmt *IS) {
328 VisitStmt(IS);
Stephen Hines292e00a2011-03-18 19:11:30 -0700329}
330
331void DestructorVisitor::VisitReturnStmt(clang::ReturnStmt *RS) {
332 mReplaceStmtStack.push(RS);
Stephen Hines292e00a2011-03-18 19:11:30 -0700333}
334
335void DestructorVisitor::VisitSwitchCase(clang::SwitchCase *SC) {
336 slangAssert(false && "Both case and default have specialized handlers");
337 VisitStmt(SC);
Stephen Hines292e00a2011-03-18 19:11:30 -0700338}
339
340void DestructorVisitor::VisitSwitchStmt(clang::SwitchStmt *SS) {
341 mSwitchDepth++;
342 VisitStmt(SS);
343 mSwitchDepth--;
Stephen Hines292e00a2011-03-18 19:11:30 -0700344}
345
346void DestructorVisitor::VisitWhileStmt(clang::WhileStmt *WS) {
347 mLoopDepth++;
348 VisitStmt(WS);
349 mLoopDepth--;
Stephen Hines292e00a2011-03-18 19:11:30 -0700350}
351
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800352clang::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 Hines03981a32010-12-14 19:45:49 -0800359
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800360 clang::FunctionDecl *ClearObjectFD = RSObjectRefCount::GetRSClearObjectFD(T);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700361 slangAssert((ClearObjectFD != nullptr) &&
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800362 "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,
Loganbe274822011-02-16 22:02:54 +0800381 clang::VK_RValue,
382 clang::OK_Ordinary,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800383 Loc);
384
385 clang::Expr *RefRSClearObjectFD =
386 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800387 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800388 clang::SourceLocation(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800389 ClearObjectFD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -0700390 false,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800391 ClearObjectFD->getLocation(),
Loganbe274822011-02-16 22:02:54 +0800392 ClearObjectFDType,
393 clang::VK_RValue,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700394 nullptr);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800395
396 clang::Expr *RSClearObjectFP =
397 clang::ImplicitCastExpr::Create(C,
398 C.getPointerType(ClearObjectFDType),
399 clang::CK_FunctionToPointerDecay,
400 RefRSClearObjectFD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700401 nullptr,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800402 clang::VK_RValue);
403
Stephen Hines1dfc4152012-09-10 20:16:04 -0700404 llvm::SmallVector<clang::Expr*, 1> ArgList;
405 ArgList.push_back(AddrRefRSVar);
406
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800407 clang::CallExpr *RSClearObjectCall =
408 new(C) clang::CallExpr(C,
409 RSClearObjectFP,
Stephen Hines1dfc4152012-09-10 20:16:04 -0700410 ArgList,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800411 ClearObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +0800412 clang::VK_RValue,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800413 Loc);
414
415 return RSClearObjectCall;
416}
417
418static int ArrayDim(const clang::Type *T) {
Stephen Hines03981a32010-12-14 19:45:49 -0800419 if (!T || !T->isArrayType()) {
420 return 0;
421 }
422
423 const clang::ConstantArrayType *CAT =
424 static_cast<const clang::ConstantArrayType *>(T);
Stephen Hines9d2c0fa2011-01-05 14:55:18 -0800425 return static_cast<int>(CAT->getSize().getSExtValue());
Stephen Hines03981a32010-12-14 19:45:49 -0800426}
427
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800428static clang::Stmt *ClearStructRSObject(
429 clang::ASTContext &C,
430 clang::DeclContext *DC,
431 clang::Expr *RefRSStruct,
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700432 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800433 clang::SourceLocation Loc);
434
435static clang::Stmt *ClearArrayRSObject(
436 clang::ASTContext &C,
437 clang::DeclContext *DC,
438 clang::Expr *RefRSArr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700439 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800440 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 Hines03981a32010-12-14 19:45:49 -0800447
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700448 clang::Stmt *StmtArray[2] = {nullptr};
Stephen Hines03981a32010-12-14 19:45:49 -0800449 int StmtCtr = 0;
450
Stephen Hines03981a32010-12-14 19:45:49 -0800451 if (NumArrayElements <= 0) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700452 return nullptr;
Stephen Hines03981a32010-12-14 19:45:49 -0800453 }
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 Wailes5abbe0e2014-08-12 15:58:29 -0700466 // nullptr << CondVar >>
Stephen Hines03981a32010-12-14 19:45:49 -0800467 // (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 Hinesf2174cf2011-02-09 23:21:37 -0800482 DC,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700483 StartLoc,
Stephen Hines03981a32010-12-14 19:45:49 -0800484 Loc,
485 &II,
486 C.IntTy,
487 C.getTrivialTypeSourceInfo(C.IntTy),
Stephen Hines03981a32010-12-14 19:45:49 -0800488 clang::SC_None);
Pirama Arumuga Nainar5d61db62015-06-09 11:34:18 -0700489 // Mark "rsIntIter" as used
490 IIVD->markUsed(C);
Stephen Hines03981a32010-12-14 19:45:49 -0800491 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,
Loganbe274822011-02-16 22:02:54 +0800503 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800504 clang::SourceLocation(),
Stephen Hines03981a32010-12-14 19:45:49 -0800505 IIVD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -0700506 false,
Stephen Hines03981a32010-12-14 19:45:49 -0800507 Loc,
Loganbe274822011-02-16 22:02:54 +0800508 C.IntTy,
509 clang::VK_RValue,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700510 nullptr);
Stephen Hines03981a32010-12-14 19:45:49 -0800511
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,
Loganbe274822011-02-16 22:02:54 +0800520 clang::VK_RValue,
521 clang::OK_Ordinary,
Stephen Hines23c43582013-01-09 20:02:04 -0800522 Loc,
523 false);
Stephen Hines03981a32010-12-14 19:45:49 -0800524
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,
Loganbe274822011-02-16 22:02:54 +0800534 clang::VK_RValue,
535 clang::OK_Ordinary,
Stephen Hines23c43582013-01-09 20:02:04 -0800536 Loc,
537 false);
Stephen Hines03981a32010-12-14 19:45:49 -0800538
539 // Inc -> "rsIntIter++"
540 clang::UnaryOperator *Inc =
541 new(C) clang::UnaryOperator(RefrsIntIter,
542 clang::UO_PostInc,
543 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800544 clang::VK_RValue,
545 clang::OK_Ordinary,
Stephen Hines03981a32010-12-14 19:45:49 -0800546 Loc);
547
548 // Body -> "rsClearObject(&VD[rsIntIter]);"
549 // Destructor loop operates on individual array elements
Stephen Hines03981a32010-12-14 19:45:49 -0800550
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800551 clang::Expr *RefRSArrPtr =
Stephen Hines03981a32010-12-14 19:45:49 -0800552 clang::ImplicitCastExpr::Create(C,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800553 C.getPointerType(BaseType->getCanonicalTypeInternal()),
Stephen Hines03981a32010-12-14 19:45:49 -0800554 clang::CK_ArrayToPointerDecay,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800555 RefRSArr,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700556 nullptr,
Stephen Hines03981a32010-12-14 19:45:49 -0800557 clang::VK_RValue);
558
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800559 clang::Expr *RefRSArrPtrSubscript =
560 new(C) clang::ArraySubscriptExpr(RefRSArrPtr,
Stephen Hines03981a32010-12-14 19:45:49 -0800561 RefrsIntIter,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800562 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800563 clang::VK_RValue,
564 clang::OK_Ordinary,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800565 Loc);
Stephen Hines03981a32010-12-14 19:45:49 -0800566
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700567 DataType DT = RSExportPrimitiveType::GetRSSpecificType(BaseType);
Stephen Hines03981a32010-12-14 19:45:49 -0800568
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700569 clang::Stmt *RSClearObjectCall = nullptr;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800570 if (BaseType->isArrayType()) {
571 RSClearObjectCall =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700572 ClearArrayRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700573 } else if (DT == DataTypeUnknown) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800574 RSClearObjectCall =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700575 ClearStructRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800576 } else {
577 RSClearObjectCall = ClearSingleRSObject(C, RefRSArrPtrSubscript, Loc);
578 }
Stephen Hines03981a32010-12-14 19:45:49 -0800579
580 clang::ForStmt *DestructorLoop =
581 new(C) clang::ForStmt(C,
582 Init,
583 Cond,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700584 nullptr, // no condVar
Stephen Hines03981a32010-12-14 19:45:49 -0800585 Inc,
586 RSClearObjectCall,
587 Loc,
588 Loc,
589 Loc);
590
591 StmtArray[StmtCtr++] = DestructorLoop;
Stephen Hines6e6578a2011-02-07 18:05:48 -0800592 slangAssert(StmtCtr == 2);
Stephen Hines03981a32010-12-14 19:45:49 -0800593
Stephen Hines23c43582013-01-09 20:02:04 -0800594 clang::CompoundStmt *CS = new(C) clang::CompoundStmt(
595 C, llvm::makeArrayRef(StmtArray, StmtCtr), Loc, Loc);
Stephen Hines03981a32010-12-14 19:45:49 -0800596
597 return CS;
598}
599
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700600static unsigned CountRSObjectTypes(clang::ASTContext &C,
601 const clang::Type *T,
602 clang::SourceLocation Loc) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800603 slangAssert(T);
604 unsigned RSObjectCount = 0;
605
606 if (T->isArrayType()) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700607 return CountRSObjectTypes(C, T->getArrayElementTypeNoTypeQual(), Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800608 }
609
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700610 DataType DT = RSExportPrimitiveType::GetRSSpecificType(T);
611 if (DT != DataTypeUnknown) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800612 return (RSExportPrimitiveType::IsRSObjectType(DT) ? 1 : 0);
613 }
614
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700615 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 Hines78e69cb2011-04-22 15:03:19 -0700625 slangAssert(false && "can't have unions with RS object types!");
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700626 return 0;
627 }
628 }
629 }
630
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800631 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 Hinesd0b5edd2011-04-18 16:38:03 -0700643 if (CountRSObjectTypes(C, FT, Loc)) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800644 // Sub-structs should only count once (as should arrays, etc.)
645 RSObjectCount++;
646 }
647 }
648
649 return RSObjectCount;
650}
651
652static clang::Stmt *ClearStructRSObject(
653 clang::ASTContext &C,
654 clang::DeclContext *DC,
655 clang::Expr *RefRSStruct,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700656 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800657 clang::SourceLocation Loc) {
658 const clang::Type *BaseType = RefRSStruct->getType().getTypePtr();
659
660 slangAssert(!BaseType->isArrayType());
661
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800662 // Structs should show up as unknown primitive types
Alex Sakhartchouk9be93602011-03-17 17:03:36 -0700663 slangAssert(RSExportPrimitiveType::GetRSSpecificType(BaseType) ==
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700664 DataTypeUnknown);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800665
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700666 unsigned FieldsToDestroy = CountRSObjectTypes(C, BaseType, Loc);
Stephen Hinesb0fabe52013-01-07 19:06:09 -0800667 slangAssert(FieldsToDestroy != 0);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800668
669 unsigned StmtCount = 0;
670 clang::Stmt **StmtArray = new clang::Stmt*[FieldsToDestroy];
Stephen Hines2bb67db2011-02-11 01:36:40 -0800671 for (unsigned i = 0; i < FieldsToDestroy; i++) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700672 StmtArray[i] = nullptr;
Stephen Hines2bb67db2011-02-11 01:36:40 -0800673 }
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800674
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 Nainar5d61db62015-06-09 11:34:18 -0700693 // 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 Hinesf2174cf2011-02-09 23:21:37 -0800698 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 Hines0b754582015-04-07 13:59:57 -0700705 clang::SourceLocation(),
Loganbe274822011-02-16 22:02:54 +0800706 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800707 clang::SourceLocation(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800708 FD,
709 FoundDecl,
Pirama Arumuga Nainar5d61db62015-06-09 11:34:18 -0700710 FDDeclNameInfo,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700711 nullptr,
Loganbe274822011-02-16 22:02:54 +0800712 OrigType->getCanonicalTypeInternal(),
713 clang::VK_RValue,
714 clang::OK_Ordinary);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800715
716 slangAssert(StmtCount < FieldsToDestroy);
717
718 if (IsArrayType) {
719 StmtArray[StmtCount++] = ClearArrayRSObject(C,
720 DC,
721 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700722 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800723 Loc);
724 } else {
725 StmtArray[StmtCount++] = ClearSingleRSObject(C,
726 RSObjectMember,
727 Loc);
728 }
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700729 } else if (FT->isStructureType() && CountRSObjectTypes(C, FT, Loc)) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800730 // 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 Hines0b754582015-04-07 13:59:57 -0700739 clang::SourceLocation(),
Loganbe274822011-02-16 22:02:54 +0800740 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800741 clang::SourceLocation(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800742 FD,
743 FoundDecl,
744 clang::DeclarationNameInfo(),
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700745 nullptr,
Loganbe274822011-02-16 22:02:54 +0800746 OrigType->getCanonicalTypeInternal(),
747 clang::VK_RValue,
748 clang::OK_Ordinary);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800749
750 if (IsArrayType) {
751 StmtArray[StmtCount++] = ClearArrayRSObject(C,
752 DC,
753 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700754 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800755 Loc);
756 } else {
757 StmtArray[StmtCount++] = ClearStructRSObject(C,
758 DC,
759 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700760 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800761 Loc);
762 }
763 }
764 }
765
766 slangAssert(StmtCount > 0);
Stephen Hines23c43582013-01-09 20:02:04 -0800767 clang::CompoundStmt *CS = new(C) clang::CompoundStmt(
768 C, llvm::makeArrayRef(StmtArray, StmtCount), Loc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800769
770 delete [] StmtArray;
771
772 return CS;
773}
774
Stephen Hines2bb67db2011-02-11 01:36:40 -0800775static clang::Stmt *CreateSingleRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800776 clang::Expr *DstExpr,
777 clang::Expr *SrcExpr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700778 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800779 clang::SourceLocation Loc) {
780 const clang::Type *T = DstExpr->getType().getTypePtr();
781 clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(T);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700782 slangAssert((SetObjectFD != nullptr) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -0800783 "rsSetObject doesn't cover all RS object types");
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800784
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 Hinesc202d2d2011-01-26 11:57:57 -0800790 clang::Expr *RefRSSetObjectFD =
791 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800792 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800793 clang::SourceLocation(),
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800794 SetObjectFD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -0700795 false,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800796 Loc,
Loganbe274822011-02-16 22:02:54 +0800797 SetObjectFDType,
798 clang::VK_RValue,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700799 nullptr);
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800800
801 clang::Expr *RSSetObjectFP =
802 clang::ImplicitCastExpr::Create(C,
803 C.getPointerType(SetObjectFDType),
804 clang::CK_FunctionToPointerDecay,
805 RefRSSetObjectFD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700806 nullptr,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800807 clang::VK_RValue);
808
Stephen Hines1dfc4152012-09-10 20:16:04 -0700809 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 Hinesc202d2d2011-01-26 11:57:57 -0800817
818 clang::CallExpr *RSSetObjectCall =
819 new(C) clang::CallExpr(C,
820 RSSetObjectFP,
821 ArgList,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800822 SetObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +0800823 clang::VK_RValue,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800824 Loc);
825
Stephen Hines2bb67db2011-02-11 01:36:40 -0800826 return RSSetObjectCall;
827}
Stephen Hinese79fb5e2011-02-01 19:12:43 -0800828
Stephen Hines2bb67db2011-02-11 01:36:40 -0800829static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800830 clang::Expr *LHS,
831 clang::Expr *RHS,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700832 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800833 clang::SourceLocation Loc);
834
Al Sutton246fa172012-02-23 13:09:08 +0000835/*static clang::Stmt *CreateArrayRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800836 clang::Expr *DstArr,
837 clang::Expr *SrcArr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700838 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800839 clang::SourceLocation Loc) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700840 clang::DeclContext *DC = nullptr;
Stephen Hines2bb67db2011-02-11 01:36:40 -0800841 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 Wailes5abbe0e2014-08-12 15:58:29 -0700848 clang::Stmt *StmtArray[2] = {nullptr};
Stephen Hines2bb67db2011-02-11 01:36:40 -0800849 int StmtCtr = 0;
850
851 if (NumArrayElements <= 0) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700852 return nullptr;
Stephen Hines2bb67db2011-02-11 01:36:40 -0800853 }
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 Liaocc887ba2011-04-19 18:36:20 -0700860 StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800861 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,
Loganbe274822011-02-16 22:02:54 +0800879 clang::NestedNameSpecifierLoc(),
Stephen Hines2bb67db2011-02-11 01:36:40 -0800880 IIVD,
881 Loc,
Loganbe274822011-02-16 22:02:54 +0800882 C.IntTy,
883 clang::VK_RValue,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700884 nullptr);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800885
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,
Loganbe274822011-02-16 22:02:54 +0800894 clang::VK_RValue,
895 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800896 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,
Loganbe274822011-02-16 22:02:54 +0800907 clang::VK_RValue,
908 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800909 Loc);
910
911 // Inc -> "rsIntIter++"
912 clang::UnaryOperator *Inc =
913 new(C) clang::UnaryOperator(RefrsIntIter,
914 clang::UO_PostInc,
915 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800916 clang::VK_RValue,
917 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800918 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 Wailes5abbe0e2014-08-12 15:58:29 -0700928 nullptr,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800929 clang::VK_RValue);
930
931 clang::Expr *DstArrPtrSubscript =
932 new(C) clang::ArraySubscriptExpr(DstArrPtr,
933 RefrsIntIter,
934 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800935 clang::VK_RValue,
936 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800937 Loc);
938
939 clang::Expr *SrcArrPtr =
940 clang::ImplicitCastExpr::Create(C,
941 C.getPointerType(BaseType->getCanonicalTypeInternal()),
942 clang::CK_ArrayToPointerDecay,
943 SrcArr,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700944 nullptr,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800945 clang::VK_RValue);
946
947 clang::Expr *SrcArrPtrSubscript =
948 new(C) clang::ArraySubscriptExpr(SrcArrPtr,
949 RefrsIntIter,
950 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800951 clang::VK_RValue,
952 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800953 Loc);
954
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700955 DataType DT = RSExportPrimitiveType::GetRSSpecificType(BaseType);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800956
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700957 clang::Stmt *RSSetObjectCall = nullptr;
Stephen Hines2bb67db2011-02-11 01:36:40 -0800958 if (BaseType->isArrayType()) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700959 RSSetObjectCall = CreateArrayRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700960 SrcArrPtrSubscript,
961 StartLoc, Loc);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700962 } else if (DT == DataTypeUnknown) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700963 RSSetObjectCall = CreateStructRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700964 SrcArrPtrSubscript,
965 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800966 } else {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700967 RSSetObjectCall = CreateSingleRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700968 SrcArrPtrSubscript,
969 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800970 }
971
972 clang::ForStmt *DestructorLoop =
973 new(C) clang::ForStmt(C,
974 Init,
975 Cond,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700976 nullptr, // no condVar
Stephen Hines2bb67db2011-02-11 01:36:40 -0800977 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 Sutton246fa172012-02-23 13:09:08 +0000990} */
Stephen Hines2bb67db2011-02-11 01:36:40 -0800991
992static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800993 clang::Expr *LHS,
994 clang::Expr *RHS,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700995 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800996 clang::SourceLocation Loc) {
Stephen Hines2bb67db2011-02-11 01:36:40 -0800997 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 Hinesd0b5edd2011-04-18 16:38:03 -07001003 unsigned FieldsToSet = CountRSObjectTypes(C, T, Loc) + 1;
Stephen Hines2bb67db2011-02-11 01:36:40 -08001004
1005 unsigned StmtCount = 0;
1006 clang::Stmt **StmtArray = new clang::Stmt*[FieldsToSet];
1007 for (unsigned i = 0; i < FieldsToSet; i++) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001008 StmtArray[i] = nullptr;
Stephen Hines2bb67db2011-02-11 01:36:40 -08001009 }
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 Hinesd0b5edd2011-04-18 16:38:03 -07001022 if (!CountRSObjectTypes(C, FT, Loc)) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001023 // 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 Hines0b754582015-04-07 13:59:57 -07001033 clang::SourceLocation(),
Loganbe274822011-02-16 22:02:54 +08001034 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001035 clang::SourceLocation(),
Stephen Hines2bb67db2011-02-11 01:36:40 -08001036 FD,
1037 FoundDecl,
1038 clang::DeclarationNameInfo(),
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001039 nullptr,
Loganbe274822011-02-16 22:02:54 +08001040 OrigType->getCanonicalTypeInternal(),
1041 clang::VK_RValue,
1042 clang::OK_Ordinary);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001043
1044 clang::MemberExpr *SrcMember =
1045 clang::MemberExpr::Create(C,
1046 RHS,
1047 false,
Stephen Hines0b754582015-04-07 13:59:57 -07001048 clang::SourceLocation(),
Loganbe274822011-02-16 22:02:54 +08001049 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001050 clang::SourceLocation(),
Stephen Hines2bb67db2011-02-11 01:36:40 -08001051 FD,
1052 FoundDecl,
1053 clang::DeclarationNameInfo(),
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001054 nullptr,
Loganbe274822011-02-16 22:02:54 +08001055 OrigType->getCanonicalTypeInternal(),
1056 clang::VK_RValue,
1057 clang::OK_Ordinary);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001058
1059 if (FT->isArrayType()) {
1060 FT = FT->getArrayElementTypeNoTypeQual();
1061 IsArrayType = true;
1062 }
1063
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001064 DataType DT = RSExportPrimitiveType::GetRSSpecificType(FT);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001065
1066 if (IsArrayType) {
Logan Chien9207a2e2011-10-21 15:39:28 +08001067 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 Hines2bb67db2011-02-11 01:36:40 -08001073 // TODO(srhines): Support setting arrays of RS objects
1074 // StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001075 // CreateArrayRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001076 } else if (DT == DataTypeUnknown) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001077 StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001078 CreateStructRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001079 } else if (RSExportPrimitiveType::IsRSObjectType(DT)) {
1080 StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001081 CreateSingleRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001082 } else {
1083 slangAssert(false);
1084 }
1085 }
1086
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001087 slangAssert(StmtCount < FieldsToSet);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001088
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 =
Loganbe274822011-02-16 22:02:54 +08001093 new(C) clang::BinaryOperator(LHS, RHS, clang::BO_Assign, QT,
Stephen Hines23c43582013-01-09 20:02:04 -08001094 clang::VK_RValue, clang::OK_Ordinary, Loc,
1095 false);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001096 StmtArray[StmtCount++] = CopyStruct;
1097
Stephen Hines23c43582013-01-09 20:02:04 -08001098 clang::CompoundStmt *CS = new(C) clang::CompoundStmt(
1099 C, llvm::makeArrayRef(StmtArray, StmtCount), Loc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001100
1101 delete [] StmtArray;
1102
1103 return CS;
1104}
1105
1106} // namespace
1107
1108void RSObjectRefCount::Scope::ReplaceRSObjectAssignment(
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001109 clang::BinaryOperator *AS) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001110
1111 clang::QualType QT = AS->getType();
1112
1113 clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD(
Stephen Hines9ae18b22014-06-10 23:53:00 -07001114 DataTypeRSAllocation)->getASTContext();
Stephen Hines2bb67db2011-02-11 01:36:40 -08001115
Stephen Hines832429f2011-02-25 16:05:37 -08001116 clang::SourceLocation Loc = AS->getExprLoc();
Stephen Hines9f1d0aa2011-12-18 15:41:00 -08001117 clang::SourceLocation StartLoc = AS->getLHS()->getExprLoc();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001118 clang::Stmt *UpdatedStmt = nullptr;
Stephen Hines2bb67db2011-02-11 01:36:40 -08001119
1120 if (!RSExportPrimitiveType::IsRSObjectType(QT.getTypePtr())) {
1121 // By definition, this is a struct assignment if we get here
1122 UpdatedStmt =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001123 CreateStructRSSetObject(C, AS->getLHS(), AS->getRHS(), StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001124 } else {
1125 UpdatedStmt =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001126 CreateSingleRSSetObject(C, AS->getLHS(), AS->getRHS(), StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001127 }
1128
Stephen Hines292e00a2011-03-18 19:11:30 -07001129 RSASTReplace R(C);
1130 R.ReplaceStmt(mCS, AS, UpdatedStmt);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001131}
1132
1133void RSObjectRefCount::Scope::AppendRSObjectInit(
1134 clang::VarDecl *VD,
1135 clang::DeclStmt *DS,
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001136 DataType DT,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001137 clang::Expr *InitExpr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001138 slangAssert(VD);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001139
1140 if (!InitExpr) {
1141 return;
1142 }
1143
Stephen Hinesa0611e62011-02-11 16:35:47 -08001144 clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD(
Stephen Hines9ae18b22014-06-10 23:53:00 -07001145 DataTypeRSAllocation)->getASTContext();
Stephen Hinesa0611e62011-02-11 16:35:47 -08001146 clang::SourceLocation Loc = RSObjectRefCount::GetRSSetObjectFD(
Stephen Hines9ae18b22014-06-10 23:53:00 -07001147 DataTypeRSAllocation)->getLocation();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001148 clang::SourceLocation StartLoc = RSObjectRefCount::GetRSSetObjectFD(
Stephen Hines9ae18b22014-06-10 23:53:00 -07001149 DataTypeRSAllocation)->getInnerLocStart();
Stephen Hinesa0611e62011-02-11 16:35:47 -08001150
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001151 if (DT == DataTypeIsStruct) {
Stephen Hinesa0611e62011-02-11 16:35:47 -08001152 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
1153 clang::DeclRefExpr *RefRSVar =
1154 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001155 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001156 clang::SourceLocation(),
Stephen Hinesa0611e62011-02-11 16:35:47 -08001157 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001158 false,
Stephen Hinesa0611e62011-02-11 16:35:47 -08001159 Loc,
Loganbe274822011-02-16 22:02:54 +08001160 T->getCanonicalTypeInternal(),
1161 clang::VK_RValue,
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001162 nullptr);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001163
1164 clang::Stmt *RSSetObjectOps =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001165 CreateStructRSSetObject(C, RefRSVar, InitExpr, StartLoc, Loc);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001166
Stephen Hines292e00a2011-03-18 19:11:30 -07001167 std::list<clang::Stmt*> StmtList;
1168 StmtList.push_back(RSSetObjectOps);
1169 AppendAfterStmt(C, mCS, DS, StmtList);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001170 return;
1171 }
1172
1173 clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(DT);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001174 slangAssert((SetObjectFD != nullptr) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -08001175 "rsSetObject doesn't cover all RS object types");
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001176
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 Hinese79fb5e2011-02-01 19:12:43 -08001182 clang::Expr *RefRSSetObjectFD =
1183 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001184 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001185 clang::SourceLocation(),
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001186 SetObjectFD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001187 false,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001188 Loc,
Loganbe274822011-02-16 22:02:54 +08001189 SetObjectFDType,
1190 clang::VK_RValue,
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001191 nullptr);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001192
1193 clang::Expr *RSSetObjectFP =
1194 clang::ImplicitCastExpr::Create(C,
1195 C.getPointerType(SetObjectFDType),
1196 clang::CK_FunctionToPointerDecay,
1197 RefRSSetObjectFD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001198 nullptr,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001199 clang::VK_RValue);
1200
1201 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
1202 clang::DeclRefExpr *RefRSVar =
1203 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001204 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001205 clang::SourceLocation(),
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001206 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001207 false,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001208 Loc,
Loganbe274822011-02-16 22:02:54 +08001209 T->getCanonicalTypeInternal(),
1210 clang::VK_RValue,
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001211 nullptr);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001212
Stephen Hines1dfc4152012-09-10 20:16:04 -07001213 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 Hinese79fb5e2011-02-01 19:12:43 -08001221
1222 clang::CallExpr *RSSetObjectCall =
1223 new(C) clang::CallExpr(C,
1224 RSSetObjectFP,
1225 ArgList,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001226 SetObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +08001227 clang::VK_RValue,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001228 Loc);
1229
Stephen Hines292e00a2011-03-18 19:11:30 -07001230 std::list<clang::Stmt*> StmtList;
1231 StmtList.push_back(RSSetObjectCall);
1232 AppendAfterStmt(C, mCS, DS, StmtList);
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001233}
1234
Stephen Hines1bdd4972010-11-08 17:35:08 -08001235void RSObjectRefCount::Scope::InsertLocalVarDestructors() {
Stephen Hines1bdd4972010-11-08 17:35:08 -08001236 for (std::list<clang::VarDecl*>::const_iterator I = mRSO.begin(),
1237 E = mRSO.end();
1238 I != E;
1239 I++) {
Stephen Hinesa883ce32011-08-11 18:52:48 -07001240 clang::VarDecl *VD = *I;
Stephen Hines3f175af2011-09-16 16:26:29 -07001241 clang::Stmt *RSClearObjectCall = ClearRSObject(VD, VD->getDeclContext());
Stephen Hinesa883ce32011-08-11 18:52:48 -07001242 if (RSClearObjectCall) {
Pirama Arumuga Nainar5d61db62015-06-09 11:34:18 -07001243 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 Hinesa883ce32011-08-11 18:52:48 -07001249 mCS,
1250 RSClearObjectCall,
1251 VD->getSourceRange().getBegin());
1252 DV.Visit(mCS);
1253 DV.InsertDestructors();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001254 }
1255 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001256}
1257
Stephen Hines3f175af2011-09-16 16:26:29 -07001258clang::Stmt *RSObjectRefCount::Scope::ClearRSObject(
1259 clang::VarDecl *VD,
1260 clang::DeclContext *DC) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001261 slangAssert(VD);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001262 clang::ASTContext &C = VD->getASTContext();
1263 clang::SourceLocation Loc = VD->getLocation();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001264 clang::SourceLocation StartLoc = VD->getInnerLocStart();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001265 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
Stephen Hines03981a32010-12-14 19:45:49 -08001266
Stephen Hines1bdd4972010-11-08 17:35:08 -08001267 // Reference expr to target RS object variable
1268 clang::DeclRefExpr *RefRSVar =
1269 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001270 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001271 clang::SourceLocation(),
Stephen Hines1bdd4972010-11-08 17:35:08 -08001272 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001273 false,
Stephen Hines1bdd4972010-11-08 17:35:08 -08001274 Loc,
Loganbe274822011-02-16 22:02:54 +08001275 T->getCanonicalTypeInternal(),
1276 clang::VK_RValue,
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001277 nullptr);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001278
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001279 if (T->isArrayType()) {
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001280 return ClearArrayRSObject(C, DC, RefRSVar, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001281 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001282
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001283 DataType DT = RSExportPrimitiveType::GetRSSpecificType(T);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001284
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001285 if (DT == DataTypeUnknown ||
1286 DT == DataTypeIsStruct) {
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001287 return ClearStructRSObject(C, DC, RefRSVar, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001288 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001289
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001290 slangAssert((RSExportPrimitiveType::IsRSObjectType(DT)) &&
1291 "Should be RS object");
Stephen Hines1bdd4972010-11-08 17:35:08 -08001292
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001293 return ClearSingleRSObject(C, RefRSVar, Loc);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001294}
1295
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001296bool RSObjectRefCount::InitializeRSObject(clang::VarDecl *VD,
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001297 DataType *DT,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001298 clang::Expr **InitExpr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001299 slangAssert(VD && DT && InitExpr);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001300 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
Stephen Hines2d095042010-11-12 18:13:56 -08001301
1302 // Loop through array types to get to base type
1303 while (T && T->isArrayType()) {
1304 T = T->getArrayElementTypeNoTypeQual();
Stephen Hines2d095042010-11-12 18:13:56 -08001305 }
1306
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001307 bool DataTypeIsStructWithRSObject = false;
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001308 *DT = RSExportPrimitiveType::GetRSSpecificType(T);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001309
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001310 if (*DT == DataTypeUnknown) {
Stephen Hinesfeaca062011-02-04 14:08:13 -08001311 if (RSExportPrimitiveType::IsStructureTypeWithRSObject(T)) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001312 *DT = DataTypeIsStruct;
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001313 DataTypeIsStructWithRSObject = true;
Stephen Hinesfeaca062011-02-04 14:08:13 -08001314 } else {
1315 return false;
1316 }
Stephen Hines2d095042010-11-12 18:13:56 -08001317 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001318
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001319 bool DataTypeIsRSObject = false;
1320 if (DataTypeIsStructWithRSObject) {
1321 DataTypeIsRSObject = true;
1322 } else {
1323 DataTypeIsRSObject = RSExportPrimitiveType::IsRSObjectType(*DT);
1324 }
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001325 *InitExpr = VD->getInit();
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001326
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001327 if (!DataTypeIsRSObject && *InitExpr) {
1328 // If we already have an initializer for a matrix type, we are done.
1329 return DataTypeIsRSObject;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001330 }
1331
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001332 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 Hines4b32ffd2010-11-05 18:47:11 -07001343}
1344
1345clang::Expr *RSObjectRefCount::CreateZeroInitializerForRSSpecificType(
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001346 DataType DT,
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001347 clang::ASTContext &C,
1348 const clang::SourceLocation &Loc) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001349 clang::Expr *Res = nullptr;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001350 switch (DT) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001351 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 Hines4b32ffd2010-11-05 18:47:11 -07001364 // (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 Wailes5abbe0e2014-08-12 15:58:29 -07001373 nullptr,
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001374 clang::VK_RValue);
1375
Stephen Hines1dfc4152012-09-10 20:16:04 -07001376 llvm::SmallVector<clang::Expr*, 1>InitList;
1377 InitList.push_back(CastToNull);
1378
1379 Res = new(C) clang::InitListExpr(C, Loc, InitList, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001380 break;
1381 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001382 case DataTypeRSMatrix2x2:
1383 case DataTypeRSMatrix3x3:
1384 case DataTypeRSMatrix4x4: {
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001385 // 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 Brouilletcec9b652014-05-14 19:33:57 -07001403 if (DT == DataTypeRSMatrix2x2)
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001404 N = 2;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001405 else if (DT == DataTypeRSMatrix3x3)
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001406 N = 3;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001407 else if (DT == DataTypeRSMatrix4x4)
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001408 N = 4;
Stephen Hines1dfc4152012-09-10 20:16:04 -07001409 unsigned N_2 = N * N;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001410
Stephen Hines1dfc4152012-09-10 20:16:04 -07001411 // 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 Hines4b32ffd2010-11-05 18:47:11 -07001415 clang::Expr *InitExpr =
Stephen Hines1dfc4152012-09-10 20:16:04 -07001416 new(C) clang::InitListExpr(C, Loc, InitVals, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001417 InitExpr->setType(C.getConstantArrayType(FloatTy,
Stephen Hines1dfc4152012-09-10 20:16:04 -07001418 llvm::APInt(32, N_2),
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001419 clang::ArrayType::Normal,
1420 /* EltTypeQuals = */0));
Stephen Hines1dfc4152012-09-10 20:16:04 -07001421 llvm::SmallVector<clang::Expr*, 1> InitExprVec;
1422 InitExprVec.push_back(InitExpr);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001423
Stephen Hines1dfc4152012-09-10 20:16:04 -07001424 Res = new(C) clang::InitListExpr(C, Loc, InitExprVec, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001425 break;
1426 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001427 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 Hines6e6578a2011-02-07 18:05:48 -08001444 slangAssert(false && "Not RS object type!");
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001445 }
1446 // No default case will enable compiler detecting the missing cases
1447 }
1448
1449 return Res;
1450}
1451
1452void 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 Brouilletcec9b652014-05-14 19:33:57 -07001459 DataType DT = DataTypeUnknown;
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001460 clang::Expr *InitExpr = nullptr;
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001461 if (InitializeRSObject(VD, &DT, &InitExpr)) {
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001462 // We need to zero-init all RS object types (including matrices), ...
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001463 getCurrentScope()->AppendRSObjectInit(VD, DS, DT, InitExpr);
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001464 // ... 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 Hinese79fb5e2011-02-01 19:12:43 -08001470 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001471 }
1472 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001473}
1474
1475void 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 Hines6e6578a2011-02-07 18:05:48 -08001484 slangAssert((getCurrentScope() == S) && "Corrupted scope stack!");
Stephen Hines1bdd4972010-11-08 17:35:08 -08001485 S->InsertLocalVarDestructors();
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001486 mScopeStack.pop();
1487 delete S;
1488 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001489}
1490
1491void RSObjectRefCount::VisitBinAssign(clang::BinaryOperator *AS) {
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001492 clang::QualType QT = AS->getType();
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001493
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001494 if (CountRSObjectTypes(mCtx, QT.getTypePtr(), AS->getExprLoc())) {
1495 getCurrentScope()->ReplaceRSObjectAssignment(AS);
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001496 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001497}
1498
1499void 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 Hines4b32ffd2010-11-05 18:47:11 -07001507}
1508
Stephen Hines688e64b2011-08-23 16:01:25 -07001509// 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.
1512clang::FunctionDecl *RSObjectRefCount::CreateStaticGlobalDtor() {
1513 Init();
1514
1515 clang::DeclContext *DC = mCtx.getTranslationUnitDecl();
1516 clang::SourceLocation loc;
1517
Stephen Hines3f175af2011-09-16 16:26:29 -07001518 llvm::StringRef SR(".rs.dtor");
1519 clang::IdentifierInfo &II = mCtx.Idents.get(SR);
1520 clang::DeclarationName N(&II);
1521 clang::FunctionProtoType::ExtProtoInfo EPI;
Stephen Hines82d72882013-03-18 18:17:57 -07001522 clang::QualType T = mCtx.getFunctionType(mCtx.VoidTy,
1523 llvm::ArrayRef<clang::QualType>(), EPI);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001524 clang::FunctionDecl *FD = nullptr;
Stephen Hines3f175af2011-09-16 16:26:29 -07001525
Stephen Hines688e64b2011-08-23 16:01:25 -07001526 // 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 Hines3f175af2011-09-16 16:26:29 -07001534 if (!FD) {
1535 // Only create FD if we are going to use it.
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001536 FD = clang::FunctionDecl::Create(mCtx, DC, loc, loc, N, T, nullptr,
Stephen Hines4b3f3ba2013-05-06 16:18:56 -07001537 clang::SC_None);
Stephen Hines3f175af2011-09-16 16:26:29 -07001538 }
Pirama Arumuga Nainar5d61db62015-06-09 11:34:18 -07001539 // 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 Hines3f175af2011-09-16 16:26:29 -07001543 // 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 Hines688e64b2011-08-23 16:01:25 -07001546 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 Wailes5abbe0e2014-08-12 15:58:29 -07001553 return nullptr;
Stephen Hines688e64b2011-08-23 16:01:25 -07001554 }
1555
Stephen Hines688e64b2011-08-23 16:01:25 -07001556 clang::CompoundStmt *CS = BuildCompoundStmt(mCtx, StmtList, loc);
1557
1558 FD->setBody(CS);
1559
1560 return FD;
1561}
1562
Stephen Hinese639eb52010-11-08 19:27:20 -08001563} // namespace slang