blob: 499c06f033de94110608efef35c4cc573833fdfb [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 Hinesd5f9d6c2010-12-15 16:11:29 -0800172// This class visits a compound statement and inserts the StmtList containing
Stephen Hines4464d822010-11-11 16:45:08 -0800173// destructors in proper locations. This includes inserting them before any
174// 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:
186 clang::ASTContext &mC;
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
201 // The list of destructors to execute for this scope.
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -0800202 std::list<clang::Stmt*> &mStmtList;
Stephen Hines292e00a2011-03-18 19:11:30 -0700203
204 // The stack of statements which should be replaced by a compound statement
205 // containing the new destructor calls followed by the original Stmt.
206 std::stack<clang::Stmt*> mReplaceStmtStack;
207
Stephen Hines4464d822010-11-11 16:45:08 -0800208 public:
Stephen Hines292e00a2011-03-18 19:11:30 -0700209 DestructorVisitor(clang::ASTContext &C,
210 clang::Stmt* OuterStmt,
211 std::list<clang::Stmt*> &StmtList);
212
213 // This code walks the collected list of Stmts to replace and actually does
214 // the replacement. It also finishes up by appending appropriate destructors
215 // to the current outermost CompoundStmt.
216 void InsertDestructors() {
217 clang::Stmt *S = NULL;
218 while (!mReplaceStmtStack.empty()) {
219 S = mReplaceStmtStack.top();
220 mReplaceStmtStack.pop();
221
222 mStmtList.push_back(S);
223 clang::CompoundStmt *CS =
224 BuildCompoundStmt(mC, mStmtList, S->getLocEnd());
225 mStmtList.pop_back();
226
227 RSASTReplace R(mC);
228 R.ReplaceStmt(mOuterStmt, S, CS);
229 }
230 clang::CompoundStmt *CS = dyn_cast<clang::CompoundStmt>(mOuterStmt);
231 slangAssert(CS);
232 if (CS) {
233 AppendAfterStmt(mC, CS, NULL, mStmtList);
234 }
235 }
236
Stephen Hines4464d822010-11-11 16:45:08 -0800237 void VisitStmt(clang::Stmt *S);
238 void VisitCompoundStmt(clang::CompoundStmt *CS);
Stephen Hines292e00a2011-03-18 19:11:30 -0700239
240 void VisitBreakStmt(clang::BreakStmt *BS);
241 void VisitCaseStmt(clang::CaseStmt *CS);
242 void VisitContinueStmt(clang::ContinueStmt *CS);
243 void VisitDefaultStmt(clang::DefaultStmt *DS);
244 void VisitDoStmt(clang::DoStmt *DS);
245 void VisitForStmt(clang::ForStmt *FS);
246 void VisitIfStmt(clang::IfStmt *IS);
247 void VisitReturnStmt(clang::ReturnStmt *RS);
248 void VisitSwitchCase(clang::SwitchCase *SC);
249 void VisitSwitchStmt(clang::SwitchStmt *SS);
250 void VisitWhileStmt(clang::WhileStmt *WS);
Stephen Hines4464d822010-11-11 16:45:08 -0800251};
252
253DestructorVisitor::DestructorVisitor(clang::ASTContext &C,
Stephen Hines292e00a2011-03-18 19:11:30 -0700254 clang::Stmt *OuterStmt,
255 std::list<clang::Stmt*> &StmtList)
Stephen Hines4464d822010-11-11 16:45:08 -0800256 : mC(C),
Stephen Hines292e00a2011-03-18 19:11:30 -0700257 mLoopDepth(0),
258 mSwitchDepth(0),
259 mOuterStmt(OuterStmt),
260 mStmtList(StmtList) {
Stephen Hines4464d822010-11-11 16:45:08 -0800261 return;
262}
263
264void DestructorVisitor::VisitStmt(clang::Stmt *S) {
265 for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end();
266 I != E;
267 I++) {
268 if (clang::Stmt *Child = *I) {
269 Visit(Child);
270 }
271 }
272 return;
273}
274
Stephen Hines292e00a2011-03-18 19:11:30 -0700275void DestructorVisitor::VisitCompoundStmt(clang::CompoundStmt *CS) {
276 VisitStmt(CS);
277 return;
278}
279
280void DestructorVisitor::VisitBreakStmt(clang::BreakStmt *BS) {
281 VisitStmt(BS);
282 if ((mLoopDepth == 0) && (mSwitchDepth == 0)) {
283 mReplaceStmtStack.push(BS);
284 }
285 return;
286}
287
288void DestructorVisitor::VisitCaseStmt(clang::CaseStmt *CS) {
289 VisitStmt(CS);
290 return;
291}
292
293void DestructorVisitor::VisitContinueStmt(clang::ContinueStmt *CS) {
294 VisitStmt(CS);
295 if (mLoopDepth == 0) {
296 // Switch statements can have nested continues.
297 mReplaceStmtStack.push(CS);
298 }
299 return;
300}
301
302void DestructorVisitor::VisitDefaultStmt(clang::DefaultStmt *DS) {
303 VisitStmt(DS);
304 return;
305}
306
307void DestructorVisitor::VisitDoStmt(clang::DoStmt *DS) {
308 mLoopDepth++;
309 VisitStmt(DS);
310 mLoopDepth--;
311 return;
312}
313
314void DestructorVisitor::VisitForStmt(clang::ForStmt *FS) {
315 mLoopDepth++;
316 VisitStmt(FS);
317 mLoopDepth--;
318 return;
319}
320
321void DestructorVisitor::VisitIfStmt(clang::IfStmt *IS) {
322 VisitStmt(IS);
323 return;
324}
325
326void DestructorVisitor::VisitReturnStmt(clang::ReturnStmt *RS) {
327 mReplaceStmtStack.push(RS);
328 return;
329}
330
331void DestructorVisitor::VisitSwitchCase(clang::SwitchCase *SC) {
332 slangAssert(false && "Both case and default have specialized handlers");
333 VisitStmt(SC);
334 return;
335}
336
337void DestructorVisitor::VisitSwitchStmt(clang::SwitchStmt *SS) {
338 mSwitchDepth++;
339 VisitStmt(SS);
340 mSwitchDepth--;
341 return;
342}
343
344void DestructorVisitor::VisitWhileStmt(clang::WhileStmt *WS) {
345 mLoopDepth++;
346 VisitStmt(WS);
347 mLoopDepth--;
348 return;
349}
350
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800351clang::Expr *ClearSingleRSObject(clang::ASTContext &C,
352 clang::Expr *RefRSVar,
353 clang::SourceLocation Loc) {
354 slangAssert(RefRSVar);
355 const clang::Type *T = RefRSVar->getType().getTypePtr();
356 slangAssert(!T->isArrayType() &&
357 "Should not be destroying arrays with this function");
Stephen Hines03981a32010-12-14 19:45:49 -0800358
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800359 clang::FunctionDecl *ClearObjectFD = RSObjectRefCount::GetRSClearObjectFD(T);
360 slangAssert((ClearObjectFD != NULL) &&
361 "rsClearObject doesn't cover all RS object types");
362
363 clang::QualType ClearObjectFDType = ClearObjectFD->getType();
364 clang::QualType ClearObjectFDArgType =
365 ClearObjectFD->getParamDecl(0)->getOriginalType();
366
367 // Example destructor for "rs_font localFont;"
368 //
369 // (CallExpr 'void'
370 // (ImplicitCastExpr 'void (*)(rs_font *)' <FunctionToPointerDecay>
371 // (DeclRefExpr 'void (rs_font *)' FunctionDecl='rsClearObject'))
372 // (UnaryOperator 'rs_font *' prefix '&'
373 // (DeclRefExpr 'rs_font':'rs_font' Var='localFont')))
374
375 // Get address of targeted RS object
376 clang::Expr *AddrRefRSVar =
377 new(C) clang::UnaryOperator(RefRSVar,
378 clang::UO_AddrOf,
379 ClearObjectFDArgType,
Loganbe274822011-02-16 22:02:54 +0800380 clang::VK_RValue,
381 clang::OK_Ordinary,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800382 Loc);
383
384 clang::Expr *RefRSClearObjectFD =
385 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800386 clang::NestedNameSpecifierLoc(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800387 ClearObjectFD,
388 ClearObjectFD->getLocation(),
Loganbe274822011-02-16 22:02:54 +0800389 ClearObjectFDType,
390 clang::VK_RValue,
391 NULL);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800392
393 clang::Expr *RSClearObjectFP =
394 clang::ImplicitCastExpr::Create(C,
395 C.getPointerType(ClearObjectFDType),
396 clang::CK_FunctionToPointerDecay,
397 RefRSClearObjectFD,
398 NULL,
399 clang::VK_RValue);
400
401 clang::CallExpr *RSClearObjectCall =
402 new(C) clang::CallExpr(C,
403 RSClearObjectFP,
404 &AddrRefRSVar,
405 1,
406 ClearObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +0800407 clang::VK_RValue,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800408 Loc);
409
410 return RSClearObjectCall;
411}
412
413static int ArrayDim(const clang::Type *T) {
Stephen Hines03981a32010-12-14 19:45:49 -0800414 if (!T || !T->isArrayType()) {
415 return 0;
416 }
417
418 const clang::ConstantArrayType *CAT =
419 static_cast<const clang::ConstantArrayType *>(T);
Stephen Hines9d2c0fa2011-01-05 14:55:18 -0800420 return static_cast<int>(CAT->getSize().getSExtValue());
Stephen Hines03981a32010-12-14 19:45:49 -0800421}
422
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800423static clang::Stmt *ClearStructRSObject(
424 clang::ASTContext &C,
425 clang::DeclContext *DC,
426 clang::Expr *RefRSStruct,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700427 clang::SourceLocation SourceLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800428 clang::SourceLocation Loc);
429
430static clang::Stmt *ClearArrayRSObject(
431 clang::ASTContext &C,
432 clang::DeclContext *DC,
433 clang::Expr *RefRSArr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700434 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800435 clang::SourceLocation Loc) {
436 const clang::Type *BaseType = RefRSArr->getType().getTypePtr();
437 slangAssert(BaseType->isArrayType());
438
439 int NumArrayElements = ArrayDim(BaseType);
440 // Actually extract out the base RS object type for use later
441 BaseType = BaseType->getArrayElementTypeNoTypeQual();
Stephen Hines03981a32010-12-14 19:45:49 -0800442
443 clang::Stmt *StmtArray[2] = {NULL};
444 int StmtCtr = 0;
445
Stephen Hines03981a32010-12-14 19:45:49 -0800446 if (NumArrayElements <= 0) {
447 return NULL;
448 }
449
450 // Example destructor loop for "rs_font fontArr[10];"
451 //
452 // (CompoundStmt
453 // (DeclStmt "int rsIntIter")
454 // (ForStmt
455 // (BinaryOperator 'int' '='
456 // (DeclRefExpr 'int' Var='rsIntIter')
457 // (IntegerLiteral 'int' 0))
458 // (BinaryOperator 'int' '<'
459 // (DeclRefExpr 'int' Var='rsIntIter')
460 // (IntegerLiteral 'int' 10)
461 // NULL << CondVar >>
462 // (UnaryOperator 'int' postfix '++'
463 // (DeclRefExpr 'int' Var='rsIntIter'))
464 // (CallExpr 'void'
465 // (ImplicitCastExpr 'void (*)(rs_font *)' <FunctionToPointerDecay>
466 // (DeclRefExpr 'void (rs_font *)' FunctionDecl='rsClearObject'))
467 // (UnaryOperator 'rs_font *' prefix '&'
468 // (ArraySubscriptExpr 'rs_font':'rs_font'
469 // (ImplicitCastExpr 'rs_font *' <ArrayToPointerDecay>
470 // (DeclRefExpr 'rs_font [10]' Var='fontArr'))
471 // (DeclRefExpr 'int' Var='rsIntIter')))))))
472
473 // Create helper variable for iterating through elements
474 clang::IdentifierInfo& II = C.Idents.get("rsIntIter");
475 clang::VarDecl *IIVD =
476 clang::VarDecl::Create(C,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800477 DC,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700478 StartLoc,
Stephen Hines03981a32010-12-14 19:45:49 -0800479 Loc,
480 &II,
481 C.IntTy,
482 C.getTrivialTypeSourceInfo(C.IntTy),
483 clang::SC_None,
484 clang::SC_None);
485 clang::Decl *IID = (clang::Decl *)IIVD;
486
487 clang::DeclGroupRef DGR = clang::DeclGroupRef::Create(C, &IID, 1);
488 StmtArray[StmtCtr++] = new(C) clang::DeclStmt(DGR, Loc, Loc);
489
490 // Form the actual destructor loop
491 // for (Init; Cond; Inc)
492 // RSClearObjectCall;
493
494 // Init -> "rsIntIter = 0"
495 clang::DeclRefExpr *RefrsIntIter =
496 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800497 clang::NestedNameSpecifierLoc(),
Stephen Hines03981a32010-12-14 19:45:49 -0800498 IIVD,
499 Loc,
Loganbe274822011-02-16 22:02:54 +0800500 C.IntTy,
501 clang::VK_RValue,
502 NULL);
Stephen Hines03981a32010-12-14 19:45:49 -0800503
504 clang::Expr *Int0 = clang::IntegerLiteral::Create(C,
505 llvm::APInt(C.getTypeSize(C.IntTy), 0), C.IntTy, Loc);
506
507 clang::BinaryOperator *Init =
508 new(C) clang::BinaryOperator(RefrsIntIter,
509 Int0,
510 clang::BO_Assign,
511 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800512 clang::VK_RValue,
513 clang::OK_Ordinary,
Stephen Hines03981a32010-12-14 19:45:49 -0800514 Loc);
515
516 // Cond -> "rsIntIter < NumArrayElements"
517 clang::Expr *NumArrayElementsExpr = clang::IntegerLiteral::Create(C,
518 llvm::APInt(C.getTypeSize(C.IntTy), NumArrayElements), C.IntTy, Loc);
519
520 clang::BinaryOperator *Cond =
521 new(C) clang::BinaryOperator(RefrsIntIter,
522 NumArrayElementsExpr,
523 clang::BO_LT,
524 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800525 clang::VK_RValue,
526 clang::OK_Ordinary,
Stephen Hines03981a32010-12-14 19:45:49 -0800527 Loc);
528
529 // Inc -> "rsIntIter++"
530 clang::UnaryOperator *Inc =
531 new(C) clang::UnaryOperator(RefrsIntIter,
532 clang::UO_PostInc,
533 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800534 clang::VK_RValue,
535 clang::OK_Ordinary,
Stephen Hines03981a32010-12-14 19:45:49 -0800536 Loc);
537
538 // Body -> "rsClearObject(&VD[rsIntIter]);"
539 // Destructor loop operates on individual array elements
Stephen Hines03981a32010-12-14 19:45:49 -0800540
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800541 clang::Expr *RefRSArrPtr =
Stephen Hines03981a32010-12-14 19:45:49 -0800542 clang::ImplicitCastExpr::Create(C,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800543 C.getPointerType(BaseType->getCanonicalTypeInternal()),
Stephen Hines03981a32010-12-14 19:45:49 -0800544 clang::CK_ArrayToPointerDecay,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800545 RefRSArr,
Stephen Hines03981a32010-12-14 19:45:49 -0800546 NULL,
547 clang::VK_RValue);
548
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800549 clang::Expr *RefRSArrPtrSubscript =
550 new(C) clang::ArraySubscriptExpr(RefRSArrPtr,
Stephen Hines03981a32010-12-14 19:45:49 -0800551 RefrsIntIter,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800552 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800553 clang::VK_RValue,
554 clang::OK_Ordinary,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800555 Loc);
Stephen Hines03981a32010-12-14 19:45:49 -0800556
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800557 RSExportPrimitiveType::DataType DT =
558 RSExportPrimitiveType::GetRSSpecificType(BaseType);
Stephen Hines03981a32010-12-14 19:45:49 -0800559
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800560 clang::Stmt *RSClearObjectCall = NULL;
561 if (BaseType->isArrayType()) {
562 RSClearObjectCall =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700563 ClearArrayRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800564 } else if (DT == RSExportPrimitiveType::DataTypeUnknown) {
565 RSClearObjectCall =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700566 ClearStructRSObject(C, DC, RefRSArrPtrSubscript, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800567 } else {
568 RSClearObjectCall = ClearSingleRSObject(C, RefRSArrPtrSubscript, Loc);
569 }
Stephen Hines03981a32010-12-14 19:45:49 -0800570
571 clang::ForStmt *DestructorLoop =
572 new(C) clang::ForStmt(C,
573 Init,
574 Cond,
575 NULL, // no condVar
576 Inc,
577 RSClearObjectCall,
578 Loc,
579 Loc,
580 Loc);
581
582 StmtArray[StmtCtr++] = DestructorLoop;
Stephen Hines6e6578a2011-02-07 18:05:48 -0800583 slangAssert(StmtCtr == 2);
Stephen Hines03981a32010-12-14 19:45:49 -0800584
585 clang::CompoundStmt *CS =
586 new(C) clang::CompoundStmt(C, StmtArray, StmtCtr, Loc, Loc);
587
588 return CS;
589}
590
Stephen Hines2bb67db2011-02-11 01:36:40 -0800591static unsigned CountRSObjectTypes(const clang::Type *T) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800592 slangAssert(T);
593 unsigned RSObjectCount = 0;
594
595 if (T->isArrayType()) {
Stephen Hines2bb67db2011-02-11 01:36:40 -0800596 return CountRSObjectTypes(T->getArrayElementTypeNoTypeQual());
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800597 }
598
599 RSExportPrimitiveType::DataType DT =
600 RSExportPrimitiveType::GetRSSpecificType(T);
601 if (DT != RSExportPrimitiveType::DataTypeUnknown) {
602 return (RSExportPrimitiveType::IsRSObjectType(DT) ? 1 : 0);
603 }
604
605 if (!T->isStructureType()) {
606 return 0;
607 }
608
609 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
610 RD = RD->getDefinition();
611 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
612 FE = RD->field_end();
613 FI != FE;
614 FI++) {
615 const clang::FieldDecl *FD = *FI;
616 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800617 if (CountRSObjectTypes(FT)) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800618 // Sub-structs should only count once (as should arrays, etc.)
619 RSObjectCount++;
620 }
621 }
622
623 return RSObjectCount;
624}
625
626static clang::Stmt *ClearStructRSObject(
627 clang::ASTContext &C,
628 clang::DeclContext *DC,
629 clang::Expr *RefRSStruct,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700630 clang::SourceLocation StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800631 clang::SourceLocation Loc) {
632 const clang::Type *BaseType = RefRSStruct->getType().getTypePtr();
633
634 slangAssert(!BaseType->isArrayType());
635
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800636 // Structs should show up as unknown primitive types
Alex Sakhartchouk9be93602011-03-17 17:03:36 -0700637 slangAssert(RSExportPrimitiveType::GetRSSpecificType(BaseType) ==
638 RSExportPrimitiveType::DataTypeUnknown);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800639
Stephen Hines2bb67db2011-02-11 01:36:40 -0800640 unsigned FieldsToDestroy = CountRSObjectTypes(BaseType);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800641
642 unsigned StmtCount = 0;
643 clang::Stmt **StmtArray = new clang::Stmt*[FieldsToDestroy];
Stephen Hines2bb67db2011-02-11 01:36:40 -0800644 for (unsigned i = 0; i < FieldsToDestroy; i++) {
645 StmtArray[i] = NULL;
646 }
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800647
648 // Populate StmtArray by creating a destructor for each RS object field
649 clang::RecordDecl *RD = BaseType->getAsStructureType()->getDecl();
650 RD = RD->getDefinition();
651 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
652 FE = RD->field_end();
653 FI != FE;
654 FI++) {
655 // We just look through all field declarations to see if we find a
656 // declaration for an RS object type (or an array of one).
657 bool IsArrayType = false;
658 clang::FieldDecl *FD = *FI;
659 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
660 const clang::Type *OrigType = FT;
661 while (FT && FT->isArrayType()) {
662 FT = FT->getArrayElementTypeNoTypeQual();
663 IsArrayType = true;
664 }
665
666 if (RSExportPrimitiveType::IsRSObjectType(FT)) {
667 clang::DeclAccessPair FoundDecl =
668 clang::DeclAccessPair::make(FD, clang::AS_none);
669 clang::MemberExpr *RSObjectMember =
670 clang::MemberExpr::Create(C,
671 RefRSStruct,
672 false,
Loganbe274822011-02-16 22:02:54 +0800673 clang::NestedNameSpecifierLoc(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800674 FD,
675 FoundDecl,
676 clang::DeclarationNameInfo(),
677 NULL,
Loganbe274822011-02-16 22:02:54 +0800678 OrigType->getCanonicalTypeInternal(),
679 clang::VK_RValue,
680 clang::OK_Ordinary);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800681
682 slangAssert(StmtCount < FieldsToDestroy);
683
684 if (IsArrayType) {
685 StmtArray[StmtCount++] = ClearArrayRSObject(C,
686 DC,
687 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700688 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800689 Loc);
690 } else {
691 StmtArray[StmtCount++] = ClearSingleRSObject(C,
692 RSObjectMember,
693 Loc);
694 }
Stephen Hines2bb67db2011-02-11 01:36:40 -0800695 } else if (FT->isStructureType() && CountRSObjectTypes(FT)) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800696 // In this case, we have a nested struct. We may not end up filling all
697 // of the spaces in StmtArray (sub-structs should handle themselves
698 // with separate compound statements).
699 clang::DeclAccessPair FoundDecl =
700 clang::DeclAccessPair::make(FD, clang::AS_none);
701 clang::MemberExpr *RSObjectMember =
702 clang::MemberExpr::Create(C,
703 RefRSStruct,
704 false,
Loganbe274822011-02-16 22:02:54 +0800705 clang::NestedNameSpecifierLoc(),
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800706 FD,
707 FoundDecl,
708 clang::DeclarationNameInfo(),
709 NULL,
Loganbe274822011-02-16 22:02:54 +0800710 OrigType->getCanonicalTypeInternal(),
711 clang::VK_RValue,
712 clang::OK_Ordinary);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800713
714 if (IsArrayType) {
715 StmtArray[StmtCount++] = ClearArrayRSObject(C,
716 DC,
717 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700718 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800719 Loc);
720 } else {
721 StmtArray[StmtCount++] = ClearStructRSObject(C,
722 DC,
723 RSObjectMember,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700724 StartLoc,
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800725 Loc);
726 }
727 }
728 }
729
730 slangAssert(StmtCount > 0);
731 clang::CompoundStmt *CS =
732 new(C) clang::CompoundStmt(C, StmtArray, StmtCount, Loc, Loc);
733
734 delete [] StmtArray;
735
736 return CS;
737}
738
Stephen Hines2bb67db2011-02-11 01:36:40 -0800739static clang::Stmt *CreateSingleRSSetObject(clang::ASTContext &C,
740 clang::Diagnostic *Diags,
741 clang::Expr *DstExpr,
742 clang::Expr *SrcExpr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700743 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800744 clang::SourceLocation Loc) {
745 const clang::Type *T = DstExpr->getType().getTypePtr();
746 clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(T);
Stephen Hines6e6578a2011-02-07 18:05:48 -0800747 slangAssert((SetObjectFD != NULL) &&
748 "rsSetObject doesn't cover all RS object types");
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800749
750 clang::QualType SetObjectFDType = SetObjectFD->getType();
751 clang::QualType SetObjectFDArgType[2];
752 SetObjectFDArgType[0] = SetObjectFD->getParamDecl(0)->getOriginalType();
753 SetObjectFDArgType[1] = SetObjectFD->getParamDecl(1)->getOriginalType();
754
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800755 clang::Expr *RefRSSetObjectFD =
756 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800757 clang::NestedNameSpecifierLoc(),
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800758 SetObjectFD,
759 Loc,
Loganbe274822011-02-16 22:02:54 +0800760 SetObjectFDType,
761 clang::VK_RValue,
762 NULL);
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800763
764 clang::Expr *RSSetObjectFP =
765 clang::ImplicitCastExpr::Create(C,
766 C.getPointerType(SetObjectFDType),
767 clang::CK_FunctionToPointerDecay,
768 RefRSSetObjectFD,
769 NULL,
770 clang::VK_RValue);
771
772 clang::Expr *ArgList[2];
Stephen Hines2bb67db2011-02-11 01:36:40 -0800773 ArgList[0] = new(C) clang::UnaryOperator(DstExpr,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800774 clang::UO_AddrOf,
775 SetObjectFDArgType[0],
Loganbe274822011-02-16 22:02:54 +0800776 clang::VK_RValue,
777 clang::OK_Ordinary,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800778 Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800779 ArgList[1] = SrcExpr;
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800780
781 clang::CallExpr *RSSetObjectCall =
782 new(C) clang::CallExpr(C,
783 RSSetObjectFP,
784 ArgList,
785 2,
786 SetObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +0800787 clang::VK_RValue,
Stephen Hinesc202d2d2011-01-26 11:57:57 -0800788 Loc);
789
Stephen Hines2bb67db2011-02-11 01:36:40 -0800790 return RSSetObjectCall;
791}
Stephen Hinese79fb5e2011-02-01 19:12:43 -0800792
Stephen Hines2bb67db2011-02-11 01:36:40 -0800793static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C,
794 clang::Diagnostic *Diags,
795 clang::Expr *LHS,
796 clang::Expr *RHS,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700797 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800798 clang::SourceLocation Loc);
799
800static clang::Stmt *CreateArrayRSSetObject(clang::ASTContext &C,
801 clang::Diagnostic *Diags,
802 clang::Expr *DstArr,
803 clang::Expr *SrcArr,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700804 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800805 clang::SourceLocation Loc) {
806 clang::DeclContext *DC = NULL;
Stephen Hines2bb67db2011-02-11 01:36:40 -0800807 const clang::Type *BaseType = DstArr->getType().getTypePtr();
808 slangAssert(BaseType->isArrayType());
809
810 int NumArrayElements = ArrayDim(BaseType);
811 // Actually extract out the base RS object type for use later
812 BaseType = BaseType->getArrayElementTypeNoTypeQual();
813
814 clang::Stmt *StmtArray[2] = {NULL};
815 int StmtCtr = 0;
816
817 if (NumArrayElements <= 0) {
818 return NULL;
819 }
820
821 // Create helper variable for iterating through elements
822 clang::IdentifierInfo& II = C.Idents.get("rsIntIter");
823 clang::VarDecl *IIVD =
824 clang::VarDecl::Create(C,
825 DC,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700826 StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800827 Loc,
828 &II,
829 C.IntTy,
830 C.getTrivialTypeSourceInfo(C.IntTy),
831 clang::SC_None,
832 clang::SC_None);
833 clang::Decl *IID = (clang::Decl *)IIVD;
834
835 clang::DeclGroupRef DGR = clang::DeclGroupRef::Create(C, &IID, 1);
836 StmtArray[StmtCtr++] = new(C) clang::DeclStmt(DGR, Loc, Loc);
837
838 // Form the actual loop
839 // for (Init; Cond; Inc)
840 // RSSetObjectCall;
841
842 // Init -> "rsIntIter = 0"
843 clang::DeclRefExpr *RefrsIntIter =
844 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +0800845 clang::NestedNameSpecifierLoc(),
Stephen Hines2bb67db2011-02-11 01:36:40 -0800846 IIVD,
847 Loc,
Loganbe274822011-02-16 22:02:54 +0800848 C.IntTy,
849 clang::VK_RValue,
850 NULL);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800851
852 clang::Expr *Int0 = clang::IntegerLiteral::Create(C,
853 llvm::APInt(C.getTypeSize(C.IntTy), 0), C.IntTy, Loc);
854
855 clang::BinaryOperator *Init =
856 new(C) clang::BinaryOperator(RefrsIntIter,
857 Int0,
858 clang::BO_Assign,
859 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800860 clang::VK_RValue,
861 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800862 Loc);
863
864 // Cond -> "rsIntIter < NumArrayElements"
865 clang::Expr *NumArrayElementsExpr = clang::IntegerLiteral::Create(C,
866 llvm::APInt(C.getTypeSize(C.IntTy), NumArrayElements), C.IntTy, Loc);
867
868 clang::BinaryOperator *Cond =
869 new(C) clang::BinaryOperator(RefrsIntIter,
870 NumArrayElementsExpr,
871 clang::BO_LT,
872 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800873 clang::VK_RValue,
874 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800875 Loc);
876
877 // Inc -> "rsIntIter++"
878 clang::UnaryOperator *Inc =
879 new(C) clang::UnaryOperator(RefrsIntIter,
880 clang::UO_PostInc,
881 C.IntTy,
Loganbe274822011-02-16 22:02:54 +0800882 clang::VK_RValue,
883 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800884 Loc);
885
886 // Body -> "rsSetObject(&Dst[rsIntIter], Src[rsIntIter]);"
887 // Loop operates on individual array elements
888
889 clang::Expr *DstArrPtr =
890 clang::ImplicitCastExpr::Create(C,
891 C.getPointerType(BaseType->getCanonicalTypeInternal()),
892 clang::CK_ArrayToPointerDecay,
893 DstArr,
894 NULL,
895 clang::VK_RValue);
896
897 clang::Expr *DstArrPtrSubscript =
898 new(C) clang::ArraySubscriptExpr(DstArrPtr,
899 RefrsIntIter,
900 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800901 clang::VK_RValue,
902 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800903 Loc);
904
905 clang::Expr *SrcArrPtr =
906 clang::ImplicitCastExpr::Create(C,
907 C.getPointerType(BaseType->getCanonicalTypeInternal()),
908 clang::CK_ArrayToPointerDecay,
909 SrcArr,
910 NULL,
911 clang::VK_RValue);
912
913 clang::Expr *SrcArrPtrSubscript =
914 new(C) clang::ArraySubscriptExpr(SrcArrPtr,
915 RefrsIntIter,
916 BaseType->getCanonicalTypeInternal(),
Loganbe274822011-02-16 22:02:54 +0800917 clang::VK_RValue,
918 clang::OK_Ordinary,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800919 Loc);
920
921 RSExportPrimitiveType::DataType DT =
922 RSExportPrimitiveType::GetRSSpecificType(BaseType);
923
924 clang::Stmt *RSSetObjectCall = NULL;
925 if (BaseType->isArrayType()) {
926 RSSetObjectCall = CreateArrayRSSetObject(C, Diags, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700927 SrcArrPtrSubscript,
928 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800929 } else if (DT == RSExportPrimitiveType::DataTypeUnknown) {
930 RSSetObjectCall = CreateStructRSSetObject(C, Diags, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700931 SrcArrPtrSubscript,
932 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800933 } else {
934 RSSetObjectCall = CreateSingleRSSetObject(C, Diags, DstArrPtrSubscript,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700935 SrcArrPtrSubscript,
936 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -0800937 }
938
939 clang::ForStmt *DestructorLoop =
940 new(C) clang::ForStmt(C,
941 Init,
942 Cond,
943 NULL, // no condVar
944 Inc,
945 RSSetObjectCall,
946 Loc,
947 Loc,
948 Loc);
949
950 StmtArray[StmtCtr++] = DestructorLoop;
951 slangAssert(StmtCtr == 2);
952
953 clang::CompoundStmt *CS =
954 new(C) clang::CompoundStmt(C, StmtArray, StmtCtr, Loc, Loc);
955
956 return CS;
957}
958
959static clang::Stmt *CreateStructRSSetObject(clang::ASTContext &C,
960 clang::Diagnostic *Diags,
961 clang::Expr *LHS,
962 clang::Expr *RHS,
Shih-wei Liaocc887ba2011-04-19 18:36:20 -0700963 clang::SourceLocation StartLoc,
Stephen Hines2bb67db2011-02-11 01:36:40 -0800964 clang::SourceLocation Loc) {
Stephen Hines2bb67db2011-02-11 01:36:40 -0800965 clang::QualType QT = LHS->getType();
966 const clang::Type *T = QT.getTypePtr();
967 slangAssert(T->isStructureType());
968 slangAssert(!RSExportPrimitiveType::IsRSObjectType(T));
969
970 // Keep an extra slot for the original copy (memcpy)
971 unsigned FieldsToSet = CountRSObjectTypes(T) + 1;
972
973 unsigned StmtCount = 0;
974 clang::Stmt **StmtArray = new clang::Stmt*[FieldsToSet];
975 for (unsigned i = 0; i < FieldsToSet; i++) {
976 StmtArray[i] = NULL;
977 }
978
979 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
980 RD = RD->getDefinition();
981 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
982 FE = RD->field_end();
983 FI != FE;
984 FI++) {
985 bool IsArrayType = false;
986 clang::FieldDecl *FD = *FI;
987 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
988 const clang::Type *OrigType = FT;
989
990 if (!CountRSObjectTypes(FT)) {
991 // Skip to next if we don't have any viable RS object types
992 continue;
993 }
994
995 clang::DeclAccessPair FoundDecl =
996 clang::DeclAccessPair::make(FD, clang::AS_none);
997 clang::MemberExpr *DstMember =
998 clang::MemberExpr::Create(C,
999 LHS,
1000 false,
Loganbe274822011-02-16 22:02:54 +08001001 clang::NestedNameSpecifierLoc(),
Stephen Hines2bb67db2011-02-11 01:36:40 -08001002 FD,
1003 FoundDecl,
1004 clang::DeclarationNameInfo(),
1005 NULL,
Loganbe274822011-02-16 22:02:54 +08001006 OrigType->getCanonicalTypeInternal(),
1007 clang::VK_RValue,
1008 clang::OK_Ordinary);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001009
1010 clang::MemberExpr *SrcMember =
1011 clang::MemberExpr::Create(C,
1012 RHS,
1013 false,
Loganbe274822011-02-16 22:02:54 +08001014 clang::NestedNameSpecifierLoc(),
Stephen Hines2bb67db2011-02-11 01:36:40 -08001015 FD,
1016 FoundDecl,
1017 clang::DeclarationNameInfo(),
1018 NULL,
Loganbe274822011-02-16 22:02:54 +08001019 OrigType->getCanonicalTypeInternal(),
1020 clang::VK_RValue,
1021 clang::OK_Ordinary);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001022
1023 if (FT->isArrayType()) {
1024 FT = FT->getArrayElementTypeNoTypeQual();
1025 IsArrayType = true;
1026 }
1027
1028 RSExportPrimitiveType::DataType DT =
1029 RSExportPrimitiveType::GetRSSpecificType(FT);
1030
1031 if (IsArrayType) {
Stephen Hines832429f2011-02-25 16:05:37 -08001032 Diags->Report(clang::FullSourceLoc(Loc, C.getSourceManager()),
1033 Diags->getCustomDiagID(clang::Diagnostic::Error,
1034 "Arrays of RS object types within structures cannot be copied"));
Stephen Hines2bb67db2011-02-11 01:36:40 -08001035 // TODO(srhines): Support setting arrays of RS objects
1036 // StmtArray[StmtCount++] =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001037 // CreateArrayRSSetObject(C, Diags, DstMember, SrcMember, StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001038 } else if (DT == RSExportPrimitiveType::DataTypeUnknown) {
1039 StmtArray[StmtCount++] =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001040 CreateStructRSSetObject(C, Diags, DstMember, SrcMember,
1041 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001042 } else if (RSExportPrimitiveType::IsRSObjectType(DT)) {
1043 StmtArray[StmtCount++] =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001044 CreateSingleRSSetObject(C, Diags, DstMember, SrcMember,
1045 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001046 } else {
1047 slangAssert(false);
1048 }
1049 }
1050
1051 slangAssert(StmtCount > 0 && StmtCount < FieldsToSet);
1052
1053 // We still need to actually do the overall struct copy. For simplicity,
1054 // we just do a straight-up assignment (which will still preserve all
1055 // the proper RS object reference counts).
1056 clang::BinaryOperator *CopyStruct =
Loganbe274822011-02-16 22:02:54 +08001057 new(C) clang::BinaryOperator(LHS, RHS, clang::BO_Assign, QT,
1058 clang::VK_RValue, clang::OK_Ordinary, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001059 StmtArray[StmtCount++] = CopyStruct;
1060
1061 clang::CompoundStmt *CS =
1062 new(C) clang::CompoundStmt(C, StmtArray, StmtCount, Loc, Loc);
1063
1064 delete [] StmtArray;
1065
1066 return CS;
1067}
1068
1069} // namespace
1070
1071void RSObjectRefCount::Scope::ReplaceRSObjectAssignment(
1072 clang::BinaryOperator *AS,
1073 clang::Diagnostic *Diags) {
1074
1075 clang::QualType QT = AS->getType();
1076
1077 clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD(
1078 RSExportPrimitiveType::DataTypeRSFont)->getASTContext();
1079
Stephen Hines832429f2011-02-25 16:05:37 -08001080 clang::SourceLocation Loc = AS->getExprLoc();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001081 clang::SourceLocation StartLoc = AS->getExprLoc();
Stephen Hines2bb67db2011-02-11 01:36:40 -08001082 clang::Stmt *UpdatedStmt = NULL;
1083
1084 if (!RSExportPrimitiveType::IsRSObjectType(QT.getTypePtr())) {
1085 // By definition, this is a struct assignment if we get here
1086 UpdatedStmt =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001087 CreateStructRSSetObject(C, Diags, AS->getLHS(), AS->getRHS(),
1088 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001089 } else {
1090 UpdatedStmt =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001091 CreateSingleRSSetObject(C, Diags, AS->getLHS(), AS->getRHS(),
1092 StartLoc, Loc);
Stephen Hines2bb67db2011-02-11 01:36:40 -08001093 }
1094
Stephen Hines292e00a2011-03-18 19:11:30 -07001095 RSASTReplace R(C);
1096 R.ReplaceStmt(mCS, AS, UpdatedStmt);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001097 return;
1098}
1099
1100void RSObjectRefCount::Scope::AppendRSObjectInit(
Stephen Hinesa0611e62011-02-11 16:35:47 -08001101 clang::Diagnostic *Diags,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001102 clang::VarDecl *VD,
1103 clang::DeclStmt *DS,
1104 RSExportPrimitiveType::DataType DT,
1105 clang::Expr *InitExpr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001106 slangAssert(VD);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001107
1108 if (!InitExpr) {
1109 return;
1110 }
1111
Stephen Hinesa0611e62011-02-11 16:35:47 -08001112 clang::ASTContext &C = RSObjectRefCount::GetRSSetObjectFD(
1113 RSExportPrimitiveType::DataTypeRSFont)->getASTContext();
1114 clang::SourceLocation Loc = RSObjectRefCount::GetRSSetObjectFD(
1115 RSExportPrimitiveType::DataTypeRSFont)->getLocation();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001116 clang::SourceLocation StartLoc = RSObjectRefCount::GetRSSetObjectFD(
1117 RSExportPrimitiveType::DataTypeRSFont)->getInnerLocStart();
Stephen Hinesa0611e62011-02-11 16:35:47 -08001118
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001119 if (DT == RSExportPrimitiveType::DataTypeIsStruct) {
Stephen Hinesa0611e62011-02-11 16:35:47 -08001120 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
1121 clang::DeclRefExpr *RefRSVar =
1122 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001123 clang::NestedNameSpecifierLoc(),
Stephen Hinesa0611e62011-02-11 16:35:47 -08001124 VD,
1125 Loc,
Loganbe274822011-02-16 22:02:54 +08001126 T->getCanonicalTypeInternal(),
1127 clang::VK_RValue,
1128 NULL);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001129
1130 clang::Stmt *RSSetObjectOps =
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001131 CreateStructRSSetObject(C, Diags, RefRSVar, InitExpr, StartLoc, Loc);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001132
Stephen Hines292e00a2011-03-18 19:11:30 -07001133 std::list<clang::Stmt*> StmtList;
1134 StmtList.push_back(RSSetObjectOps);
1135 AppendAfterStmt(C, mCS, DS, StmtList);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001136 return;
1137 }
1138
1139 clang::FunctionDecl *SetObjectFD = RSObjectRefCount::GetRSSetObjectFD(DT);
Stephen Hines6e6578a2011-02-07 18:05:48 -08001140 slangAssert((SetObjectFD != NULL) &&
1141 "rsSetObject doesn't cover all RS object types");
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001142
1143 clang::QualType SetObjectFDType = SetObjectFD->getType();
1144 clang::QualType SetObjectFDArgType[2];
1145 SetObjectFDArgType[0] = SetObjectFD->getParamDecl(0)->getOriginalType();
1146 SetObjectFDArgType[1] = SetObjectFD->getParamDecl(1)->getOriginalType();
1147
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001148 clang::Expr *RefRSSetObjectFD =
1149 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001150 clang::NestedNameSpecifierLoc(),
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001151 SetObjectFD,
1152 Loc,
Loganbe274822011-02-16 22:02:54 +08001153 SetObjectFDType,
1154 clang::VK_RValue,
1155 NULL);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001156
1157 clang::Expr *RSSetObjectFP =
1158 clang::ImplicitCastExpr::Create(C,
1159 C.getPointerType(SetObjectFDType),
1160 clang::CK_FunctionToPointerDecay,
1161 RefRSSetObjectFD,
1162 NULL,
1163 clang::VK_RValue);
1164
1165 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
1166 clang::DeclRefExpr *RefRSVar =
1167 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001168 clang::NestedNameSpecifierLoc(),
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001169 VD,
1170 Loc,
Loganbe274822011-02-16 22:02:54 +08001171 T->getCanonicalTypeInternal(),
1172 clang::VK_RValue,
1173 NULL);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001174
1175 clang::Expr *ArgList[2];
1176 ArgList[0] = new(C) clang::UnaryOperator(RefRSVar,
1177 clang::UO_AddrOf,
1178 SetObjectFDArgType[0],
Loganbe274822011-02-16 22:02:54 +08001179 clang::VK_RValue,
1180 clang::OK_Ordinary,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001181 Loc);
1182 ArgList[1] = InitExpr;
1183
1184 clang::CallExpr *RSSetObjectCall =
1185 new(C) clang::CallExpr(C,
1186 RSSetObjectFP,
1187 ArgList,
1188 2,
1189 SetObjectFD->getCallResultType(),
Loganbe274822011-02-16 22:02:54 +08001190 clang::VK_RValue,
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001191 Loc);
1192
Stephen Hines292e00a2011-03-18 19:11:30 -07001193 std::list<clang::Stmt*> StmtList;
1194 StmtList.push_back(RSSetObjectCall);
1195 AppendAfterStmt(C, mCS, DS, StmtList);
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001196
1197 return;
1198}
1199
Stephen Hines1bdd4972010-11-08 17:35:08 -08001200void RSObjectRefCount::Scope::InsertLocalVarDestructors() {
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -08001201 std::list<clang::Stmt*> RSClearObjectCalls;
Stephen Hines1bdd4972010-11-08 17:35:08 -08001202 for (std::list<clang::VarDecl*>::const_iterator I = mRSO.begin(),
1203 E = mRSO.end();
1204 I != E;
1205 I++) {
Stephen Hinesa858cb62011-01-17 12:17:51 -08001206 clang::Stmt *S = ClearRSObject(*I);
1207 if (S) {
1208 RSClearObjectCalls.push_back(S);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001209 }
1210 }
1211 if (RSClearObjectCalls.size() > 0) {
Stephen Hines292e00a2011-03-18 19:11:30 -07001212 DestructorVisitor DV((*mRSO.begin())->getASTContext(),
1213 mCS,
1214 RSClearObjectCalls);
Stephen Hines4464d822010-11-11 16:45:08 -08001215 DV.Visit(mCS);
Stephen Hines292e00a2011-03-18 19:11:30 -07001216 DV.InsertDestructors();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001217 }
1218 return;
1219}
1220
Stephen Hinesd5f9d6c2010-12-15 16:11:29 -08001221clang::Stmt *RSObjectRefCount::Scope::ClearRSObject(clang::VarDecl *VD) {
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001222 slangAssert(VD);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001223 clang::ASTContext &C = VD->getASTContext();
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001224 clang::DeclContext *DC = VD->getDeclContext();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001225 clang::SourceLocation Loc = VD->getLocation();
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001226 clang::SourceLocation StartLoc = VD->getInnerLocStart();
Stephen Hines1bdd4972010-11-08 17:35:08 -08001227 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
Stephen Hines03981a32010-12-14 19:45:49 -08001228
Stephen Hines1bdd4972010-11-08 17:35:08 -08001229 // Reference expr to target RS object variable
1230 clang::DeclRefExpr *RefRSVar =
1231 clang::DeclRefExpr::Create(C,
Loganbe274822011-02-16 22:02:54 +08001232 clang::NestedNameSpecifierLoc(),
Stephen Hines1bdd4972010-11-08 17:35:08 -08001233 VD,
1234 Loc,
Loganbe274822011-02-16 22:02:54 +08001235 T->getCanonicalTypeInternal(),
1236 clang::VK_RValue,
1237 NULL);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001238
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001239 if (T->isArrayType()) {
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001240 return ClearArrayRSObject(C, DC, RefRSVar, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001241 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001242
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001243 RSExportPrimitiveType::DataType DT =
1244 RSExportPrimitiveType::GetRSSpecificType(T);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001245
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001246 if (DT == RSExportPrimitiveType::DataTypeUnknown ||
1247 DT == RSExportPrimitiveType::DataTypeIsStruct) {
Shih-wei Liaocc887ba2011-04-19 18:36:20 -07001248 return ClearStructRSObject(C, DC, RefRSVar, StartLoc, Loc);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001249 }
Stephen Hines1bdd4972010-11-08 17:35:08 -08001250
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001251 slangAssert((RSExportPrimitiveType::IsRSObjectType(DT)) &&
1252 "Should be RS object");
Stephen Hines1bdd4972010-11-08 17:35:08 -08001253
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001254 return ClearSingleRSObject(C, RefRSVar, Loc);
Stephen Hines1bdd4972010-11-08 17:35:08 -08001255}
1256
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001257bool RSObjectRefCount::InitializeRSObject(clang::VarDecl *VD,
1258 RSExportPrimitiveType::DataType *DT,
1259 clang::Expr **InitExpr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001260 slangAssert(VD && DT && InitExpr);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001261 const clang::Type *T = RSExportType::GetTypeOfDecl(VD);
Stephen Hines2d095042010-11-12 18:13:56 -08001262
1263 // Loop through array types to get to base type
1264 while (T && T->isArrayType()) {
1265 T = T->getArrayElementTypeNoTypeQual();
Stephen Hines2d095042010-11-12 18:13:56 -08001266 }
1267
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001268 bool DataTypeIsStructWithRSObject = false;
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001269 *DT = RSExportPrimitiveType::GetRSSpecificType(T);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001270
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001271 if (*DT == RSExportPrimitiveType::DataTypeUnknown) {
Stephen Hinesfeaca062011-02-04 14:08:13 -08001272 if (RSExportPrimitiveType::IsStructureTypeWithRSObject(T)) {
1273 *DT = RSExportPrimitiveType::DataTypeIsStruct;
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001274 DataTypeIsStructWithRSObject = true;
Stephen Hinesfeaca062011-02-04 14:08:13 -08001275 } else {
1276 return false;
1277 }
Stephen Hines2d095042010-11-12 18:13:56 -08001278 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001279
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001280 bool DataTypeIsRSObject = false;
1281 if (DataTypeIsStructWithRSObject) {
1282 DataTypeIsRSObject = true;
1283 } else {
1284 DataTypeIsRSObject = RSExportPrimitiveType::IsRSObjectType(*DT);
1285 }
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001286 *InitExpr = VD->getInit();
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001287
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001288 if (!DataTypeIsRSObject && *InitExpr) {
1289 // If we already have an initializer for a matrix type, we are done.
1290 return DataTypeIsRSObject;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001291 }
1292
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001293 clang::Expr *ZeroInitializer =
1294 CreateZeroInitializerForRSSpecificType(*DT,
1295 VD->getASTContext(),
1296 VD->getLocation());
1297
1298 if (ZeroInitializer) {
1299 ZeroInitializer->setType(T->getCanonicalTypeInternal());
1300 VD->setInit(ZeroInitializer);
1301 }
1302
1303 return DataTypeIsRSObject;
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001304}
1305
1306clang::Expr *RSObjectRefCount::CreateZeroInitializerForRSSpecificType(
1307 RSExportPrimitiveType::DataType DT,
1308 clang::ASTContext &C,
1309 const clang::SourceLocation &Loc) {
1310 clang::Expr *Res = NULL;
1311 switch (DT) {
Stephen Hinesfeaca062011-02-04 14:08:13 -08001312 case RSExportPrimitiveType::DataTypeIsStruct:
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001313 case RSExportPrimitiveType::DataTypeRSElement:
1314 case RSExportPrimitiveType::DataTypeRSType:
1315 case RSExportPrimitiveType::DataTypeRSAllocation:
1316 case RSExportPrimitiveType::DataTypeRSSampler:
1317 case RSExportPrimitiveType::DataTypeRSScript:
1318 case RSExportPrimitiveType::DataTypeRSMesh:
1319 case RSExportPrimitiveType::DataTypeRSProgramFragment:
1320 case RSExportPrimitiveType::DataTypeRSProgramVertex:
1321 case RSExportPrimitiveType::DataTypeRSProgramRaster:
1322 case RSExportPrimitiveType::DataTypeRSProgramStore:
1323 case RSExportPrimitiveType::DataTypeRSFont: {
1324 // (ImplicitCastExpr 'nullptr_t'
1325 // (IntegerLiteral 0)))
1326 llvm::APInt Zero(C.getTypeSize(C.IntTy), 0);
1327 clang::Expr *Int0 = clang::IntegerLiteral::Create(C, Zero, C.IntTy, Loc);
1328 clang::Expr *CastToNull =
1329 clang::ImplicitCastExpr::Create(C,
1330 C.NullPtrTy,
1331 clang::CK_IntegralToPointer,
1332 Int0,
1333 NULL,
1334 clang::VK_RValue);
1335
Stephen Hinese639eb52010-11-08 19:27:20 -08001336 Res = new(C) clang::InitListExpr(C, Loc, &CastToNull, 1, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001337 break;
1338 }
1339 case RSExportPrimitiveType::DataTypeRSMatrix2x2:
1340 case RSExportPrimitiveType::DataTypeRSMatrix3x3:
1341 case RSExportPrimitiveType::DataTypeRSMatrix4x4: {
1342 // RS matrix is not completely an RS object. They hold data by themselves.
1343 // (InitListExpr rs_matrix2x2
1344 // (InitListExpr float[4]
1345 // (FloatingLiteral 0)
1346 // (FloatingLiteral 0)
1347 // (FloatingLiteral 0)
1348 // (FloatingLiteral 0)))
1349 clang::QualType FloatTy = C.FloatTy;
1350 // Constructor sets value to 0.0f by default
1351 llvm::APFloat Val(C.getFloatTypeSemantics(FloatTy));
1352 clang::FloatingLiteral *Float0Val =
1353 clang::FloatingLiteral::Create(C,
1354 Val,
1355 /* isExact = */true,
1356 FloatTy,
1357 Loc);
1358
1359 unsigned N = 0;
1360 if (DT == RSExportPrimitiveType::DataTypeRSMatrix2x2)
1361 N = 2;
1362 else if (DT == RSExportPrimitiveType::DataTypeRSMatrix3x3)
1363 N = 3;
1364 else if (DT == RSExportPrimitiveType::DataTypeRSMatrix4x4)
1365 N = 4;
1366
1367 // Directly allocate 16 elements instead of dynamically allocate N*N
1368 clang::Expr *InitVals[16];
1369 for (unsigned i = 0; i < sizeof(InitVals) / sizeof(InitVals[0]); i++)
1370 InitVals[i] = Float0Val;
1371 clang::Expr *InitExpr =
Stephen Hinese639eb52010-11-08 19:27:20 -08001372 new(C) clang::InitListExpr(C, Loc, InitVals, N * N, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001373 InitExpr->setType(C.getConstantArrayType(FloatTy,
1374 llvm::APInt(32, 4),
1375 clang::ArrayType::Normal,
1376 /* EltTypeQuals = */0));
1377
Stephen Hinese639eb52010-11-08 19:27:20 -08001378 Res = new(C) clang::InitListExpr(C, Loc, &InitExpr, 1, Loc);
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001379 break;
1380 }
1381 case RSExportPrimitiveType::DataTypeUnknown:
1382 case RSExportPrimitiveType::DataTypeFloat16:
1383 case RSExportPrimitiveType::DataTypeFloat32:
1384 case RSExportPrimitiveType::DataTypeFloat64:
1385 case RSExportPrimitiveType::DataTypeSigned8:
1386 case RSExportPrimitiveType::DataTypeSigned16:
1387 case RSExportPrimitiveType::DataTypeSigned32:
1388 case RSExportPrimitiveType::DataTypeSigned64:
1389 case RSExportPrimitiveType::DataTypeUnsigned8:
1390 case RSExportPrimitiveType::DataTypeUnsigned16:
1391 case RSExportPrimitiveType::DataTypeUnsigned32:
1392 case RSExportPrimitiveType::DataTypeUnsigned64:
1393 case RSExportPrimitiveType::DataTypeBoolean:
1394 case RSExportPrimitiveType::DataTypeUnsigned565:
1395 case RSExportPrimitiveType::DataTypeUnsigned5551:
1396 case RSExportPrimitiveType::DataTypeUnsigned4444:
1397 case RSExportPrimitiveType::DataTypeMax: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001398 slangAssert(false && "Not RS object type!");
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001399 }
1400 // No default case will enable compiler detecting the missing cases
1401 }
1402
1403 return Res;
1404}
1405
1406void RSObjectRefCount::VisitDeclStmt(clang::DeclStmt *DS) {
1407 for (clang::DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
1408 I != E;
1409 I++) {
1410 clang::Decl *D = *I;
1411 if (D->getKind() == clang::Decl::Var) {
1412 clang::VarDecl *VD = static_cast<clang::VarDecl*>(D);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001413 RSExportPrimitiveType::DataType DT =
1414 RSExportPrimitiveType::DataTypeUnknown;
1415 clang::Expr *InitExpr = NULL;
1416 if (InitializeRSObject(VD, &DT, &InitExpr)) {
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001417 getCurrentScope()->addRSObject(VD);
Stephen Hinesa0611e62011-02-11 16:35:47 -08001418 getCurrentScope()->AppendRSObjectInit(mDiags, VD, DS, DT, InitExpr);
Stephen Hinese79fb5e2011-02-01 19:12:43 -08001419 }
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001420 }
1421 }
1422 return;
1423}
1424
1425void RSObjectRefCount::VisitCompoundStmt(clang::CompoundStmt *CS) {
1426 if (!CS->body_empty()) {
1427 // Push a new scope
1428 Scope *S = new Scope(CS);
1429 mScopeStack.push(S);
1430
1431 VisitStmt(CS);
1432
1433 // Destroy the scope
Stephen Hines6e6578a2011-02-07 18:05:48 -08001434 slangAssert((getCurrentScope() == S) && "Corrupted scope stack!");
Stephen Hines1bdd4972010-11-08 17:35:08 -08001435 S->InsertLocalVarDestructors();
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001436 mScopeStack.pop();
1437 delete S;
1438 }
1439 return;
1440}
1441
1442void RSObjectRefCount::VisitBinAssign(clang::BinaryOperator *AS) {
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001443 clang::QualType QT = AS->getType();
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001444
Stephen Hines2bb67db2011-02-11 01:36:40 -08001445 if (CountRSObjectTypes(QT.getTypePtr())) {
1446 getCurrentScope()->ReplaceRSObjectAssignment(AS, mDiags);
Stephen Hinesc202d2d2011-01-26 11:57:57 -08001447 }
1448
Stephen Hines4b32ffd2010-11-05 18:47:11 -07001449 return;
1450}
1451
1452void RSObjectRefCount::VisitStmt(clang::Stmt *S) {
1453 for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end();
1454 I != E;
1455 I++) {
1456 if (clang::Stmt *Child = *I) {
1457 Visit(Child);
1458 }
1459 }
1460 return;
1461}
1462
Stephen Hinese639eb52010-11-08 19:27:20 -08001463} // namespace slang