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