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