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