1 //===--- TypePrinter.cpp - Pretty-Print Clang Types -----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This contains code to print types from Clang's type system. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/Decl.h" 15 #include "clang/AST/DeclObjC.h" 16 #include "clang/AST/DeclTemplate.h" 17 #include "clang/AST/Expr.h" 18 #include "clang/AST/Type.h" 19 #include "clang/AST/PrettyPrinter.h" 20 #include "clang/Basic/LangOptions.h" 21 #include "clang/Basic/SourceManager.h" 22 #include "llvm/ADT/StringExtras.h" 23 #include "llvm/Support/raw_ostream.h" 24 using namespace clang; 25 26 namespace { 27 /// \brief RAII object that enables printing of the ARC __strong lifetime 28 /// qualifier. 29 class IncludeStrongLifetimeRAII { 30 PrintingPolicy &Policy; 31 bool Old; 32 33 public: 34 explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy) 35 : Policy(Policy), Old(Policy.SuppressStrongLifetime) { 36 Policy.SuppressStrongLifetime = false; 37 } 38 39 ~IncludeStrongLifetimeRAII() { 40 Policy.SuppressStrongLifetime = Old; 41 } 42 }; 43 44 class TypePrinter { 45 PrintingPolicy Policy; 46 47 public: 48 explicit TypePrinter(const PrintingPolicy &Policy) : Policy(Policy) { } 49 50 void print(const Type *ty, Qualifiers qs, std::string &buffer); 51 void print(QualType T, std::string &S); 52 void AppendScope(DeclContext *DC, std::string &S); 53 void printTag(TagDecl *T, std::string &S); 54 #define ABSTRACT_TYPE(CLASS, PARENT) 55 #define TYPE(CLASS, PARENT) \ 56 void print##CLASS(const CLASS##Type *T, std::string &S); 57 #include "clang/AST/TypeNodes.def" 58 }; 59 } 60 61 static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { 62 if (TypeQuals & Qualifiers::Const) { 63 if (!S.empty()) S += ' '; 64 S += "const"; 65 } 66 if (TypeQuals & Qualifiers::Volatile) { 67 if (!S.empty()) S += ' '; 68 S += "volatile"; 69 } 70 if (TypeQuals & Qualifiers::Restrict) { 71 if (!S.empty()) S += ' '; 72 S += "restrict"; 73 } 74 } 75 76 void TypePrinter::print(QualType t, std::string &buffer) { 77 SplitQualType split = t.split(); 78 print(split.first, split.second, buffer); 79 } 80 81 void TypePrinter::print(const Type *T, Qualifiers Quals, std::string &buffer) { 82 if (!T) { 83 buffer += "NULL TYPE"; 84 return; 85 } 86 87 if (Policy.SuppressSpecifiers && T->isSpecifierType()) 88 return; 89 90 // Print qualifiers as appropriate. 91 92 // CanPrefixQualifiers - We prefer to print type qualifiers before the type, 93 // so that we get "const int" instead of "int const", but we can't do this if 94 // the type is complex. For example if the type is "int*", we *must* print 95 // "int * const", printing "const int *" is different. Only do this when the 96 // type expands to a simple string. 97 bool CanPrefixQualifiers = false; 98 bool NeedARCStrongQualifier = false; 99 Type::TypeClass TC = T->getTypeClass(); 100 if (const AutoType *AT = dyn_cast<AutoType>(T)) 101 TC = AT->desugar()->getTypeClass(); 102 if (const SubstTemplateTypeParmType *Subst 103 = dyn_cast<SubstTemplateTypeParmType>(T)) 104 TC = Subst->getReplacementType()->getTypeClass(); 105 106 switch (TC) { 107 case Type::Builtin: 108 case Type::Complex: 109 case Type::UnresolvedUsing: 110 case Type::Typedef: 111 case Type::TypeOfExpr: 112 case Type::TypeOf: 113 case Type::Decltype: 114 case Type::UnaryTransform: 115 case Type::Record: 116 case Type::Enum: 117 case Type::Elaborated: 118 case Type::TemplateTypeParm: 119 case Type::SubstTemplateTypeParmPack: 120 case Type::TemplateSpecialization: 121 case Type::InjectedClassName: 122 case Type::DependentName: 123 case Type::DependentTemplateSpecialization: 124 case Type::ObjCObject: 125 case Type::ObjCInterface: 126 case Type::Atomic: 127 CanPrefixQualifiers = true; 128 break; 129 130 case Type::ObjCObjectPointer: 131 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() || 132 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType(); 133 break; 134 135 case Type::ConstantArray: 136 case Type::IncompleteArray: 137 case Type::VariableArray: 138 case Type::DependentSizedArray: 139 NeedARCStrongQualifier = true; 140 // Fall through 141 142 case Type::Pointer: 143 case Type::BlockPointer: 144 case Type::LValueReference: 145 case Type::RValueReference: 146 case Type::MemberPointer: 147 case Type::DependentSizedExtVector: 148 case Type::Vector: 149 case Type::ExtVector: 150 case Type::FunctionProto: 151 case Type::FunctionNoProto: 152 case Type::Paren: 153 case Type::Attributed: 154 case Type::PackExpansion: 155 case Type::SubstTemplateTypeParm: 156 case Type::Auto: 157 CanPrefixQualifiers = false; 158 break; 159 } 160 161 if (!CanPrefixQualifiers && !Quals.empty()) { 162 std::string qualsBuffer; 163 if (NeedARCStrongQualifier) { 164 IncludeStrongLifetimeRAII Strong(Policy); 165 Quals.getAsStringInternal(qualsBuffer, Policy); 166 } else { 167 Quals.getAsStringInternal(qualsBuffer, Policy); 168 } 169 170 if (!qualsBuffer.empty()) { 171 if (!buffer.empty()) { 172 qualsBuffer += ' '; 173 qualsBuffer += buffer; 174 } 175 std::swap(buffer, qualsBuffer); 176 } 177 } 178 179 switch (T->getTypeClass()) { 180 #define ABSTRACT_TYPE(CLASS, PARENT) 181 #define TYPE(CLASS, PARENT) case Type::CLASS: \ 182 print##CLASS(cast<CLASS##Type>(T), buffer); \ 183 break; 184 #include "clang/AST/TypeNodes.def" 185 } 186 187 // If we're adding the qualifiers as a prefix, do it now. 188 if (CanPrefixQualifiers && !Quals.empty()) { 189 std::string qualsBuffer; 190 if (NeedARCStrongQualifier) { 191 IncludeStrongLifetimeRAII Strong(Policy); 192 Quals.getAsStringInternal(qualsBuffer, Policy); 193 } else { 194 Quals.getAsStringInternal(qualsBuffer, Policy); 195 } 196 197 if (!qualsBuffer.empty()) { 198 if (!buffer.empty()) { 199 qualsBuffer += ' '; 200 qualsBuffer += buffer; 201 } 202 std::swap(buffer, qualsBuffer); 203 } 204 } 205 } 206 207 void TypePrinter::printBuiltin(const BuiltinType *T, std::string &S) { 208 if (S.empty()) { 209 S = T->getName(Policy); 210 } else { 211 // Prefix the basic type, e.g. 'int X'. 212 S = ' ' + S; 213 S = T->getName(Policy) + S; 214 } 215 } 216 217 void TypePrinter::printComplex(const ComplexType *T, std::string &S) { 218 print(T->getElementType(), S); 219 S = "_Complex " + S; 220 } 221 222 void TypePrinter::printPointer(const PointerType *T, std::string &S) { 223 S = '*' + S; 224 225 // Handle things like 'int (*A)[4];' correctly. 226 // FIXME: this should include vectors, but vectors use attributes I guess. 227 if (isa<ArrayType>(T->getPointeeType())) 228 S = '(' + S + ')'; 229 230 IncludeStrongLifetimeRAII Strong(Policy); 231 print(T->getPointeeType(), S); 232 } 233 234 void TypePrinter::printBlockPointer(const BlockPointerType *T, std::string &S) { 235 S = '^' + S; 236 print(T->getPointeeType(), S); 237 } 238 239 void TypePrinter::printLValueReference(const LValueReferenceType *T, 240 std::string &S) { 241 S = '&' + S; 242 243 // Handle things like 'int (&A)[4];' correctly. 244 // FIXME: this should include vectors, but vectors use attributes I guess. 245 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 246 S = '(' + S + ')'; 247 248 IncludeStrongLifetimeRAII Strong(Policy); 249 print(T->getPointeeTypeAsWritten(), S); 250 } 251 252 void TypePrinter::printRValueReference(const RValueReferenceType *T, 253 std::string &S) { 254 S = "&&" + S; 255 256 // Handle things like 'int (&&A)[4];' correctly. 257 // FIXME: this should include vectors, but vectors use attributes I guess. 258 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 259 S = '(' + S + ')'; 260 261 IncludeStrongLifetimeRAII Strong(Policy); 262 print(T->getPointeeTypeAsWritten(), S); 263 } 264 265 void TypePrinter::printMemberPointer(const MemberPointerType *T, 266 std::string &S) { 267 std::string C; 268 print(QualType(T->getClass(), 0), C); 269 C += "::*"; 270 S = C + S; 271 272 // Handle things like 'int (Cls::*A)[4];' correctly. 273 // FIXME: this should include vectors, but vectors use attributes I guess. 274 if (isa<ArrayType>(T->getPointeeType())) 275 S = '(' + S + ')'; 276 277 IncludeStrongLifetimeRAII Strong(Policy); 278 print(T->getPointeeType(), S); 279 } 280 281 void TypePrinter::printConstantArray(const ConstantArrayType *T, 282 std::string &S) { 283 S += '['; 284 S += llvm::utostr(T->getSize().getZExtValue()); 285 S += ']'; 286 287 IncludeStrongLifetimeRAII Strong(Policy); 288 print(T->getElementType(), S); 289 } 290 291 void TypePrinter::printIncompleteArray(const IncompleteArrayType *T, 292 std::string &S) { 293 S += "[]"; 294 IncludeStrongLifetimeRAII Strong(Policy); 295 print(T->getElementType(), S); 296 } 297 298 void TypePrinter::printVariableArray(const VariableArrayType *T, 299 std::string &S) { 300 S += '['; 301 302 if (T->getIndexTypeQualifiers().hasQualifiers()) { 303 AppendTypeQualList(S, T->getIndexTypeCVRQualifiers()); 304 S += ' '; 305 } 306 307 if (T->getSizeModifier() == VariableArrayType::Static) 308 S += "static"; 309 else if (T->getSizeModifier() == VariableArrayType::Star) 310 S += '*'; 311 312 if (T->getSizeExpr()) { 313 std::string SStr; 314 llvm::raw_string_ostream s(SStr); 315 T->getSizeExpr()->printPretty(s, 0, Policy); 316 S += s.str(); 317 } 318 S += ']'; 319 320 IncludeStrongLifetimeRAII Strong(Policy); 321 print(T->getElementType(), S); 322 } 323 324 void TypePrinter::printDependentSizedArray(const DependentSizedArrayType *T, 325 std::string &S) { 326 S += '['; 327 328 if (T->getSizeExpr()) { 329 std::string SStr; 330 llvm::raw_string_ostream s(SStr); 331 T->getSizeExpr()->printPretty(s, 0, Policy); 332 S += s.str(); 333 } 334 S += ']'; 335 336 IncludeStrongLifetimeRAII Strong(Policy); 337 print(T->getElementType(), S); 338 } 339 340 void TypePrinter::printDependentSizedExtVector( 341 const DependentSizedExtVectorType *T, 342 std::string &S) { 343 print(T->getElementType(), S); 344 345 S += " __attribute__((ext_vector_type("; 346 if (T->getSizeExpr()) { 347 std::string SStr; 348 llvm::raw_string_ostream s(SStr); 349 T->getSizeExpr()->printPretty(s, 0, Policy); 350 S += s.str(); 351 } 352 S += ")))"; 353 } 354 355 void TypePrinter::printVector(const VectorType *T, std::string &S) { 356 switch (T->getVectorKind()) { 357 case VectorType::AltiVecPixel: 358 S = "__vector __pixel " + S; 359 break; 360 case VectorType::AltiVecBool: 361 print(T->getElementType(), S); 362 S = "__vector __bool " + S; 363 break; 364 case VectorType::AltiVecVector: 365 print(T->getElementType(), S); 366 S = "__vector " + S; 367 break; 368 case VectorType::NeonVector: 369 print(T->getElementType(), S); 370 S = ("__attribute__((neon_vector_type(" + 371 llvm::utostr_32(T->getNumElements()) + "))) " + S); 372 break; 373 case VectorType::NeonPolyVector: 374 print(T->getElementType(), S); 375 S = ("__attribute__((neon_polyvector_type(" + 376 llvm::utostr_32(T->getNumElements()) + "))) " + S); 377 break; 378 case VectorType::GenericVector: { 379 // FIXME: We prefer to print the size directly here, but have no way 380 // to get the size of the type. 381 print(T->getElementType(), S); 382 std::string V = "__attribute__((__vector_size__("; 383 V += llvm::utostr_32(T->getNumElements()); // convert back to bytes. 384 std::string ET; 385 print(T->getElementType(), ET); 386 V += " * sizeof(" + ET + ")))) "; 387 S = V + S; 388 break; 389 } 390 } 391 } 392 393 void TypePrinter::printExtVector(const ExtVectorType *T, std::string &S) { 394 S += " __attribute__((ext_vector_type("; 395 S += llvm::utostr_32(T->getNumElements()); 396 S += ")))"; 397 print(T->getElementType(), S); 398 } 399 400 void TypePrinter::printFunctionProto(const FunctionProtoType *T, 401 std::string &S) { 402 // If needed for precedence reasons, wrap the inner part in grouping parens. 403 if (!S.empty()) 404 S = "(" + S + ")"; 405 406 S += "("; 407 std::string Tmp; 408 PrintingPolicy ParamPolicy(Policy); 409 ParamPolicy.SuppressSpecifiers = false; 410 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) { 411 if (i) S += ", "; 412 print(T->getArgType(i), Tmp); 413 S += Tmp; 414 Tmp.clear(); 415 } 416 417 if (T->isVariadic()) { 418 if (T->getNumArgs()) 419 S += ", "; 420 S += "..."; 421 } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) { 422 // Do not emit int() if we have a proto, emit 'int(void)'. 423 S += "void"; 424 } 425 426 S += ")"; 427 428 FunctionType::ExtInfo Info = T->getExtInfo(); 429 switch(Info.getCC()) { 430 case CC_Default: 431 default: break; 432 case CC_C: 433 S += " __attribute__((cdecl))"; 434 break; 435 case CC_X86StdCall: 436 S += " __attribute__((stdcall))"; 437 break; 438 case CC_X86FastCall: 439 S += " __attribute__((fastcall))"; 440 break; 441 case CC_X86ThisCall: 442 S += " __attribute__((thiscall))"; 443 break; 444 case CC_X86Pascal: 445 S += " __attribute__((pascal))"; 446 break; 447 case CC_AAPCS: 448 S += " __attribute__((pcs(\"aapcs\")))"; 449 break; 450 case CC_AAPCS_VFP: 451 S += " __attribute__((pcs(\"aapcs-vfp\")))"; 452 break; 453 } 454 if (Info.getNoReturn()) 455 S += " __attribute__((noreturn))"; 456 if (Info.getRegParm()) 457 S += " __attribute__((regparm (" + 458 llvm::utostr_32(Info.getRegParm()) + ")))"; 459 460 AppendTypeQualList(S, T->getTypeQuals()); 461 462 switch (T->getRefQualifier()) { 463 case RQ_None: 464 break; 465 466 case RQ_LValue: 467 S += " &"; 468 break; 469 470 case RQ_RValue: 471 S += " &&"; 472 break; 473 } 474 475 if (T->hasDynamicExceptionSpec()) { 476 S += " throw("; 477 if (T->getExceptionSpecType() == EST_MSAny) 478 S += "..."; 479 else 480 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) { 481 if (I) 482 S += ", "; 483 484 std::string ExceptionType; 485 print(T->getExceptionType(I), ExceptionType); 486 S += ExceptionType; 487 } 488 S += ")"; 489 } else if (isNoexceptExceptionSpec(T->getExceptionSpecType())) { 490 S += " noexcept"; 491 if (T->getExceptionSpecType() == EST_ComputedNoexcept) { 492 S += "("; 493 llvm::raw_string_ostream EOut(S); 494 T->getNoexceptExpr()->printPretty(EOut, 0, Policy); 495 EOut.flush(); 496 S += EOut.str(); 497 S += ")"; 498 } 499 } 500 501 print(T->getResultType(), S); 502 } 503 504 void TypePrinter::printFunctionNoProto(const FunctionNoProtoType *T, 505 std::string &S) { 506 // If needed for precedence reasons, wrap the inner part in grouping parens. 507 if (!S.empty()) 508 S = "(" + S + ")"; 509 510 S += "()"; 511 if (T->getNoReturnAttr()) 512 S += " __attribute__((noreturn))"; 513 print(T->getResultType(), S); 514 } 515 516 static void printTypeSpec(const NamedDecl *D, std::string &S) { 517 IdentifierInfo *II = D->getIdentifier(); 518 if (S.empty()) 519 S = II->getName().str(); 520 else 521 S = II->getName().str() + ' ' + S; 522 } 523 524 void TypePrinter::printUnresolvedUsing(const UnresolvedUsingType *T, 525 std::string &S) { 526 printTypeSpec(T->getDecl(), S); 527 } 528 529 void TypePrinter::printTypedef(const TypedefType *T, std::string &S) { 530 printTypeSpec(T->getDecl(), S); 531 } 532 533 void TypePrinter::printTypeOfExpr(const TypeOfExprType *T, std::string &S) { 534 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. 535 S = ' ' + S; 536 std::string Str; 537 llvm::raw_string_ostream s(Str); 538 T->getUnderlyingExpr()->printPretty(s, 0, Policy); 539 S = "typeof " + s.str() + S; 540 } 541 542 void TypePrinter::printTypeOf(const TypeOfType *T, std::string &S) { 543 if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. 544 S = ' ' + S; 545 std::string Tmp; 546 print(T->getUnderlyingType(), Tmp); 547 S = "typeof(" + Tmp + ")" + S; 548 } 549 550 void TypePrinter::printDecltype(const DecltypeType *T, std::string &S) { 551 if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'. 552 S = ' ' + S; 553 std::string Str; 554 llvm::raw_string_ostream s(Str); 555 T->getUnderlyingExpr()->printPretty(s, 0, Policy); 556 S = "decltype(" + s.str() + ")" + S; 557 } 558 559 void TypePrinter::printUnaryTransform(const UnaryTransformType *T, 560 std::string &S) { 561 if (!S.empty()) 562 S = ' ' + S; 563 std::string Str; 564 IncludeStrongLifetimeRAII Strong(Policy); 565 print(T->getBaseType(), Str); 566 567 switch (T->getUTTKind()) { 568 case UnaryTransformType::EnumUnderlyingType: 569 S = "__underlying_type(" + Str + ")" + S; 570 break; 571 } 572 } 573 574 void TypePrinter::printAuto(const AutoType *T, std::string &S) { 575 // If the type has been deduced, do not print 'auto'. 576 if (T->isDeduced()) { 577 print(T->getDeducedType(), S); 578 } else { 579 if (!S.empty()) // Prefix the basic type, e.g. 'auto X'. 580 S = ' ' + S; 581 S = "auto" + S; 582 } 583 } 584 585 void TypePrinter::printAtomic(const AtomicType *T, std::string &S) { 586 if (!S.empty()) 587 S = ' ' + S; 588 std::string Str; 589 IncludeStrongLifetimeRAII Strong(Policy); 590 print(T->getValueType(), Str); 591 592 S = "_Atomic(" + Str + ")" + S; 593 } 594 595 /// Appends the given scope to the end of a string. 596 void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) { 597 if (DC->isTranslationUnit()) return; 598 AppendScope(DC->getParent(), Buffer); 599 600 unsigned OldSize = Buffer.size(); 601 602 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 603 if (NS->getIdentifier()) 604 Buffer += NS->getNameAsString(); 605 else 606 Buffer += "<anonymous>"; 607 } else if (ClassTemplateSpecializationDecl *Spec 608 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 609 IncludeStrongLifetimeRAII Strong(Policy); 610 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 611 std::string TemplateArgsStr 612 = TemplateSpecializationType::PrintTemplateArgumentList( 613 TemplateArgs.data(), 614 TemplateArgs.size(), 615 Policy); 616 Buffer += Spec->getIdentifier()->getName(); 617 Buffer += TemplateArgsStr; 618 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 619 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl()) 620 Buffer += Typedef->getIdentifier()->getName(); 621 else if (Tag->getIdentifier()) 622 Buffer += Tag->getIdentifier()->getName(); 623 } 624 625 if (Buffer.size() != OldSize) 626 Buffer += "::"; 627 } 628 629 void TypePrinter::printTag(TagDecl *D, std::string &InnerString) { 630 if (Policy.SuppressTag) 631 return; 632 633 std::string Buffer; 634 bool HasKindDecoration = false; 635 636 // bool SuppressTagKeyword 637 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword; 638 639 // We don't print tags unless this is an elaborated type. 640 // In C, we just assume every RecordType is an elaborated type. 641 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword || 642 D->getTypedefNameForAnonDecl())) { 643 HasKindDecoration = true; 644 Buffer += D->getKindName(); 645 Buffer += ' '; 646 } 647 648 // Compute the full nested-name-specifier for this type. 649 // In C, this will always be empty except when the type 650 // being printed is anonymous within other Record. 651 if (!Policy.SuppressScope) 652 AppendScope(D->getDeclContext(), Buffer); 653 654 if (const IdentifierInfo *II = D->getIdentifier()) 655 Buffer += II->getNameStart(); 656 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) { 657 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 658 Buffer += Typedef->getIdentifier()->getNameStart(); 659 } else { 660 // Make an unambiguous representation for anonymous types, e.g. 661 // <anonymous enum at /usr/include/string.h:120:9> 662 llvm::raw_string_ostream OS(Buffer); 663 OS << "<anonymous"; 664 665 if (Policy.AnonymousTagLocations) { 666 // Suppress the redundant tag keyword if we just printed one. 667 // We don't have to worry about ElaboratedTypes here because you can't 668 // refer to an anonymous type with one. 669 if (!HasKindDecoration) 670 OS << " " << D->getKindName(); 671 672 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( 673 D->getLocation()); 674 if (PLoc.isValid()) { 675 OS << " at " << PLoc.getFilename() 676 << ':' << PLoc.getLine() 677 << ':' << PLoc.getColumn(); 678 } 679 } 680 681 OS << '>'; 682 } 683 684 // If this is a class template specialization, print the template 685 // arguments. 686 if (ClassTemplateSpecializationDecl *Spec 687 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 688 const TemplateArgument *Args; 689 unsigned NumArgs; 690 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { 691 const TemplateSpecializationType *TST = 692 cast<TemplateSpecializationType>(TAW->getType()); 693 Args = TST->getArgs(); 694 NumArgs = TST->getNumArgs(); 695 } else { 696 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 697 Args = TemplateArgs.data(); 698 NumArgs = TemplateArgs.size(); 699 } 700 IncludeStrongLifetimeRAII Strong(Policy); 701 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args, 702 NumArgs, 703 Policy); 704 } 705 706 if (!InnerString.empty()) { 707 Buffer += ' '; 708 Buffer += InnerString; 709 } 710 711 std::swap(Buffer, InnerString); 712 } 713 714 void TypePrinter::printRecord(const RecordType *T, std::string &S) { 715 printTag(T->getDecl(), S); 716 } 717 718 void TypePrinter::printEnum(const EnumType *T, std::string &S) { 719 printTag(T->getDecl(), S); 720 } 721 722 void TypePrinter::printTemplateTypeParm(const TemplateTypeParmType *T, 723 std::string &S) { 724 if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'. 725 S = ' ' + S; 726 727 if (IdentifierInfo *Id = T->getIdentifier()) 728 S = Id->getName().str() + S; 729 else 730 S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' + 731 llvm::utostr_32(T->getIndex()) + S; 732 } 733 734 void TypePrinter::printSubstTemplateTypeParm(const SubstTemplateTypeParmType *T, 735 std::string &S) { 736 IncludeStrongLifetimeRAII Strong(Policy); 737 print(T->getReplacementType(), S); 738 } 739 740 void TypePrinter::printSubstTemplateTypeParmPack( 741 const SubstTemplateTypeParmPackType *T, 742 std::string &S) { 743 IncludeStrongLifetimeRAII Strong(Policy); 744 printTemplateTypeParm(T->getReplacedParameter(), S); 745 } 746 747 void TypePrinter::printTemplateSpecialization( 748 const TemplateSpecializationType *T, 749 std::string &S) { 750 IncludeStrongLifetimeRAII Strong(Policy); 751 std::string SpecString; 752 753 { 754 llvm::raw_string_ostream OS(SpecString); 755 T->getTemplateName().print(OS, Policy); 756 } 757 758 SpecString += TemplateSpecializationType::PrintTemplateArgumentList( 759 T->getArgs(), 760 T->getNumArgs(), 761 Policy); 762 if (S.empty()) 763 S.swap(SpecString); 764 else 765 S = SpecString + ' ' + S; 766 } 767 768 void TypePrinter::printInjectedClassName(const InjectedClassNameType *T, 769 std::string &S) { 770 printTemplateSpecialization(T->getInjectedTST(), S); 771 } 772 773 void TypePrinter::printElaborated(const ElaboratedType *T, std::string &S) { 774 std::string MyString; 775 776 { 777 llvm::raw_string_ostream OS(MyString); 778 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 779 if (T->getKeyword() != ETK_None) 780 OS << " "; 781 NestedNameSpecifier* Qualifier = T->getQualifier(); 782 if (Qualifier) 783 Qualifier->print(OS, Policy); 784 } 785 786 std::string TypeStr; 787 PrintingPolicy InnerPolicy(Policy); 788 InnerPolicy.SuppressTagKeyword = true; 789 InnerPolicy.SuppressScope = true; 790 TypePrinter(InnerPolicy).print(T->getNamedType(), TypeStr); 791 792 MyString += TypeStr; 793 if (S.empty()) 794 S.swap(MyString); 795 else 796 S = MyString + ' ' + S; 797 } 798 799 void TypePrinter::printParen(const ParenType *T, std::string &S) { 800 if (!S.empty() && !isa<FunctionType>(T->getInnerType())) 801 S = '(' + S + ')'; 802 print(T->getInnerType(), S); 803 } 804 805 void TypePrinter::printDependentName(const DependentNameType *T, std::string &S) { 806 std::string MyString; 807 808 { 809 llvm::raw_string_ostream OS(MyString); 810 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 811 if (T->getKeyword() != ETK_None) 812 OS << " "; 813 814 T->getQualifier()->print(OS, Policy); 815 816 OS << T->getIdentifier()->getName(); 817 } 818 819 if (S.empty()) 820 S.swap(MyString); 821 else 822 S = MyString + ' ' + S; 823 } 824 825 void TypePrinter::printDependentTemplateSpecialization( 826 const DependentTemplateSpecializationType *T, std::string &S) { 827 IncludeStrongLifetimeRAII Strong(Policy); 828 std::string MyString; 829 { 830 llvm::raw_string_ostream OS(MyString); 831 832 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 833 if (T->getKeyword() != ETK_None) 834 OS << " "; 835 836 if (T->getQualifier()) 837 T->getQualifier()->print(OS, Policy); 838 OS << T->getIdentifier()->getName(); 839 OS << TemplateSpecializationType::PrintTemplateArgumentList( 840 T->getArgs(), 841 T->getNumArgs(), 842 Policy); 843 } 844 845 if (S.empty()) 846 S.swap(MyString); 847 else 848 S = MyString + ' ' + S; 849 } 850 851 void TypePrinter::printPackExpansion(const PackExpansionType *T, 852 std::string &S) { 853 print(T->getPattern(), S); 854 S += "..."; 855 } 856 857 void TypePrinter::printAttributed(const AttributedType *T, 858 std::string &S) { 859 // Prefer the macro forms of the GC and ownership qualifiers. 860 if (T->getAttrKind() == AttributedType::attr_objc_gc || 861 T->getAttrKind() == AttributedType::attr_objc_ownership) 862 return print(T->getEquivalentType(), S); 863 864 print(T->getModifiedType(), S); 865 866 // TODO: not all attributes are GCC-style attributes. 867 S += " __attribute__(("; 868 switch (T->getAttrKind()) { 869 case AttributedType::attr_address_space: 870 S += "address_space("; 871 S += T->getEquivalentType().getAddressSpace(); 872 S += ")"; 873 break; 874 875 case AttributedType::attr_vector_size: { 876 S += "__vector_size__("; 877 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) { 878 S += vector->getNumElements(); 879 S += " * sizeof("; 880 881 std::string tmp; 882 print(vector->getElementType(), tmp); 883 S += tmp; 884 S += ")"; 885 } 886 S += ")"; 887 break; 888 } 889 890 case AttributedType::attr_neon_vector_type: 891 case AttributedType::attr_neon_polyvector_type: { 892 if (T->getAttrKind() == AttributedType::attr_neon_vector_type) 893 S += "neon_vector_type("; 894 else 895 S += "neon_polyvector_type("; 896 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>(); 897 S += llvm::utostr_32(vector->getNumElements()); 898 S += ")"; 899 break; 900 } 901 902 case AttributedType::attr_regparm: { 903 S += "regparm("; 904 QualType t = T->getEquivalentType(); 905 while (!t->isFunctionType()) 906 t = t->getPointeeType(); 907 S += t->getAs<FunctionType>()->getRegParmType(); 908 S += ")"; 909 break; 910 } 911 912 case AttributedType::attr_objc_gc: { 913 S += "objc_gc("; 914 915 QualType tmp = T->getEquivalentType(); 916 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) { 917 QualType next = tmp->getPointeeType(); 918 if (next == tmp) break; 919 tmp = next; 920 } 921 922 if (tmp.isObjCGCWeak()) 923 S += "weak"; 924 else 925 S += "strong"; 926 S += ")"; 927 break; 928 } 929 930 case AttributedType::attr_objc_ownership: 931 S += "objc_ownership("; 932 switch (T->getEquivalentType().getObjCLifetime()) { 933 case Qualifiers::OCL_None: llvm_unreachable("no ownership!"); break; 934 case Qualifiers::OCL_ExplicitNone: S += "none"; break; 935 case Qualifiers::OCL_Strong: S += "strong"; break; 936 case Qualifiers::OCL_Weak: S += "weak"; break; 937 case Qualifiers::OCL_Autoreleasing: S += "autoreleasing"; break; 938 } 939 S += ")"; 940 break; 941 942 case AttributedType::attr_noreturn: S += "noreturn"; break; 943 case AttributedType::attr_cdecl: S += "cdecl"; break; 944 case AttributedType::attr_fastcall: S += "fastcall"; break; 945 case AttributedType::attr_stdcall: S += "stdcall"; break; 946 case AttributedType::attr_thiscall: S += "thiscall"; break; 947 case AttributedType::attr_pascal: S += "pascal"; break; 948 case AttributedType::attr_pcs: { 949 S += "pcs("; 950 QualType t = T->getEquivalentType(); 951 while (!t->isFunctionType()) 952 t = t->getPointeeType(); 953 S += (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ? 954 "\"aapcs\"" : "\"aapcs-vfp\""); 955 S += ")"; 956 break; 957 } 958 } 959 S += "))"; 960 } 961 962 void TypePrinter::printObjCInterface(const ObjCInterfaceType *T, 963 std::string &S) { 964 if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. 965 S = ' ' + S; 966 967 std::string ObjCQIString = T->getDecl()->getNameAsString(); 968 S = ObjCQIString + S; 969 } 970 971 void TypePrinter::printObjCObject(const ObjCObjectType *T, 972 std::string &S) { 973 if (T->qual_empty()) 974 return print(T->getBaseType(), S); 975 976 std::string tmp; 977 print(T->getBaseType(), tmp); 978 tmp += '<'; 979 bool isFirst = true; 980 for (ObjCObjectType::qual_iterator 981 I = T->qual_begin(), E = T->qual_end(); I != E; ++I) { 982 if (isFirst) 983 isFirst = false; 984 else 985 tmp += ','; 986 tmp += (*I)->getNameAsString(); 987 } 988 tmp += '>'; 989 990 if (!S.empty()) { 991 tmp += ' '; 992 tmp += S; 993 } 994 std::swap(tmp, S); 995 } 996 997 void TypePrinter::printObjCObjectPointer(const ObjCObjectPointerType *T, 998 std::string &S) { 999 std::string ObjCQIString; 1000 1001 T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString, 1002 Policy); 1003 if (!ObjCQIString.empty()) 1004 ObjCQIString += ' '; 1005 1006 if (T->isObjCIdType() || T->isObjCQualifiedIdType()) 1007 ObjCQIString += "id"; 1008 else if (T->isObjCClassType() || T->isObjCQualifiedClassType()) 1009 ObjCQIString += "Class"; 1010 else if (T->isObjCSelType()) 1011 ObjCQIString += "SEL"; 1012 else 1013 ObjCQIString += T->getInterfaceDecl()->getNameAsString(); 1014 1015 if (!T->qual_empty()) { 1016 ObjCQIString += '<'; 1017 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(), 1018 E = T->qual_end(); 1019 I != E; ++I) { 1020 ObjCQIString += (*I)->getNameAsString(); 1021 if (I+1 != E) 1022 ObjCQIString += ','; 1023 } 1024 ObjCQIString += '>'; 1025 } 1026 1027 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType()) 1028 ObjCQIString += " *"; // Don't forget the implicit pointer. 1029 else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. 1030 S = ' ' + S; 1031 1032 S = ObjCQIString + S; 1033 } 1034 1035 std::string TemplateSpecializationType:: 1036 PrintTemplateArgumentList(const TemplateArgumentListInfo &Args, 1037 const PrintingPolicy &Policy) { 1038 return PrintTemplateArgumentList(Args.getArgumentArray(), 1039 Args.size(), 1040 Policy); 1041 } 1042 1043 std::string 1044 TemplateSpecializationType::PrintTemplateArgumentList( 1045 const TemplateArgument *Args, 1046 unsigned NumArgs, 1047 const PrintingPolicy &Policy, 1048 bool SkipBrackets) { 1049 std::string SpecString; 1050 if (!SkipBrackets) 1051 SpecString += '<'; 1052 1053 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1054 if (SpecString.size() > unsigned(!SkipBrackets)) 1055 SpecString += ", "; 1056 1057 // Print the argument into a string. 1058 std::string ArgString; 1059 if (Args[Arg].getKind() == TemplateArgument::Pack) { 1060 ArgString = PrintTemplateArgumentList(Args[Arg].pack_begin(), 1061 Args[Arg].pack_size(), 1062 Policy, true); 1063 } else { 1064 llvm::raw_string_ostream ArgOut(ArgString); 1065 Args[Arg].print(Policy, ArgOut); 1066 } 1067 1068 // If this is the first argument and its string representation 1069 // begins with the global scope specifier ('::foo'), add a space 1070 // to avoid printing the diagraph '<:'. 1071 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1072 SpecString += ' '; 1073 1074 SpecString += ArgString; 1075 } 1076 1077 // If the last character of our string is '>', add another space to 1078 // keep the two '>''s separate tokens. We don't *have* to do this in 1079 // C++0x, but it's still good hygiene. 1080 if (!SpecString.empty() && SpecString[SpecString.size() - 1] == '>') 1081 SpecString += ' '; 1082 1083 if (!SkipBrackets) 1084 SpecString += '>'; 1085 1086 return SpecString; 1087 } 1088 1089 // Sadly, repeat all that with TemplateArgLoc. 1090 std::string TemplateSpecializationType:: 1091 PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs, 1092 const PrintingPolicy &Policy) { 1093 std::string SpecString; 1094 SpecString += '<'; 1095 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1096 if (SpecString.size() > 1) 1097 SpecString += ", "; 1098 1099 // Print the argument into a string. 1100 std::string ArgString; 1101 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) { 1102 ArgString = PrintTemplateArgumentList( 1103 Args[Arg].getArgument().pack_begin(), 1104 Args[Arg].getArgument().pack_size(), 1105 Policy, true); 1106 } else { 1107 llvm::raw_string_ostream ArgOut(ArgString); 1108 Args[Arg].getArgument().print(Policy, ArgOut); 1109 } 1110 1111 // If this is the first argument and its string representation 1112 // begins with the global scope specifier ('::foo'), add a space 1113 // to avoid printing the diagraph '<:'. 1114 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1115 SpecString += ' '; 1116 1117 SpecString += ArgString; 1118 } 1119 1120 // If the last character of our string is '>', add another space to 1121 // keep the two '>''s separate tokens. We don't *have* to do this in 1122 // C++0x, but it's still good hygiene. 1123 if (SpecString[SpecString.size() - 1] == '>') 1124 SpecString += ' '; 1125 1126 SpecString += '>'; 1127 1128 return SpecString; 1129 } 1130 1131 void QualType::dump(const char *msg) const { 1132 std::string R = "identifier"; 1133 LangOptions LO; 1134 getAsStringInternal(R, PrintingPolicy(LO)); 1135 if (msg) 1136 llvm::errs() << msg << ": "; 1137 llvm::errs() << R << "\n"; 1138 } 1139 void QualType::dump() const { 1140 dump(""); 1141 } 1142 1143 void Type::dump() const { 1144 QualType(this, 0).dump(); 1145 } 1146 1147 std::string Qualifiers::getAsString() const { 1148 LangOptions LO; 1149 return getAsString(PrintingPolicy(LO)); 1150 } 1151 1152 // Appends qualifiers to the given string, separated by spaces. Will 1153 // prefix a space if the string is non-empty. Will not append a final 1154 // space. 1155 void Qualifiers::getAsStringInternal(std::string &S, 1156 const PrintingPolicy& Policy) const { 1157 AppendTypeQualList(S, getCVRQualifiers()); 1158 if (unsigned addrspace = getAddressSpace()) { 1159 if (!S.empty()) S += ' '; 1160 S += "__attribute__((address_space("; 1161 S += llvm::utostr_32(addrspace); 1162 S += ")))"; 1163 } 1164 if (Qualifiers::GC gc = getObjCGCAttr()) { 1165 if (!S.empty()) S += ' '; 1166 if (gc == Qualifiers::Weak) 1167 S += "__weak"; 1168 else 1169 S += "__strong"; 1170 } 1171 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) { 1172 if (!S.empty() && 1173 !(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)) 1174 S += ' '; 1175 1176 switch (lifetime) { 1177 case Qualifiers::OCL_None: llvm_unreachable("none but true"); 1178 case Qualifiers::OCL_ExplicitNone: S += "__unsafe_unretained"; break; 1179 case Qualifiers::OCL_Strong: 1180 if (!Policy.SuppressStrongLifetime) 1181 S += "__strong"; 1182 break; 1183 1184 case Qualifiers::OCL_Weak: S += "__weak"; break; 1185 case Qualifiers::OCL_Autoreleasing: S += "__autoreleasing"; break; 1186 } 1187 } 1188 } 1189 1190 std::string QualType::getAsString(const Type *ty, Qualifiers qs) { 1191 std::string buffer; 1192 LangOptions options; 1193 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options)); 1194 return buffer; 1195 } 1196 1197 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, 1198 std::string &buffer, 1199 const PrintingPolicy &policy) { 1200 TypePrinter(policy).print(ty, qs, buffer); 1201 } 1202