blob: f32eeb2d0075713e52dd392836303a57f47e5751 [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
Stephen Hinesf2174cf2011-02-09 23:21:37 -080035clang::FunctionDecl *RSObjectRefCount::
Stephen Hines1bdd4972010-11-08 17:35:08 -080036 RSSetObjectFD[RSExportPrimitiveType::LastRSObjectType -
37 RSExportPrimitiveType::FirstRSObjectType + 1];
Stephen Hinesf2174cf2011-02-09 23:21:37 -080038clang::FunctionDecl *RSObjectRefCount::
Stephen Hines1bdd4972010-11-08 17:35:08 -080039 RSClearObjectFD[RSExportPrimitiveType::LastRSObjectType -
40 RSExportPrimitiveType::FirstRSObjectType + 1];
41
Stephen Hinesf2174cf2011-02-09 23:21:37 -080042void RSObjectRefCount::GetRSRefCountingFunctions(clang::ASTContext &C) {
Stephen Hines1bdd4972010-11-08 17:35:08 -080043 for (unsigned i = 0;
44 i < (sizeof(RSClearObjectFD) / sizeof(clang::FunctionDecl*));
45 i++) {
46 RSSetObjectFD[i] = NULL;
47 RSClearObjectFD[i] = NULL;
48 }
49
50 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
51
52 for (clang::DeclContext::decl_iterator I = TUDecl->decls_begin(),
53 E = TUDecl->decls_end(); I != E; I++) {
54 if ((I->getKind() >= clang::Decl::firstFunction) &&
55 (I->getKind() <= clang::Decl::lastFunction)) {
56 clang::FunctionDecl *FD = static_cast<clang::FunctionDecl*>(*I);
57
58 // points to RSSetObjectFD or RSClearObjectFD
59 clang::FunctionDecl **RSObjectFD;
60
61 if (FD->getName() == "rsSetObject") {
Stephen Hines6e6578a2011-02-07 18:05:48 -080062 slangAssert((FD->getNumParams() == 2) &&
63 "Invalid rsSetObject function prototype (# params)");
Stephen Hines1bdd4972010-11-08 17:35:08 -080064 RSObjectFD = RSSetObjectFD;
65 } else if (FD->getName() == "rsClearObject") {
Stephen Hines6e6578a2011-02-07 18:05:48 -080066 slangAssert((FD->getNumParams() == 1) &&
67 "Invalid rsClearObject function prototype (# params)");
Stephen Hines1bdd4972010-11-08 17:35:08 -080068 RSObjectFD = RSClearObjectFD;
Stephen Hinese639eb52010-11-08 19:27:20 -080069 } else {
Stephen Hines1bdd4972010-11-08 17:35:08 -080070 continue;
71 }
72
73 const clang::ParmVarDecl *PVD = FD->getParamDecl(0);
74 clang::QualType PVT = PVD->getOriginalType();
75 // The first parameter must be a pointer like rs_allocation*
Stephen Hines6e6578a2011-02-07 18:05:48 -080076 slangAssert(PVT->isPointerType() &&
77 "Invalid rs{Set,Clear}Object function prototype (pointer param)");
Stephen Hines1bdd4972010-11-08 17:35:08 -080078
79 // The rs object type passed to the FD
80 clang::QualType RST = PVT->getPointeeType();
81 RSExportPrimitiveType::DataType DT =
82 RSExportPrimitiveType::GetRSSpecificType(RST.getTypePtr());
Stephen Hines6e6578a2011-02-07 18:05:48 -080083 slangAssert(RSExportPrimitiveType::IsRSObjectType(DT)
Stephen Hines1bdd4972010-11-08 17:35:08 -080084 && "must be RS object type");
85
86 RSObjectFD[(DT - RSExportPrimitiveType::FirstRSObjectType)] = FD;
87 }
88 }
89}
90
Stephen Hines4464d822010-11-11 16:45:08 -080091namespace {
92
Stephen Hines292e00a2011-03-18 19:11:30 -070093// This function constructs a new CompoundStmt from the input StmtList.
94static clang::CompoundStmt* BuildCompoundStmt(clang::ASTContext &C,
95 std::list<clang::Stmt*> &StmtList, clang::SourceLocation Loc) {
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -080096 unsigned NewStmtCount = StmtList.size();
Stephen Hines292e00a2011-03-18 19:11:30 -070097 unsigned CompoundStmtCount = 0;
Stephen Hines1bdd4972010-11-08 17:35:08 -080098
Stephen Hines292e00a2011-03-18 19:11:30 -070099 clang::Stmt **CompoundStmtList;
100 CompoundStmtList = new clang::Stmt*[NewStmtCount];
101
102 std::list<clang::Stmt*>::const_iterator I = StmtList.begin();
103 std::list<clang::Stmt*>::const_iterator E = StmtList.end();
104 for ( ; I != E; I++) {
105 CompoundStmtList[CompoundStmtCount++] = *I;
106 }
107 slangAssert(CompoundStmtCount == NewStmtCount);
108
109 clang::CompoundStmt *CS = new(C) clang::CompoundStmt(C,
110 CompoundStmtList,
111 CompoundStmtCount,
112 Loc,
113 Loc);
114
115 delete [] CompoundStmtList;
116
117 return CS;
118}
119
120static void AppendAfterStmt(clang::ASTContext &C,
121 clang::CompoundStmt *CS,
122 clang::Stmt *S,
123 std::list<clang::Stmt*> &StmtList) {
124 slangAssert(CS);
125 clang::CompoundStmt::body_iterator bI = CS->body_begin();
126 clang::CompoundStmt::body_iterator bE = CS->body_end();
127 clang::Stmt **UpdatedStmtList =
128 new clang::Stmt*[CS->size() + StmtList.size()];
Stephen Hines1bdd4972010-11-08 17:35:08 -0800129
130 unsigned UpdatedStmtCount = 0;
Stephen Hines292e00a2011-03-18 19:11:30 -0700131 unsigned Once = 0;
132 for ( ; bI != bE; bI++) {
133 if (!S && ((*bI)->getStmtClass() == clang::Stmt::ReturnStmtClass)) {
134 // If we come across a return here, we don't have anything we can
135 // reasonably replace. We should have already inserted our destructor
136 // code in the proper spot, so we just clean up and return.
137 delete [] UpdatedStmtList;
Stephen Hines1bdd4972010-11-08 17:35:08 -0800138
Stephen Hines292e00a2011-03-18 19:11:30 -0700139 return;
140 }
141
142 UpdatedStmtList[UpdatedStmtCount++] = *bI;
143
144 if ((*bI == S) && !Once) {
145 Once++;
146 std::list<clang::Stmt*>::const_iterator I = StmtList.begin();
147 std::list<clang::Stmt*>::const_iterator E = StmtList.end();
148 for ( ; I != E; I++) {
149 UpdatedStmtList[UpdatedStmtCount++] = *I;
150 }
151 }
152 }
153 slangAssert(Once <= 1);
154
155 // When S is NULL, we are appending to the end of the CompoundStmt.
156 if (!S) {
157 slangAssert(Once == 0);
Stephen Hines03981a32010-12-14 19:45:49 -0800158 std::list<clang::Stmt*>::const_iterator I = StmtList.begin();
Stephen Hines292e00a2011-03-18 19:11:30 -0700159 std::list<clang::Stmt*>::const_iterator E = StmtList.end();
160 for ( ; I != E; I++) {
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -0800161 UpdatedStmtList[UpdatedStmtCount++] = *I;
Stephen Hines4464d822010-11-11 16:45:08 -0800162 }
Stephen Hines1bdd4972010-11-08 17:35:08 -0800163 }
164
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -0800165 CS->setStmts(C, UpdatedStmtList, UpdatedStmtCount);
Stephen Hines1bdd4972010-11-08 17:35:08 -0800166
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -0800167 delete [] UpdatedStmtList;
Stephen Hines1bdd4972010-11-08 17:35:08 -0800168
169 return;
170}
171
Stephen Hinesa883ce32011-08-11 18:52:48 -0700172// This class visits a compound statement and inserts DtorStmt
173// in proper locations. This includes inserting it before any
Stephen Hines4464d822010-11-11 16:45:08 -0800174// return statement in any sub-block, at the end of the logical enclosing
175// scope (compound statement), and/or before any break/continue statement that
176// would resume outside the declared scope. We will not handle the case for
177// goto statements that leave a local scope.
Stephen Hines292e00a2011-03-18 19:11:30 -0700178//
179// To accomplish these goals, it collects a list of sub-Stmt's that
180// correspond to scope exit points. It then uses an RSASTReplace visitor to
181// transform the AST, inserting appropriate destructors before each of those
182// sub-Stmt's (and also before the exit of the outermost containing Stmt for
183// the scope).
Stephen Hines4464d822010-11-11 16:45:08 -0800184class DestructorVisitor : public clang::StmtVisitor<DestructorVisitor> {
185 private:
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700186 clang::ASTContext &mCtx;
Stephen Hines292e00a2011-03-18 19:11:30 -0700187
188 // The loop depth of the currently visited node.
189 int mLoopDepth;
190
191 // The switch statement depth of the currently visited node.
192 // Note that this is tracked separately from the loop depth because
193 // SwitchStmt-contained ContinueStmt's should have destructors for the
194 // corresponding loop scope.
195 int mSwitchDepth;
196
197 // The outermost statement block that we are currently visiting.
198 // This should always be a CompoundStmt.
199 clang::Stmt *mOuterStmt;
200
Stephen Hinesa883ce32011-08-11 18:52:48 -0700201 // The destructor to execute for this scope/variable.
202 clang::Stmt* mDtorStmt;
Stephen Hines292e00a2011-03-18 19:11:30 -0700203
204 // The stack of statements which should be replaced by a compound statement
Stephen Hinesa883ce32011-08-11 18:52:48 -0700205 // containing the new destructor call followed by the original Stmt.
Stephen Hines292e00a2011-03-18 19:11:30 -0700206 std::stack<clang::Stmt*> mReplaceStmtStack;
207
Stephen Hinesa883ce32011-08-11 18:52:48 -0700208 // The source location for the variable declaration that we are trying to
209 // insert destructors for. Note that InsertDestructors() will not generate
210 // destructor calls for source locations that occur lexically before this
211 // location.
212 clang::SourceLocation mVarLoc;
213
Stephen Hines4464d822010-11-11 16:45:08 -0800214 public:
Stephen Hines292e00a2011-03-18 19:11:30 -0700215 DestructorVisitor(clang::ASTContext &C,
216 clang::Stmt* OuterStmt,
Stephen Hinesa883ce32011-08-11 18:52:48 -0700217 clang::Stmt* DtorStmt,
218 clang::SourceLocation VarLoc);
Stephen Hines292e00a2011-03-18 19:11:30 -0700219
220 // This code walks the collected list of Stmts to replace and actually does
Stephen Hinesa883ce32011-08-11 18:52:48 -0700221 // the replacement. It also finishes up by appending the destructor to the
222 // current outermost CompoundStmt.
Stephen Hines292e00a2011-03-18 19:11:30 -0700223 void InsertDestructors() {
224 clang::Stmt *S = NULL;
Stephen Hinesa883ce32011-08-11 18:52:48 -0700225 clang::SourceManager &SM = mCtx.getSourceManager();
226 std::list<clang::Stmt *> StmtList;
227 StmtList.push_back(mDtorStmt);
228
Stephen Hines292e00a2011-03-18 19:11:30 -0700229 while (!mReplaceStmtStack.empty()) {
230 S = mReplaceStmtStack.top();
231 mReplaceStmtStack.pop();
232
Stephen Hinesa883ce32011-08-11 18:52:48 -0700233 // Skip all source locations that occur before the variable's
234 // declaration, since it won't have been initialized yet.
235 if (SM.isBeforeInTranslationUnit(S->getLocStart(), mVarLoc)) {
236 continue;
237 }
238
239 StmtList.push_back(S);
Stephen Hines292e00a2011-03-18 19:11:30 -0700240 clang::CompoundStmt *CS =
Stephen Hinesa883ce32011-08-11 18:52:48 -0700241 BuildCompoundStmt(mCtx, StmtList, S->getLocEnd());
242 StmtList.pop_back();
Stephen Hines292e00a2011-03-18 19:11:30 -0700243
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700244 RSASTReplace R(mCtx);
Stephen Hines292e00a2011-03-18 19:11:30 -0700245 R.ReplaceStmt(mOuterStmt, S, CS);
246 }
Logan Chienab992e52011-07-20 22:06:52 +0800247 clang::CompoundStmt *CS =
248 llvm::dyn_cast<clang::CompoundStmt>(mOuterStmt);
Stephen Hines292e00a2011-03-18 19:11:30 -0700249 slangAssert(CS);
Stephen Hinesa883ce32011-08-11 18:52:48 -0700250 AppendAfterStmt(mCtx, CS, NULL, StmtList);
Stephen Hines292e00a2011-03-18 19:11:30 -0700251 }
252
Stephen Hines4464d822010-11-11 16:45:08 -0800253 void VisitStmt(clang::Stmt *S);
254 void VisitCompoundStmt(clang::CompoundStmt *CS);
Stephen Hines292e00a2011-03-18 19:11:30 -0700255
256 void VisitBreakStmt(clang::BreakStmt *BS);
257 void VisitCaseStmt(clang::CaseStmt *CS);
258 void VisitContinueStmt(clang::ContinueStmt *CS);
259 void VisitDefaultStmt(clang::DefaultStmt *DS);
260 void VisitDoStmt(clang::DoStmt *DS);
261 void VisitForStmt(clang::ForStmt *FS);
262 void VisitIfStmt(clang::IfStmt *IS);
263 void VisitReturnStmt(clang::ReturnStmt *RS);
264 void VisitSwitchCase(clang::SwitchCase *SC);
265 void VisitSwitchStmt(clang::SwitchStmt *SS);
266 void VisitWhileStmt(clang::WhileStmt *WS);
Stephen Hines4464d822010-11-11 16:45:08 -0800267};
268
269DestructorVisitor::DestructorVisitor(clang::ASTContext &C,
Stephen Hines292e00a2011-03-18 19:11:30 -0700270 clang::Stmt *OuterStmt,
Stephen Hinesa883ce32011-08-11 18:52:48 -0700271 clang::Stmt *DtorStmt,
272 clang::SourceLocation VarLoc)
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700273 : mCtx(C),
Stephen Hines292e00a2011-03-18 19:11:30 -0700274 mLoopDepth(0),
275 mSwitchDepth(0),
276 mOuterStmt(OuterStmt),
Stephen Hinesa883ce32011-08-11 18:52:48 -0700277 mDtorStmt(DtorStmt),
278 mVarLoc(VarLoc) {
Stephen Hines4464d822010-11-11 16:45:08 -0800279 return;
280}
281
282void DestructorVisitor::VisitStmt(clang::Stmt *S) {
283 for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end();
284 I != E;
285 I++) {
286 if (clang::Stmt *Child = *I) {
287 Visit(Child);
288 }
289 }
290 return;
291}
292
Stephen Hines292e00a2011-03-18 19:11:30 -0700293void DestructorVisitor::VisitCompoundStmt(clang::CompoundStmt *CS) {
294 VisitStmt(CS);
295 return;
296}
297
298void DestructorVisitor::VisitBreakStmt(clang::BreakStmt *BS) {
299 VisitStmt(BS);
300 if ((mLoopDepth == 0) && (mSwitchDepth == 0)) {
301 mReplaceStmtStack.push(BS);
302 }
303 return;
304}
305
306void DestructorVisitor::VisitCaseStmt(clang::CaseStmt *CS) {
307 VisitStmt(CS);
308 return;
309}
310
311void DestructorVisitor::VisitContinueStmt(clang::ContinueStmt *CS) {
312 VisitStmt(CS);
313 if (mLoopDepth == 0) {
314 // Switch statements can have nested continues.
315 mReplaceStmtStack.push(CS);
316 }
317 return;
318}
319
320void DestructorVisitor::VisitDefaultStmt(clang::DefaultStmt *DS) {
321 VisitStmt(DS);
322 return;
323}
324
325void DestructorVisitor::VisitDoStmt(clang::DoStmt *DS) {
326 mLoopDepth++;
327 VisitStmt(DS);
328 mLoopDepth--;
329 return;
330}
331
332void DestructorVisitor::VisitForStmt(clang::ForStmt *FS) {
333 mLoopDepth++;
334 VisitStmt(FS);
335 mLoopDepth--;
336 return;
337}
338
339void DestructorVisitor::VisitIfStmt(clang::IfStmt *IS) {
340 VisitStmt(IS);
341 return;
342}
343
344void DestructorVisitor::VisitReturnStmt(clang::ReturnStmt *RS) {
345 mReplaceStmtStack.push(RS);
346 return;
347}
348
349void DestructorVisitor::VisitSwitchCase(clang::SwitchCase *SC) {
350 slangAssert(false && "Both case and default have specialized handlers");
351 VisitStmt(SC);
352 return;
353}
354
355void DestructorVisitor::VisitSwitchStmt(clang::SwitchStmt *SS) {
356 mSwitchDepth++;
357 VisitStmt(SS);
358 mSwitchDepth--;
359 return;
360}
361
362void DestructorVisitor::VisitWhileStmt(clang::WhileStmt *WS) {
363 mLoopDepth++;
364 VisitStmt(WS);
365 mLoopDepth--;
366 return;
367}
368
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800369clang::Expr *ClearSingleRSObject(clang::ASTContext &C,
370 clang::Expr *RefRSVar,
371 clang::SourceLocation Loc) {
372 slangAssert(RefRSVar);
373 const clang::Type *T = RefRSVar->getType().getTypePtr();
374 slangAssert(!T->isArrayType() &&
375 "Should not be destroying arrays with this function");
Stephen Hines03981a32010-12-14 19:45:49 -0800376
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800377 clang::FunctionDecl *ClearObjectFD = RSObjectRefCount::GetRSClearObjectFD(T);
378 slangAssert((ClearObjectFD != NULL) &&
379 "rsClearObject doesn't cover all RS object types");
380
381 clang::QualType ClearObjectFDType = ClearObjectFD->getType();
382 clang::QualType ClearObjectFDArgType =
383 ClearObjectFD->getParamDecl(0)->getOriginalType();
384
385 // Example destructor for "rs_font localFont;"
386 //
387 // (CallExpr 'void'
388 // (ImplicitCastExpr 'void (*)(rs_font *)' <FunctionToPointerDecay>
389 // (DeclRefExpr 'void (rs_font *)' FunctionDecl='rsClearObject'))
390 // (UnaryOperator 'rs_font *' prefix '&'
391 // (DeclRefExpr 'rs_font':'rs_font' Var='localFont')))
392
393 // Get address of targeted RS object
394 clang::Expr *AddrRefRSVar =
395 new(C) clang::UnaryOperator(RefRSVar,
396 clang::UO_AddrOf,
397 ClearObjectFDArgType,
Loganbe274822011-02-16 22:02:54 +0800398 clang::VK_RValue,
399 clang::OK_Ordinary,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800400 Loc);
401
402 clang::Expr *RefRSClearObjectFD =
403 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800404 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800405 clang::SourceLocation(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800406 ClearObjectFD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -0700407 false,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800408 ClearObjectFD->getLocation(),
Loganbe274822011-02-16 22:02:54 +0800409 ClearObjectFDType,
410 clang::VK_RValue,
411 NULL);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800412
413 clang::Expr *RSClearObjectFP =
414 clang::ImplicitCastExpr::Create(C,
415 C.getPointerType(ClearObjectFDType),
416 clang::CK_FunctionToPointerDecay,
417 RefRSClearObjectFD,
418 NULL,
419 clang::VK_RValue);
420
Stephen Hines1dfc4152012-09-10 20:16:04 -0700421 llvm::SmallVector<clang::Expr*, 1> ArgList;
422 ArgList.push_back(AddrRefRSVar);
423
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800424 clang::CallExpr *RSClearObjectCall =
425 new(C) clang::CallExpr(C,
426 RSClearObjectFP,
Stephen Hines1dfc4152012-09-10 20:16:04 -0700427 ArgList,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800428 ClearObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +0800429 clang::VK_RValue,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800430 Loc);
431
432 return RSClearObjectCall;
433}
434
435static int ArrayDim(const clang::Type *T) {
Stephen Hines03981a32010-12-14 19:45:49 -0800436 if (!T || !T->isArrayType()) {
437 return 0;
438 }
439
440 const clang::ConstantArrayType *CAT =
441 static_cast<const clang::ConstantArrayType *>(T);
Stephen Hines9d2c0fa2011-01-05 14:55:18 -0800442 return static_cast<int>(CAT->getSize().getSExtValue());
Stephen Hines03981a32010-12-14 19:45:49 -0800443}
444
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800445static clang::Stmt *ClearStructRSObject(
446 clang::ASTContext &C,
447 clang::DeclContext *DC,
448 clang::Expr *RefRSStruct,
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700449 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800450 clang::SourceLocation Loc);
451
452static clang::Stmt *ClearArrayRSObject(
453 clang::ASTContext &C,
454 clang::DeclContext *DC,
455 clang::Expr *RefRSArr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700456 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800457 clang::SourceLocation Loc) {
458 const clang::Type *BaseType = RefRSArr->getType().getTypePtr();
459 slangAssert(BaseType->isArrayType());
460
461 int NumArrayElements = ArrayDim(BaseType);
462 // Actually extract out the base RS object type for use later
463 BaseType = BaseType->getArrayElementTypeNoTypeQual();
Stephen Hines03981a32010-12-14 19:45:49 -0800464
465 clang::Stmt *StmtArray[2] = {NULL};
466 int StmtCtr = 0;
467
Stephen Hines03981a32010-12-14 19:45:49 -0800468 if (NumArrayElements <= 0) {
469 return NULL;
470 }
471
472 // Example destructor loop for "rs_font fontArr[10];"
473 //
474 // (CompoundStmt
475 // (DeclStmt "int rsIntIter")
476 // (ForStmt
477 // (BinaryOperator 'int' '='
478 // (DeclRefExpr 'int' Var='rsIntIter')
479 // (IntegerLiteral 'int' 0))
480 // (BinaryOperator 'int' '<'
481 // (DeclRefExpr 'int' Var='rsIntIter')
482 // (IntegerLiteral 'int' 10)
483 // NULL << CondVar >>
484 // (UnaryOperator 'int' postfix '++'
485 // (DeclRefExpr 'int' Var='rsIntIter'))
486 // (CallExpr 'void'
487 // (ImplicitCastExpr 'void (*)(rs_font *)' <FunctionToPointerDecay>
488 // (DeclRefExpr 'void (rs_font *)' FunctionDecl='rsClearObject'))
489 // (UnaryOperator 'rs_font *' prefix '&'
490 // (ArraySubscriptExpr 'rs_font':'rs_font'
491 // (ImplicitCastExpr 'rs_font *' <ArrayToPointerDecay>
492 // (DeclRefExpr 'rs_font [10]' Var='fontArr'))
493 // (DeclRefExpr 'int' Var='rsIntIter')))))))
494
495 // Create helper variable for iterating through elements
496 clang::IdentifierInfo& II = C.Idents.get("rsIntIter");
497 clang::VarDecl *IIVD =
498 clang::VarDecl::Create(C,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800499 DC,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700500 StartLoc,
Stephen Hines03981a32010-12-14 19:45:49 -0800501 Loc,
502 &II,
503 C.IntTy,
504 C.getTrivialTypeSourceInfo(C.IntTy),
505 clang::SC_None,
506 clang::SC_None);
507 clang::Decl *IID = (clang::Decl *)IIVD;
508
509 clang::DeclGroupRef DGR = clang::DeclGroupRef::Create(C, &IID, 1);
510 StmtArray[StmtCtr++] = new(C) clang::DeclStmt(DGR, Loc, Loc);
511
512 // Form the actual destructor loop
513 // for (Init; Cond; Inc)
514 // RSClearObjectCall;
515
516 // Init -> "rsIntIter = 0"
517 clang::DeclRefExpr *RefrsIntIter =
518 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800519 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800520 clang::SourceLocation(),
Stephen Hines03981a32010-12-14 19:45:49 -0800521 IIVD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -0700522 false,
Stephen Hines03981a32010-12-14 19:45:49 -0800523 Loc,
Loganbe274822011-02-16 22:02:54 +0800524 C.IntTy,
525 clang::VK_RValue,
526 NULL);
Stephen Hines03981a32010-12-14 19:45:49 -0800527
528 clang::Expr *Int0 = clang::IntegerLiteral::Create(C,
529 llvm::APInt(C.getTypeSize(C.IntTy), 0), C.IntTy, Loc);
530
531 clang::BinaryOperator *Init =
532 new(C) clang::BinaryOperator(RefrsIntIter,
533 Int0,
534 clang::BO_Assign,
535 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800536 clang::VK_RValue,
537 clang::OK_Ordinary,
Stephen Hines03981a32010-12-14 19:45:49 -0800538 Loc);
539
540 // Cond -> "rsIntIter < NumArrayElements"
541 clang::Expr *NumArrayElementsExpr = clang::IntegerLiteral::Create(C,
542 llvm::APInt(C.getTypeSize(C.IntTy), NumArrayElements), C.IntTy, Loc);
543
544 clang::BinaryOperator *Cond =
545 new(C) clang::BinaryOperator(RefrsIntIter,
546 NumArrayElementsExpr,
547 clang::BO_LT,
548 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800549 clang::VK_RValue,
550 clang::OK_Ordinary,
Stephen Hines03981a32010-12-14 19:45:49 -0800551 Loc);
552
553 // Inc -> "rsIntIter++"
554 clang::UnaryOperator *Inc =
555 new(C) clang::UnaryOperator(RefrsIntIter,
556 clang::UO_PostInc,
557 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800558 clang::VK_RValue,
559 clang::OK_Ordinary,
Stephen Hines03981a32010-12-14 19:45:49 -0800560 Loc);
561
562 // Body -> "rsClearObject(&VD[rsIntIter]);"
563 // Destructor loop operates on individual array elements
Stephen Hines03981a32010-12-14 19:45:49 -0800564
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800565 clang::Expr *RefRSArrPtr =
Stephen Hines03981a32010-12-14 19:45:49 -0800566 clang::ImplicitCastExpr::Create(C,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800567 C.getPointerType(BaseType->getCanonicalTypeInternal()),
Stephen Hines03981a32010-12-14 19:45:49 -0800568 clang::CK_ArrayToPointerDecay,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800569 RefRSArr,
Stephen Hines03981a32010-12-14 19:45:49 -0800570 NULL,
571 clang::VK_RValue);
572
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800573 clang::Expr *RefRSArrPtrSubscript =
574 new(C) clang::ArraySubscriptExpr(RefRSArrPtr,
Stephen Hines03981a32010-12-14 19:45:49 -0800575 RefrsIntIter,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800576 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800577 clang::VK_RValue,
578 clang::OK_Ordinary,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800579 Loc);
Stephen Hines03981a32010-12-14 19:45:49 -0800580
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800581 RSExportPrimitiveType::DataType DT =
582 RSExportPrimitiveType::GetRSSpecificType(BaseType);
Stephen Hines03981a32010-12-14 19:45:49 -0800583
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800584 clang::Stmt *RSClearObjectCall = NULL;
585 if (BaseType->isArrayType()) {
586 RSClearObjectCall =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700587 ClearArrayRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800588 } else if (DT == RSExportPrimitiveType::DataTypeUnknown) {
589 RSClearObjectCall =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700590 ClearStructRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800591 } else {
592 RSClearObjectCall = ClearSingleRSObject(C, RefRSArrPtrSubscript, Loc);
593 }
Stephen Hines03981a32010-12-14 19:45:49 -0800594
595 clang::ForStmt *DestructorLoop =
596 new(C) clang::ForStmt(C,
597 Init,
598 Cond,
599 NULL, // no condVar
600 Inc,
601 RSClearObjectCall,
602 Loc,
603 Loc,
604 Loc);
605
606 StmtArray[StmtCtr++] = DestructorLoop;
Stephen Hines6e6578a2011-02-07 18:05:48 -0800607 slangAssert(StmtCtr == 2);
Stephen Hines03981a32010-12-14 19:45:49 -0800608
609 clang::CompoundStmt *CS =
610 new(C) clang::CompoundStmt(C, StmtArray, StmtCtr, Loc, Loc);
611
612 return CS;
613}
614
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700615static unsigned CountRSObjectTypes(clang::ASTContext &C,
616 const clang::Type *T,
617 clang::SourceLocation Loc) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800618 slangAssert(T);
619 unsigned RSObjectCount = 0;
620
621 if (T->isArrayType()) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700622 return CountRSObjectTypes(C, T->getArrayElementTypeNoTypeQual(), Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800623 }
624
625 RSExportPrimitiveType::DataType DT =
626 RSExportPrimitiveType::GetRSSpecificType(T);
627 if (DT != RSExportPrimitiveType::DataTypeUnknown) {
628 return (RSExportPrimitiveType::IsRSObjectType(DT) ? 1 : 0);
629 }
630
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700631 if (T->isUnionType()) {
632 clang::RecordDecl *RD = T->getAsUnionType()->getDecl();
633 RD = RD->getDefinition();
634 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
635 FE = RD->field_end();
636 FI != FE;
637 FI++) {
638 const clang::FieldDecl *FD = *FI;
639 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
640 if (CountRSObjectTypes(C, FT, Loc)) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700641 slangAssert(false && "can't have unions with RS object types!");
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700642 return 0;
643 }
644 }
645 }
646
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800647 if (!T->isStructureType()) {
648 return 0;
649 }
650
651 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
652 RD = RD->getDefinition();
653 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
654 FE = RD->field_end();
655 FI != FE;
656 FI++) {
657 const clang::FieldDecl *FD = *FI;
658 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700659 if (CountRSObjectTypes(C, FT, Loc)) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800660 // Sub-structs should only count once (as should arrays, etc.)
661 RSObjectCount++;
662 }
663 }
664
665 return RSObjectCount;
666}
667
668static clang::Stmt *ClearStructRSObject(
669 clang::ASTContext &C,
670 clang::DeclContext *DC,
671 clang::Expr *RefRSStruct,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700672 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800673 clang::SourceLocation Loc) {
674 const clang::Type *BaseType = RefRSStruct->getType().getTypePtr();
675
676 slangAssert(!BaseType->isArrayType());
677
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800678 // Structs should show up as unknown primitive types
Alex Sakhartchouk9be93602011-03-17 17:03:36 -0700679 slangAssert(RSExportPrimitiveType::GetRSSpecificType(BaseType) ==
680 RSExportPrimitiveType::DataTypeUnknown);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800681
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700682 unsigned FieldsToDestroy = CountRSObjectTypes(C, BaseType, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800683
684 unsigned StmtCount = 0;
685 clang::Stmt **StmtArray = new clang::Stmt*[FieldsToDestroy];
Stephen Hines2bb67db2011-02-11 01:36:40 -0800686 for (unsigned i = 0; i < FieldsToDestroy; i++) {
687 StmtArray[i] = NULL;
688 }
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800689
690 // Populate StmtArray by creating a destructor for each RS object field
691 clang::RecordDecl *RD = BaseType->getAsStructureType()->getDecl();
692 RD = RD->getDefinition();
693 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
694 FE = RD->field_end();
695 FI != FE;
696 FI++) {
697 // We just look through all field declarations to see if we find a
698 // declaration for an RS object type (or an array of one).
699 bool IsArrayType = false;
700 clang::FieldDecl *FD = *FI;
701 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
702 const clang::Type *OrigType = FT;
703 while (FT && FT->isArrayType()) {
704 FT = FT->getArrayElementTypeNoTypeQual();
705 IsArrayType = true;
706 }
707
708 if (RSExportPrimitiveType::IsRSObjectType(FT)) {
709 clang::DeclAccessPair FoundDecl =
710 clang::DeclAccessPair::make(FD, clang::AS_none);
711 clang::MemberExpr *RSObjectMember =
712 clang::MemberExpr::Create(C,
713 RefRSStruct,
714 false,
Loganbe274822011-02-16 22:02:54 +0800715 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800716 clang::SourceLocation(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800717 FD,
718 FoundDecl,
719 clang::DeclarationNameInfo(),
720 NULL,
Loganbe274822011-02-16 22:02:54 +0800721 OrigType->getCanonicalTypeInternal(),
722 clang::VK_RValue,
723 clang::OK_Ordinary);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800724
725 slangAssert(StmtCount < FieldsToDestroy);
726
727 if (IsArrayType) {
728 StmtArray[StmtCount++] = ClearArrayRSObject(C,
729 DC,
730 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700731 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800732 Loc);
733 } else {
734 StmtArray[StmtCount++] = ClearSingleRSObject(C,
735 RSObjectMember,
736 Loc);
737 }
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700738 } else if (FT->isStructureType() && CountRSObjectTypes(C, FT, Loc)) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800739 // In this case, we have a nested struct. We may not end up filling all
740 // of the spaces in StmtArray (sub-structs should handle themselves
741 // with separate compound statements).
742 clang::DeclAccessPair FoundDecl =
743 clang::DeclAccessPair::make(FD, clang::AS_none);
744 clang::MemberExpr *RSObjectMember =
745 clang::MemberExpr::Create(C,
746 RefRSStruct,
747 false,
Loganbe274822011-02-16 22:02:54 +0800748 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800749 clang::SourceLocation(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800750 FD,
751 FoundDecl,
752 clang::DeclarationNameInfo(),
753 NULL,
Loganbe274822011-02-16 22:02:54 +0800754 OrigType->getCanonicalTypeInternal(),
755 clang::VK_RValue,
756 clang::OK_Ordinary);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800757
758 if (IsArrayType) {
759 StmtArray[StmtCount++] = ClearArrayRSObject(C,
760 DC,
761 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700762 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800763 Loc);
764 } else {
765 StmtArray[StmtCount++] = ClearStructRSObject(C,
766 DC,
767 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700768 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800769 Loc);
770 }
771 }
772 }
773
774 slangAssert(StmtCount > 0);
775 clang::CompoundStmt *CS =
776 new(C) clang::CompoundStmt(C, StmtArray, StmtCount, Loc, Loc);
777
778 delete [] StmtArray;
779
780 return CS;
781}
782
Stephen Hines2bb67db2011-02-11 01:36:40 -0800783static clang::Stmt *CreateSingleRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800784 clang::Expr *DstExpr,
785 clang::Expr *SrcExpr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700786 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800787 clang::SourceLocation Loc) {
788 const clang::Type *T = DstExpr->getType().getTypePtr();
789 clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(T);
Stephen Hines6e6578a2011-02-07 18:05:48 -0800790 slangAssert((SetObjectFD != NULL) &&
791 "rsSetObject doesn't cover all RS object types");
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800792
793 clang::QualType SetObjectFDType = SetObjectFD->getType();
794 clang::QualType SetObjectFDArgType[2];
795 SetObjectFDArgType[0] = SetObjectFD->getParamDecl(0)->getOriginalType();
796 SetObjectFDArgType[1] = SetObjectFD->getParamDecl(1)->getOriginalType();
797
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800798 clang::Expr *RefRSSetObjectFD =
799 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800800 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -0800801 clang::SourceLocation(),
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800802 SetObjectFD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -0700803 false,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800804 Loc,
Loganbe274822011-02-16 22:02:54 +0800805 SetObjectFDType,
806 clang::VK_RValue,
807 NULL);
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800808
809 clang::Expr *RSSetObjectFP =
810 clang::ImplicitCastExpr::Create(C,
811 C.getPointerType(SetObjectFDType),
812 clang::CK_FunctionToPointerDecay,
813 RefRSSetObjectFD,
814 NULL,
815 clang::VK_RValue);
816
Stephen Hines1dfc4152012-09-10 20:16:04 -0700817 llvm::SmallVector<clang::Expr*, 2> ArgList;
818 ArgList.push_back(new(C) clang::UnaryOperator(DstExpr,
819 clang::UO_AddrOf,
820 SetObjectFDArgType[0],
821 clang::VK_RValue,
822 clang::OK_Ordinary,
823 Loc));
824 ArgList.push_back(SrcExpr);
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800825
826 clang::CallExpr *RSSetObjectCall =
827 new(C) clang::CallExpr(C,
828 RSSetObjectFP,
829 ArgList,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800830 SetObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +0800831 clang::VK_RValue,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800832 Loc);
833
Stephen Hines2bb67db2011-02-11 01:36:40 -0800834 return RSSetObjectCall;
835}
Stephen Hinese79fb5e2011-02-01 19:12:43 -0800836
Stephen Hines2bb67db2011-02-11 01:36:40 -0800837static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800838 clang::Expr *LHS,
839 clang::Expr *RHS,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700840 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800841 clang::SourceLocation Loc);
842
Al Sutton246fa172012-02-23 13:09:08 +0000843/*static clang::Stmt *CreateArrayRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800844 clang::Expr *DstArr,
845 clang::Expr *SrcArr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700846 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800847 clang::SourceLocation Loc) {
848 clang::DeclContext *DC = NULL;
Stephen Hines2bb67db2011-02-11 01:36:40 -0800849 const clang::Type *BaseType = DstArr->getType().getTypePtr();
850 slangAssert(BaseType->isArrayType());
851
852 int NumArrayElements = ArrayDim(BaseType);
853 // Actually extract out the base RS object type for use later
854 BaseType = BaseType->getArrayElementTypeNoTypeQual();
855
856 clang::Stmt *StmtArray[2] = {NULL};
857 int StmtCtr = 0;
858
859 if (NumArrayElements <= 0) {
860 return NULL;
861 }
862
863 // Create helper variable for iterating through elements
864 clang::IdentifierInfo& II = C.Idents.get("rsIntIter");
865 clang::VarDecl *IIVD =
866 clang::VarDecl::Create(C,
867 DC,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700868 StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800869 Loc,
870 &II,
871 C.IntTy,
872 C.getTrivialTypeSourceInfo(C.IntTy),
873 clang::SC_None,
874 clang::SC_None);
875 clang::Decl *IID = (clang::Decl *)IIVD;
876
877 clang::DeclGroupRef DGR = clang::DeclGroupRef::Create(C, &IID, 1);
878 StmtArray[StmtCtr++] = new(C) clang::DeclStmt(DGR, Loc, Loc);
879
880 // Form the actual loop
881 // for (Init; Cond; Inc)
882 // RSSetObjectCall;
883
884 // Init -> "rsIntIter = 0"
885 clang::DeclRefExpr *RefrsIntIter =
886 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800887 clang::NestedNameSpecifierLoc(),
Stephen Hines2bb67db2011-02-11 01:36:40 -0800888 IIVD,
889 Loc,
Loganbe274822011-02-16 22:02:54 +0800890 C.IntTy,
891 clang::VK_RValue,
892 NULL);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800893
894 clang::Expr *Int0 = clang::IntegerLiteral::Create(C,
895 llvm::APInt(C.getTypeSize(C.IntTy), 0), C.IntTy, Loc);
896
897 clang::BinaryOperator *Init =
898 new(C) clang::BinaryOperator(RefrsIntIter,
899 Int0,
900 clang::BO_Assign,
901 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800902 clang::VK_RValue,
903 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800904 Loc);
905
906 // Cond -> "rsIntIter < NumArrayElements"
907 clang::Expr *NumArrayElementsExpr = clang::IntegerLiteral::Create(C,
908 llvm::APInt(C.getTypeSize(C.IntTy), NumArrayElements), C.IntTy, Loc);
909
910 clang::BinaryOperator *Cond =
911 new(C) clang::BinaryOperator(RefrsIntIter,
912 NumArrayElementsExpr,
913 clang::BO_LT,
914 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800915 clang::VK_RValue,
916 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800917 Loc);
918
919 // Inc -> "rsIntIter++"
920 clang::UnaryOperator *Inc =
921 new(C) clang::UnaryOperator(RefrsIntIter,
922 clang::UO_PostInc,
923 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800924 clang::VK_RValue,
925 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800926 Loc);
927
928 // Body -> "rsSetObject(&Dst[rsIntIter], Src[rsIntIter]);"
929 // Loop operates on individual array elements
930
931 clang::Expr *DstArrPtr =
932 clang::ImplicitCastExpr::Create(C,
933 C.getPointerType(BaseType->getCanonicalTypeInternal()),
934 clang::CK_ArrayToPointerDecay,
935 DstArr,
936 NULL,
937 clang::VK_RValue);
938
939 clang::Expr *DstArrPtrSubscript =
940 new(C) clang::ArraySubscriptExpr(DstArrPtr,
941 RefrsIntIter,
942 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800943 clang::VK_RValue,
944 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800945 Loc);
946
947 clang::Expr *SrcArrPtr =
948 clang::ImplicitCastExpr::Create(C,
949 C.getPointerType(BaseType->getCanonicalTypeInternal()),
950 clang::CK_ArrayToPointerDecay,
951 SrcArr,
952 NULL,
953 clang::VK_RValue);
954
955 clang::Expr *SrcArrPtrSubscript =
956 new(C) clang::ArraySubscriptExpr(SrcArrPtr,
957 RefrsIntIter,
958 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800959 clang::VK_RValue,
960 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800961 Loc);
962
963 RSExportPrimitiveType::DataType DT =
964 RSExportPrimitiveType::GetRSSpecificType(BaseType);
965
966 clang::Stmt *RSSetObjectCall = NULL;
967 if (BaseType->isArrayType()) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700968 RSSetObjectCall = CreateArrayRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700969 SrcArrPtrSubscript,
970 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800971 } else if (DT == RSExportPrimitiveType::DataTypeUnknown) {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700972 RSSetObjectCall = CreateStructRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700973 SrcArrPtrSubscript,
974 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800975 } else {
Stephen Hinesd0b5edd2011-04-18 16:38:03 -0700976 RSSetObjectCall = CreateSingleRSSetObject(C, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700977 SrcArrPtrSubscript,
978 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800979 }
980
981 clang::ForStmt *DestructorLoop =
982 new(C) clang::ForStmt(C,
983 Init,
984 Cond,
985 NULL, // no condVar
986 Inc,
987 RSSetObjectCall,
988 Loc,
989 Loc,
990 Loc);
991
992 StmtArray[StmtCtr++] = DestructorLoop;
993 slangAssert(StmtCtr == 2);
994
995 clang::CompoundStmt *CS =
996 new(C) clang::CompoundStmt(C, StmtArray, StmtCtr, Loc, Loc);
997
998 return CS;
Al Sutton246fa172012-02-23 13:09:08 +0000999} */
Stephen Hines2bb67db2011-02-11 01:36:40 -08001000
1001static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C,
Stephen Hines2bb67db2011-02-11 01:36:40 -08001002 clang::Expr *LHS,
1003 clang::Expr *RHS,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001004 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -08001005 clang::SourceLocation Loc) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001006 clang::QualType QT = LHS->getType();
1007 const clang::Type *T = QT.getTypePtr();
1008 slangAssert(T->isStructureType());
1009 slangAssert(!RSExportPrimitiveType::IsRSObjectType(T));
1010
1011 // Keep an extra slot for the original copy (memcpy)
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001012 unsigned FieldsToSet = CountRSObjectTypes(C, T, Loc) + 1;
Stephen Hines2bb67db2011-02-11 01:36:40 -08001013
1014 unsigned StmtCount = 0;
1015 clang::Stmt **StmtArray = new clang::Stmt*[FieldsToSet];
1016 for (unsigned i = 0; i < FieldsToSet; i++) {
1017 StmtArray[i] = NULL;
1018 }
1019
1020 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
1021 RD = RD->getDefinition();
1022 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1023 FE = RD->field_end();
1024 FI != FE;
1025 FI++) {
1026 bool IsArrayType = false;
1027 clang::FieldDecl *FD = *FI;
1028 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
1029 const clang::Type *OrigType = FT;
1030
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001031 if (!CountRSObjectTypes(C, FT, Loc)) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001032 // Skip to next if we don't have any viable RS object types
1033 continue;
1034 }
1035
1036 clang::DeclAccessPair FoundDecl =
1037 clang::DeclAccessPair::make(FD, clang::AS_none);
1038 clang::MemberExpr *DstMember =
1039 clang::MemberExpr::Create(C,
1040 LHS,
1041 false,
Loganbe274822011-02-16 22:02:54 +08001042 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001043 clang::SourceLocation(),
Stephen Hines2bb67db2011-02-11 01:36:40 -08001044 FD,
1045 FoundDecl,
1046 clang::DeclarationNameInfo(),
1047 NULL,
Loganbe274822011-02-16 22:02:54 +08001048 OrigType->getCanonicalTypeInternal(),
1049 clang::VK_RValue,
1050 clang::OK_Ordinary);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001051
1052 clang::MemberExpr *SrcMember =
1053 clang::MemberExpr::Create(C,
1054 RHS,
1055 false,
Loganbe274822011-02-16 22:02:54 +08001056 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001057 clang::SourceLocation(),
Stephen Hines2bb67db2011-02-11 01:36:40 -08001058 FD,
1059 FoundDecl,
1060 clang::DeclarationNameInfo(),
1061 NULL,
Loganbe274822011-02-16 22:02:54 +08001062 OrigType->getCanonicalTypeInternal(),
1063 clang::VK_RValue,
1064 clang::OK_Ordinary);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001065
1066 if (FT->isArrayType()) {
1067 FT = FT->getArrayElementTypeNoTypeQual();
1068 IsArrayType = true;
1069 }
1070
1071 RSExportPrimitiveType::DataType DT =
1072 RSExportPrimitiveType::GetRSSpecificType(FT);
1073
1074 if (IsArrayType) {
Logan Chien9207a2e2011-10-21 15:39:28 +08001075 clang::DiagnosticsEngine &DiagEngine = C.getDiagnostics();
1076 DiagEngine.Report(
1077 clang::FullSourceLoc(Loc, C.getSourceManager()),
1078 DiagEngine.getCustomDiagID(
1079 clang::DiagnosticsEngine::Error,
1080 "Arrays of RS object types within structures cannot be copied"));
Stephen Hines2bb67db2011-02-11 01:36:40 -08001081 // TODO(srhines): Support setting arrays of RS objects
1082 // StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001083 // CreateArrayRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001084 } else if (DT == RSExportPrimitiveType::DataTypeUnknown) {
1085 StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001086 CreateStructRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001087 } else if (RSExportPrimitiveType::IsRSObjectType(DT)) {
1088 StmtArray[StmtCount++] =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001089 CreateSingleRSSetObject(C, DstMember, SrcMember, StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001090 } else {
1091 slangAssert(false);
1092 }
1093 }
1094
1095 slangAssert(StmtCount > 0 && StmtCount < FieldsToSet);
1096
1097 // We still need to actually do the overall struct copy. For simplicity,
1098 // we just do a straight-up assignment (which will still preserve all
1099 // the proper RS object reference counts).
1100 clang::BinaryOperator *CopyStruct =
Loganbe274822011-02-16 22:02:54 +08001101 new(C) clang::BinaryOperator(LHS, RHS, clang::BO_Assign, QT,
1102 clang::VK_RValue, clang::OK_Ordinary, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001103 StmtArray[StmtCount++] = CopyStruct;
1104
1105 clang::CompoundStmt *CS =
1106 new(C) clang::CompoundStmt(C, StmtArray, StmtCount, Loc, Loc);
1107
1108 delete [] StmtArray;
1109
1110 return CS;
1111}
1112
1113} // namespace
1114
1115void RSObjectRefCount::Scope::ReplaceRSObjectAssignment(
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001116 clang::BinaryOperator *AS) {
Stephen Hines2bb67db2011-02-11 01:36:40 -08001117
1118 clang::QualType QT = AS->getType();
1119
1120 clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD(
1121 RSExportPrimitiveType::DataTypeRSFont)->getASTContext();
1122
Stephen Hines832429f2011-02-25 16:05:37 -08001123 clang::SourceLocation Loc = AS->getExprLoc();
Stephen Hines9f1d0aa2011-12-18 15:41:00 -08001124 clang::SourceLocation StartLoc = AS->getLHS()->getExprLoc();
Stephen Hines2bb67db2011-02-11 01:36:40 -08001125 clang::Stmt *UpdatedStmt = NULL;
1126
1127 if (!RSExportPrimitiveType::IsRSObjectType(QT.getTypePtr())) {
1128 // By definition, this is a struct assignment if we get here
1129 UpdatedStmt =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001130 CreateStructRSSetObject(C, AS->getLHS(), AS->getRHS(), StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001131 } else {
1132 UpdatedStmt =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001133 CreateSingleRSSetObject(C, AS->getLHS(), AS->getRHS(), StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001134 }
1135
Stephen Hines292e00a2011-03-18 19:11:30 -07001136 RSASTReplace R(C);
1137 R.ReplaceStmt(mCS, AS, UpdatedStmt);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001138 return;
1139}
1140
1141void RSObjectRefCount::Scope::AppendRSObjectInit(
1142 clang::VarDecl *VD,
1143 clang::DeclStmt *DS,
1144 RSExportPrimitiveType::DataType DT,
1145 clang::Expr *InitExpr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001146 slangAssert(VD);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001147
1148 if (!InitExpr) {
1149 return;
1150 }
1151
Stephen Hinesa0611e62011-02-11 16:35:47 -08001152 clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD(
1153 RSExportPrimitiveType::DataTypeRSFont)->getASTContext();
1154 clang::SourceLocation Loc = RSObjectRefCount::GetRSSetObjectFD(
1155 RSExportPrimitiveType::DataTypeRSFont)->getLocation();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001156 clang::SourceLocation StartLoc = RSObjectRefCount::GetRSSetObjectFD(
1157 RSExportPrimitiveType::DataTypeRSFont)->getInnerLocStart();
Stephen Hinesa0611e62011-02-11 16:35:47 -08001158
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001159 if (DT == RSExportPrimitiveType::DataTypeIsStruct) {
Stephen Hinesa0611e62011-02-11 16:35:47 -08001160 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
1161 clang::DeclRefExpr *RefRSVar =
1162 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001163 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001164 clang::SourceLocation(),
Stephen Hinesa0611e62011-02-11 16:35:47 -08001165 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001166 false,
Stephen Hinesa0611e62011-02-11 16:35:47 -08001167 Loc,
Loganbe274822011-02-16 22:02:54 +08001168 T->getCanonicalTypeInternal(),
1169 clang::VK_RValue,
1170 NULL);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001171
1172 clang::Stmt *RSSetObjectOps =
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001173 CreateStructRSSetObject(C, RefRSVar, InitExpr, StartLoc, Loc);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001174
Stephen Hines292e00a2011-03-18 19:11:30 -07001175 std::list<clang::Stmt*> StmtList;
1176 StmtList.push_back(RSSetObjectOps);
1177 AppendAfterStmt(C, mCS, DS, StmtList);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001178 return;
1179 }
1180
1181 clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(DT);
Stephen Hines6e6578a2011-02-07 18:05:48 -08001182 slangAssert((SetObjectFD != NULL) &&
1183 "rsSetObject doesn't cover all RS object types");
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001184
1185 clang::QualType SetObjectFDType = SetObjectFD->getType();
1186 clang::QualType SetObjectFDArgType[2];
1187 SetObjectFDArgType[0] = SetObjectFD->getParamDecl(0)->getOriginalType();
1188 SetObjectFDArgType[1] = SetObjectFD->getParamDecl(1)->getOriginalType();
1189
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001190 clang::Expr *RefRSSetObjectFD =
1191 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001192 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001193 clang::SourceLocation(),
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001194 SetObjectFD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001195 false,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001196 Loc,
Loganbe274822011-02-16 22:02:54 +08001197 SetObjectFDType,
1198 clang::VK_RValue,
1199 NULL);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001200
1201 clang::Expr *RSSetObjectFP =
1202 clang::ImplicitCastExpr::Create(C,
1203 C.getPointerType(SetObjectFDType),
1204 clang::CK_FunctionToPointerDecay,
1205 RefRSSetObjectFD,
1206 NULL,
1207 clang::VK_RValue);
1208
1209 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
1210 clang::DeclRefExpr *RefRSVar =
1211 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001212 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001213 clang::SourceLocation(),
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001214 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001215 false,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001216 Loc,
Loganbe274822011-02-16 22:02:54 +08001217 T->getCanonicalTypeInternal(),
1218 clang::VK_RValue,
1219 NULL);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001220
Stephen Hines1dfc4152012-09-10 20:16:04 -07001221 llvm::SmallVector<clang::Expr*, 2> ArgList;
1222 ArgList.push_back(new(C) clang::UnaryOperator(RefRSVar,
1223 clang::UO_AddrOf,
1224 SetObjectFDArgType[0],
1225 clang::VK_RValue,
1226 clang::OK_Ordinary,
1227 Loc));
1228 ArgList.push_back(InitExpr);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001229
1230 clang::CallExpr *RSSetObjectCall =
1231 new(C) clang::CallExpr(C,
1232 RSSetObjectFP,
1233 ArgList,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001234 SetObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +08001235 clang::VK_RValue,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001236 Loc);
1237
Stephen Hines292e00a2011-03-18 19:11:30 -07001238 std::list<clang::Stmt*> StmtList;
1239 StmtList.push_back(RSSetObjectCall);
1240 AppendAfterStmt(C, mCS, DS, StmtList);
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001241
1242 return;
1243}
1244
Stephen Hines1bdd4972010-11-08 17:35:08 -08001245void RSObjectRefCount::Scope::InsertLocalVarDestructors() {
Stephen Hines1bdd4972010-11-08 17:35:08 -08001246 for (std::list<clang::VarDecl*>::const_iterator I = mRSO.begin(),
1247 E = mRSO.end();
1248 I != E;
1249 I++) {
Stephen Hinesa883ce32011-08-11 18:52:48 -07001250 clang::VarDecl *VD = *I;
Stephen Hines3f175af2011-09-16 16:26:29 -07001251 clang::Stmt *RSClearObjectCall = ClearRSObject(VD, VD->getDeclContext());
Stephen Hinesa883ce32011-08-11 18:52:48 -07001252 if (RSClearObjectCall) {
1253 DestructorVisitor DV((*mRSO.begin())->getASTContext(),
1254 mCS,
1255 RSClearObjectCall,
1256 VD->getSourceRange().getBegin());
1257 DV.Visit(mCS);
1258 DV.InsertDestructors();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001259 }
1260 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001261 return;
1262}
1263
Stephen Hines3f175af2011-09-16 16:26:29 -07001264clang::Stmt *RSObjectRefCount::Scope::ClearRSObject(
1265 clang::VarDecl *VD,
1266 clang::DeclContext *DC) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001267 slangAssert(VD);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001268 clang::ASTContext &C = VD->getASTContext();
1269 clang::SourceLocation Loc = VD->getLocation();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001270 clang::SourceLocation StartLoc = VD->getInnerLocStart();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001271 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
Stephen Hines03981a32010-12-14 19:45:49 -08001272
Stephen Hines1bdd4972010-11-08 17:35:08 -08001273 // Reference expr to target RS object variable
1274 clang::DeclRefExpr *RefRSVar =
1275 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001276 clang::NestedNameSpecifierLoc(),
Stephen Hines0444de02012-03-02 23:19:06 -08001277 clang::SourceLocation(),
Stephen Hines1bdd4972010-11-08 17:35:08 -08001278 VD,
Shih-wei Liaoe2597ac2012-03-24 02:44:33 -07001279 false,
Stephen Hines1bdd4972010-11-08 17:35:08 -08001280 Loc,
Loganbe274822011-02-16 22:02:54 +08001281 T->getCanonicalTypeInternal(),
1282 clang::VK_RValue,
1283 NULL);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001284
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001285 if (T->isArrayType()) {
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001286 return ClearArrayRSObject(C, DC, RefRSVar, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001287 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001288
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001289 RSExportPrimitiveType::DataType DT =
1290 RSExportPrimitiveType::GetRSSpecificType(T);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001291
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001292 if (DT == RSExportPrimitiveType::DataTypeUnknown ||
1293 DT == RSExportPrimitiveType::DataTypeIsStruct) {
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001294 return ClearStructRSObject(C, DC, RefRSVar, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001295 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001296
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001297 slangAssert((RSExportPrimitiveType::IsRSObjectType(DT)) &&
1298 "Should be RS object");
Stephen Hines1bdd4972010-11-08 17:35:08 -08001299
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001300 return ClearSingleRSObject(C, RefRSVar, Loc);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001301}
1302
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001303bool RSObjectRefCount::InitializeRSObject(clang::VarDecl *VD,
1304 RSExportPrimitiveType::DataType *DT,
1305 clang::Expr **InitExpr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001306 slangAssert(VD && DT && InitExpr);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001307 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
Stephen Hines2d095042010-11-12 18:13:56 -08001308
1309 // Loop through array types to get to base type
1310 while (T && T->isArrayType()) {
1311 T = T->getArrayElementTypeNoTypeQual();
Stephen Hines2d095042010-11-12 18:13:56 -08001312 }
1313
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001314 bool DataTypeIsStructWithRSObject = false;
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001315 *DT = RSExportPrimitiveType::GetRSSpecificType(T);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001316
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001317 if (*DT == RSExportPrimitiveType::DataTypeUnknown) {
Stephen Hinesfeaca062011-02-04 14:08:13 -08001318 if (RSExportPrimitiveType::IsStructureTypeWithRSObject(T)) {
1319 *DT = RSExportPrimitiveType::DataTypeIsStruct;
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001320 DataTypeIsStructWithRSObject = true;
Stephen Hinesfeaca062011-02-04 14:08:13 -08001321 } else {
1322 return false;
1323 }
Stephen Hines2d095042010-11-12 18:13:56 -08001324 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001325
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001326 bool DataTypeIsRSObject = false;
1327 if (DataTypeIsStructWithRSObject) {
1328 DataTypeIsRSObject = true;
1329 } else {
1330 DataTypeIsRSObject = RSExportPrimitiveType::IsRSObjectType(*DT);
1331 }
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001332 *InitExpr = VD->getInit();
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001333
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001334 if (!DataTypeIsRSObject && *InitExpr) {
1335 // If we already have an initializer for a matrix type, we are done.
1336 return DataTypeIsRSObject;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001337 }
1338
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001339 clang::Expr *ZeroInitializer =
1340 CreateZeroInitializerForRSSpecificType(*DT,
1341 VD->getASTContext(),
1342 VD->getLocation());
1343
1344 if (ZeroInitializer) {
1345 ZeroInitializer->setType(T->getCanonicalTypeInternal());
1346 VD->setInit(ZeroInitializer);
1347 }
1348
1349 return DataTypeIsRSObject;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001350}
1351
1352clang::Expr *RSObjectRefCount::CreateZeroInitializerForRSSpecificType(
1353 RSExportPrimitiveType::DataType DT,
1354 clang::ASTContext &C,
1355 const clang::SourceLocation &Loc) {
1356 clang::Expr *Res = NULL;
1357 switch (DT) {
Stephen Hinesfeaca062011-02-04 14:08:13 -08001358 case RSExportPrimitiveType::DataTypeIsStruct:
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001359 case RSExportPrimitiveType::DataTypeRSElement:
1360 case RSExportPrimitiveType::DataTypeRSType:
1361 case RSExportPrimitiveType::DataTypeRSAllocation:
1362 case RSExportPrimitiveType::DataTypeRSSampler:
1363 case RSExportPrimitiveType::DataTypeRSScript:
1364 case RSExportPrimitiveType::DataTypeRSMesh:
Jason Samsdb6dfdf2011-11-07 18:11:10 -08001365 case RSExportPrimitiveType::DataTypeRSPath:
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001366 case RSExportPrimitiveType::DataTypeRSProgramFragment:
1367 case RSExportPrimitiveType::DataTypeRSProgramVertex:
1368 case RSExportPrimitiveType::DataTypeRSProgramRaster:
1369 case RSExportPrimitiveType::DataTypeRSProgramStore:
1370 case RSExportPrimitiveType::DataTypeRSFont: {
1371 // (ImplicitCastExpr 'nullptr_t'
1372 // (IntegerLiteral 0)))
1373 llvm::APInt Zero(C.getTypeSize(C.IntTy), 0);
1374 clang::Expr *Int0 = clang::IntegerLiteral::Create(C, Zero, C.IntTy, Loc);
1375 clang::Expr *CastToNull =
1376 clang::ImplicitCastExpr::Create(C,
1377 C.NullPtrTy,
1378 clang::CK_IntegralToPointer,
1379 Int0,
1380 NULL,
1381 clang::VK_RValue);
1382
Stephen Hines1dfc4152012-09-10 20:16:04 -07001383 llvm::SmallVector<clang::Expr*, 1>InitList;
1384 InitList.push_back(CastToNull);
1385
1386 Res = new(C) clang::InitListExpr(C, Loc, InitList, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001387 break;
1388 }
1389 case RSExportPrimitiveType::DataTypeRSMatrix2x2:
1390 case RSExportPrimitiveType::DataTypeRSMatrix3x3:
1391 case RSExportPrimitiveType::DataTypeRSMatrix4x4: {
1392 // RS matrix is not completely an RS object. They hold data by themselves.
1393 // (InitListExpr rs_matrix2x2
1394 // (InitListExpr float[4]
1395 // (FloatingLiteral 0)
1396 // (FloatingLiteral 0)
1397 // (FloatingLiteral 0)
1398 // (FloatingLiteral 0)))
1399 clang::QualType FloatTy = C.FloatTy;
1400 // Constructor sets value to 0.0f by default
1401 llvm::APFloat Val(C.getFloatTypeSemantics(FloatTy));
1402 clang::FloatingLiteral *Float0Val =
1403 clang::FloatingLiteral::Create(C,
1404 Val,
1405 /* isExact = */true,
1406 FloatTy,
1407 Loc);
1408
1409 unsigned N = 0;
1410 if (DT == RSExportPrimitiveType::DataTypeRSMatrix2x2)
1411 N = 2;
1412 else if (DT == RSExportPrimitiveType::DataTypeRSMatrix3x3)
1413 N = 3;
1414 else if (DT == RSExportPrimitiveType::DataTypeRSMatrix4x4)
1415 N = 4;
Stephen Hines1dfc4152012-09-10 20:16:04 -07001416 unsigned N_2 = N * N;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001417
Stephen Hines1dfc4152012-09-10 20:16:04 -07001418 // Assume we are going to be allocating 16 elements, since 4x4 is max.
1419 llvm::SmallVector<clang::Expr*, 16> InitVals;
1420 for (unsigned i = 0; i < N_2; i++)
1421 InitVals.push_back(Float0Val);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001422 clang::Expr *InitExpr =
Stephen Hines1dfc4152012-09-10 20:16:04 -07001423 new(C) clang::InitListExpr(C, Loc, InitVals, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001424 InitExpr->setType(C.getConstantArrayType(FloatTy,
Stephen Hines1dfc4152012-09-10 20:16:04 -07001425 llvm::APInt(32, N_2),
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001426 clang::ArrayType::Normal,
1427 /* EltTypeQuals = */0));
Stephen Hines1dfc4152012-09-10 20:16:04 -07001428 llvm::SmallVector<clang::Expr*, 1> InitExprVec;
1429 InitExprVec.push_back(InitExpr);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001430
Stephen Hines1dfc4152012-09-10 20:16:04 -07001431 Res = new(C) clang::InitListExpr(C, Loc, InitExprVec, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001432 break;
1433 }
1434 case RSExportPrimitiveType::DataTypeUnknown:
1435 case RSExportPrimitiveType::DataTypeFloat16:
1436 case RSExportPrimitiveType::DataTypeFloat32:
1437 case RSExportPrimitiveType::DataTypeFloat64:
1438 case RSExportPrimitiveType::DataTypeSigned8:
1439 case RSExportPrimitiveType::DataTypeSigned16:
1440 case RSExportPrimitiveType::DataTypeSigned32:
1441 case RSExportPrimitiveType::DataTypeSigned64:
1442 case RSExportPrimitiveType::DataTypeUnsigned8:
1443 case RSExportPrimitiveType::DataTypeUnsigned16:
1444 case RSExportPrimitiveType::DataTypeUnsigned32:
1445 case RSExportPrimitiveType::DataTypeUnsigned64:
1446 case RSExportPrimitiveType::DataTypeBoolean:
1447 case RSExportPrimitiveType::DataTypeUnsigned565:
1448 case RSExportPrimitiveType::DataTypeUnsigned5551:
1449 case RSExportPrimitiveType::DataTypeUnsigned4444:
1450 case RSExportPrimitiveType::DataTypeMax: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001451 slangAssert(false && "Not RS object type!");
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001452 }
1453 // No default case will enable compiler detecting the missing cases
1454 }
1455
1456 return Res;
1457}
1458
1459void RSObjectRefCount::VisitDeclStmt(clang::DeclStmt *DS) {
1460 for (clang::DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1461 I != E;
1462 I++) {
1463 clang::Decl *D = *I;
1464 if (D->getKind() == clang::Decl::Var) {
1465 clang::VarDecl *VD = static_cast<clang::VarDecl*>(D);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001466 RSExportPrimitiveType::DataType DT =
1467 RSExportPrimitiveType::DataTypeUnknown;
1468 clang::Expr *InitExpr = NULL;
1469 if (InitializeRSObject(VD, &DT, &InitExpr)) {
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001470 getCurrentScope()->addRSObject(VD);
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001471 getCurrentScope()->AppendRSObjectInit(VD, DS, DT, InitExpr);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001472 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001473 }
1474 }
1475 return;
1476}
1477
1478void RSObjectRefCount::VisitCompoundStmt(clang::CompoundStmt *CS) {
1479 if (!CS->body_empty()) {
1480 // Push a new scope
1481 Scope *S = new Scope(CS);
1482 mScopeStack.push(S);
1483
1484 VisitStmt(CS);
1485
1486 // Destroy the scope
Stephen Hines6e6578a2011-02-07 18:05:48 -08001487 slangAssert((getCurrentScope() == S) && "Corrupted scope stack!");
Stephen Hines1bdd4972010-11-08 17:35:08 -08001488 S->InsertLocalVarDestructors();
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001489 mScopeStack.pop();
1490 delete S;
1491 }
1492 return;
1493}
1494
1495void RSObjectRefCount::VisitBinAssign(clang::BinaryOperator *AS) {
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001496 clang::QualType QT = AS->getType();
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001497
Stephen Hinesd0b5edd2011-04-18 16:38:03 -07001498 if (CountRSObjectTypes(mCtx, QT.getTypePtr(), AS->getExprLoc())) {
1499 getCurrentScope()->ReplaceRSObjectAssignment(AS);
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001500 }
1501
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001502 return;
1503}
1504
1505void RSObjectRefCount::VisitStmt(clang::Stmt *S) {
1506 for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end();
1507 I != E;
1508 I++) {
1509 if (clang::Stmt *Child = *I) {
1510 Visit(Child);
1511 }
1512 }
1513 return;
1514}
1515
Stephen Hines688e64b2011-08-23 16:01:25 -07001516// This function walks the list of global variables and (potentially) creates
1517// a single global static destructor function that properly decrements
1518// reference counts on the contained RS object types.
1519clang::FunctionDecl *RSObjectRefCount::CreateStaticGlobalDtor() {
1520 Init();
1521
1522 clang::DeclContext *DC = mCtx.getTranslationUnitDecl();
1523 clang::SourceLocation loc;
1524
Stephen Hines3f175af2011-09-16 16:26:29 -07001525 llvm::StringRef SR(".rs.dtor");
1526 clang::IdentifierInfo &II = mCtx.Idents.get(SR);
1527 clang::DeclarationName N(&II);
1528 clang::FunctionProtoType::ExtProtoInfo EPI;
1529 clang::QualType T = mCtx.getFunctionType(mCtx.VoidTy, NULL, 0, EPI);
1530 clang::FunctionDecl *FD = NULL;
1531
Stephen Hines688e64b2011-08-23 16:01:25 -07001532 // Generate rsClearObject() call chains for every global variable
1533 // (whether static or extern).
1534 std::list<clang::Stmt *> StmtList;
1535 for (clang::DeclContext::decl_iterator I = DC->decls_begin(),
1536 E = DC->decls_end(); I != E; I++) {
1537 clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*I);
1538 if (VD) {
1539 if (CountRSObjectTypes(mCtx, VD->getType().getTypePtr(), loc)) {
Stephen Hines3f175af2011-09-16 16:26:29 -07001540 if (!FD) {
1541 // Only create FD if we are going to use it.
1542 FD = clang::FunctionDecl::Create(mCtx, DC, loc, loc, N, T, NULL);
1543 }
1544 // Make sure to create any helpers within the function's DeclContext,
1545 // not the one associated with the global translation unit.
1546 clang::Stmt *RSClearObjectCall = Scope::ClearRSObject(VD, FD);
Stephen Hines688e64b2011-08-23 16:01:25 -07001547 StmtList.push_back(RSClearObjectCall);
1548 }
1549 }
1550 }
1551
1552 // Nothing needs to be destroyed, so don't emit a dtor.
1553 if (StmtList.empty()) {
1554 return NULL;
1555 }
1556
Stephen Hines688e64b2011-08-23 16:01:25 -07001557 clang::CompoundStmt *CS = BuildCompoundStmt(mCtx, StmtList, loc);
1558
1559 FD->setBody(CS);
1560
1561 return FD;
1562}
1563
Stephen Hinese639eb52010-11-08 19:27:20 -08001564} // namespace slang