| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 The University of Utah |
| 4 |
// All rights reserved. |
| 5 |
// |
| 6 |
// This file is distributed under the University of Illinois Open Source |
| 7 |
// License. See the file COPYING for details. |
| 8 |
// |
| 9 |
//===----------------------------------------------------------------------===// |
| 10 |
|
| 11 |
#if HAVE_CONFIG_H |
| 12 |
# include <config.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "Transformation.h" |
| 16 |
|
| 17 |
#include <sstream> |
| 18 |
|
| 19 |
#include "clang/AST/RecursiveASTVisitor.h" |
| 20 |
#include "clang/AST/ASTContext.h" |
| 21 |
#include "clang/Lex/Lexer.h" |
| 22 |
#include "clang/Basic/SourceManager.h" |
| 23 |
#include "llvm/Support/raw_ostream.h" |
| 24 |
#include "llvm/ADT/SmallString.h" |
| 25 |
|
| 26 |
using namespace clang; |
| 27 |
|
| 28 |
class TransNameQueryVisitor : public |
| 29 |
RecursiveASTVisitor<TransNameQueryVisitor> { |
| 30 |
|
| 31 |
public: |
| 32 |
TransNameQueryVisitor(TransNameQueryWrap *Instance, |
| 33 |
const std::string &Prefix) |
| 34 |
: WrapInstance(Instance), |
| 35 |
NamePrefix(Prefix) |
| 36 |
{ } |
| 37 |
|
| 38 |
bool VisitVarDecl(VarDecl *VD); |
| 39 |
|
| 40 |
private: |
| 41 |
TransNameQueryWrap *WrapInstance; |
| 42 |
|
| 43 |
const std::string NamePrefix; |
| 44 |
|
| 45 |
}; |
| 46 |
|
| 47 |
bool TransNameQueryVisitor::VisitVarDecl(VarDecl *VD) |
| 48 |
{ |
| 49 |
std::string Name = VD->getNameAsString(); |
| 50 |
size_t Sz = NamePrefix.size(); |
| 51 |
|
| 52 |
if (Name.compare(0, Sz, NamePrefix)) |
| 53 |
return true; |
| 54 |
|
| 55 |
std::string PostfixStr = Name.substr(Sz); |
| 56 |
TransAssert((PostfixStr.size() > 0) && "Bad trans tmp name!"); |
| 57 |
|
| 58 |
std::stringstream TmpSS(PostfixStr); |
| 59 |
unsigned int PostfixV; |
| 60 |
if (!(TmpSS >> PostfixV)) |
| 61 |
TransAssert(0 && "Non-integer trans tmp name!"); |
| 62 |
|
| 63 |
if (PostfixV > WrapInstance->MaxPostfix) |
| 64 |
WrapInstance->MaxPostfix = PostfixV; |
| 65 |
|
| 66 |
return true; |
| 67 |
} |
| 68 |
|
| 69 |
TransNameQueryWrap::TransNameQueryWrap(const std::string &Prefix) |
| 70 |
: NamePrefix(Prefix), |
| 71 |
MaxPostfix(0) |
| 72 |
{ |
| 73 |
NameQueryVisitor = new TransNameQueryVisitor(this, Prefix); |
| 74 |
} |
| 75 |
|
| 76 |
TransNameQueryWrap::~TransNameQueryWrap(void) |
| 77 |
{ |
| 78 |
delete NameQueryVisitor; |
| 79 |
} |
| 80 |
|
| 81 |
bool TransNameQueryWrap::TraverseDecl(Decl *D) |
| 82 |
{ |
| 83 |
return NameQueryVisitor->TraverseDecl(D); |
| 84 |
} |
| 85 |
|
| 86 |
void Transformation::Initialize(ASTContext &context) |
| 87 |
{ |
| 88 |
Context = &context; |
| 89 |
SrcManager = &Context->getSourceManager(); |
| 90 |
TheRewriter.setSourceMgr(Context->getSourceManager(), |
| 91 |
Context->getLangOpts()); |
| 92 |
RewriteHelper = RewriteUtils::GetInstance(&TheRewriter); |
| 93 |
} |
| 94 |
|
| 95 |
void Transformation::outputTransformedSource(llvm::raw_ostream &OutStream) |
| 96 |
{ |
| 97 |
FileID MainFileID = SrcManager->getMainFileID(); |
| 98 |
const RewriteBuffer *RWBuf = TheRewriter.getRewriteBufferFor(MainFileID); |
| 99 |
|
| 100 |
// RWBuf is non-empty upon any rewrites |
| 101 |
TransAssert(RWBuf && "Empty RewriteBuffer!"); |
| 102 |
OutStream << std::string(RWBuf->begin(), RWBuf->end()); |
| 103 |
OutStream.flush(); |
| 104 |
} |
| 105 |
|
| 106 |
void Transformation::outputOriginalSource(llvm::raw_ostream &OutStream) |
| 107 |
{ |
| 108 |
FileID MainFileID = SrcManager->getMainFileID(); |
| 109 |
auto MainBuf = SrcManager->getBufferOrNone(MainFileID); |
| 110 |
TransAssert(MainBuf && "Empty MainBuf!"); |
| 111 |
OutStream << MainBuf->getBufferStart(); |
| 112 |
OutStream.flush(); |
| 113 |
} |
| 114 |
|
| 115 |
void Transformation::getTransErrorMsg(std::string &ErrorMsg) |
| 116 |
{ |
| 117 |
if (TransError == TransSuccess) { |
| 118 |
ErrorMsg = ""; |
| 119 |
} |
| 120 |
else if (TransError == TransInternalError) { |
| 121 |
ErrorMsg = "Internal transformation error!"; |
| 122 |
} |
| 123 |
else if (TransError == TransMaxInstanceError) { |
| 124 |
ErrorMsg = |
| 125 |
"The counter value exceeded the number of transformation instances!"; |
| 126 |
} |
| 127 |
else if (TransError == TransMaxVarsError) { |
| 128 |
ErrorMsg = "Too many variables!"; |
| 129 |
} |
| 130 |
else if (TransError == TransMaxClassesError) { |
| 131 |
ErrorMsg = "Too many classes!"; |
| 132 |
} |
| 133 |
else if (TransError == TransNoValidVarsError) { |
| 134 |
ErrorMsg = "No variables need to be renamed!"; |
| 135 |
} |
| 136 |
else if (TransError == TransNoValidFunsError) { |
| 137 |
ErrorMsg = "No valid function declarations exist!"; |
| 138 |
} |
| 139 |
else if (TransError == TransNoValidParamsError) { |
| 140 |
ErrorMsg = "No valid parameters declarations exist!"; |
| 141 |
} |
| 142 |
else if (TransError == TransNoTextModificationError) { |
| 143 |
ErrorMsg = "No modification to the transformed program!"; |
| 144 |
} |
| 145 |
else if (TransError == TransToCounterTooBigError) { |
| 146 |
ErrorMsg = |
| 147 |
"The to-counter value exceeded the number of transformation instances!"; |
| 148 |
} |
| 149 |
else { |
| 150 |
TransAssert(0 && "Unknown transformation error!"); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
const Expr * |
| 155 |
Transformation::ignoreSubscriptExprParenCasts(const Expr *E) |
| 156 |
{ |
| 157 |
const Expr *NewE = E->IgnoreParenCasts(); |
| 158 |
const ArraySubscriptExpr *ASE; |
| 159 |
while (true) { |
| 160 |
ASE = dyn_cast<ArraySubscriptExpr>(NewE); |
| 161 |
if (!ASE) |
| 162 |
break; |
| 163 |
NewE = ASE->getBase()->IgnoreParenCasts(); |
| 164 |
} |
| 165 |
TransAssert(NewE && "NULL NewE!"); |
| 166 |
return NewE; |
| 167 |
} |
| 168 |
|
| 169 |
const Expr *Transformation::getInitExprByIndex(IndexVector &Idxs, |
| 170 |
const InitListExpr *ILE) |
| 171 |
{ |
| 172 |
const InitListExpr *SubILE = ILE; |
| 173 |
const Expr *Exp = NULL; |
| 174 |
unsigned int Count = 0; |
| 175 |
for (IndexVector::const_reverse_iterator I = Idxs.rbegin(), |
| 176 |
E = Idxs.rend(); I != E; ++I) { |
| 177 |
Count++; |
| 178 |
unsigned int Idx; |
| 179 |
|
| 180 |
const Type *T = SubILE->getType().getTypePtr(); |
| 181 |
if (T->isUnionType()) |
| 182 |
Idx = 0; |
| 183 |
else |
| 184 |
Idx = (*I); |
| 185 |
|
| 186 |
// Incomplete initialization list |
| 187 |
if (Idx >= SubILE->getNumInits()) |
| 188 |
return NULL; |
| 189 |
|
| 190 |
Exp = SubILE->getInit(Idx); |
| 191 |
TransAssert(Exp && "NULL Exp!"); |
| 192 |
|
| 193 |
SubILE = dyn_cast<InitListExpr>(Exp); |
| 194 |
if (!SubILE) |
| 195 |
break; |
| 196 |
} |
| 197 |
|
| 198 |
TransAssert(Exp && "Exp cannot be NULL"); |
| 199 |
// If array-to-pointer-decay happens, the Count can |
| 200 |
// be different from the size of Idxs. In this case, |
| 201 |
// we just return NULL. |
| 202 |
if (Count == Idxs.size()) |
| 203 |
return Exp; |
| 204 |
else |
| 205 |
return NULL; |
| 206 |
} |
| 207 |
|
| 208 |
const Expr *Transformation::getArrayBaseExprAndIdxs( |
| 209 |
const ArraySubscriptExpr *ASE, IndexVector &Idxs) |
| 210 |
{ |
| 211 |
const Expr *BaseE = NULL; |
| 212 |
while (ASE) { |
| 213 |
const Expr *IdxE = ASE->getIdx(); |
| 214 |
unsigned int Idx = 0; |
| 215 |
Expr::EvalResult Result; |
| 216 |
if (IdxE && IdxE->EvaluateAsInt(Result, *Context)) { |
| 217 |
// this will truncate a possible uint64 value to uint32 value |
| 218 |
llvm::APSInt IVal = Result.Val.getInt(); |
| 219 |
Idx = (unsigned int)(*IVal.getRawData()); |
| 220 |
} |
| 221 |
BaseE = ASE->getBase()->IgnoreParenCasts(); |
| 222 |
ASE = dyn_cast<ArraySubscriptExpr>(BaseE); |
| 223 |
Idxs.push_back(Idx); |
| 224 |
} |
| 225 |
return BaseE; |
| 226 |
} |
| 227 |
|
| 228 |
const Expr *Transformation::getInitExprFromBase(const Expr *BaseE, |
| 229 |
IndexVector &Idxs) |
| 230 |
{ |
| 231 |
TransAssert(BaseE && "Bad Array Base Expression!"); |
| 232 |
const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseE); |
| 233 |
if (!DRE) |
| 234 |
return NULL; |
| 235 |
|
| 236 |
const ValueDecl *OrigDecl = DRE->getDecl(); |
| 237 |
const VarDecl *VD = dyn_cast<VarDecl>(OrigDecl); |
| 238 |
if (!VD) |
| 239 |
return NULL; |
| 240 |
|
| 241 |
const Type * Ty = VD->getType().getTypePtr(); |
| 242 |
if (Ty->isPointerType()) |
| 243 |
return NULL; |
| 244 |
const Expr *InitE = VD->getAnyInitializer(); |
| 245 |
|
| 246 |
if (!InitE) |
| 247 |
return NULL; |
| 248 |
// We don't have routine for CXXConstructExpr |
| 249 |
if (dyn_cast<CXXConstructExpr>(InitE)) |
| 250 |
return NULL; |
| 251 |
|
| 252 |
const InitListExpr *ILE = dyn_cast<InitListExpr>(InitE); |
| 253 |
// not always the case we will have an InitListExpr, e.g., |
| 254 |
// RHS is a struct, or dereferencing a struct pointer, etc |
| 255 |
if (!ILE) |
| 256 |
return NULL; |
| 257 |
return getInitExprByIndex(Idxs, ILE); |
| 258 |
} |
| 259 |
|
| 260 |
const Expr *Transformation::getArraySubscriptElem( |
| 261 |
const ArraySubscriptExpr *ASE) |
| 262 |
{ |
| 263 |
IndexVector ArrayDims; |
| 264 |
const Expr *BaseE = getArrayBaseExprAndIdxs(ASE, ArrayDims); |
| 265 |
return getInitExprFromBase(BaseE, ArrayDims); |
| 266 |
} |
| 267 |
|
| 268 |
const Expr *Transformation::getMemberExprBaseExprAndIdxs( |
| 269 |
const MemberExpr *ME, IndexVector &Idxs) |
| 270 |
{ |
| 271 |
const Expr *BaseE = NULL; |
| 272 |
while (ME) { |
| 273 |
ValueDecl *VD = ME->getMemberDecl(); |
| 274 |
FieldDecl *FD = dyn_cast<FieldDecl>(VD); |
| 275 |
if (!FD) |
| 276 |
return NULL; |
| 277 |
unsigned int Idx = FD->getFieldIndex(); |
| 278 |
Idxs.push_back(Idx); |
| 279 |
|
| 280 |
BaseE = ME->getBase()->IgnoreParenCasts(); |
| 281 |
const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(BaseE); |
| 282 |
if (ASE) { |
| 283 |
BaseE = getArrayBaseExprAndIdxs(ASE, Idxs); |
| 284 |
if (!BaseE) |
| 285 |
return NULL; |
| 286 |
} |
| 287 |
ME = dyn_cast<MemberExpr>(BaseE); |
| 288 |
} |
| 289 |
return BaseE; |
| 290 |
} |
| 291 |
|
| 292 |
bool Transformation::isCXXMemberExpr(const MemberExpr *ME) |
| 293 |
{ |
| 294 |
const ValueDecl *VD = ME->getMemberDecl(); |
| 295 |
|
| 296 |
const FieldDecl *FD = dyn_cast<FieldDecl>(VD); |
| 297 |
// VD can be either CXXMethodDecl, EnumConstantDecl or |
| 298 |
// VarDecl (static data member) |
| 299 |
if (!FD) |
| 300 |
return true; |
| 301 |
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(FD->getParent()); |
| 302 |
if (!CXXRD) |
| 303 |
return false; |
| 304 |
|
| 305 |
return !(CXXRD->isCLike()); |
| 306 |
} |
| 307 |
|
| 308 |
const Expr *Transformation::getMemberExprElem(const MemberExpr *ME) |
| 309 |
{ |
| 310 |
if (isCXXMemberExpr(ME)) |
| 311 |
return NULL; |
| 312 |
|
| 313 |
IndexVector Idxs; |
| 314 |
const Expr *BaseE = getMemberExprBaseExprAndIdxs(ME, Idxs); |
| 315 |
if (!BaseE) |
| 316 |
return NULL; |
| 317 |
return getInitExprFromBase(BaseE, Idxs); |
| 318 |
} |
| 319 |
|
| 320 |
unsigned int Transformation::getArrayDimension(const ArrayType *ArrayTy) |
| 321 |
{ |
| 322 |
unsigned int Dim = 1; |
| 323 |
const Type *ArrayElemTy = ArrayTy->getElementType().getTypePtr(); |
| 324 |
while (ArrayElemTy->isArrayType()) { |
| 325 |
const ArrayType *AT = dyn_cast<ArrayType>(ArrayElemTy); |
| 326 |
ArrayElemTy = AT->getElementType().getTypePtr(); |
| 327 |
Dim++; |
| 328 |
} |
| 329 |
return Dim; |
| 330 |
} |
| 331 |
|
| 332 |
unsigned int Transformation::getArrayDimensionAndTypes( |
| 333 |
const ArrayType *ArrayTy, |
| 334 |
ArraySubTypeVector &TyVec) |
| 335 |
{ |
| 336 |
unsigned int Dim = 1; |
| 337 |
const Type *ArrayElemTy = ArrayTy->getElementType().getTypePtr(); |
| 338 |
TyVec.push_back(ArrayTy); |
| 339 |
while (ArrayElemTy->isArrayType()) { |
| 340 |
const ArrayType *AT = dyn_cast<ArrayType>(ArrayElemTy); |
| 341 |
TyVec.push_back(AT); |
| 342 |
ArrayElemTy = AT->getElementType().getTypePtr(); |
| 343 |
Dim++; |
| 344 |
} |
| 345 |
return Dim; |
| 346 |
} |
| 347 |
|
| 348 |
const Type *Transformation::getArrayBaseElemType(const ArrayType *ArrayTy) |
| 349 |
{ |
| 350 |
return ArrayTy->getBaseElementTypeUnsafe(); |
| 351 |
} |
| 352 |
|
| 353 |
unsigned int Transformation::getConstArraySize( |
| 354 |
const ConstantArrayType *CstArrayTy) |
| 355 |
{ |
| 356 |
unsigned int Sz; |
| 357 |
llvm::APInt Result = CstArrayTy->getSize(); |
| 358 |
|
| 359 |
llvm::SmallString<8> IntStr; |
| 360 |
Result.toStringUnsigned(IntStr); |
| 361 |
|
| 362 |
std::stringstream TmpSS(IntStr.str().str()); |
| 363 |
|
| 364 |
if (!(TmpSS >> Sz)) { |
| 365 |
return UINT_MAX; |
| 366 |
} |
| 367 |
return Sz; |
| 368 |
} |
| 369 |
|
| 370 |
// This is a more complete implementation to deal with mixed |
| 371 |
// array and structs/unions |
| 372 |
const Expr *Transformation::getBaseExprAndIdxs(const Expr *E, |
| 373 |
IndexVector &Idxs) |
| 374 |
{ |
| 375 |
const Expr *BaseE = NULL; |
| 376 |
while (E) { |
| 377 |
E = E->IgnoreParenCasts(); |
| 378 |
BaseE = E; |
| 379 |
|
| 380 |
Expr::StmtClass SC = E->getStmtClass(); |
| 381 |
if (SC == Expr::MemberExprClass) { |
| 382 |
const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 383 |
ValueDecl *VD = ME->getMemberDecl(); |
| 384 |
FieldDecl *FD = dyn_cast<FieldDecl>(VD); |
| 385 |
TransAssert(FD && "Bad FD!\n"); |
| 386 |
|
| 387 |
unsigned int Idx = FD->getFieldIndex(); |
| 388 |
Idxs.push_back(Idx); |
| 389 |
E = ME->getBase(); |
| 390 |
} |
| 391 |
else if (SC == Expr::ArraySubscriptExprClass) { |
| 392 |
const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E); |
| 393 |
const Expr *IdxE = ASE->getIdx(); |
| 394 |
unsigned int Idx = 0; |
| 395 |
Expr::EvalResult Result; |
| 396 |
|
| 397 |
// If we cannot have an integeral index, use 0. |
| 398 |
if (IdxE && IdxE->EvaluateAsInt(Result, *Context)) { |
| 399 |
llvm::APSInt IVal = Result.Val.getInt(); |
| 400 |
llvm::SmallString<8> IntStr; |
| 401 |
IVal.toString(IntStr, 10); |
| 402 |
std::stringstream TmpSS(IntStr.str().str()); |
| 403 |
if (!(TmpSS >> Idx)) |
| 404 |
TransAssert(0 && "Non-integer value!"); |
| 405 |
} |
| 406 |
Idxs.push_back(Idx); |
| 407 |
E = ASE->getBase(); |
| 408 |
} |
| 409 |
else { |
| 410 |
break; |
| 411 |
} |
| 412 |
} |
| 413 |
return BaseE; |
| 414 |
} |
| 415 |
|
| 416 |
const Expr *Transformation::getBaseExprAndIdxExprs( |
| 417 |
const ArraySubscriptExpr *ASE, ExprVector &IdxExprs) |
| 418 |
{ |
| 419 |
const Expr *BaseE = NULL; |
| 420 |
while (ASE) { |
| 421 |
const Expr *IdxE = ASE->getIdx(); |
| 422 |
IdxExprs.push_back(IdxE); |
| 423 |
BaseE = ASE->getBase()->IgnoreParenCasts(); |
| 424 |
ASE = dyn_cast<ArraySubscriptExpr>(BaseE); |
| 425 |
} |
| 426 |
return BaseE; |
| 427 |
} |
| 428 |
|
| 429 |
const Type *Transformation::getBasePointerElemType(const Type *Ty) |
| 430 |
{ |
| 431 |
QualType QT = Ty->getPointeeType(); |
| 432 |
while (!QT.isNull()) { |
| 433 |
Ty = QT.getTypePtr(); |
| 434 |
QT = Ty->getPointeeType(); |
| 435 |
} |
| 436 |
TransAssert(Ty && "NULL Type Ptr!"); |
| 437 |
return Ty; |
| 438 |
} |
| 439 |
|
| 440 |
int Transformation::getIndexAsInteger(const Expr *E) |
| 441 |
{ |
| 442 |
Expr::EvalResult Result; |
| 443 |
if (!E->EvaluateAsInt(Result, *Context)) |
| 444 |
TransAssert(0 && "Failed to Evaluate index!"); |
| 445 |
|
| 446 |
int Idx; |
| 447 |
llvm::APSInt IVal = Result.Val.getInt(); |
| 448 |
Idx = (int)(*IVal.getRawData()); |
| 449 |
return Idx; |
| 450 |
} |
| 451 |
|
| 452 |
const Type* Transformation::getBaseType(const Type *T) |
| 453 |
{ |
| 454 |
if (T->isPointerType() || T->isReferenceType()) |
| 455 |
return getBaseType(T->getPointeeType().getTypePtr()); |
| 456 |
else if (T->isRecordType()) |
| 457 |
T = T->getAs<RecordType>(); |
| 458 |
else if (T->isEnumeralType()) |
| 459 |
T = T->getAs<EnumType>(); |
| 460 |
else if (T->getTypeClass() == Type::Typedef) |
| 461 |
T = T->getAs<TypedefType>(); |
| 462 |
else if (T->isArrayType()) |
| 463 |
return getBaseType(T->castAsArrayTypeUnsafe()-> |
| 464 |
getElementType().getTypePtr()); |
| 465 |
|
| 466 |
return T; |
| 467 |
} |
| 468 |
|
| 469 |
// Lookup a function decl from a top-level DeclContext |
| 470 |
// It's slow... |
| 471 |
const FunctionDecl *Transformation::lookupFunctionDeclInGlobal( |
| 472 |
DeclarationName &DName, const DeclContext *Ctx) |
| 473 |
{ |
| 474 |
DeclContext::lookup_result Result = Ctx->lookup(DName); |
| 475 |
for (auto I = Result.begin(), E = Result.end(); |
| 476 |
I != E; ++I) { |
| 477 |
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 478 |
return FD; |
| 479 |
} |
| 480 |
|
| 481 |
const FunctionTemplateDecl *TD = NULL; |
| 482 |
if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(*I)) { |
| 483 |
TD = dyn_cast<FunctionTemplateDecl>(USD->getTargetDecl()); |
| 484 |
} |
| 485 |
else { |
| 486 |
TD = dyn_cast<FunctionTemplateDecl>(*I); |
| 487 |
} |
| 488 |
if (TD) |
| 489 |
return TD->getTemplatedDecl(); |
| 490 |
} |
| 491 |
|
| 492 |
for (DeclContext::decl_iterator I = Ctx->decls_begin(), |
| 493 |
E = Ctx->decls_end(); I != E; ++I) { |
| 494 |
if (const ClassTemplateDecl *ClassTemplate = |
| 495 |
dyn_cast<ClassTemplateDecl>(*I)) { |
| 496 |
const CXXRecordDecl *CXXRD = ClassTemplate->getTemplatedDecl(); |
| 497 |
if (const FunctionDecl *FD = lookupFunctionDeclInGlobal(DName, CXXRD)) { |
| 498 |
return FD; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
const DeclContext *SubCtx = dyn_cast<DeclContext>(*I); |
| 503 |
if (!SubCtx || dyn_cast<LinkageSpecDecl>(SubCtx)) |
| 504 |
continue; |
| 505 |
|
| 506 |
if (const FunctionDecl *FD = lookupFunctionDeclInGlobal(DName, SubCtx)) { |
| 507 |
return FD; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
return NULL; |
| 512 |
} |
| 513 |
|
| 514 |
const FunctionDecl *Transformation::lookupFunctionDeclFromBases( |
| 515 |
DeclarationName &DName, |
| 516 |
const CXXRecordDecl *CXXRD, |
| 517 |
DeclContextSet &VisitedCtxs) |
| 518 |
{ |
| 519 |
if (!CXXRD->hasDefinition()) { |
| 520 |
return NULL; |
| 521 |
} |
| 522 |
for (CXXRecordDecl::base_class_const_iterator I = |
| 523 |
CXXRD->bases_begin(), E = CXXRD->bases_end(); I != E; ++I) { |
| 524 |
|
| 525 |
const CXXBaseSpecifier *BS = I; |
| 526 |
const Type *Ty = BS->getType().getTypePtr(); |
| 527 |
const CXXRecordDecl *Base = getBaseDeclFromType(Ty); |
| 528 |
// it's not always the case we could resolve a base specifier, e.g. |
| 529 |
// Ty is of DependentName |
| 530 |
if (!Base) |
| 531 |
continue; |
| 532 |
const CXXRecordDecl *BaseDef = Base->getDefinition(); |
| 533 |
if (!BaseDef) |
| 534 |
continue; |
| 535 |
if (const FunctionDecl *FD = |
| 536 |
lookupFunctionDecl(DName, BaseDef, VisitedCtxs)) |
| 537 |
return FD; |
| 538 |
} |
| 539 |
return NULL; |
| 540 |
} |
| 541 |
|
| 542 |
const FunctionDecl *Transformation::lookupFunctionDeclFromCtx( |
| 543 |
DeclarationName &DName, |
| 544 |
const DeclContext *Ctx, |
| 545 |
DeclContextSet &VisitedCtxs) |
| 546 |
{ |
| 547 |
if (dyn_cast<LinkageSpecDecl>(Ctx)) |
| 548 |
return NULL; |
| 549 |
DeclContext::lookup_result Result = Ctx->lookup(DName); |
| 550 |
for (auto I = Result.begin(), E = Result.end(); |
| 551 |
I != E; ++I) { |
| 552 |
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 553 |
return FD; |
| 554 |
} |
| 555 |
|
| 556 |
const FunctionTemplateDecl *TD = NULL; |
| 557 |
if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(*I)) { |
| 558 |
TD = dyn_cast<FunctionTemplateDecl>(USD->getTargetDecl()); |
| 559 |
} |
| 560 |
else { |
| 561 |
TD = dyn_cast<FunctionTemplateDecl>(*I); |
| 562 |
} |
| 563 |
if (TD) |
| 564 |
return TD->getTemplatedDecl(); |
| 565 |
|
| 566 |
if (const UnresolvedUsingValueDecl *UUD = |
| 567 |
dyn_cast<UnresolvedUsingValueDecl>(*I)) { |
| 568 |
const NestedNameSpecifier *NNS = UUD->getQualifier(); |
| 569 |
const DeclContext *Ctx = getDeclContextFromSpecifier(NNS); |
| 570 |
if (!Ctx) |
| 571 |
continue; |
| 572 |
if (const FunctionDecl *FD = |
| 573 |
lookupFunctionDecl(DName, Ctx, VisitedCtxs)) |
| 574 |
return FD; |
| 575 |
} |
| 576 |
} |
| 577 |
return NULL; |
| 578 |
} |
| 579 |
|
| 580 |
const FunctionDecl *Transformation::lookupFunctionDecl( |
| 581 |
DeclarationName &DName, |
| 582 |
const DeclContext *Ctx, |
| 583 |
DeclContextSet &VisitedCtxs) |
| 584 |
{ |
| 585 |
if (dyn_cast<LinkageSpecDecl>(Ctx)) |
| 586 |
return NULL; |
| 587 |
if (VisitedCtxs.count(Ctx)) |
| 588 |
return NULL; |
| 589 |
VisitedCtxs.insert(Ctx); |
| 590 |
|
| 591 |
if (const FunctionDecl *FD = |
| 592 |
lookupFunctionDeclFromCtx(DName, Ctx, VisitedCtxs)) |
| 593 |
return FD; |
| 594 |
|
| 595 |
// lookup base classes: |
| 596 |
// this would be slow and may re-visit some Ctx. |
| 597 |
// Probably we should cache those Ctx(s) which have been visited |
| 598 |
// to speedup lookup |
| 599 |
if (Ctx->isRecord()) { |
| 600 |
const RecordDecl *RD = dyn_cast<RecordDecl>(Ctx); |
| 601 |
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 602 |
if (const FunctionDecl *FD = |
| 603 |
lookupFunctionDeclFromBases(DName, CXXRD, VisitedCtxs)) |
| 604 |
return FD; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
for (auto *I : Ctx->using_directives()) { |
| 609 |
const NamespaceDecl *ND = I->getNominatedNamespace(); |
| 610 |
// avoid infinite recursion |
| 611 |
if (ND->getLookupParent() == Ctx) |
| 612 |
return NULL; |
| 613 |
if (const FunctionDecl *FD = lookupFunctionDecl(DName, ND, VisitedCtxs)) |
| 614 |
return FD; |
| 615 |
} |
| 616 |
|
| 617 |
const DeclContext *ParentCtx = Ctx->getLookupParent(); |
| 618 |
if (!ParentCtx || dyn_cast<LinkageSpecDecl>(ParentCtx)) |
| 619 |
return NULL; |
| 620 |
|
| 621 |
return lookupFunctionDecl(DName, ParentCtx, VisitedCtxs); |
| 622 |
} |
| 623 |
|
| 624 |
const DeclContext *Transformation::getDeclContextFromSpecifier( |
| 625 |
const NestedNameSpecifier *NNS) |
| 626 |
{ |
| 627 |
for (; NNS; NNS = NNS->getPrefix()) { |
| 628 |
NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); |
| 629 |
|
| 630 |
switch (Kind) { |
| 631 |
case NestedNameSpecifier::Namespace: { |
| 632 |
return NNS->getAsNamespace()->getCanonicalDecl(); |
| 633 |
} |
| 634 |
case NestedNameSpecifier::NamespaceAlias: { |
| 635 |
const NamespaceAliasDecl *NAD = NNS->getAsNamespaceAlias(); |
| 636 |
return NAD->getNamespace()->getCanonicalDecl(); |
| 637 |
} |
| 638 |
#if LLVM_VERSION_MAJOR < 21 |
| 639 |
case NestedNameSpecifier::TypeSpecWithTemplate: // Fall-through |
| 640 |
#endif |
| 641 |
case NestedNameSpecifier::TypeSpec: { |
| 642 |
const Type *Ty = NNS->getAsType(); |
| 643 |
if (const RecordType *RT = Ty->getAs<RecordType>()) |
| 644 |
return RT->getDecl(); |
| 645 |
if (const TypedefType *TT = Ty->getAs<TypedefType>()) { |
| 646 |
const TypedefNameDecl *TypeDecl = TT->getDecl(); |
| 647 |
const Type *UnderlyingTy = TypeDecl->getUnderlyingType().getTypePtr(); |
| 648 |
if (const RecordType *RT = UnderlyingTy->getAs<RecordType>()) |
| 649 |
return RT->getDecl(); |
| 650 |
if (const TemplateSpecializationType *TST = |
| 651 |
UnderlyingTy->getAs<TemplateSpecializationType>()) { |
| 652 |
return getBaseDeclFromTemplateSpecializationType(TST); |
| 653 |
} |
| 654 |
} |
| 655 |
break; |
| 656 |
} |
| 657 |
default: |
| 658 |
break; |
| 659 |
} |
| 660 |
} |
| 661 |
return NULL; |
| 662 |
} |
| 663 |
|
| 664 |
bool Transformation::isSpecialRecordDecl(const RecordDecl *RD) |
| 665 |
{ |
| 666 |
std::string Name = RD->getNameAsString(); |
| 667 |
return (Name == "__va_list_tag"); |
| 668 |
} |
| 669 |
|
| 670 |
const CXXRecordDecl *Transformation::getBaseDeclFromTemplateSpecializationType( |
| 671 |
const TemplateSpecializationType *TSTy) |
| 672 |
{ |
| 673 |
TemplateName TplName = TSTy->getTemplateName(); |
| 674 |
TemplateDecl *TplD = TplName.getAsTemplateDecl(); |
| 675 |
TransAssert(TplD && "Invalid TemplateDecl!"); |
| 676 |
if (dyn_cast<TemplateTemplateParmDecl>(TplD)) { |
| 677 |
return NULL; |
| 678 |
} |
| 679 |
NamedDecl *ND = TplD->getTemplatedDecl(); |
| 680 |
TransAssert(ND && "Invalid NamedDecl!"); |
| 681 |
|
| 682 |
if (TypedefNameDecl *TdefD = dyn_cast<TypedefNameDecl>(ND)) { |
| 683 |
const Type *UnderlyingTy = TdefD->getUnderlyingType().getTypePtr(); |
| 684 |
return getBaseDeclFromType(UnderlyingTy); |
| 685 |
} |
| 686 |
|
| 687 |
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(ND); |
| 688 |
TransAssert(CXXRD && "Invalid CXXRD!"); |
| 689 |
if (CXXRD->hasDefinition()) |
| 690 |
return CXXRD->getDefinition(); |
| 691 |
else |
| 692 |
return CXXRD; |
| 693 |
} |
| 694 |
|
| 695 |
// This function could return NULL |
| 696 |
const CXXRecordDecl *Transformation::getBaseDeclFromType(const Type *Ty) |
| 697 |
{ |
| 698 |
const CXXRecordDecl *Base = NULL; |
| 699 |
Type::TypeClass TyClass = Ty->getTypeClass(); |
| 700 |
|
| 701 |
switch (TyClass) { |
| 702 |
case Type::TemplateSpecialization: { |
| 703 |
const TemplateSpecializationType *TSTy = |
| 704 |
dyn_cast<TemplateSpecializationType>(Ty); |
| 705 |
return getBaseDeclFromTemplateSpecializationType(TSTy); |
| 706 |
} |
| 707 |
|
| 708 |
case Type::DependentTemplateSpecialization: { |
| 709 |
return NULL; |
| 710 |
} |
| 711 |
|
| 712 |
case Type::Elaborated: { |
| 713 |
const ElaboratedType *ETy = dyn_cast<ElaboratedType>(Ty); |
| 714 |
const Type *NamedT = ETy->getNamedType().getTypePtr(); |
| 715 |
return getBaseDeclFromType(NamedT); |
| 716 |
} |
| 717 |
|
| 718 |
case Type::Paren: { |
| 719 |
const ParenType *PT = dyn_cast<ParenType>(Ty); |
| 720 |
const Type *InnerTy = PT->getInnerType().getTypePtr(); |
| 721 |
return getBaseDeclFromType(InnerTy); |
| 722 |
} |
| 723 |
|
| 724 |
case Type::Typedef: { |
| 725 |
const TypedefType *TdefTy = dyn_cast<TypedefType>(Ty); |
| 726 |
const TypedefNameDecl *TdefD = TdefTy->getDecl(); |
| 727 |
const Type *UnderlyingTy = TdefD->getUnderlyingType().getTypePtr(); |
| 728 |
return getBaseDeclFromType(UnderlyingTy); |
| 729 |
} |
| 730 |
|
| 731 |
case Type::Decltype: { |
| 732 |
const DecltypeType *DT = dyn_cast<DecltypeType>(Ty); |
| 733 |
const Type *UnderlyingTy = DT->getUnderlyingType().getTypePtr(); |
| 734 |
return getBaseDeclFromType(UnderlyingTy); |
| 735 |
} |
| 736 |
|
| 737 |
case Type::ConstantArray: |
| 738 |
case Type::DependentSizedArray: |
| 739 |
case Type::IncompleteArray: |
| 740 |
case Type::VariableArray: { // fall-through |
| 741 |
const ArrayType *AT = dyn_cast<ArrayType>(Ty); |
| 742 |
const Type *ElemTy = AT->getElementType().getTypePtr(); |
| 743 |
return getBaseDeclFromType(ElemTy); |
| 744 |
} |
| 745 |
|
| 746 |
case Type::MemberPointer: { |
| 747 |
const MemberPointerType *MPT = dyn_cast<MemberPointerType>(Ty); |
| 748 |
const Type *PT = MPT->getPointeeType().getTypePtr(); |
| 749 |
return getBaseDeclFromType(PT); |
| 750 |
} |
| 751 |
|
| 752 |
case Type::Pointer: { |
| 753 |
const PointerType *PT = dyn_cast<PointerType>(Ty); |
| 754 |
const Type *PTy = PT->getPointeeType().getTypePtr(); |
| 755 |
return getBaseDeclFromType(PTy); |
| 756 |
} |
| 757 |
|
| 758 |
case Type::SubstTemplateTypeParm: { |
| 759 |
const SubstTemplateTypeParmType *TP = |
| 760 |
dyn_cast<SubstTemplateTypeParmType>(Ty); |
| 761 |
const Type *ST = TP->getReplacementType().getTypePtr(); |
| 762 |
return getBaseDeclFromType(ST); |
| 763 |
} |
| 764 |
|
| 765 |
case Type::DependentName: { |
| 766 |
// It's not always the case that we could resolve a dependent name type. |
| 767 |
// For example, |
| 768 |
// template<typename T1, typename T2> |
| 769 |
// struct AAA { typedef T2 new_type; }; |
| 770 |
// template<typename T3> |
| 771 |
// struct BBB : public AAA<int, T3>::new_type { }; |
| 772 |
// In the above code, we can't figure out what new_type refers to |
| 773 |
// until BBB is instantiated |
| 774 |
// Due to this reason, simply return NULL from here. |
| 775 |
return NULL; |
| 776 |
} |
| 777 |
|
| 778 |
case Type::TemplateTypeParm: { |
| 779 |
// Yet another case we might not know the base class, e.g., |
| 780 |
// template<typename T1> |
| 781 |
// class AAA { |
| 782 |
// struct BBB : T1 {}; |
| 783 |
// }; |
| 784 |
return NULL; |
| 785 |
} |
| 786 |
|
| 787 |
case Type::Enum: |
| 788 |
case Type::FunctionProto: |
| 789 |
case Type::FunctionNoProto: |
| 790 |
case Type::SubstTemplateTypeParmPack: |
| 791 |
case Type::PackExpansion: |
| 792 |
case Type::Vector: |
| 793 |
case Type::ExtVector: |
| 794 |
case Type::Builtin: // fall-through |
| 795 |
return NULL; |
| 796 |
|
| 797 |
case Type::Auto: { |
| 798 |
const AutoType *AutoTy = dyn_cast<AutoType>(Ty); |
| 799 |
const Type *AT = AutoTy->getDeducedType().getTypePtrOrNull(); |
| 800 |
if (!AT) |
| 801 |
return NULL; |
| 802 |
return getBaseDeclFromType(AT); |
| 803 |
} |
| 804 |
|
| 805 |
case Type::TypeOfExpr: { |
| 806 |
const Expr *E = dyn_cast<TypeOfExprType>(Ty)->getUnderlyingExpr(); |
| 807 |
return getBaseDeclFromType(E->getType().getTypePtr()); |
| 808 |
} |
| 809 |
|
| 810 |
case Type::TypeOf: { |
| 811 |
return getBaseDeclFromType( |
| 812 |
dyn_cast<TypeOfType>(Ty)->getUnmodifiedType().getTypePtr()); |
| 813 |
} |
| 814 |
|
| 815 |
default: |
| 816 |
Base = Ty->getAsCXXRecordDecl(); |
| 817 |
TransAssert(Base && "Bad base class type!"); |
| 818 |
|
| 819 |
// getAsCXXRecordDecl could return a ClassTemplateSpecializationDecl. |
| 820 |
// For example: |
| 821 |
// template <class T> class AAA { }; |
| 822 |
// typedef AAA<int> BBB; |
| 823 |
// class CCC : BBB { }; |
| 824 |
// In the above code, BBB is of type ClassTemplateSpecializationDecl |
| 825 |
if (const ClassTemplateSpecializationDecl *CTSDecl = |
| 826 |
dyn_cast<ClassTemplateSpecializationDecl>(Base)) { |
| 827 |
Base = CTSDecl->getSpecializedTemplate()->getTemplatedDecl(); |
| 828 |
TransAssert(Base && |
| 829 |
"Bad base decl from ClassTemplateSpecializationDecl!"); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
return Base; |
| 834 |
} |
| 835 |
|
| 836 |
bool Transformation::isParameterPack(const NamedDecl *ND) |
| 837 |
{ |
| 838 |
if (const NonTypeTemplateParmDecl *NonTypeD = |
| 839 |
dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 840 |
return NonTypeD->isParameterPack(); |
| 841 |
} |
| 842 |
else if (const TemplateTypeParmDecl *TypeD = |
| 843 |
dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 844 |
return TypeD->isParameterPack(); |
| 845 |
} |
| 846 |
else if (const TemplateTemplateParmDecl *TmplD = |
| 847 |
dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 848 |
return TmplD->isParameterPack(); |
| 849 |
} |
| 850 |
else { |
| 851 |
TransAssert(0 && "Unknown template parameter type!"); |
| 852 |
return false; |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
unsigned Transformation::getNumCtorWrittenInitializers( |
| 857 |
const CXXConstructorDecl &Ctor) |
| 858 |
{ |
| 859 |
unsigned Num = 0; |
| 860 |
for (CXXConstructorDecl::init_const_iterator I = Ctor.init_begin(), |
| 861 |
E = Ctor.init_end(); I != E; ++I) { |
| 862 |
if ((*I)->isWritten()) |
| 863 |
Num++; |
| 864 |
} |
| 865 |
return Num; |
| 866 |
} |
| 867 |
|
| 868 |
bool Transformation::isBeforeColonColon(TypeLoc &Loc) |
| 869 |
{ |
| 870 |
SourceLocation EndLoc = Loc.getEndLoc(); |
| 871 |
SourceLocation ColonColonLoc = |
| 872 |
Lexer::findLocationAfterToken(EndLoc, |
| 873 |
tok::coloncolon, |
| 874 |
*SrcManager, |
| 875 |
Context->getLangOpts(), |
| 876 |
/*SkipTrailingWhitespaceAndNewLine=*/true); |
| 877 |
return ColonColonLoc.isValid(); |
| 878 |
} |
| 879 |
|
| 880 |
bool Transformation::replaceDependentNameString(const Type *Ty, |
| 881 |
ArrayRef<TemplateArgument> Args, |
| 882 |
std::string &Str, |
| 883 |
bool &Typename) |
| 884 |
{ |
| 885 |
TransAssert((Ty->getTypeClass() == Type::DependentName) && |
| 886 |
"Not DependentNameType!"); |
| 887 |
|
| 888 |
const DependentNameType *DNT = dyn_cast<DependentNameType>(Ty); |
| 889 |
const IdentifierInfo *IdInfo = DNT->getIdentifier(); |
| 890 |
if (!IdInfo) |
| 891 |
return false; |
| 892 |
const NestedNameSpecifier *Specifier = DNT->getQualifier(); |
| 893 |
if (!Specifier) |
| 894 |
return false; |
| 895 |
const Type *DependentTy = Specifier->getAsType(); |
| 896 |
if (!DependentTy) |
| 897 |
return false; |
| 898 |
|
| 899 |
const TemplateTypeParmType *ParmTy = |
| 900 |
DependentTy->getAs<TemplateTypeParmType>(); |
| 901 |
if (!ParmTy) |
| 902 |
return false; |
| 903 |
|
| 904 |
unsigned Idx = ParmTy->getIndex(); |
| 905 |
TransAssert((Idx < Args.size()) && "Bad Parm Index!"); |
| 906 |
const TemplateArgument Arg = Args[Idx]; |
| 907 |
if (Arg.getKind() != TemplateArgument::Type) |
| 908 |
return false; |
| 909 |
QualType ArgQT = Arg.getAsType(); |
| 910 |
ArgQT.getAsStringInternal(Str, Context->getPrintingPolicy()); |
| 911 |
Str += "::"; |
| 912 |
Str += IdInfo->getName(); |
| 913 |
Typename = true; |
| 914 |
return true; |
| 915 |
} |
| 916 |
|
| 917 |
bool Transformation::getTemplateTypeParmString( |
| 918 |
const TemplateTypeParmType *ParmTy, |
| 919 |
ArrayRef<TemplateArgument> Args, |
| 920 |
std::string &Str) |
| 921 |
{ |
| 922 |
unsigned Idx = ParmTy->getIndex(); |
| 923 |
// we could have default template args, skip this case for now |
| 924 |
if (Idx >= Args.size()) |
| 925 |
return false; |
| 926 |
const TemplateArgument Arg = Args[Idx]; |
| 927 |
if (Arg.getKind() != TemplateArgument::Type) |
| 928 |
return false; |
| 929 |
QualType ArgQT = Arg.getAsType(); |
| 930 |
ArgQT.getAsStringInternal(Str, Context->getPrintingPolicy()); |
| 931 |
return true; |
| 932 |
} |
| 933 |
|
| 934 |
bool Transformation::getTypedefString(const StringRef &Name, |
| 935 |
const CXXRecordDecl *CXXRD, |
| 936 |
ArrayRef<TemplateArgument> Args, |
| 937 |
std::string &Str, |
| 938 |
bool &Typename) |
| 939 |
{ |
| 940 |
Str = ""; |
| 941 |
for (DeclContext::decl_iterator I = CXXRD->decls_begin(), |
| 942 |
E = CXXRD->decls_end(); I != E; ++I) { |
| 943 |
const TypedefDecl *D = dyn_cast<TypedefDecl>(*I); |
| 944 |
if (!D || (D->getNameAsString() != Name)) |
| 945 |
continue; |
| 946 |
const Type *UnderlyingTy = D->getUnderlyingType().getTypePtr(); |
| 947 |
Type::TypeClass TC = UnderlyingTy->getTypeClass(); |
| 948 |
if (TC == Type::DependentName) { |
| 949 |
if (replaceDependentNameString(UnderlyingTy, Args, |
| 950 |
Str, Typename)) |
| 951 |
return true; |
| 952 |
} |
| 953 |
else if (const TemplateTypeParmType *ParmTy = |
| 954 |
UnderlyingTy->getAs<TemplateTypeParmType>()) { |
| 955 |
if (getTemplateTypeParmString(ParmTy, Args, Str)) |
| 956 |
return true; |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
// check base class |
| 961 |
for (CXXRecordDecl::base_class_const_iterator I = |
| 962 |
CXXRD->bases_begin(), E = CXXRD->bases_end(); I != E; ++I) { |
| 963 |
const CXXBaseSpecifier *BS = I; |
| 964 |
const Type *Ty = BS->getType().getTypePtr(); |
| 965 |
const CXXRecordDecl *Base = getBaseDeclFromType(Ty); |
| 966 |
// it could happen if we have a dependent base specifier |
| 967 |
if (!Base) |
| 968 |
continue; |
| 969 |
const CXXRecordDecl *BaseDef = Base->getDefinition(); |
| 970 |
if (!BaseDef) |
| 971 |
continue; |
| 972 |
if (getTypedefString(Name, BaseDef, Args, Str, Typename)) |
| 973 |
return true; |
| 974 |
} |
| 975 |
|
| 976 |
// TODO: really simplified lookup process, maybe need |
| 977 |
// to check other decl context? |
| 978 |
return false; |
| 979 |
} |
| 980 |
|
| 981 |
bool Transformation::getDependentNameTypeString( |
| 982 |
const DependentNameType *DNT, std::string &Str, bool &Typename) |
| 983 |
{ |
| 984 |
const IdentifierInfo *IdInfo = DNT->getIdentifier(); |
| 985 |
if (!IdInfo) |
| 986 |
return false; |
| 987 |
const NestedNameSpecifier *Specifier = DNT->getQualifier(); |
| 988 |
if (!Specifier) |
| 989 |
return false; |
| 990 |
const Type *Ty = Specifier->getAsType(); |
| 991 |
if (!Ty) |
| 992 |
return false; |
| 993 |
const CXXRecordDecl *Base = getBaseDeclFromType(Ty); |
| 994 |
if (!Base) |
| 995 |
return false; |
| 996 |
const CXXRecordDecl *BaseDef = Base->getDefinition(); |
| 997 |
if (!BaseDef) |
| 998 |
return false; |
| 999 |
|
| 1000 |
ArrayRef<TemplateArgument> Args; |
| 1001 |
if (const TemplateSpecializationType *TST = |
| 1002 |
Ty->getAs<TemplateSpecializationType>()) { |
| 1003 |
Args = TST->template_arguments(); |
| 1004 |
} |
| 1005 |
return getTypedefString(IdInfo->getName(), |
| 1006 |
BaseDef, Args, Str, Typename); |
| 1007 |
} |
| 1008 |
|
| 1009 |
bool Transformation::getTypeString(const QualType &QT, |
| 1010 |
std::string &Str, |
| 1011 |
bool &Typename) |
| 1012 |
{ |
| 1013 |
const Type *Ty = QT.getTypePtr(); |
| 1014 |
Type::TypeClass TC = Ty->getTypeClass(); |
| 1015 |
|
| 1016 |
switch (TC) { |
| 1017 |
case Type::SubstTemplateTypeParm: { |
| 1018 |
const SubstTemplateTypeParmType *TP = |
| 1019 |
dyn_cast<SubstTemplateTypeParmType>(Ty); |
| 1020 |
return getTypeString(TP->getReplacementType(), Str, Typename); |
| 1021 |
} |
| 1022 |
|
| 1023 |
case Type::Elaborated: { |
| 1024 |
const ElaboratedType *ETy = dyn_cast<ElaboratedType>(Ty); |
| 1025 |
return getTypeString(ETy->getNamedType(), Str, Typename); |
| 1026 |
} |
| 1027 |
|
| 1028 |
case Type::Typedef: { |
| 1029 |
const TypedefType *TdefTy = dyn_cast<TypedefType>(Ty); |
| 1030 |
const TypedefNameDecl *TdefD = TdefTy->getDecl(); |
| 1031 |
return getTypeString(TdefD->getUnderlyingType(), Str, Typename); |
| 1032 |
} |
| 1033 |
|
| 1034 |
case Type::DependentName: { |
| 1035 |
const DependentNameType *DNT = dyn_cast<DependentNameType>(Ty); |
| 1036 |
return getDependentNameTypeString(DNT, Str, Typename); |
| 1037 |
} |
| 1038 |
|
| 1039 |
case Type::Record: |
| 1040 |
case Type::Builtin: { // fall-through |
| 1041 |
QT.getAsStringInternal(Str, Context->getPrintingPolicy()); |
| 1042 |
return true; |
| 1043 |
} |
| 1044 |
|
| 1045 |
default: |
| 1046 |
return false; |
| 1047 |
} |
| 1048 |
|
| 1049 |
TransAssert(0 && "Unreachable code!"); |
| 1050 |
return false; |
| 1051 |
} |
| 1052 |
|
| 1053 |
unsigned Transformation::getNumExplicitDecls(const CXXRecordDecl *CXXRD) |
| 1054 |
{ |
| 1055 |
const DeclContext *Ctx = dyn_cast<DeclContext>(CXXRD); |
| 1056 |
TransAssert(Ctx && "Invalid DeclContext!"); |
| 1057 |
|
| 1058 |
unsigned Num = 0; |
| 1059 |
for (DeclContext::decl_iterator I = Ctx->decls_begin(), |
| 1060 |
E = Ctx->decls_end(); I != E; ++I) { |
| 1061 |
if (!(*I)->isImplicit()) |
| 1062 |
Num++; |
| 1063 |
} |
| 1064 |
return Num; |
| 1065 |
} |
| 1066 |
|
| 1067 |
bool Transformation::isInIncludedFile(SourceLocation Loc) const |
| 1068 |
{ |
| 1069 |
Loc = getRealLocation(Loc); |
| 1070 |
return SrcManager->getFileID(Loc) != SrcManager->getMainFileID(); |
| 1071 |
} |
| 1072 |
|
| 1073 |
bool Transformation::isInIncludedFile(const Decl *D) const |
| 1074 |
{ |
| 1075 |
return isInIncludedFile(D->getLocation()); |
| 1076 |
} |
| 1077 |
|
| 1078 |
bool Transformation::isInIncludedFile(const Stmt *S) const |
| 1079 |
{ |
| 1080 |
return isInIncludedFile(S->getBeginLoc()); |
| 1081 |
} |
| 1082 |
|
| 1083 |
SourceLocation Transformation::getRealLocation(SourceLocation Loc) const |
| 1084 |
{ |
| 1085 |
if (Loc.isMacroID()) |
| 1086 |
return SrcManager->getExpansionLoc(Loc); |
| 1087 |
return Loc; |
| 1088 |
} |
| 1089 |
|
| 1090 |
bool Transformation::isDeclaringRecordDecl(const RecordDecl *RD) |
| 1091 |
{ |
| 1092 |
SourceLocation SemiLoc = |
| 1093 |
Lexer::findLocationAfterToken(RD->getSourceRange().getEnd(), |
| 1094 |
tok::semi, |
| 1095 |
*SrcManager, |
| 1096 |
Context->getLangOpts(), |
| 1097 |
/*SkipTrailingWhitespaceAndNewLine=*/true); |
| 1098 |
return SemiLoc.isInvalid(); |
| 1099 |
} |
| 1100 |
|
| 1101 |
Transformation::~Transformation(void) |
| 1102 |
{ |
| 1103 |
RewriteUtils::Finalize(); |
| 1104 |
} |
| 1105 |
|