blob: f0ac22d133522aa506bc382706a8ae052c435261 [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"
Stephen Hines4b32ffd2010-11-05 18:47:11 -070029#include "slang_rs.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++) {
Stephen Hines1bdd4972010-11-08 17:35:08 -080045 RSSetObjectFD[i] = NULL;
46 RSClearObjectFD[i] = NULL;
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 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
154 // When S is NULL, we are appending to the end of the CompoundStmt.
155 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() {
221 clang::Stmt *S = NULL;
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);
Stephen Hinesa883ce32011-08-11 18:52:48 -0700247 AppendAfterStmt(mCtx, CS, NULL, 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);
361 slangAssert((ClearObjectFD != NULL) &&
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,
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,
394 NULL);
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,
401 NULL,
402 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
448 clang::Stmt *StmtArray[2] = {NULL};
449 int StmtCtr = 0;
450
Stephen Hines03981a32010-12-14 19:45:49 -0800451 if (NumArrayElements <= 0) {
452 return NULL;
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)
466 // NULL << CondVar >>
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 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);
489 clang::Decl *IID = (clang::Decl *)IIVD;
490
491 clang::DeclGroupRef DGR = clang::DeclGroupRef::Create(C, &IID, 1);
492 StmtArray[StmtCtr++] = new(C) clang::DeclStmt(DGR, Loc, Loc);
493
494 // Form the actual destructor loop
495 // for (Init; Cond; Inc)
496 // RSClearObjectCall;
497
498 // Init -> "rsIntIter = 0"
499 clang::DeclRefExpr *RefrsIntIter =
500 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800501 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800502 clang::SourceLocation(),
Stephen Hines03981a32010-12-14 19:45:49 -0800503 IIVD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -0700504 false,
Stephen Hines03981a32010-12-14 19:45:49 -0800505 Loc,
Loganbe274822011-02-16 22:02:54 +0800506 C.IntTy,
507 clang::VK_RValue,
508 NULL);
Stephen Hines03981a32010-12-14 19:45:49 -0800509
510 clang::Expr *Int0 = clang::IntegerLiteral::Create(C,
511 llvm::APInt(C.getTypeSize(C.IntTy), 0), C.IntTy, Loc);
512
513 clang::BinaryOperator *Init =
514 new(C) clang::BinaryOperator(RefrsIntIter,
515 Int0,
516 clang::BO_Assign,
517 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800518 clang::VK_RValue,
519 clang::OK_Ordinary,
Stephen Hines23c43582013-01-09 20:02:04 -0800520 Loc,
521 false);
Stephen Hines03981a32010-12-14 19:45:49 -0800522
523 // Cond -> "rsIntIter < NumArrayElements"
524 clang::Expr *NumArrayElementsExpr = clang::IntegerLiteral::Create(C,
525 llvm::APInt(C.getTypeSize(C.IntTy), NumArrayElements), C.IntTy, Loc);
526
527 clang::BinaryOperator *Cond =
528 new(C) clang::BinaryOperator(RefrsIntIter,
529 NumArrayElementsExpr,
530 clang::BO_LT,
531 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800532 clang::VK_RValue,
533 clang::OK_Ordinary,
Stephen Hines23c43582013-01-09 20:02:04 -0800534 Loc,
535 false);
Stephen Hines03981a32010-12-14 19:45:49 -0800536
537 // Inc -> "rsIntIter++"
538 clang::UnaryOperator *Inc =
539 new(C) clang::UnaryOperator(RefrsIntIter,
540 clang::UO_PostInc,
541 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800542 clang::VK_RValue,
543 clang::OK_Ordinary,
Stephen Hines03981a32010-12-14 19:45:49 -0800544 Loc);
545
546 // Body -> "rsClearObject(&VD[rsIntIter]);"
547 // Destructor loop operates on individual array elements
Stephen Hines03981a32010-12-14 19:45:49 -0800548
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800549 clang::Expr *RefRSArrPtr =
Stephen Hines03981a32010-12-14 19:45:49 -0800550 clang::ImplicitCastExpr::Create(C,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800551 C.getPointerType(BaseType->getCanonicalTypeInternal()),
Stephen Hines03981a32010-12-14 19:45:49 -0800552 clang::CK_ArrayToPointerDecay,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800553 RefRSArr,
Stephen Hines03981a32010-12-14 19:45:49 -0800554 NULL,
555 clang::VK_RValue);
556
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800557 clang::Expr *RefRSArrPtrSubscript =
558 new(C) clang::ArraySubscriptExpr(RefRSArrPtr,
Stephen Hines03981a32010-12-14 19:45:49 -0800559 RefrsIntIter,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800560 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800561 clang::VK_RValue,
562 clang::OK_Ordinary,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800563 Loc);
Stephen Hines03981a32010-12-14 19:45:49 -0800564
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700565 DataType DT = RSExportPrimitiveType::GetRSSpecificType(BaseType);
Stephen Hines03981a32010-12-14 19:45:49 -0800566
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800567 clang::Stmt *RSClearObjectCall = NULL;
568 if (BaseType->isArrayType()) {
569 RSClearObjectCall =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700570 ClearArrayRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700571 } else if (DT == DataTypeUnknown) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800572 RSClearObjectCall =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700573 ClearStructRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800574 } else {
575 RSClearObjectCall = ClearSingleRSObject(C, RefRSArrPtrSubscript, Loc);
576 }
Stephen Hines03981a32010-12-14 19:45:49 -0800577
578 clang::ForStmt *DestructorLoop =
579 new(C) clang::ForStmt(C,
580 Init,
581 Cond,
582 NULL, // no condVar
583 Inc,
584 RSClearObjectCall,
585 Loc,
586 Loc,
587 Loc);
588
589 StmtArray[StmtCtr++] = DestructorLoop;
Stephen Hines6e6578a2011-02-07 18:05:48 -0800590 slangAssert(StmtCtr == 2);
Stephen Hines03981a32010-12-14 19:45:49 -0800591
Stephen Hines23c43582013-01-09 20:02:04 -0800592 clang::CompoundStmt *CS = new(C) clang::CompoundStmt(
593 C, llvm::makeArrayRef(StmtArray, StmtCtr), Loc, Loc);
Stephen Hines03981a32010-12-14 19:45:49 -0800594
595 return CS;
596}
597
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700598static unsigned CountRSObjectTypes(clang::ASTContext &C,
599 const clang::Type *T,
600 clang::SourceLocation Loc) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800601 slangAssert(T);
602 unsigned RSObjectCount = 0;
603
604 if (T->isArrayType()) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700605 return CountRSObjectTypes(C, T->getArrayElementTypeNoTypeQual(), Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800606 }
607
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700608 DataType DT = RSExportPrimitiveType::GetRSSpecificType(T);
609 if (DT != DataTypeUnknown) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800610 return (RSExportPrimitiveType::IsRSObjectType(DT) ? 1 : 0);
611 }
612
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700613 if (T->isUnionType()) {
614 clang::RecordDecl *RD = T->getAsUnionType()->getDecl();
615 RD = RD->getDefinition();
616 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
617 FE = RD->field_end();
618 FI != FE;
619 FI++) {
620 const clang::FieldDecl *FD = *FI;
621 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
622 if (CountRSObjectTypes(C, FT, Loc)) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700623 slangAssert(false && "can't have unions with RS object types!");
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700624 return 0;
625 }
626 }
627 }
628
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800629 if (!T->isStructureType()) {
630 return 0;
631 }
632
633 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
634 RD = RD->getDefinition();
635 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
636 FE = RD->field_end();
637 FI != FE;
638 FI++) {
639 const clang::FieldDecl *FD = *FI;
640 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700641 if (CountRSObjectTypes(C, FT, Loc)) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800642 // Sub-structs should only count once (as should arrays, etc.)
643 RSObjectCount++;
644 }
645 }
646
647 return RSObjectCount;
648}
649
650static clang::Stmt *ClearStructRSObject(
651 clang::ASTContext &C,
652 clang::DeclContext *DC,
653 clang::Expr *RefRSStruct,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700654 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800655 clang::SourceLocation Loc) {
656 const clang::Type *BaseType = RefRSStruct->getType().getTypePtr();
657
658 slangAssert(!BaseType->isArrayType());
659
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800660 // Structs should show up as unknown primitive types
Alex Sakhartchouk9be93602011-03-17 17:03:36 -0700661 slangAssert(RSExportPrimitiveType::GetRSSpecificType(BaseType) ==
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700662 DataTypeUnknown);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800663
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700664 unsigned FieldsToDestroy = CountRSObjectTypes(C, BaseType, Loc);
Stephen Hinesb0fabe52013-01-07 19:06:09 -0800665 slangAssert(FieldsToDestroy != 0);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800666
667 unsigned StmtCount = 0;
668 clang::Stmt **StmtArray = new clang::Stmt*[FieldsToDestroy];
Stephen Hines2bb67db2011-02-11 01:36:40 -0800669 for (unsigned i = 0; i < FieldsToDestroy; i++) {
670 StmtArray[i] = NULL;
671 }
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800672
673 // Populate StmtArray by creating a destructor for each RS object field
674 clang::RecordDecl *RD = BaseType->getAsStructureType()->getDecl();
675 RD = RD->getDefinition();
676 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
677 FE = RD->field_end();
678 FI != FE;
679 FI++) {
680 // We just look through all field declarations to see if we find a
681 // declaration for an RS object type (or an array of one).
682 bool IsArrayType = false;
683 clang::FieldDecl *FD = *FI;
684 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
685 const clang::Type *OrigType = FT;
686 while (FT && FT->isArrayType()) {
687 FT = FT->getArrayElementTypeNoTypeQual();
688 IsArrayType = true;
689 }
690
691 if (RSExportPrimitiveType::IsRSObjectType(FT)) {
692 clang::DeclAccessPair FoundDecl =
693 clang::DeclAccessPair::make(FD, clang::AS_none);
694 clang::MemberExpr *RSObjectMember =
695 clang::MemberExpr::Create(C,
696 RefRSStruct,
697 false,
Loganbe274822011-02-16 22:02:54 +0800698 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800699 clang::SourceLocation(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800700 FD,
701 FoundDecl,
702 clang::DeclarationNameInfo(),
703 NULL,
Loganbe274822011-02-16 22:02:54 +0800704 OrigType->getCanonicalTypeInternal(),
705 clang::VK_RValue,
706 clang::OK_Ordinary);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800707
708 slangAssert(StmtCount < FieldsToDestroy);
709
710 if (IsArrayType) {
711 StmtArray[StmtCount++] = ClearArrayRSObject(C,
712 DC,
713 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700714 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800715 Loc);
716 } else {
717 StmtArray[StmtCount++] = ClearSingleRSObject(C,
718 RSObjectMember,
719 Loc);
720 }
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700721 } else if (FT->isStructureType() && CountRSObjectTypes(C, FT, Loc)) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800722 // In this case, we have a nested struct. We may not end up filling all
723 // of the spaces in StmtArray (sub-structs should handle themselves
724 // with separate compound statements).
725 clang::DeclAccessPair FoundDecl =
726 clang::DeclAccessPair::make(FD, clang::AS_none);
727 clang::MemberExpr *RSObjectMember =
728 clang::MemberExpr::Create(C,
729 RefRSStruct,
730 false,
Loganbe274822011-02-16 22:02:54 +0800731 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800732 clang::SourceLocation(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800733 FD,
734 FoundDecl,
735 clang::DeclarationNameInfo(),
736 NULL,
Loganbe274822011-02-16 22:02:54 +0800737 OrigType->getCanonicalTypeInternal(),
738 clang::VK_RValue,
739 clang::OK_Ordinary);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800740
741 if (IsArrayType) {
742 StmtArray[StmtCount++] = ClearArrayRSObject(C,
743 DC,
744 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700745 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800746 Loc);
747 } else {
748 StmtArray[StmtCount++] = ClearStructRSObject(C,
749 DC,
750 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700751 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800752 Loc);
753 }
754 }
755 }
756
757 slangAssert(StmtCount > 0);
Stephen Hines23c43582013-01-09 20:02:04 -0800758 clang::CompoundStmt *CS = new(C) clang::CompoundStmt(
759 C, llvm::makeArrayRef(StmtArray, StmtCount), Loc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800760
761 delete [] StmtArray;
762
763 return CS;
764}
765
Stephen Hines2bb67db2011-02-11 01:36:40 -0800766static clang::Stmt *CreateSingleRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800767 clang::Expr *DstExpr,
768 clang::Expr *SrcExpr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700769 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800770 clang::SourceLocation Loc) {
771 const clang::Type *T = DstExpr->getType().getTypePtr();
772 clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(T);
Stephen Hines6e6578a2011-02-07 18:05:48 -0800773 slangAssert((SetObjectFD != NULL) &&
774 "rsSetObject doesn't cover all RS object types");
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800775
776 clang::QualType SetObjectFDType = SetObjectFD->getType();
777 clang::QualType SetObjectFDArgType[2];
778 SetObjectFDArgType[0] = SetObjectFD->getParamDecl(0)->getOriginalType();
779 SetObjectFDArgType[1] = SetObjectFD->getParamDecl(1)->getOriginalType();
780
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800781 clang::Expr *RefRSSetObjectFD =
782 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800783 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800784 clang::SourceLocation(),
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800785 SetObjectFD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -0700786 false,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800787 Loc,
Loganbe274822011-02-16 22:02:54 +0800788 SetObjectFDType,
789 clang::VK_RValue,
790 NULL);
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800791
792 clang::Expr *RSSetObjectFP =
793 clang::ImplicitCastExpr::Create(C,
794 C.getPointerType(SetObjectFDType),
795 clang::CK_FunctionToPointerDecay,
796 RefRSSetObjectFD,
797 NULL,
798 clang::VK_RValue);
799
Stephen Hines1dfc4152012-09-10 20:16:04 -0700800 llvm::SmallVector<clang::Expr*, 2> ArgList;
801 ArgList.push_back(new(C) clang::UnaryOperator(DstExpr,
802 clang::UO_AddrOf,
803 SetObjectFDArgType[0],
804 clang::VK_RValue,
805 clang::OK_Ordinary,
806 Loc));
807 ArgList.push_back(SrcExpr);
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800808
809 clang::CallExpr *RSSetObjectCall =
810 new(C) clang::CallExpr(C,
811 RSSetObjectFP,
812 ArgList,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800813 SetObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +0800814 clang::VK_RValue,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800815 Loc);
816
Stephen Hines2bb67db2011-02-11 01:36:40 -0800817 return RSSetObjectCall;
818}
Stephen Hinese79fb5e2011-02-01 19:12:43 -0800819
Stephen Hines2bb67db2011-02-11 01:36:40 -0800820static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800821 clang::Expr *LHS,
822 clang::Expr *RHS,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700823 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800824 clang::SourceLocation Loc);
825
Al Sutton246fa172012-02-23 13:09:08 +0000826/*static clang::Stmt *CreateArrayRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800827 clang::Expr *DstArr,
828 clang::Expr *SrcArr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700829 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800830 clang::SourceLocation Loc) {
831 clang::DeclContext *DC = NULL;
Stephen Hines2bb67db2011-02-11 01:36:40 -0800832 const clang::Type *BaseType = DstArr->getType().getTypePtr();
833 slangAssert(BaseType->isArrayType());
834
835 int NumArrayElements = ArrayDim(BaseType);
836 // Actually extract out the base RS object type for use later
837 BaseType = BaseType->getArrayElementTypeNoTypeQual();
838
839 clang::Stmt *StmtArray[2] = {NULL};
840 int StmtCtr = 0;
841
842 if (NumArrayElements <= 0) {
843 return NULL;
844 }
845
846 // Create helper variable for iterating through elements
847 clang::IdentifierInfo& II = C.Idents.get("rsIntIter");
848 clang::VarDecl *IIVD =
849 clang::VarDecl::Create(C,
850 DC,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700851 StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800852 Loc,
853 &II,
854 C.IntTy,
855 C.getTrivialTypeSourceInfo(C.IntTy),
856 clang::SC_None,
857 clang::SC_None);
858 clang::Decl *IID = (clang::Decl *)IIVD;
859
860 clang::DeclGroupRef DGR = clang::DeclGroupRef::Create(C, &IID, 1);
861 StmtArray[StmtCtr++] = new(C) clang::DeclStmt(DGR, Loc, Loc);
862
863 // Form the actual loop
864 // for (Init; Cond; Inc)
865 // RSSetObjectCall;
866
867 // Init -> "rsIntIter = 0"
868 clang::DeclRefExpr *RefrsIntIter =
869 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800870 clang::NestedNameSpecifierLoc(),
Stephen Hines2bb67db2011-02-11 01:36:40 -0800871 IIVD,
872 Loc,
Loganbe274822011-02-16 22:02:54 +0800873 C.IntTy,
874 clang::VK_RValue,
875 NULL);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800876
877 clang::Expr *Int0 = clang::IntegerLiteral::Create(C,
878 llvm::APInt(C.getTypeSize(C.IntTy), 0), C.IntTy, Loc);
879
880 clang::BinaryOperator *Init =
881 new(C) clang::BinaryOperator(RefrsIntIter,
882 Int0,
883 clang::BO_Assign,
884 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800885 clang::VK_RValue,
886 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800887 Loc);
888
889 // Cond -> "rsIntIter < NumArrayElements"
890 clang::Expr *NumArrayElementsExpr = clang::IntegerLiteral::Create(C,
891 llvm::APInt(C.getTypeSize(C.IntTy), NumArrayElements), C.IntTy, Loc);
892
893 clang::BinaryOperator *Cond =
894 new(C) clang::BinaryOperator(RefrsIntIter,
895 NumArrayElementsExpr,
896 clang::BO_LT,
897 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800898 clang::VK_RValue,
899 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800900 Loc);
901
902 // Inc -> "rsIntIter++"
903 clang::UnaryOperator *Inc =
904 new(C) clang::UnaryOperator(RefrsIntIter,
905 clang::UO_PostInc,
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 // Body -> "rsSetObject(&Dst[rsIntIter], Src[rsIntIter]);"
912 // Loop operates on individual array elements
913
914 clang::Expr *DstArrPtr =
915 clang::ImplicitCastExpr::Create(C,
916 C.getPointerType(BaseType->getCanonicalTypeInternal()),
917 clang::CK_ArrayToPointerDecay,
918 DstArr,
919 NULL,
920 clang::VK_RValue);
921
922 clang::Expr *DstArrPtrSubscript =
923 new(C) clang::ArraySubscriptExpr(DstArrPtr,
924 RefrsIntIter,
925 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800926 clang::VK_RValue,
927 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800928 Loc);
929
930 clang::Expr *SrcArrPtr =
931 clang::ImplicitCastExpr::Create(C,
932 C.getPointerType(BaseType->getCanonicalTypeInternal()),
933 clang::CK_ArrayToPointerDecay,
934 SrcArr,
935 NULL,
936 clang::VK_RValue);
937
938 clang::Expr *SrcArrPtrSubscript =
939 new(C) clang::ArraySubscriptExpr(SrcArrPtr,
940 RefrsIntIter,
941 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800942 clang::VK_RValue,
943 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800944 Loc);
945
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700946 DataType DT = RSExportPrimitiveType::GetRSSpecificType(BaseType);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800947
948 clang::Stmt *RSSetObjectCall = NULL;
949 if (BaseType->isArrayType()) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700950 RSSetObjectCall = CreateArrayRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700951 SrcArrPtrSubscript,
952 StartLoc, Loc);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700953 } else if (DT == DataTypeUnknown) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700954 RSSetObjectCall = CreateStructRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700955 SrcArrPtrSubscript,
956 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800957 } else {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700958 RSSetObjectCall = CreateSingleRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700959 SrcArrPtrSubscript,
960 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800961 }
962
963 clang::ForStmt *DestructorLoop =
964 new(C) clang::ForStmt(C,
965 Init,
966 Cond,
967 NULL, // no condVar
968 Inc,
969 RSSetObjectCall,
970 Loc,
971 Loc,
972 Loc);
973
974 StmtArray[StmtCtr++] = DestructorLoop;
975 slangAssert(StmtCtr == 2);
976
977 clang::CompoundStmt *CS =
978 new(C) clang::CompoundStmt(C, StmtArray, StmtCtr, Loc, Loc);
979
980 return CS;
Al Sutton246fa172012-02-23 13:09:08 +0000981} */
Stephen Hines2bb67db2011-02-11 01:36:40 -0800982
983static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800984 clang::Expr *LHS,
985 clang::Expr *RHS,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700986 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800987 clang::SourceLocation Loc) {
Stephen Hines2bb67db2011-02-11 01:36:40 -0800988 clang::QualType QT = LHS->getType();
989 const clang::Type *T = QT.getTypePtr();
990 slangAssert(T->isStructureType());
991 slangAssert(!RSExportPrimitiveType::IsRSObjectType(T));
992
993 // Keep an extra slot for the original copy (memcpy)
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700994 unsigned FieldsToSet = CountRSObjectTypes(C, T, Loc) + 1;
Stephen Hines2bb67db2011-02-11 01:36:40 -0800995
996 unsigned StmtCount = 0;
997 clang::Stmt **StmtArray = new clang::Stmt*[FieldsToSet];
998 for (unsigned i = 0; i < FieldsToSet; i++) {
999 StmtArray[i] = NULL;
1000 }
1001
1002 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
1003 RD = RD->getDefinition();
1004 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1005 FE = RD->field_end();
1006 FI != FE;
1007 FI++) {
1008 bool IsArrayType = false;
1009 clang::FieldDecl *FD = *FI;
1010 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
1011 const clang::Type *OrigType = FT;
1012
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001013 if (!CountRSObjectTypes(C, FT, Loc)) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001014 // Skip to next if we don't have any viable RS object types
1015 continue;
1016 }
1017
1018 clang::DeclAccessPair FoundDecl =
1019 clang::DeclAccessPair::make(FD, clang::AS_none);
1020 clang::MemberExpr *DstMember =
1021 clang::MemberExpr::Create(C,
1022 LHS,
1023 false,
Loganbe274822011-02-16 22:02:54 +08001024 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001025 clang::SourceLocation(),
Stephen Hines2bb67db2011-02-11 01:36:40 -08001026 FD,
1027 FoundDecl,
1028 clang::DeclarationNameInfo(),
1029 NULL,
Loganbe274822011-02-16 22:02:54 +08001030 OrigType->getCanonicalTypeInternal(),
1031 clang::VK_RValue,
1032 clang::OK_Ordinary);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001033
1034 clang::MemberExpr *SrcMember =
1035 clang::MemberExpr::Create(C,
1036 RHS,
1037 false,
Loganbe274822011-02-16 22:02:54 +08001038 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001039 clang::SourceLocation(),
Stephen Hines2bb67db2011-02-11 01:36:40 -08001040 FD,
1041 FoundDecl,
1042 clang::DeclarationNameInfo(),
1043 NULL,
Loganbe274822011-02-16 22:02:54 +08001044 OrigType->getCanonicalTypeInternal(),
1045 clang::VK_RValue,
1046 clang::OK_Ordinary);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001047
1048 if (FT->isArrayType()) {
1049 FT = FT->getArrayElementTypeNoTypeQual();
1050 IsArrayType = true;
1051 }
1052
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001053 DataType DT = RSExportPrimitiveType::GetRSSpecificType(FT);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001054
1055 if (IsArrayType) {
Logan Chien9207a2e2011-10-21 15:39:28 +08001056 clang::DiagnosticsEngine &DiagEngine = C.getDiagnostics();
1057 DiagEngine.Report(
1058 clang::FullSourceLoc(Loc, C.getSourceManager()),
1059 DiagEngine.getCustomDiagID(
1060 clang::DiagnosticsEngine::Error,
1061 "Arrays of RS object types within structures cannot be copied"));
Stephen Hines2bb67db2011-02-11 01:36:40 -08001062 // TODO(srhines): Support setting arrays of RS objects
1063 // StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001064 // CreateArrayRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001065 } else if (DT == DataTypeUnknown) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001066 StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001067 CreateStructRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001068 } else if (RSExportPrimitiveType::IsRSObjectType(DT)) {
1069 StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001070 CreateSingleRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001071 } else {
1072 slangAssert(false);
1073 }
1074 }
1075
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001076 slangAssert(StmtCount < FieldsToSet);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001077
1078 // We still need to actually do the overall struct copy. For simplicity,
1079 // we just do a straight-up assignment (which will still preserve all
1080 // the proper RS object reference counts).
1081 clang::BinaryOperator *CopyStruct =
Loganbe274822011-02-16 22:02:54 +08001082 new(C) clang::BinaryOperator(LHS, RHS, clang::BO_Assign, QT,
Stephen Hines23c43582013-01-09 20:02:04 -08001083 clang::VK_RValue, clang::OK_Ordinary, Loc,
1084 false);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001085 StmtArray[StmtCount++] = CopyStruct;
1086
Stephen Hines23c43582013-01-09 20:02:04 -08001087 clang::CompoundStmt *CS = new(C) clang::CompoundStmt(
1088 C, llvm::makeArrayRef(StmtArray, StmtCount), Loc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001089
1090 delete [] StmtArray;
1091
1092 return CS;
1093}
1094
1095} // namespace
1096
1097void RSObjectRefCount::Scope::ReplaceRSObjectAssignment(
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001098 clang::BinaryOperator *AS) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001099
1100 clang::QualType QT = AS->getType();
1101
1102 clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD(
Stephen Hines9ae18b22014-06-10 23:53:00 -07001103 DataTypeRSAllocation)->getASTContext();
Stephen Hines2bb67db2011-02-11 01:36:40 -08001104
Stephen Hines832429f2011-02-25 16:05:37 -08001105 clang::SourceLocation Loc = AS->getExprLoc();
Stephen Hines9f1d0aa2011-12-18 15:41:00 -08001106 clang::SourceLocation StartLoc = AS->getLHS()->getExprLoc();
Stephen Hines2bb67db2011-02-11 01:36:40 -08001107 clang::Stmt *UpdatedStmt = NULL;
1108
1109 if (!RSExportPrimitiveType::IsRSObjectType(QT.getTypePtr())) {
1110 // By definition, this is a struct assignment if we get here
1111 UpdatedStmt =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001112 CreateStructRSSetObject(C, AS->getLHS(), AS->getRHS(), StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001113 } else {
1114 UpdatedStmt =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001115 CreateSingleRSSetObject(C, AS->getLHS(), AS->getRHS(), StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001116 }
1117
Stephen Hines292e00a2011-03-18 19:11:30 -07001118 RSASTReplace R(C);
1119 R.ReplaceStmt(mCS, AS, UpdatedStmt);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001120}
1121
1122void RSObjectRefCount::Scope::AppendRSObjectInit(
1123 clang::VarDecl *VD,
1124 clang::DeclStmt *DS,
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001125 DataType DT,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001126 clang::Expr *InitExpr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001127 slangAssert(VD);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001128
1129 if (!InitExpr) {
1130 return;
1131 }
1132
Stephen Hinesa0611e62011-02-11 16:35:47 -08001133 clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD(
Stephen Hines9ae18b22014-06-10 23:53:00 -07001134 DataTypeRSAllocation)->getASTContext();
Stephen Hinesa0611e62011-02-11 16:35:47 -08001135 clang::SourceLocation Loc = RSObjectRefCount::GetRSSetObjectFD(
Stephen Hines9ae18b22014-06-10 23:53:00 -07001136 DataTypeRSAllocation)->getLocation();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001137 clang::SourceLocation StartLoc = RSObjectRefCount::GetRSSetObjectFD(
Stephen Hines9ae18b22014-06-10 23:53:00 -07001138 DataTypeRSAllocation)->getInnerLocStart();
Stephen Hinesa0611e62011-02-11 16:35:47 -08001139
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001140 if (DT == DataTypeIsStruct) {
Stephen Hinesa0611e62011-02-11 16:35:47 -08001141 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
1142 clang::DeclRefExpr *RefRSVar =
1143 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001144 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001145 clang::SourceLocation(),
Stephen Hinesa0611e62011-02-11 16:35:47 -08001146 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001147 false,
Stephen Hinesa0611e62011-02-11 16:35:47 -08001148 Loc,
Loganbe274822011-02-16 22:02:54 +08001149 T->getCanonicalTypeInternal(),
1150 clang::VK_RValue,
1151 NULL);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001152
1153 clang::Stmt *RSSetObjectOps =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001154 CreateStructRSSetObject(C, RefRSVar, InitExpr, StartLoc, Loc);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001155
Stephen Hines292e00a2011-03-18 19:11:30 -07001156 std::list<clang::Stmt*> StmtList;
1157 StmtList.push_back(RSSetObjectOps);
1158 AppendAfterStmt(C, mCS, DS, StmtList);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001159 return;
1160 }
1161
1162 clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(DT);
Stephen Hines6e6578a2011-02-07 18:05:48 -08001163 slangAssert((SetObjectFD != NULL) &&
1164 "rsSetObject doesn't cover all RS object types");
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001165
1166 clang::QualType SetObjectFDType = SetObjectFD->getType();
1167 clang::QualType SetObjectFDArgType[2];
1168 SetObjectFDArgType[0] = SetObjectFD->getParamDecl(0)->getOriginalType();
1169 SetObjectFDArgType[1] = SetObjectFD->getParamDecl(1)->getOriginalType();
1170
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001171 clang::Expr *RefRSSetObjectFD =
1172 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001173 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001174 clang::SourceLocation(),
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001175 SetObjectFD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001176 false,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001177 Loc,
Loganbe274822011-02-16 22:02:54 +08001178 SetObjectFDType,
1179 clang::VK_RValue,
1180 NULL);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001181
1182 clang::Expr *RSSetObjectFP =
1183 clang::ImplicitCastExpr::Create(C,
1184 C.getPointerType(SetObjectFDType),
1185 clang::CK_FunctionToPointerDecay,
1186 RefRSSetObjectFD,
1187 NULL,
1188 clang::VK_RValue);
1189
1190 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
1191 clang::DeclRefExpr *RefRSVar =
1192 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001193 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001194 clang::SourceLocation(),
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001195 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001196 false,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001197 Loc,
Loganbe274822011-02-16 22:02:54 +08001198 T->getCanonicalTypeInternal(),
1199 clang::VK_RValue,
1200 NULL);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001201
Stephen Hines1dfc4152012-09-10 20:16:04 -07001202 llvm::SmallVector<clang::Expr*, 2> ArgList;
1203 ArgList.push_back(new(C) clang::UnaryOperator(RefRSVar,
1204 clang::UO_AddrOf,
1205 SetObjectFDArgType[0],
1206 clang::VK_RValue,
1207 clang::OK_Ordinary,
1208 Loc));
1209 ArgList.push_back(InitExpr);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001210
1211 clang::CallExpr *RSSetObjectCall =
1212 new(C) clang::CallExpr(C,
1213 RSSetObjectFP,
1214 ArgList,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001215 SetObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +08001216 clang::VK_RValue,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001217 Loc);
1218
Stephen Hines292e00a2011-03-18 19:11:30 -07001219 std::list<clang::Stmt*> StmtList;
1220 StmtList.push_back(RSSetObjectCall);
1221 AppendAfterStmt(C, mCS, DS, StmtList);
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001222}
1223
Stephen Hines1bdd4972010-11-08 17:35:08 -08001224void RSObjectRefCount::Scope::InsertLocalVarDestructors() {
Stephen Hines1bdd4972010-11-08 17:35:08 -08001225 for (std::list<clang::VarDecl*>::const_iterator I = mRSO.begin(),
1226 E = mRSO.end();
1227 I != E;
1228 I++) {
Stephen Hinesa883ce32011-08-11 18:52:48 -07001229 clang::VarDecl *VD = *I;
Stephen Hines3f175af2011-09-16 16:26:29 -07001230 clang::Stmt *RSClearObjectCall = ClearRSObject(VD, VD->getDeclContext());
Stephen Hinesa883ce32011-08-11 18:52:48 -07001231 if (RSClearObjectCall) {
1232 DestructorVisitor DV((*mRSO.begin())->getASTContext(),
1233 mCS,
1234 RSClearObjectCall,
1235 VD->getSourceRange().getBegin());
1236 DV.Visit(mCS);
1237 DV.InsertDestructors();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001238 }
1239 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001240}
1241
Stephen Hines3f175af2011-09-16 16:26:29 -07001242clang::Stmt *RSObjectRefCount::Scope::ClearRSObject(
1243 clang::VarDecl *VD,
1244 clang::DeclContext *DC) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001245 slangAssert(VD);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001246 clang::ASTContext &C = VD->getASTContext();
1247 clang::SourceLocation Loc = VD->getLocation();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001248 clang::SourceLocation StartLoc = VD->getInnerLocStart();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001249 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
Stephen Hines03981a32010-12-14 19:45:49 -08001250
Stephen Hines1bdd4972010-11-08 17:35:08 -08001251 // Reference expr to target RS object variable
1252 clang::DeclRefExpr *RefRSVar =
1253 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001254 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001255 clang::SourceLocation(),
Stephen Hines1bdd4972010-11-08 17:35:08 -08001256 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001257 false,
Stephen Hines1bdd4972010-11-08 17:35:08 -08001258 Loc,
Loganbe274822011-02-16 22:02:54 +08001259 T->getCanonicalTypeInternal(),
1260 clang::VK_RValue,
1261 NULL);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001262
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001263 if (T->isArrayType()) {
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001264 return ClearArrayRSObject(C, DC, RefRSVar, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001265 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001266
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001267 DataType DT = RSExportPrimitiveType::GetRSSpecificType(T);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001268
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001269 if (DT == DataTypeUnknown ||
1270 DT == DataTypeIsStruct) {
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001271 return ClearStructRSObject(C, DC, RefRSVar, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001272 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001273
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001274 slangAssert((RSExportPrimitiveType::IsRSObjectType(DT)) &&
1275 "Should be RS object");
Stephen Hines1bdd4972010-11-08 17:35:08 -08001276
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001277 return ClearSingleRSObject(C, RefRSVar, Loc);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001278}
1279
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001280bool RSObjectRefCount::InitializeRSObject(clang::VarDecl *VD,
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001281 DataType *DT,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001282 clang::Expr **InitExpr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001283 slangAssert(VD && DT && InitExpr);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001284 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
Stephen Hines2d095042010-11-12 18:13:56 -08001285
1286 // Loop through array types to get to base type
1287 while (T && T->isArrayType()) {
1288 T = T->getArrayElementTypeNoTypeQual();
Stephen Hines2d095042010-11-12 18:13:56 -08001289 }
1290
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001291 bool DataTypeIsStructWithRSObject = false;
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001292 *DT = RSExportPrimitiveType::GetRSSpecificType(T);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001293
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001294 if (*DT == DataTypeUnknown) {
Stephen Hinesfeaca062011-02-04 14:08:13 -08001295 if (RSExportPrimitiveType::IsStructureTypeWithRSObject(T)) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001296 *DT = DataTypeIsStruct;
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001297 DataTypeIsStructWithRSObject = true;
Stephen Hinesfeaca062011-02-04 14:08:13 -08001298 } else {
1299 return false;
1300 }
Stephen Hines2d095042010-11-12 18:13:56 -08001301 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001302
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001303 bool DataTypeIsRSObject = false;
1304 if (DataTypeIsStructWithRSObject) {
1305 DataTypeIsRSObject = true;
1306 } else {
1307 DataTypeIsRSObject = RSExportPrimitiveType::IsRSObjectType(*DT);
1308 }
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001309 *InitExpr = VD->getInit();
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001310
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001311 if (!DataTypeIsRSObject && *InitExpr) {
1312 // If we already have an initializer for a matrix type, we are done.
1313 return DataTypeIsRSObject;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001314 }
1315
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001316 clang::Expr *ZeroInitializer =
1317 CreateZeroInitializerForRSSpecificType(*DT,
1318 VD->getASTContext(),
1319 VD->getLocation());
1320
1321 if (ZeroInitializer) {
1322 ZeroInitializer->setType(T->getCanonicalTypeInternal());
1323 VD->setInit(ZeroInitializer);
1324 }
1325
1326 return DataTypeIsRSObject;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001327}
1328
1329clang::Expr *RSObjectRefCount::CreateZeroInitializerForRSSpecificType(
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001330 DataType DT,
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001331 clang::ASTContext &C,
1332 const clang::SourceLocation &Loc) {
1333 clang::Expr *Res = NULL;
1334 switch (DT) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001335 case DataTypeIsStruct:
1336 case DataTypeRSElement:
1337 case DataTypeRSType:
1338 case DataTypeRSAllocation:
1339 case DataTypeRSSampler:
1340 case DataTypeRSScript:
1341 case DataTypeRSMesh:
1342 case DataTypeRSPath:
1343 case DataTypeRSProgramFragment:
1344 case DataTypeRSProgramVertex:
1345 case DataTypeRSProgramRaster:
1346 case DataTypeRSProgramStore:
1347 case DataTypeRSFont: {
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001348 // (ImplicitCastExpr 'nullptr_t'
1349 // (IntegerLiteral 0)))
1350 llvm::APInt Zero(C.getTypeSize(C.IntTy), 0);
1351 clang::Expr *Int0 = clang::IntegerLiteral::Create(C, Zero, C.IntTy, Loc);
1352 clang::Expr *CastToNull =
1353 clang::ImplicitCastExpr::Create(C,
1354 C.NullPtrTy,
1355 clang::CK_IntegralToPointer,
1356 Int0,
1357 NULL,
1358 clang::VK_RValue);
1359
Stephen Hines1dfc4152012-09-10 20:16:04 -07001360 llvm::SmallVector<clang::Expr*, 1>InitList;
1361 InitList.push_back(CastToNull);
1362
1363 Res = new(C) clang::InitListExpr(C, Loc, InitList, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001364 break;
1365 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001366 case DataTypeRSMatrix2x2:
1367 case DataTypeRSMatrix3x3:
1368 case DataTypeRSMatrix4x4: {
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001369 // RS matrix is not completely an RS object. They hold data by themselves.
1370 // (InitListExpr rs_matrix2x2
1371 // (InitListExpr float[4]
1372 // (FloatingLiteral 0)
1373 // (FloatingLiteral 0)
1374 // (FloatingLiteral 0)
1375 // (FloatingLiteral 0)))
1376 clang::QualType FloatTy = C.FloatTy;
1377 // Constructor sets value to 0.0f by default
1378 llvm::APFloat Val(C.getFloatTypeSemantics(FloatTy));
1379 clang::FloatingLiteral *Float0Val =
1380 clang::FloatingLiteral::Create(C,
1381 Val,
1382 /* isExact = */true,
1383 FloatTy,
1384 Loc);
1385
1386 unsigned N = 0;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001387 if (DT == DataTypeRSMatrix2x2)
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001388 N = 2;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001389 else if (DT == DataTypeRSMatrix3x3)
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001390 N = 3;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001391 else if (DT == DataTypeRSMatrix4x4)
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001392 N = 4;
Stephen Hines1dfc4152012-09-10 20:16:04 -07001393 unsigned N_2 = N * N;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001394
Stephen Hines1dfc4152012-09-10 20:16:04 -07001395 // Assume we are going to be allocating 16 elements, since 4x4 is max.
1396 llvm::SmallVector<clang::Expr*, 16> InitVals;
1397 for (unsigned i = 0; i < N_2; i++)
1398 InitVals.push_back(Float0Val);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001399 clang::Expr *InitExpr =
Stephen Hines1dfc4152012-09-10 20:16:04 -07001400 new(C) clang::InitListExpr(C, Loc, InitVals, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001401 InitExpr->setType(C.getConstantArrayType(FloatTy,
Stephen Hines1dfc4152012-09-10 20:16:04 -07001402 llvm::APInt(32, N_2),
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001403 clang::ArrayType::Normal,
1404 /* EltTypeQuals = */0));
Stephen Hines1dfc4152012-09-10 20:16:04 -07001405 llvm::SmallVector<clang::Expr*, 1> InitExprVec;
1406 InitExprVec.push_back(InitExpr);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001407
Stephen Hines1dfc4152012-09-10 20:16:04 -07001408 Res = new(C) clang::InitListExpr(C, Loc, InitExprVec, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001409 break;
1410 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001411 case DataTypeUnknown:
1412 case DataTypeFloat16:
1413 case DataTypeFloat32:
1414 case DataTypeFloat64:
1415 case DataTypeSigned8:
1416 case DataTypeSigned16:
1417 case DataTypeSigned32:
1418 case DataTypeSigned64:
1419 case DataTypeUnsigned8:
1420 case DataTypeUnsigned16:
1421 case DataTypeUnsigned32:
1422 case DataTypeUnsigned64:
1423 case DataTypeBoolean:
1424 case DataTypeUnsigned565:
1425 case DataTypeUnsigned5551:
1426 case DataTypeUnsigned4444:
1427 case DataTypeMax: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001428 slangAssert(false && "Not RS object type!");
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001429 }
1430 // No default case will enable compiler detecting the missing cases
1431 }
1432
1433 return Res;
1434}
1435
1436void RSObjectRefCount::VisitDeclStmt(clang::DeclStmt *DS) {
1437 for (clang::DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1438 I != E;
1439 I++) {
1440 clang::Decl *D = *I;
1441 if (D->getKind() == clang::Decl::Var) {
1442 clang::VarDecl *VD = static_cast<clang::VarDecl*>(D);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001443 DataType DT = DataTypeUnknown;
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001444 clang::Expr *InitExpr = NULL;
1445 if (InitializeRSObject(VD, &DT, &InitExpr)) {
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001446 // We need to zero-init all RS object types (including matrices), ...
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001447 getCurrentScope()->AppendRSObjectInit(VD, DS, DT, InitExpr);
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001448 // ... but, only add to the list of RS objects if we have some
1449 // non-matrix RS object fields.
1450 if (CountRSObjectTypes(mCtx, VD->getType().getTypePtr(),
1451 VD->getLocation())) {
1452 getCurrentScope()->addRSObject(VD);
1453 }
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001454 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001455 }
1456 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001457}
1458
1459void RSObjectRefCount::VisitCompoundStmt(clang::CompoundStmt *CS) {
1460 if (!CS->body_empty()) {
1461 // Push a new scope
1462 Scope *S = new Scope(CS);
1463 mScopeStack.push(S);
1464
1465 VisitStmt(CS);
1466
1467 // Destroy the scope
Stephen Hines6e6578a2011-02-07 18:05:48 -08001468 slangAssert((getCurrentScope() == S) && "Corrupted scope stack!");
Stephen Hines1bdd4972010-11-08 17:35:08 -08001469 S->InsertLocalVarDestructors();
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001470 mScopeStack.pop();
1471 delete S;
1472 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001473}
1474
1475void RSObjectRefCount::VisitBinAssign(clang::BinaryOperator *AS) {
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001476 clang::QualType QT = AS->getType();
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001477
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001478 if (CountRSObjectTypes(mCtx, QT.getTypePtr(), AS->getExprLoc())) {
1479 getCurrentScope()->ReplaceRSObjectAssignment(AS);
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001480 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001481}
1482
1483void RSObjectRefCount::VisitStmt(clang::Stmt *S) {
1484 for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end();
1485 I != E;
1486 I++) {
1487 if (clang::Stmt *Child = *I) {
1488 Visit(Child);
1489 }
1490 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001491}
1492
Stephen Hines688e64b2011-08-23 16:01:25 -07001493// This function walks the list of global variables and (potentially) creates
1494// a single global static destructor function that properly decrements
1495// reference counts on the contained RS object types.
1496clang::FunctionDecl *RSObjectRefCount::CreateStaticGlobalDtor() {
1497 Init();
1498
1499 clang::DeclContext *DC = mCtx.getTranslationUnitDecl();
1500 clang::SourceLocation loc;
1501
Stephen Hines3f175af2011-09-16 16:26:29 -07001502 llvm::StringRef SR(".rs.dtor");
1503 clang::IdentifierInfo &II = mCtx.Idents.get(SR);
1504 clang::DeclarationName N(&II);
1505 clang::FunctionProtoType::ExtProtoInfo EPI;
Stephen Hines82d72882013-03-18 18:17:57 -07001506 clang::QualType T = mCtx.getFunctionType(mCtx.VoidTy,
1507 llvm::ArrayRef<clang::QualType>(), EPI);
Stephen Hines3f175af2011-09-16 16:26:29 -07001508 clang::FunctionDecl *FD = NULL;
1509
Stephen Hines688e64b2011-08-23 16:01:25 -07001510 // Generate rsClearObject() call chains for every global variable
1511 // (whether static or extern).
1512 std::list<clang::Stmt *> StmtList;
1513 for (clang::DeclContext::decl_iterator I = DC->decls_begin(),
1514 E = DC->decls_end(); I != E; I++) {
1515 clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*I);
1516 if (VD) {
1517 if (CountRSObjectTypes(mCtx, VD->getType().getTypePtr(), loc)) {
Stephen Hines3f175af2011-09-16 16:26:29 -07001518 if (!FD) {
1519 // Only create FD if we are going to use it.
Stephen Hines4b3f3ba2013-05-06 16:18:56 -07001520 FD = clang::FunctionDecl::Create(mCtx, DC, loc, loc, N, T, NULL,
1521 clang::SC_None);
Stephen Hines3f175af2011-09-16 16:26:29 -07001522 }
1523 // Make sure to create any helpers within the function's DeclContext,
1524 // not the one associated with the global translation unit.
1525 clang::Stmt *RSClearObjectCall = Scope::ClearRSObject(VD, FD);
Stephen Hines688e64b2011-08-23 16:01:25 -07001526 StmtList.push_back(RSClearObjectCall);
1527 }
1528 }
1529 }
1530
1531 // Nothing needs to be destroyed, so don't emit a dtor.
1532 if (StmtList.empty()) {
1533 return NULL;
1534 }
1535
Stephen Hines688e64b2011-08-23 16:01:25 -07001536 clang::CompoundStmt *CS = BuildCompoundStmt(mCtx, StmtList, loc);
1537
1538 FD->setBody(CS);
1539
1540 return FD;
1541}
1542
Stephen Hinese639eb52010-11-08 19:27:20 -08001543} // namespace slang