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/PrettyPrinter.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclObjC.h" 18 #include "clang/AST/DeclTemplate.h" 19 #include "clang/AST/Expr.h" 20 #include "clang/AST/Type.h" 21 #include "clang/Basic/LangOptions.h" 22 #include "clang/Basic/SourceManager.h" 23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/ADT/StringExtras.h" 25 #include "llvm/Support/SaveAndRestore.h" 26 #include "llvm/Support/raw_ostream.h" 27 using namespace clang; 28 29 namespace { 30 /// \brief RAII object that enables printing of the ARC __strong lifetime 31 /// qualifier. 32 class IncludeStrongLifetimeRAII { 33 PrintingPolicy &Policy; 34 bool Old; 35 36 public: 37 explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy) 38 : Policy(Policy), Old(Policy.SuppressStrongLifetime) { 39 Policy.SuppressStrongLifetime = false; 40 } 41 42 ~IncludeStrongLifetimeRAII() { 43 Policy.SuppressStrongLifetime = Old; 44 } 45 }; 46 47 class ParamPolicyRAII { 48 PrintingPolicy &Policy; 49 bool Old; 50 51 public: 52 explicit ParamPolicyRAII(PrintingPolicy &Policy) 53 : Policy(Policy), Old(Policy.SuppressSpecifiers) { 54 Policy.SuppressSpecifiers = false; 55 } 56 57 ~ParamPolicyRAII() { 58 Policy.SuppressSpecifiers = Old; 59 } 60 }; 61 62 class ElaboratedTypePolicyRAII { 63 PrintingPolicy &Policy; 64 bool SuppressTagKeyword; 65 bool SuppressScope; 66 67 public: 68 explicit ElaboratedTypePolicyRAII(PrintingPolicy &Policy) : Policy(Policy) { 69 SuppressTagKeyword = Policy.SuppressTagKeyword; 70 SuppressScope = Policy.SuppressScope; 71 Policy.SuppressTagKeyword = true; 72 Policy.SuppressScope = true; 73 } 74 75 ~ElaboratedTypePolicyRAII() { 76 Policy.SuppressTagKeyword = SuppressTagKeyword; 77 Policy.SuppressScope = SuppressScope; 78 } 79 }; 80 81 class TypePrinter { 82 PrintingPolicy Policy; 83 bool HasEmptyPlaceHolder; 84 85 public: 86 explicit TypePrinter(const PrintingPolicy &Policy) 87 : Policy(Policy), HasEmptyPlaceHolder(false) { } 88 89 void print(const Type *ty, Qualifiers qs, raw_ostream &OS, 90 StringRef PlaceHolder); 91 void print(QualType T, raw_ostream &OS, StringRef PlaceHolder); 92 93 static bool canPrefixQualifiers(const Type *T, bool &NeedARCStrongQualifier); 94 void spaceBeforePlaceHolder(raw_ostream &OS); 95 void printTypeSpec(const NamedDecl *D, raw_ostream &OS); 96 97 void printBefore(const Type *ty, Qualifiers qs, raw_ostream &OS); 98 void printBefore(QualType T, raw_ostream &OS); 99 void printAfter(const Type *ty, Qualifiers qs, raw_ostream &OS); 100 void printAfter(QualType T, raw_ostream &OS); 101 void AppendScope(DeclContext *DC, raw_ostream &OS); 102 void printTag(TagDecl *T, raw_ostream &OS); 103 #define ABSTRACT_TYPE(CLASS, PARENT) 104 #define TYPE(CLASS, PARENT) \ 105 void print##CLASS##Before(const CLASS##Type *T, raw_ostream &OS); \ 106 void print##CLASS##After(const CLASS##Type *T, raw_ostream &OS); 107 #include "clang/AST/TypeNodes.def" 108 }; 109 } 110 111 static void AppendTypeQualList(raw_ostream &OS, unsigned TypeQuals) { 112 bool appendSpace = false; 113 if (TypeQuals & Qualifiers::Const) { 114 OS << "const"; 115 appendSpace = true; 116 } 117 if (TypeQuals & Qualifiers::Volatile) { 118 if (appendSpace) OS << ' '; 119 OS << "volatile"; 120 appendSpace = true; 121 } 122 if (TypeQuals & Qualifiers::Restrict) { 123 if (appendSpace) OS << ' '; 124 OS << "restrict"; 125 } 126 } 127 128 void TypePrinter::spaceBeforePlaceHolder(raw_ostream &OS) { 129 if (!HasEmptyPlaceHolder) 130 OS << ' '; 131 } 132 133 void TypePrinter::print(QualType t, raw_ostream &OS, StringRef PlaceHolder) { 134 SplitQualType split = t.split(); 135 print(split.Ty, split.Quals, OS, PlaceHolder); 136 } 137 138 void TypePrinter::print(const Type *T, Qualifiers Quals, raw_ostream &OS, 139 StringRef PlaceHolder) { 140 if (!T) { 141 OS << "NULL TYPE"; 142 return; 143 } 144 145 SaveAndRestore<bool> PHVal(HasEmptyPlaceHolder, PlaceHolder.empty()); 146 147 printBefore(T, Quals, OS); 148 OS << PlaceHolder; 149 printAfter(T, Quals, OS); 150 } 151 152 bool TypePrinter::canPrefixQualifiers(const Type *T, 153 bool &NeedARCStrongQualifier) { 154 // CanPrefixQualifiers - We prefer to print type qualifiers before the type, 155 // so that we get "const int" instead of "int const", but we can't do this if 156 // the type is complex. For example if the type is "int*", we *must* print 157 // "int * const", printing "const int *" is different. Only do this when the 158 // type expands to a simple string. 159 bool CanPrefixQualifiers = false; 160 NeedARCStrongQualifier = false; 161 Type::TypeClass TC = T->getTypeClass(); 162 if (const AutoType *AT = dyn_cast<AutoType>(T)) 163 TC = AT->desugar()->getTypeClass(); 164 if (const SubstTemplateTypeParmType *Subst 165 = dyn_cast<SubstTemplateTypeParmType>(T)) 166 TC = Subst->getReplacementType()->getTypeClass(); 167 168 switch (TC) { 169 case Type::Builtin: 170 case Type::Complex: 171 case Type::UnresolvedUsing: 172 case Type::Typedef: 173 case Type::TypeOfExpr: 174 case Type::TypeOf: 175 case Type::Decltype: 176 case Type::UnaryTransform: 177 case Type::Record: 178 case Type::Enum: 179 case Type::Elaborated: 180 case Type::TemplateTypeParm: 181 case Type::SubstTemplateTypeParmPack: 182 case Type::TemplateSpecialization: 183 case Type::InjectedClassName: 184 case Type::DependentName: 185 case Type::DependentTemplateSpecialization: 186 case Type::ObjCObject: 187 case Type::ObjCInterface: 188 case Type::Atomic: 189 CanPrefixQualifiers = true; 190 break; 191 192 case Type::ObjCObjectPointer: 193 CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() || 194 T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType(); 195 break; 196 197 case Type::ConstantArray: 198 case Type::IncompleteArray: 199 case Type::VariableArray: 200 case Type::DependentSizedArray: 201 NeedARCStrongQualifier = true; 202 // Fall through 203 204 case Type::Pointer: 205 case Type::BlockPointer: 206 case Type::LValueReference: 207 case Type::RValueReference: 208 case Type::MemberPointer: 209 case Type::DependentSizedExtVector: 210 case Type::Vector: 211 case Type::ExtVector: 212 case Type::FunctionProto: 213 case Type::FunctionNoProto: 214 case Type::Paren: 215 case Type::Attributed: 216 case Type::PackExpansion: 217 case Type::SubstTemplateTypeParm: 218 case Type::Auto: 219 CanPrefixQualifiers = false; 220 break; 221 } 222 223 return CanPrefixQualifiers; 224 } 225 226 void TypePrinter::printBefore(QualType T, raw_ostream &OS) { 227 SplitQualType Split = T.split(); 228 229 // If we have cv1 T, where T is substituted for cv2 U, only print cv1 - cv2 230 // at this level. 231 Qualifiers Quals = Split.Quals; 232 if (const SubstTemplateTypeParmType *Subst = 233 dyn_cast<SubstTemplateTypeParmType>(Split.Ty)) 234 Quals -= QualType(Subst, 0).getQualifiers(); 235 236 printBefore(Split.Ty, Quals, OS); 237 } 238 239 /// \brief Prints the part of the type string before an identifier, e.g. for 240 /// "int foo[10]" it prints "int ". 241 void TypePrinter::printBefore(const Type *T,Qualifiers Quals, raw_ostream &OS) { 242 if (Policy.SuppressSpecifiers && T->isSpecifierType()) 243 return; 244 245 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder); 246 247 // Print qualifiers as appropriate. 248 249 bool CanPrefixQualifiers = false; 250 bool NeedARCStrongQualifier = false; 251 CanPrefixQualifiers = canPrefixQualifiers(T, NeedARCStrongQualifier); 252 253 if (CanPrefixQualifiers && !Quals.empty()) { 254 if (NeedARCStrongQualifier) { 255 IncludeStrongLifetimeRAII Strong(Policy); 256 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true); 257 } else { 258 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/true); 259 } 260 } 261 262 bool hasAfterQuals = false; 263 if (!CanPrefixQualifiers && !Quals.empty()) { 264 hasAfterQuals = !Quals.isEmptyWhenPrinted(Policy); 265 if (hasAfterQuals) 266 HasEmptyPlaceHolder = false; 267 } 268 269 switch (T->getTypeClass()) { 270 #define ABSTRACT_TYPE(CLASS, PARENT) 271 #define TYPE(CLASS, PARENT) case Type::CLASS: \ 272 print##CLASS##Before(cast<CLASS##Type>(T), OS); \ 273 break; 274 #include "clang/AST/TypeNodes.def" 275 } 276 277 if (hasAfterQuals) { 278 if (NeedARCStrongQualifier) { 279 IncludeStrongLifetimeRAII Strong(Policy); 280 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get()); 281 } else { 282 Quals.print(OS, Policy, /*appendSpaceIfNonEmpty=*/!PrevPHIsEmpty.get()); 283 } 284 } 285 } 286 287 void TypePrinter::printAfter(QualType t, raw_ostream &OS) { 288 SplitQualType split = t.split(); 289 printAfter(split.Ty, split.Quals, OS); 290 } 291 292 /// \brief Prints the part of the type string after an identifier, e.g. for 293 /// "int foo[10]" it prints "[10]". 294 void TypePrinter::printAfter(const Type *T, Qualifiers Quals, raw_ostream &OS) { 295 switch (T->getTypeClass()) { 296 #define ABSTRACT_TYPE(CLASS, PARENT) 297 #define TYPE(CLASS, PARENT) case Type::CLASS: \ 298 print##CLASS##After(cast<CLASS##Type>(T), OS); \ 299 break; 300 #include "clang/AST/TypeNodes.def" 301 } 302 } 303 304 void TypePrinter::printBuiltinBefore(const BuiltinType *T, raw_ostream &OS) { 305 OS << T->getName(Policy); 306 spaceBeforePlaceHolder(OS); 307 } 308 void TypePrinter::printBuiltinAfter(const BuiltinType *T, raw_ostream &OS) { } 309 310 void TypePrinter::printComplexBefore(const ComplexType *T, raw_ostream &OS) { 311 OS << "_Complex "; 312 printBefore(T->getElementType(), OS); 313 } 314 void TypePrinter::printComplexAfter(const ComplexType *T, raw_ostream &OS) { 315 printAfter(T->getElementType(), OS); 316 } 317 318 void TypePrinter::printPointerBefore(const PointerType *T, raw_ostream &OS) { 319 IncludeStrongLifetimeRAII Strong(Policy); 320 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 321 printBefore(T->getPointeeType(), OS); 322 // Handle things like 'int (*A)[4];' correctly. 323 // FIXME: this should include vectors, but vectors use attributes I guess. 324 if (isa<ArrayType>(T->getPointeeType())) 325 OS << '('; 326 OS << '*'; 327 } 328 void TypePrinter::printPointerAfter(const PointerType *T, raw_ostream &OS) { 329 IncludeStrongLifetimeRAII Strong(Policy); 330 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 331 // Handle things like 'int (*A)[4];' correctly. 332 // FIXME: this should include vectors, but vectors use attributes I guess. 333 if (isa<ArrayType>(T->getPointeeType())) 334 OS << ')'; 335 printAfter(T->getPointeeType(), OS); 336 } 337 338 void TypePrinter::printBlockPointerBefore(const BlockPointerType *T, 339 raw_ostream &OS) { 340 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 341 printBefore(T->getPointeeType(), OS); 342 OS << '^'; 343 } 344 void TypePrinter::printBlockPointerAfter(const BlockPointerType *T, 345 raw_ostream &OS) { 346 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 347 printAfter(T->getPointeeType(), OS); 348 } 349 350 void TypePrinter::printLValueReferenceBefore(const LValueReferenceType *T, 351 raw_ostream &OS) { 352 IncludeStrongLifetimeRAII Strong(Policy); 353 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 354 printBefore(T->getPointeeTypeAsWritten(), OS); 355 // Handle things like 'int (&A)[4];' correctly. 356 // FIXME: this should include vectors, but vectors use attributes I guess. 357 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 358 OS << '('; 359 OS << '&'; 360 } 361 void TypePrinter::printLValueReferenceAfter(const LValueReferenceType *T, 362 raw_ostream &OS) { 363 IncludeStrongLifetimeRAII Strong(Policy); 364 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 365 // Handle things like 'int (&A)[4];' correctly. 366 // FIXME: this should include vectors, but vectors use attributes I guess. 367 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 368 OS << ')'; 369 printAfter(T->getPointeeTypeAsWritten(), OS); 370 } 371 372 void TypePrinter::printRValueReferenceBefore(const RValueReferenceType *T, 373 raw_ostream &OS) { 374 IncludeStrongLifetimeRAII Strong(Policy); 375 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 376 printBefore(T->getPointeeTypeAsWritten(), OS); 377 // Handle things like 'int (&&A)[4];' correctly. 378 // FIXME: this should include vectors, but vectors use attributes I guess. 379 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 380 OS << '('; 381 OS << "&&"; 382 } 383 void TypePrinter::printRValueReferenceAfter(const RValueReferenceType *T, 384 raw_ostream &OS) { 385 IncludeStrongLifetimeRAII Strong(Policy); 386 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 387 // Handle things like 'int (&&A)[4];' correctly. 388 // FIXME: this should include vectors, but vectors use attributes I guess. 389 if (isa<ArrayType>(T->getPointeeTypeAsWritten())) 390 OS << ')'; 391 printAfter(T->getPointeeTypeAsWritten(), OS); 392 } 393 394 void TypePrinter::printMemberPointerBefore(const MemberPointerType *T, 395 raw_ostream &OS) { 396 IncludeStrongLifetimeRAII Strong(Policy); 397 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 398 printBefore(T->getPointeeType(), OS); 399 // Handle things like 'int (Cls::*A)[4];' correctly. 400 // FIXME: this should include vectors, but vectors use attributes I guess. 401 if (isa<ArrayType>(T->getPointeeType())) 402 OS << '('; 403 404 PrintingPolicy InnerPolicy(Policy); 405 InnerPolicy.SuppressTag = false; 406 TypePrinter(InnerPolicy).print(QualType(T->getClass(), 0), OS, StringRef()); 407 408 OS << "::*"; 409 } 410 void TypePrinter::printMemberPointerAfter(const MemberPointerType *T, 411 raw_ostream &OS) { 412 IncludeStrongLifetimeRAII Strong(Policy); 413 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 414 // Handle things like 'int (Cls::*A)[4];' correctly. 415 // FIXME: this should include vectors, but vectors use attributes I guess. 416 if (isa<ArrayType>(T->getPointeeType())) 417 OS << ')'; 418 printAfter(T->getPointeeType(), OS); 419 } 420 421 void TypePrinter::printConstantArrayBefore(const ConstantArrayType *T, 422 raw_ostream &OS) { 423 IncludeStrongLifetimeRAII Strong(Policy); 424 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 425 printBefore(T->getElementType(), OS); 426 } 427 void TypePrinter::printConstantArrayAfter(const ConstantArrayType *T, 428 raw_ostream &OS) { 429 OS << '[' << T->getSize().getZExtValue() << ']'; 430 printAfter(T->getElementType(), OS); 431 } 432 433 void TypePrinter::printIncompleteArrayBefore(const IncompleteArrayType *T, 434 raw_ostream &OS) { 435 IncludeStrongLifetimeRAII Strong(Policy); 436 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 437 printBefore(T->getElementType(), OS); 438 } 439 void TypePrinter::printIncompleteArrayAfter(const IncompleteArrayType *T, 440 raw_ostream &OS) { 441 OS << "[]"; 442 printAfter(T->getElementType(), OS); 443 } 444 445 void TypePrinter::printVariableArrayBefore(const VariableArrayType *T, 446 raw_ostream &OS) { 447 IncludeStrongLifetimeRAII Strong(Policy); 448 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 449 printBefore(T->getElementType(), OS); 450 } 451 void TypePrinter::printVariableArrayAfter(const VariableArrayType *T, 452 raw_ostream &OS) { 453 OS << '['; 454 if (T->getIndexTypeQualifiers().hasQualifiers()) { 455 AppendTypeQualList(OS, T->getIndexTypeCVRQualifiers()); 456 OS << ' '; 457 } 458 459 if (T->getSizeModifier() == VariableArrayType::Static) 460 OS << "static"; 461 else if (T->getSizeModifier() == VariableArrayType::Star) 462 OS << '*'; 463 464 if (T->getSizeExpr()) 465 T->getSizeExpr()->printPretty(OS, 0, Policy); 466 OS << ']'; 467 468 printAfter(T->getElementType(), OS); 469 } 470 471 void TypePrinter::printDependentSizedArrayBefore( 472 const DependentSizedArrayType *T, 473 raw_ostream &OS) { 474 IncludeStrongLifetimeRAII Strong(Policy); 475 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 476 printBefore(T->getElementType(), OS); 477 } 478 void TypePrinter::printDependentSizedArrayAfter( 479 const DependentSizedArrayType *T, 480 raw_ostream &OS) { 481 OS << '['; 482 if (T->getSizeExpr()) 483 T->getSizeExpr()->printPretty(OS, 0, Policy); 484 OS << ']'; 485 printAfter(T->getElementType(), OS); 486 } 487 488 void TypePrinter::printDependentSizedExtVectorBefore( 489 const DependentSizedExtVectorType *T, 490 raw_ostream &OS) { 491 printBefore(T->getElementType(), OS); 492 } 493 void TypePrinter::printDependentSizedExtVectorAfter( 494 const DependentSizedExtVectorType *T, 495 raw_ostream &OS) { 496 OS << " __attribute__((ext_vector_type("; 497 if (T->getSizeExpr()) 498 T->getSizeExpr()->printPretty(OS, 0, Policy); 499 OS << ")))"; 500 printAfter(T->getElementType(), OS); 501 } 502 503 void TypePrinter::printVectorBefore(const VectorType *T, raw_ostream &OS) { 504 switch (T->getVectorKind()) { 505 case VectorType::AltiVecPixel: 506 OS << "__vector __pixel "; 507 break; 508 case VectorType::AltiVecBool: 509 OS << "__vector __bool "; 510 printBefore(T->getElementType(), OS); 511 break; 512 case VectorType::AltiVecVector: 513 OS << "__vector "; 514 printBefore(T->getElementType(), OS); 515 break; 516 case VectorType::NeonVector: 517 OS << "__attribute__((neon_vector_type(" 518 << T->getNumElements() << "))) "; 519 printBefore(T->getElementType(), OS); 520 break; 521 case VectorType::NeonPolyVector: 522 OS << "__attribute__((neon_polyvector_type(" << 523 T->getNumElements() << "))) "; 524 printBefore(T->getElementType(), OS); 525 break; 526 case VectorType::GenericVector: { 527 // FIXME: We prefer to print the size directly here, but have no way 528 // to get the size of the type. 529 OS << "__attribute__((__vector_size__(" 530 << T->getNumElements() 531 << " * sizeof("; 532 print(T->getElementType(), OS, StringRef()); 533 OS << ")))) "; 534 printBefore(T->getElementType(), OS); 535 break; 536 } 537 } 538 } 539 void TypePrinter::printVectorAfter(const VectorType *T, raw_ostream &OS) { 540 printAfter(T->getElementType(), OS); 541 } 542 543 void TypePrinter::printExtVectorBefore(const ExtVectorType *T, 544 raw_ostream &OS) { 545 printBefore(T->getElementType(), OS); 546 } 547 void TypePrinter::printExtVectorAfter(const ExtVectorType *T, raw_ostream &OS) { 548 printAfter(T->getElementType(), OS); 549 OS << " __attribute__((ext_vector_type("; 550 OS << T->getNumElements(); 551 OS << ")))"; 552 } 553 554 void 555 FunctionProtoType::printExceptionSpecification(raw_ostream &OS, 556 const PrintingPolicy &Policy) 557 const { 558 559 if (hasDynamicExceptionSpec()) { 560 OS << " throw("; 561 if (getExceptionSpecType() == EST_MSAny) 562 OS << "..."; 563 else 564 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) { 565 if (I) 566 OS << ", "; 567 568 OS << getExceptionType(I).stream(Policy); 569 } 570 OS << ')'; 571 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) { 572 OS << " noexcept"; 573 if (getExceptionSpecType() == EST_ComputedNoexcept) { 574 OS << '('; 575 getNoexceptExpr()->printPretty(OS, 0, Policy); 576 OS << ')'; 577 } 578 } 579 } 580 581 void TypePrinter::printFunctionProtoBefore(const FunctionProtoType *T, 582 raw_ostream &OS) { 583 if (T->hasTrailingReturn()) { 584 OS << "auto "; 585 if (!HasEmptyPlaceHolder) 586 OS << '('; 587 } else { 588 // If needed for precedence reasons, wrap the inner part in grouping parens. 589 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 590 printBefore(T->getResultType(), OS); 591 if (!PrevPHIsEmpty.get()) 592 OS << '('; 593 } 594 } 595 596 void TypePrinter::printFunctionProtoAfter(const FunctionProtoType *T, 597 raw_ostream &OS) { 598 // If needed for precedence reasons, wrap the inner part in grouping parens. 599 if (!HasEmptyPlaceHolder) 600 OS << ')'; 601 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 602 603 OS << '('; 604 { 605 ParamPolicyRAII ParamPolicy(Policy); 606 for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) { 607 if (i) OS << ", "; 608 print(T->getArgType(i), OS, StringRef()); 609 } 610 } 611 612 if (T->isVariadic()) { 613 if (T->getNumArgs()) 614 OS << ", "; 615 OS << "..."; 616 } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) { 617 // Do not emit int() if we have a proto, emit 'int(void)'. 618 OS << "void"; 619 } 620 621 OS << ')'; 622 623 FunctionType::ExtInfo Info = T->getExtInfo(); 624 switch(Info.getCC()) { 625 case CC_Default: break; 626 case CC_C: 627 OS << " __attribute__((cdecl))"; 628 break; 629 case CC_X86StdCall: 630 OS << " __attribute__((stdcall))"; 631 break; 632 case CC_X86FastCall: 633 OS << " __attribute__((fastcall))"; 634 break; 635 case CC_X86ThisCall: 636 OS << " __attribute__((thiscall))"; 637 break; 638 case CC_X86Pascal: 639 OS << " __attribute__((pascal))"; 640 break; 641 case CC_AAPCS: 642 OS << " __attribute__((pcs(\"aapcs\")))"; 643 break; 644 case CC_AAPCS_VFP: 645 OS << " __attribute__((pcs(\"aapcs-vfp\")))"; 646 break; 647 case CC_PnaclCall: 648 OS << " __attribute__((pnaclcall))"; 649 break; 650 } 651 if (Info.getNoReturn()) 652 OS << " __attribute__((noreturn))"; 653 if (Info.getRegParm()) 654 OS << " __attribute__((regparm (" 655 << Info.getRegParm() << ")))"; 656 657 if (unsigned quals = T->getTypeQuals()) { 658 OS << ' '; 659 AppendTypeQualList(OS, quals); 660 } 661 662 switch (T->getRefQualifier()) { 663 case RQ_None: 664 break; 665 666 case RQ_LValue: 667 OS << " &"; 668 break; 669 670 case RQ_RValue: 671 OS << " &&"; 672 break; 673 } 674 T->printExceptionSpecification(OS, Policy); 675 676 if (T->hasTrailingReturn()) { 677 OS << " -> "; 678 print(T->getResultType(), OS, StringRef()); 679 } else 680 printAfter(T->getResultType(), OS); 681 } 682 683 void TypePrinter::printFunctionNoProtoBefore(const FunctionNoProtoType *T, 684 raw_ostream &OS) { 685 // If needed for precedence reasons, wrap the inner part in grouping parens. 686 SaveAndRestore<bool> PrevPHIsEmpty(HasEmptyPlaceHolder, false); 687 printBefore(T->getResultType(), OS); 688 if (!PrevPHIsEmpty.get()) 689 OS << '('; 690 } 691 void TypePrinter::printFunctionNoProtoAfter(const FunctionNoProtoType *T, 692 raw_ostream &OS) { 693 // If needed for precedence reasons, wrap the inner part in grouping parens. 694 if (!HasEmptyPlaceHolder) 695 OS << ')'; 696 SaveAndRestore<bool> NonEmptyPH(HasEmptyPlaceHolder, false); 697 698 OS << "()"; 699 if (T->getNoReturnAttr()) 700 OS << " __attribute__((noreturn))"; 701 printAfter(T->getResultType(), OS); 702 } 703 704 void TypePrinter::printTypeSpec(const NamedDecl *D, raw_ostream &OS) { 705 IdentifierInfo *II = D->getIdentifier(); 706 OS << II->getName(); 707 spaceBeforePlaceHolder(OS); 708 } 709 710 void TypePrinter::printUnresolvedUsingBefore(const UnresolvedUsingType *T, 711 raw_ostream &OS) { 712 printTypeSpec(T->getDecl(), OS); 713 } 714 void TypePrinter::printUnresolvedUsingAfter(const UnresolvedUsingType *T, 715 raw_ostream &OS) { } 716 717 void TypePrinter::printTypedefBefore(const TypedefType *T, raw_ostream &OS) { 718 printTypeSpec(T->getDecl(), OS); 719 } 720 void TypePrinter::printTypedefAfter(const TypedefType *T, raw_ostream &OS) { } 721 722 void TypePrinter::printTypeOfExprBefore(const TypeOfExprType *T, 723 raw_ostream &OS) { 724 OS << "typeof "; 725 T->getUnderlyingExpr()->printPretty(OS, 0, Policy); 726 spaceBeforePlaceHolder(OS); 727 } 728 void TypePrinter::printTypeOfExprAfter(const TypeOfExprType *T, 729 raw_ostream &OS) { } 730 731 void TypePrinter::printTypeOfBefore(const TypeOfType *T, raw_ostream &OS) { 732 OS << "typeof("; 733 print(T->getUnderlyingType(), OS, StringRef()); 734 OS << ')'; 735 spaceBeforePlaceHolder(OS); 736 } 737 void TypePrinter::printTypeOfAfter(const TypeOfType *T, raw_ostream &OS) { } 738 739 void TypePrinter::printDecltypeBefore(const DecltypeType *T, raw_ostream &OS) { 740 OS << "decltype("; 741 T->getUnderlyingExpr()->printPretty(OS, 0, Policy); 742 OS << ')'; 743 spaceBeforePlaceHolder(OS); 744 } 745 void TypePrinter::printDecltypeAfter(const DecltypeType *T, raw_ostream &OS) { } 746 747 void TypePrinter::printUnaryTransformBefore(const UnaryTransformType *T, 748 raw_ostream &OS) { 749 IncludeStrongLifetimeRAII Strong(Policy); 750 751 switch (T->getUTTKind()) { 752 case UnaryTransformType::EnumUnderlyingType: 753 OS << "__underlying_type("; 754 print(T->getBaseType(), OS, StringRef()); 755 OS << ')'; 756 spaceBeforePlaceHolder(OS); 757 return; 758 } 759 760 printBefore(T->getBaseType(), OS); 761 } 762 void TypePrinter::printUnaryTransformAfter(const UnaryTransformType *T, 763 raw_ostream &OS) { 764 IncludeStrongLifetimeRAII Strong(Policy); 765 766 switch (T->getUTTKind()) { 767 case UnaryTransformType::EnumUnderlyingType: 768 return; 769 } 770 771 printAfter(T->getBaseType(), OS); 772 } 773 774 void TypePrinter::printAutoBefore(const AutoType *T, raw_ostream &OS) { 775 // If the type has been deduced, do not print 'auto'. 776 if (T->isDeduced()) { 777 printBefore(T->getDeducedType(), OS); 778 } else { 779 OS << "auto"; 780 spaceBeforePlaceHolder(OS); 781 } 782 } 783 void TypePrinter::printAutoAfter(const AutoType *T, raw_ostream &OS) { 784 // If the type has been deduced, do not print 'auto'. 785 if (T->isDeduced()) 786 printAfter(T->getDeducedType(), OS); 787 } 788 789 void TypePrinter::printAtomicBefore(const AtomicType *T, raw_ostream &OS) { 790 IncludeStrongLifetimeRAII Strong(Policy); 791 792 OS << "_Atomic("; 793 print(T->getValueType(), OS, StringRef()); 794 OS << ')'; 795 spaceBeforePlaceHolder(OS); 796 } 797 void TypePrinter::printAtomicAfter(const AtomicType *T, raw_ostream &OS) { } 798 799 /// Appends the given scope to the end of a string. 800 void TypePrinter::AppendScope(DeclContext *DC, raw_ostream &OS) { 801 if (DC->isTranslationUnit()) return; 802 if (DC->isFunctionOrMethod()) return; 803 AppendScope(DC->getParent(), OS); 804 805 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { 806 if (Policy.SuppressUnwrittenScope && 807 (NS->isAnonymousNamespace() || NS->isInline())) 808 return; 809 if (NS->getIdentifier()) 810 OS << NS->getName() << "::"; 811 else 812 OS << "<anonymous>::"; 813 } else if (ClassTemplateSpecializationDecl *Spec 814 = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { 815 IncludeStrongLifetimeRAII Strong(Policy); 816 OS << Spec->getIdentifier()->getName(); 817 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 818 TemplateSpecializationType::PrintTemplateArgumentList(OS, 819 TemplateArgs.data(), 820 TemplateArgs.size(), 821 Policy); 822 OS << "::"; 823 } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { 824 if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl()) 825 OS << Typedef->getIdentifier()->getName() << "::"; 826 else if (Tag->getIdentifier()) 827 OS << Tag->getIdentifier()->getName() << "::"; 828 else 829 return; 830 } 831 } 832 833 void TypePrinter::printTag(TagDecl *D, raw_ostream &OS) { 834 if (Policy.SuppressTag) 835 return; 836 837 bool HasKindDecoration = false; 838 839 // bool SuppressTagKeyword 840 // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword; 841 842 // We don't print tags unless this is an elaborated type. 843 // In C, we just assume every RecordType is an elaborated type. 844 if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword || 845 D->getTypedefNameForAnonDecl())) { 846 HasKindDecoration = true; 847 OS << D->getKindName(); 848 OS << ' '; 849 } 850 851 // Compute the full nested-name-specifier for this type. 852 // In C, this will always be empty except when the type 853 // being printed is anonymous within other Record. 854 if (!Policy.SuppressScope) 855 AppendScope(D->getDeclContext(), OS); 856 857 if (const IdentifierInfo *II = D->getIdentifier()) 858 OS << II->getName(); 859 else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) { 860 assert(Typedef->getIdentifier() && "Typedef without identifier?"); 861 OS << Typedef->getIdentifier()->getName(); 862 } else { 863 // Make an unambiguous representation for anonymous types, e.g. 864 // <anonymous enum at /usr/include/string.h:120:9> 865 866 if (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda()) { 867 OS << "<lambda"; 868 HasKindDecoration = true; 869 } else { 870 OS << "<anonymous"; 871 } 872 873 if (Policy.AnonymousTagLocations) { 874 // Suppress the redundant tag keyword if we just printed one. 875 // We don't have to worry about ElaboratedTypes here because you can't 876 // refer to an anonymous type with one. 877 if (!HasKindDecoration) 878 OS << " " << D->getKindName(); 879 880 PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( 881 D->getLocation()); 882 if (PLoc.isValid()) { 883 OS << " at " << PLoc.getFilename() 884 << ':' << PLoc.getLine() 885 << ':' << PLoc.getColumn(); 886 } 887 } 888 889 OS << '>'; 890 } 891 892 // If this is a class template specialization, print the template 893 // arguments. 894 if (ClassTemplateSpecializationDecl *Spec 895 = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 896 const TemplateArgument *Args; 897 unsigned NumArgs; 898 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { 899 const TemplateSpecializationType *TST = 900 cast<TemplateSpecializationType>(TAW->getType()); 901 Args = TST->getArgs(); 902 NumArgs = TST->getNumArgs(); 903 } else { 904 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); 905 Args = TemplateArgs.data(); 906 NumArgs = TemplateArgs.size(); 907 } 908 IncludeStrongLifetimeRAII Strong(Policy); 909 TemplateSpecializationType::PrintTemplateArgumentList(OS, 910 Args, NumArgs, 911 Policy); 912 } 913 914 spaceBeforePlaceHolder(OS); 915 } 916 917 void TypePrinter::printRecordBefore(const RecordType *T, raw_ostream &OS) { 918 printTag(T->getDecl(), OS); 919 } 920 void TypePrinter::printRecordAfter(const RecordType *T, raw_ostream &OS) { } 921 922 void TypePrinter::printEnumBefore(const EnumType *T, raw_ostream &OS) { 923 printTag(T->getDecl(), OS); 924 } 925 void TypePrinter::printEnumAfter(const EnumType *T, raw_ostream &OS) { } 926 927 void TypePrinter::printTemplateTypeParmBefore(const TemplateTypeParmType *T, 928 raw_ostream &OS) { 929 if (IdentifierInfo *Id = T->getIdentifier()) 930 OS << Id->getName(); 931 else 932 OS << "type-parameter-" << T->getDepth() << '-' << T->getIndex(); 933 spaceBeforePlaceHolder(OS); 934 } 935 void TypePrinter::printTemplateTypeParmAfter(const TemplateTypeParmType *T, 936 raw_ostream &OS) { } 937 938 void TypePrinter::printSubstTemplateTypeParmBefore( 939 const SubstTemplateTypeParmType *T, 940 raw_ostream &OS) { 941 IncludeStrongLifetimeRAII Strong(Policy); 942 printBefore(T->getReplacementType(), OS); 943 } 944 void TypePrinter::printSubstTemplateTypeParmAfter( 945 const SubstTemplateTypeParmType *T, 946 raw_ostream &OS) { 947 IncludeStrongLifetimeRAII Strong(Policy); 948 printAfter(T->getReplacementType(), OS); 949 } 950 951 void TypePrinter::printSubstTemplateTypeParmPackBefore( 952 const SubstTemplateTypeParmPackType *T, 953 raw_ostream &OS) { 954 IncludeStrongLifetimeRAII Strong(Policy); 955 printTemplateTypeParmBefore(T->getReplacedParameter(), OS); 956 } 957 void TypePrinter::printSubstTemplateTypeParmPackAfter( 958 const SubstTemplateTypeParmPackType *T, 959 raw_ostream &OS) { 960 IncludeStrongLifetimeRAII Strong(Policy); 961 printTemplateTypeParmAfter(T->getReplacedParameter(), OS); 962 } 963 964 void TypePrinter::printTemplateSpecializationBefore( 965 const TemplateSpecializationType *T, 966 raw_ostream &OS) { 967 IncludeStrongLifetimeRAII Strong(Policy); 968 T->getTemplateName().print(OS, Policy); 969 970 TemplateSpecializationType::PrintTemplateArgumentList(OS, 971 T->getArgs(), 972 T->getNumArgs(), 973 Policy); 974 spaceBeforePlaceHolder(OS); 975 } 976 void TypePrinter::printTemplateSpecializationAfter( 977 const TemplateSpecializationType *T, 978 raw_ostream &OS) { } 979 980 void TypePrinter::printInjectedClassNameBefore(const InjectedClassNameType *T, 981 raw_ostream &OS) { 982 printTemplateSpecializationBefore(T->getInjectedTST(), OS); 983 } 984 void TypePrinter::printInjectedClassNameAfter(const InjectedClassNameType *T, 985 raw_ostream &OS) { } 986 987 void TypePrinter::printElaboratedBefore(const ElaboratedType *T, 988 raw_ostream &OS) { 989 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 990 if (T->getKeyword() != ETK_None) 991 OS << " "; 992 NestedNameSpecifier* Qualifier = T->getQualifier(); 993 if (Qualifier) 994 Qualifier->print(OS, Policy); 995 996 ElaboratedTypePolicyRAII PolicyRAII(Policy); 997 printBefore(T->getNamedType(), OS); 998 } 999 void TypePrinter::printElaboratedAfter(const ElaboratedType *T, 1000 raw_ostream &OS) { 1001 ElaboratedTypePolicyRAII PolicyRAII(Policy); 1002 printAfter(T->getNamedType(), OS); 1003 } 1004 1005 void TypePrinter::printParenBefore(const ParenType *T, raw_ostream &OS) { 1006 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1007 printBefore(T->getInnerType(), OS); 1008 OS << '('; 1009 } else 1010 printBefore(T->getInnerType(), OS); 1011 } 1012 void TypePrinter::printParenAfter(const ParenType *T, raw_ostream &OS) { 1013 if (!HasEmptyPlaceHolder && !isa<FunctionType>(T->getInnerType())) { 1014 OS << ')'; 1015 printAfter(T->getInnerType(), OS); 1016 } else 1017 printAfter(T->getInnerType(), OS); 1018 } 1019 1020 void TypePrinter::printDependentNameBefore(const DependentNameType *T, 1021 raw_ostream &OS) { 1022 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1023 if (T->getKeyword() != ETK_None) 1024 OS << " "; 1025 1026 T->getQualifier()->print(OS, Policy); 1027 1028 OS << T->getIdentifier()->getName(); 1029 spaceBeforePlaceHolder(OS); 1030 } 1031 void TypePrinter::printDependentNameAfter(const DependentNameType *T, 1032 raw_ostream &OS) { } 1033 1034 void TypePrinter::printDependentTemplateSpecializationBefore( 1035 const DependentTemplateSpecializationType *T, raw_ostream &OS) { 1036 IncludeStrongLifetimeRAII Strong(Policy); 1037 1038 OS << TypeWithKeyword::getKeywordName(T->getKeyword()); 1039 if (T->getKeyword() != ETK_None) 1040 OS << " "; 1041 1042 if (T->getQualifier()) 1043 T->getQualifier()->print(OS, Policy); 1044 OS << T->getIdentifier()->getName(); 1045 TemplateSpecializationType::PrintTemplateArgumentList(OS, 1046 T->getArgs(), 1047 T->getNumArgs(), 1048 Policy); 1049 spaceBeforePlaceHolder(OS); 1050 } 1051 void TypePrinter::printDependentTemplateSpecializationAfter( 1052 const DependentTemplateSpecializationType *T, raw_ostream &OS) { } 1053 1054 void TypePrinter::printPackExpansionBefore(const PackExpansionType *T, 1055 raw_ostream &OS) { 1056 printBefore(T->getPattern(), OS); 1057 } 1058 void TypePrinter::printPackExpansionAfter(const PackExpansionType *T, 1059 raw_ostream &OS) { 1060 printAfter(T->getPattern(), OS); 1061 OS << "..."; 1062 } 1063 1064 void TypePrinter::printAttributedBefore(const AttributedType *T, 1065 raw_ostream &OS) { 1066 // Prefer the macro forms of the GC and ownership qualifiers. 1067 if (T->getAttrKind() == AttributedType::attr_objc_gc || 1068 T->getAttrKind() == AttributedType::attr_objc_ownership) 1069 return printBefore(T->getEquivalentType(), OS); 1070 1071 printBefore(T->getModifiedType(), OS); 1072 } 1073 1074 void TypePrinter::printAttributedAfter(const AttributedType *T, 1075 raw_ostream &OS) { 1076 // Prefer the macro forms of the GC and ownership qualifiers. 1077 if (T->getAttrKind() == AttributedType::attr_objc_gc || 1078 T->getAttrKind() == AttributedType::attr_objc_ownership) 1079 return printAfter(T->getEquivalentType(), OS); 1080 1081 // TODO: not all attributes are GCC-style attributes. 1082 OS << " __attribute__(("; 1083 switch (T->getAttrKind()) { 1084 case AttributedType::attr_address_space: 1085 OS << "address_space("; 1086 OS << T->getEquivalentType().getAddressSpace(); 1087 OS << ')'; 1088 break; 1089 1090 case AttributedType::attr_vector_size: { 1091 OS << "__vector_size__("; 1092 if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) { 1093 OS << vector->getNumElements(); 1094 OS << " * sizeof("; 1095 print(vector->getElementType(), OS, StringRef()); 1096 OS << ')'; 1097 } 1098 OS << ')'; 1099 break; 1100 } 1101 1102 case AttributedType::attr_neon_vector_type: 1103 case AttributedType::attr_neon_polyvector_type: { 1104 if (T->getAttrKind() == AttributedType::attr_neon_vector_type) 1105 OS << "neon_vector_type("; 1106 else 1107 OS << "neon_polyvector_type("; 1108 const VectorType *vector = T->getEquivalentType()->getAs<VectorType>(); 1109 OS << vector->getNumElements(); 1110 OS << ')'; 1111 break; 1112 } 1113 1114 case AttributedType::attr_regparm: { 1115 OS << "regparm("; 1116 QualType t = T->getEquivalentType(); 1117 while (!t->isFunctionType()) 1118 t = t->getPointeeType(); 1119 OS << t->getAs<FunctionType>()->getRegParmType(); 1120 OS << ')'; 1121 break; 1122 } 1123 1124 case AttributedType::attr_objc_gc: { 1125 OS << "objc_gc("; 1126 1127 QualType tmp = T->getEquivalentType(); 1128 while (tmp.getObjCGCAttr() == Qualifiers::GCNone) { 1129 QualType next = tmp->getPointeeType(); 1130 if (next == tmp) break; 1131 tmp = next; 1132 } 1133 1134 if (tmp.isObjCGCWeak()) 1135 OS << "weak"; 1136 else 1137 OS << "strong"; 1138 OS << ')'; 1139 break; 1140 } 1141 1142 case AttributedType::attr_objc_ownership: 1143 OS << "objc_ownership("; 1144 switch (T->getEquivalentType().getObjCLifetime()) { 1145 case Qualifiers::OCL_None: llvm_unreachable("no ownership!"); 1146 case Qualifiers::OCL_ExplicitNone: OS << "none"; break; 1147 case Qualifiers::OCL_Strong: OS << "strong"; break; 1148 case Qualifiers::OCL_Weak: OS << "weak"; break; 1149 case Qualifiers::OCL_Autoreleasing: OS << "autoreleasing"; break; 1150 } 1151 OS << ')'; 1152 break; 1153 1154 case AttributedType::attr_noreturn: OS << "noreturn"; break; 1155 case AttributedType::attr_cdecl: OS << "cdecl"; break; 1156 case AttributedType::attr_fastcall: OS << "fastcall"; break; 1157 case AttributedType::attr_stdcall: OS << "stdcall"; break; 1158 case AttributedType::attr_thiscall: OS << "thiscall"; break; 1159 case AttributedType::attr_pascal: OS << "pascal"; break; 1160 case AttributedType::attr_pcs: { 1161 OS << "pcs("; 1162 QualType t = T->getEquivalentType(); 1163 while (!t->isFunctionType()) 1164 t = t->getPointeeType(); 1165 OS << (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ? 1166 "\"aapcs\"" : "\"aapcs-vfp\""); 1167 OS << ')'; 1168 break; 1169 } 1170 case AttributedType::attr_pnaclcall: OS << "pnaclcall"; break; 1171 } 1172 OS << "))"; 1173 } 1174 1175 void TypePrinter::printObjCInterfaceBefore(const ObjCInterfaceType *T, 1176 raw_ostream &OS) { 1177 OS << T->getDecl()->getName(); 1178 spaceBeforePlaceHolder(OS); 1179 } 1180 void TypePrinter::printObjCInterfaceAfter(const ObjCInterfaceType *T, 1181 raw_ostream &OS) { } 1182 1183 void TypePrinter::printObjCObjectBefore(const ObjCObjectType *T, 1184 raw_ostream &OS) { 1185 if (T->qual_empty()) 1186 return printBefore(T->getBaseType(), OS); 1187 1188 print(T->getBaseType(), OS, StringRef()); 1189 OS << '<'; 1190 bool isFirst = true; 1191 for (ObjCObjectType::qual_iterator 1192 I = T->qual_begin(), E = T->qual_end(); I != E; ++I) { 1193 if (isFirst) 1194 isFirst = false; 1195 else 1196 OS << ','; 1197 OS << (*I)->getName(); 1198 } 1199 OS << '>'; 1200 spaceBeforePlaceHolder(OS); 1201 } 1202 void TypePrinter::printObjCObjectAfter(const ObjCObjectType *T, 1203 raw_ostream &OS) { 1204 if (T->qual_empty()) 1205 return printAfter(T->getBaseType(), OS); 1206 } 1207 1208 void TypePrinter::printObjCObjectPointerBefore(const ObjCObjectPointerType *T, 1209 raw_ostream &OS) { 1210 T->getPointeeType().getLocalQualifiers().print(OS, Policy, 1211 /*appendSpaceIfNonEmpty=*/true); 1212 1213 if (T->isObjCIdType() || T->isObjCQualifiedIdType()) 1214 OS << "id"; 1215 else if (T->isObjCClassType() || T->isObjCQualifiedClassType()) 1216 OS << "Class"; 1217 else if (T->isObjCSelType()) 1218 OS << "SEL"; 1219 else 1220 OS << T->getInterfaceDecl()->getName(); 1221 1222 if (!T->qual_empty()) { 1223 OS << '<'; 1224 for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(), 1225 E = T->qual_end(); 1226 I != E; ++I) { 1227 OS << (*I)->getName(); 1228 if (I+1 != E) 1229 OS << ','; 1230 } 1231 OS << '>'; 1232 } 1233 1234 if (!T->isObjCIdType() && !T->isObjCQualifiedIdType()) { 1235 OS << " *"; // Don't forget the implicit pointer. 1236 } else { 1237 spaceBeforePlaceHolder(OS); 1238 } 1239 } 1240 void TypePrinter::printObjCObjectPointerAfter(const ObjCObjectPointerType *T, 1241 raw_ostream &OS) { } 1242 1243 void TemplateSpecializationType:: 1244 PrintTemplateArgumentList(raw_ostream &OS, 1245 const TemplateArgumentListInfo &Args, 1246 const PrintingPolicy &Policy) { 1247 return PrintTemplateArgumentList(OS, 1248 Args.getArgumentArray(), 1249 Args.size(), 1250 Policy); 1251 } 1252 1253 void 1254 TemplateSpecializationType::PrintTemplateArgumentList( 1255 raw_ostream &OS, 1256 const TemplateArgument *Args, 1257 unsigned NumArgs, 1258 const PrintingPolicy &Policy, 1259 bool SkipBrackets) { 1260 if (!SkipBrackets) 1261 OS << '<'; 1262 1263 bool needSpace = false; 1264 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1265 if (Arg > 0) 1266 OS << ", "; 1267 1268 // Print the argument into a string. 1269 SmallString<128> Buf; 1270 llvm::raw_svector_ostream ArgOS(Buf); 1271 if (Args[Arg].getKind() == TemplateArgument::Pack) { 1272 PrintTemplateArgumentList(ArgOS, 1273 Args[Arg].pack_begin(), 1274 Args[Arg].pack_size(), 1275 Policy, true); 1276 } else { 1277 Args[Arg].print(Policy, ArgOS); 1278 } 1279 StringRef ArgString = ArgOS.str(); 1280 1281 // If this is the first argument and its string representation 1282 // begins with the global scope specifier ('::foo'), add a space 1283 // to avoid printing the diagraph '<:'. 1284 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1285 OS << ' '; 1286 1287 OS << ArgString; 1288 1289 needSpace = (!ArgString.empty() && ArgString.back() == '>'); 1290 } 1291 1292 // If the last character of our string is '>', add another space to 1293 // keep the two '>''s separate tokens. We don't *have* to do this in 1294 // C++0x, but it's still good hygiene. 1295 if (needSpace) 1296 OS << ' '; 1297 1298 if (!SkipBrackets) 1299 OS << '>'; 1300 } 1301 1302 // Sadly, repeat all that with TemplateArgLoc. 1303 void TemplateSpecializationType:: 1304 PrintTemplateArgumentList(raw_ostream &OS, 1305 const TemplateArgumentLoc *Args, unsigned NumArgs, 1306 const PrintingPolicy &Policy) { 1307 OS << '<'; 1308 1309 bool needSpace = false; 1310 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1311 if (Arg > 0) 1312 OS << ", "; 1313 1314 // Print the argument into a string. 1315 SmallString<128> Buf; 1316 llvm::raw_svector_ostream ArgOS(Buf); 1317 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) { 1318 PrintTemplateArgumentList(ArgOS, 1319 Args[Arg].getArgument().pack_begin(), 1320 Args[Arg].getArgument().pack_size(), 1321 Policy, true); 1322 } else { 1323 Args[Arg].getArgument().print(Policy, ArgOS); 1324 } 1325 StringRef ArgString = ArgOS.str(); 1326 1327 // If this is the first argument and its string representation 1328 // begins with the global scope specifier ('::foo'), add a space 1329 // to avoid printing the diagraph '<:'. 1330 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1331 OS << ' '; 1332 1333 OS << ArgString; 1334 1335 needSpace = (!ArgString.empty() && ArgString.back() == '>'); 1336 } 1337 1338 // If the last character of our string is '>', add another space to 1339 // keep the two '>''s separate tokens. We don't *have* to do this in 1340 // C++0x, but it's still good hygiene. 1341 if (needSpace) 1342 OS << ' '; 1343 1344 OS << '>'; 1345 } 1346 1347 void 1348 FunctionProtoType::printExceptionSpecification(std::string &S, 1349 const PrintingPolicy &Policy) 1350 const { 1351 1352 if (hasDynamicExceptionSpec()) { 1353 S += " throw("; 1354 if (getExceptionSpecType() == EST_MSAny) 1355 S += "..."; 1356 else 1357 for (unsigned I = 0, N = getNumExceptions(); I != N; ++I) { 1358 if (I) 1359 S += ", "; 1360 1361 S += getExceptionType(I).getAsString(Policy); 1362 } 1363 S += ")"; 1364 } else if (isNoexceptExceptionSpec(getExceptionSpecType())) { 1365 S += " noexcept"; 1366 if (getExceptionSpecType() == EST_ComputedNoexcept) { 1367 S += "("; 1368 llvm::raw_string_ostream EOut(S); 1369 getNoexceptExpr()->printPretty(EOut, 0, Policy); 1370 EOut.flush(); 1371 S += EOut.str(); 1372 S += ")"; 1373 } 1374 } 1375 } 1376 1377 std::string TemplateSpecializationType:: 1378 PrintTemplateArgumentList(const TemplateArgumentListInfo &Args, 1379 const PrintingPolicy &Policy) { 1380 return PrintTemplateArgumentList(Args.getArgumentArray(), 1381 Args.size(), 1382 Policy); 1383 } 1384 1385 std::string 1386 TemplateSpecializationType::PrintTemplateArgumentList( 1387 const TemplateArgument *Args, 1388 unsigned NumArgs, 1389 const PrintingPolicy &Policy, 1390 bool SkipBrackets) { 1391 std::string SpecString; 1392 if (!SkipBrackets) 1393 SpecString += '<'; 1394 1395 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1396 if (SpecString.size() > unsigned(!SkipBrackets)) 1397 SpecString += ", "; 1398 1399 // Print the argument into a string. 1400 std::string ArgString; 1401 if (Args[Arg].getKind() == TemplateArgument::Pack) { 1402 ArgString = PrintTemplateArgumentList(Args[Arg].pack_begin(), 1403 Args[Arg].pack_size(), 1404 Policy, true); 1405 } else { 1406 llvm::raw_string_ostream ArgOut(ArgString); 1407 Args[Arg].print(Policy, ArgOut); 1408 } 1409 1410 // If this is the first argument and its string representation 1411 // begins with the global scope specifier ('::foo'), add a space 1412 // to avoid printing the diagraph '<:'. 1413 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1414 SpecString += ' '; 1415 1416 SpecString += ArgString; 1417 } 1418 1419 // If the last character of our string is '>', add another space to 1420 // keep the two '>''s separate tokens. We don't *have* to do this in 1421 // C++0x, but it's still good hygiene. 1422 if (!SpecString.empty() && SpecString[SpecString.size() - 1] == '>') 1423 SpecString += ' '; 1424 1425 if (!SkipBrackets) 1426 SpecString += '>'; 1427 1428 return SpecString; 1429 } 1430 1431 // Sadly, repeat all that with TemplateArgLoc. 1432 std::string TemplateSpecializationType:: 1433 PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs, 1434 const PrintingPolicy &Policy) { 1435 std::string SpecString; 1436 SpecString += '<'; 1437 for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { 1438 if (SpecString.size() > 1) 1439 SpecString += ", "; 1440 1441 // Print the argument into a string. 1442 std::string ArgString; 1443 if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) { 1444 ArgString = PrintTemplateArgumentList( 1445 Args[Arg].getArgument().pack_begin(), 1446 Args[Arg].getArgument().pack_size(), 1447 Policy, true); 1448 } else { 1449 llvm::raw_string_ostream ArgOut(ArgString); 1450 Args[Arg].getArgument().print(Policy, ArgOut); 1451 } 1452 1453 // If this is the first argument and its string representation 1454 // begins with the global scope specifier ('::foo'), add a space 1455 // to avoid printing the diagraph '<:'. 1456 if (!Arg && !ArgString.empty() && ArgString[0] == ':') 1457 SpecString += ' '; 1458 1459 SpecString += ArgString; 1460 } 1461 1462 // If the last character of our string is '>', add another space to 1463 // keep the two '>''s separate tokens. We don't *have* to do this in 1464 // C++0x, but it's still good hygiene. 1465 if (SpecString[SpecString.size() - 1] == '>') 1466 SpecString += ' '; 1467 1468 SpecString += '>'; 1469 1470 return SpecString; 1471 } 1472 1473 void QualType::dump(const char *msg) const { 1474 if (msg) 1475 llvm::errs() << msg << ": "; 1476 LangOptions LO; 1477 print(llvm::errs(), PrintingPolicy(LO), "identifier"); 1478 llvm::errs() << '\n'; 1479 } 1480 void QualType::dump() const { 1481 dump(0); 1482 } 1483 1484 void Type::dump() const { 1485 QualType(this, 0).dump(); 1486 } 1487 1488 std::string Qualifiers::getAsString() const { 1489 LangOptions LO; 1490 return getAsString(PrintingPolicy(LO)); 1491 } 1492 1493 // Appends qualifiers to the given string, separated by spaces. Will 1494 // prefix a space if the string is non-empty. Will not append a final 1495 // space. 1496 std::string Qualifiers::getAsString(const PrintingPolicy &Policy) const { 1497 SmallString<64> Buf; 1498 llvm::raw_svector_ostream StrOS(Buf); 1499 print(StrOS, Policy); 1500 return StrOS.str(); 1501 } 1502 1503 bool Qualifiers::isEmptyWhenPrinted(const PrintingPolicy &Policy) const { 1504 if (getCVRQualifiers()) 1505 return false; 1506 1507 if (getAddressSpace()) 1508 return false; 1509 1510 if (getObjCGCAttr()) 1511 return false; 1512 1513 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) 1514 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)) 1515 return false; 1516 1517 return true; 1518 } 1519 1520 // Appends qualifiers to the given string, separated by spaces. Will 1521 // prefix a space if the string is non-empty. Will not append a final 1522 // space. 1523 void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy, 1524 bool appendSpaceIfNonEmpty) const { 1525 bool addSpace = false; 1526 1527 unsigned quals = getCVRQualifiers(); 1528 if (quals) { 1529 AppendTypeQualList(OS, quals); 1530 addSpace = true; 1531 } 1532 if (unsigned addrspace = getAddressSpace()) { 1533 if (addSpace) 1534 OS << ' '; 1535 addSpace = true; 1536 switch (addrspace) { 1537 case LangAS::opencl_global: 1538 OS << "__global"; 1539 break; 1540 case LangAS::opencl_local: 1541 OS << "__local"; 1542 break; 1543 case LangAS::opencl_constant: 1544 OS << "__constant"; 1545 break; 1546 default: 1547 OS << "__attribute__((address_space("; 1548 OS << addrspace; 1549 OS << ")))"; 1550 } 1551 } 1552 if (Qualifiers::GC gc = getObjCGCAttr()) { 1553 if (addSpace) 1554 OS << ' '; 1555 addSpace = true; 1556 if (gc == Qualifiers::Weak) 1557 OS << "__weak"; 1558 else 1559 OS << "__strong"; 1560 } 1561 if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) { 1562 if (!(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)){ 1563 if (addSpace) 1564 OS << ' '; 1565 addSpace = true; 1566 } 1567 1568 switch (lifetime) { 1569 case Qualifiers::OCL_None: llvm_unreachable("none but true"); 1570 case Qualifiers::OCL_ExplicitNone: OS << "__unsafe_unretained"; break; 1571 case Qualifiers::OCL_Strong: 1572 if (!Policy.SuppressStrongLifetime) 1573 OS << "__strong"; 1574 break; 1575 1576 case Qualifiers::OCL_Weak: OS << "__weak"; break; 1577 case Qualifiers::OCL_Autoreleasing: OS << "__autoreleasing"; break; 1578 } 1579 } 1580 1581 if (appendSpaceIfNonEmpty && addSpace) 1582 OS << ' '; 1583 } 1584 1585 std::string QualType::getAsString(const PrintingPolicy &Policy) const { 1586 std::string S; 1587 getAsStringInternal(S, Policy); 1588 return S; 1589 } 1590 1591 std::string QualType::getAsString(const Type *ty, Qualifiers qs) { 1592 std::string buffer; 1593 LangOptions options; 1594 getAsStringInternal(ty, qs, buffer, PrintingPolicy(options)); 1595 return buffer; 1596 } 1597 1598 void QualType::print(const Type *ty, Qualifiers qs, 1599 raw_ostream &OS, const PrintingPolicy &policy, 1600 const Twine &PlaceHolder) { 1601 SmallString<128> PHBuf; 1602 StringRef PH; 1603 if (PlaceHolder.isSingleStringRef()) 1604 PH = PlaceHolder.getSingleStringRef(); 1605 else 1606 PH = PlaceHolder.toStringRef(PHBuf); 1607 1608 TypePrinter(policy).print(ty, qs, OS, PH); 1609 } 1610 1611 void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, 1612 std::string &buffer, 1613 const PrintingPolicy &policy) { 1614 SmallString<256> Buf; 1615 llvm::raw_svector_ostream StrOS(Buf); 1616 TypePrinter(policy).print(ty, qs, StrOS, buffer); 1617 std::string str = StrOS.str(); 1618 buffer.swap(str); 1619 } 1620