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