Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1 | //===--- TypePrinter.cpp - Pretty-Print Clang Types -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to print types from Clang's type system. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
| 15 | #include "clang/AST/DeclObjC.h" |
| 16 | #include "clang/AST/DeclTemplate.h" |
| 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/Type.h" |
| 19 | #include "clang/AST/PrettyPrinter.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
| 27 | class TypePrinter { |
| 28 | PrintingPolicy Policy; |
| 29 | |
| 30 | public: |
| 31 | explicit TypePrinter(const PrintingPolicy &Policy) : Policy(Policy) { } |
Abramo Bagnara | 493ec51 | 2010-04-27 21:08:25 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 33 | void Print(QualType T, std::string &S); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 34 | void AppendScope(DeclContext *DC, std::string &S); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 35 | void PrintTag(TagDecl *T, std::string &S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 36 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 37 | #define TYPE(CLASS, PARENT) \ |
| 38 | void Print##CLASS(const CLASS##Type *T, std::string &S); |
| 39 | #include "clang/AST/TypeNodes.def" |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { |
| 44 | if (TypeQuals & Qualifiers::Const) { |
| 45 | if (!S.empty()) S += ' '; |
| 46 | S += "const"; |
| 47 | } |
| 48 | if (TypeQuals & Qualifiers::Volatile) { |
| 49 | if (!S.empty()) S += ' '; |
| 50 | S += "volatile"; |
| 51 | } |
| 52 | if (TypeQuals & Qualifiers::Restrict) { |
| 53 | if (!S.empty()) S += ' '; |
| 54 | S += "restrict"; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void TypePrinter::Print(QualType T, std::string &S) { |
| 59 | if (T.isNull()) { |
| 60 | S += "NULL TYPE"; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (Policy.SuppressSpecifiers && T->isSpecifierType()) |
| 65 | return; |
| 66 | |
| 67 | // Print qualifiers as appropriate. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 68 | Qualifiers Quals = T.getLocalQualifiers(); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 69 | if (!Quals.empty()) { |
| 70 | std::string TQS; |
| 71 | Quals.getAsStringInternal(TQS, Policy); |
| 72 | |
| 73 | if (!S.empty()) { |
| 74 | TQS += ' '; |
| 75 | TQS += S; |
| 76 | } |
| 77 | std::swap(S, TQS); |
| 78 | } |
| 79 | |
| 80 | switch (T->getTypeClass()) { |
| 81 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 82 | #define TYPE(CLASS, PARENT) case Type::CLASS: \ |
| 83 | Print##CLASS(cast<CLASS##Type>(T.getTypePtr()), S); \ |
| 84 | break; |
| 85 | #include "clang/AST/TypeNodes.def" |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void TypePrinter::PrintBuiltin(const BuiltinType *T, std::string &S) { |
| 90 | if (S.empty()) { |
| 91 | S = T->getName(Policy.LangOpts); |
| 92 | } else { |
| 93 | // Prefix the basic type, e.g. 'int X'. |
| 94 | S = ' ' + S; |
| 95 | S = T->getName(Policy.LangOpts) + S; |
| 96 | } |
| 97 | } |
| 98 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 99 | void TypePrinter::PrintComplex(const ComplexType *T, std::string &S) { |
| 100 | Print(T->getElementType(), S); |
| 101 | S = "_Complex " + S; |
| 102 | } |
| 103 | |
| 104 | void TypePrinter::PrintPointer(const PointerType *T, std::string &S) { |
| 105 | S = '*' + S; |
| 106 | |
| 107 | // Handle things like 'int (*A)[4];' correctly. |
| 108 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 109 | if (isa<ArrayType>(T->getPointeeType())) |
| 110 | S = '(' + S + ')'; |
| 111 | |
| 112 | Print(T->getPointeeType(), S); |
| 113 | } |
| 114 | |
| 115 | void TypePrinter::PrintBlockPointer(const BlockPointerType *T, std::string &S) { |
| 116 | S = '^' + S; |
| 117 | Print(T->getPointeeType(), S); |
| 118 | } |
| 119 | |
| 120 | void TypePrinter::PrintLValueReference(const LValueReferenceType *T, |
| 121 | std::string &S) { |
| 122 | S = '&' + S; |
| 123 | |
| 124 | // Handle things like 'int (&A)[4];' correctly. |
| 125 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 126 | if (isa<ArrayType>(T->getPointeeTypeAsWritten())) |
| 127 | S = '(' + S + ')'; |
| 128 | |
| 129 | Print(T->getPointeeTypeAsWritten(), S); |
| 130 | } |
| 131 | |
| 132 | void TypePrinter::PrintRValueReference(const RValueReferenceType *T, |
| 133 | std::string &S) { |
| 134 | S = "&&" + S; |
| 135 | |
| 136 | // Handle things like 'int (&&A)[4];' correctly. |
| 137 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 138 | if (isa<ArrayType>(T->getPointeeTypeAsWritten())) |
| 139 | S = '(' + S + ')'; |
| 140 | |
| 141 | Print(T->getPointeeTypeAsWritten(), S); |
| 142 | } |
| 143 | |
| 144 | void TypePrinter::PrintMemberPointer(const MemberPointerType *T, |
| 145 | std::string &S) { |
| 146 | std::string C; |
| 147 | Print(QualType(T->getClass(), 0), C); |
| 148 | C += "::*"; |
| 149 | S = C + S; |
| 150 | |
| 151 | // Handle things like 'int (Cls::*A)[4];' correctly. |
| 152 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 153 | if (isa<ArrayType>(T->getPointeeType())) |
| 154 | S = '(' + S + ')'; |
| 155 | |
| 156 | Print(T->getPointeeType(), S); |
| 157 | } |
| 158 | |
| 159 | void TypePrinter::PrintConstantArray(const ConstantArrayType *T, |
| 160 | std::string &S) { |
| 161 | S += '['; |
| 162 | S += llvm::utostr(T->getSize().getZExtValue()); |
| 163 | S += ']'; |
| 164 | |
| 165 | Print(T->getElementType(), S); |
| 166 | } |
| 167 | |
| 168 | void TypePrinter::PrintIncompleteArray(const IncompleteArrayType *T, |
| 169 | std::string &S) { |
| 170 | S += "[]"; |
| 171 | Print(T->getElementType(), S); |
| 172 | } |
| 173 | |
| 174 | void TypePrinter::PrintVariableArray(const VariableArrayType *T, |
| 175 | std::string &S) { |
| 176 | S += '['; |
| 177 | |
| 178 | if (T->getIndexTypeQualifiers().hasQualifiers()) { |
| 179 | AppendTypeQualList(S, T->getIndexTypeCVRQualifiers()); |
| 180 | S += ' '; |
| 181 | } |
| 182 | |
| 183 | if (T->getSizeModifier() == VariableArrayType::Static) |
| 184 | S += "static"; |
| 185 | else if (T->getSizeModifier() == VariableArrayType::Star) |
| 186 | S += '*'; |
| 187 | |
| 188 | if (T->getSizeExpr()) { |
| 189 | std::string SStr; |
| 190 | llvm::raw_string_ostream s(SStr); |
| 191 | T->getSizeExpr()->printPretty(s, 0, Policy); |
| 192 | S += s.str(); |
| 193 | } |
| 194 | S += ']'; |
| 195 | |
| 196 | Print(T->getElementType(), S); |
| 197 | } |
| 198 | |
| 199 | void TypePrinter::PrintDependentSizedArray(const DependentSizedArrayType *T, |
| 200 | std::string &S) { |
| 201 | S += '['; |
| 202 | |
| 203 | if (T->getSizeExpr()) { |
| 204 | std::string SStr; |
| 205 | llvm::raw_string_ostream s(SStr); |
| 206 | T->getSizeExpr()->printPretty(s, 0, Policy); |
| 207 | S += s.str(); |
| 208 | } |
| 209 | S += ']'; |
| 210 | |
| 211 | Print(T->getElementType(), S); |
| 212 | } |
| 213 | |
| 214 | void TypePrinter::PrintDependentSizedExtVector( |
| 215 | const DependentSizedExtVectorType *T, |
| 216 | std::string &S) { |
| 217 | Print(T->getElementType(), S); |
| 218 | |
| 219 | S += " __attribute__((ext_vector_type("; |
| 220 | if (T->getSizeExpr()) { |
| 221 | std::string SStr; |
| 222 | llvm::raw_string_ostream s(SStr); |
| 223 | T->getSizeExpr()->printPretty(s, 0, Policy); |
| 224 | S += s.str(); |
| 225 | } |
| 226 | S += ")))"; |
| 227 | } |
| 228 | |
| 229 | void TypePrinter::PrintVector(const VectorType *T, std::string &S) { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 230 | if (T->isAltiVec()) { |
| 231 | if (T->isPixel()) |
| 232 | S = "__vector __pixel " + S; |
| 233 | else { |
| 234 | Print(T->getElementType(), S); |
| 235 | S = "__vector " + S; |
| 236 | } |
| 237 | } else { |
| 238 | // FIXME: We prefer to print the size directly here, but have no way |
| 239 | // to get the size of the type. |
| 240 | Print(T->getElementType(), S); |
| 241 | std::string V = "__attribute__((__vector_size__("; |
| 242 | V += llvm::utostr_32(T->getNumElements()); // convert back to bytes. |
| 243 | std::string ET; |
| 244 | Print(T->getElementType(), ET); |
| 245 | V += " * sizeof(" + ET + ")))) "; |
| 246 | S = V + S; |
| 247 | } |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void TypePrinter::PrintExtVector(const ExtVectorType *T, std::string &S) { |
| 251 | S += " __attribute__((ext_vector_type("; |
| 252 | S += llvm::utostr_32(T->getNumElements()); |
| 253 | S += ")))"; |
| 254 | Print(T->getElementType(), S); |
| 255 | } |
| 256 | |
| 257 | void TypePrinter::PrintFunctionProto(const FunctionProtoType *T, |
| 258 | std::string &S) { |
| 259 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 260 | if (!S.empty()) |
| 261 | S = "(" + S + ")"; |
| 262 | |
| 263 | S += "("; |
| 264 | std::string Tmp; |
| 265 | PrintingPolicy ParamPolicy(Policy); |
| 266 | ParamPolicy.SuppressSpecifiers = false; |
| 267 | for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) { |
| 268 | if (i) S += ", "; |
| 269 | Print(T->getArgType(i), Tmp); |
| 270 | S += Tmp; |
| 271 | Tmp.clear(); |
| 272 | } |
| 273 | |
| 274 | if (T->isVariadic()) { |
| 275 | if (T->getNumArgs()) |
| 276 | S += ", "; |
| 277 | S += "..."; |
| 278 | } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) { |
| 279 | // Do not emit int() if we have a proto, emit 'int(void)'. |
| 280 | S += "void"; |
| 281 | } |
| 282 | |
| 283 | S += ")"; |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 284 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 285 | FunctionType::ExtInfo Info = T->getExtInfo(); |
| 286 | switch(Info.getCC()) { |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 287 | case CC_Default: |
| 288 | default: break; |
| 289 | case CC_C: |
| 290 | S += " __attribute__((cdecl))"; |
| 291 | break; |
| 292 | case CC_X86StdCall: |
| 293 | S += " __attribute__((stdcall))"; |
| 294 | break; |
| 295 | case CC_X86FastCall: |
| 296 | S += " __attribute__((fastcall))"; |
| 297 | break; |
| 298 | } |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 299 | if (Info.getNoReturn()) |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 300 | S += " __attribute__((noreturn))"; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 301 | if (Info.getRegParm()) |
| 302 | S += " __attribute__((regparm (" + |
| 303 | llvm::utostr_32(Info.getRegParm()) + ")))"; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 304 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 305 | if (T->hasExceptionSpec()) { |
| 306 | S += " throw("; |
| 307 | if (T->hasAnyExceptionSpec()) |
| 308 | S += "..."; |
| 309 | else |
| 310 | for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) { |
| 311 | if (I) |
| 312 | S += ", "; |
| 313 | |
| 314 | std::string ExceptionType; |
| 315 | Print(T->getExceptionType(I), ExceptionType); |
| 316 | S += ExceptionType; |
| 317 | } |
| 318 | S += ")"; |
| 319 | } |
| 320 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 321 | AppendTypeQualList(S, T->getTypeQuals()); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 322 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 323 | Print(T->getResultType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void TypePrinter::PrintFunctionNoProto(const FunctionNoProtoType *T, |
| 327 | std::string &S) { |
| 328 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 329 | if (!S.empty()) |
| 330 | S = "(" + S + ")"; |
| 331 | |
| 332 | S += "()"; |
| 333 | if (T->getNoReturnAttr()) |
| 334 | S += " __attribute__((noreturn))"; |
| 335 | Print(T->getResultType(), S); |
| 336 | } |
| 337 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 338 | static void PrintTypeSpec(const NamedDecl *D, std::string &S) { |
| 339 | IdentifierInfo *II = D->getIdentifier(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 340 | if (S.empty()) |
| 341 | S = II->getName().str(); |
| 342 | else |
| 343 | S = II->getName().str() + ' ' + S; |
| 344 | } |
| 345 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 346 | void TypePrinter::PrintUnresolvedUsing(const UnresolvedUsingType *T, |
| 347 | std::string &S) { |
| 348 | PrintTypeSpec(T->getDecl(), S); |
| 349 | } |
| 350 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 351 | void TypePrinter::PrintTypedef(const TypedefType *T, std::string &S) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 352 | PrintTypeSpec(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void TypePrinter::PrintTypeOfExpr(const TypeOfExprType *T, std::string &S) { |
| 356 | if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. |
| 357 | S = ' ' + S; |
| 358 | std::string Str; |
| 359 | llvm::raw_string_ostream s(Str); |
| 360 | T->getUnderlyingExpr()->printPretty(s, 0, Policy); |
| 361 | S = "typeof " + s.str() + S; |
| 362 | } |
| 363 | |
| 364 | void TypePrinter::PrintTypeOf(const TypeOfType *T, std::string &S) { |
| 365 | if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. |
| 366 | S = ' ' + S; |
| 367 | std::string Tmp; |
| 368 | Print(T->getUnderlyingType(), Tmp); |
| 369 | S = "typeof(" + Tmp + ")" + S; |
| 370 | } |
| 371 | |
| 372 | void TypePrinter::PrintDecltype(const DecltypeType *T, std::string &S) { |
| 373 | if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'. |
| 374 | S = ' ' + S; |
| 375 | std::string Str; |
| 376 | llvm::raw_string_ostream s(Str); |
| 377 | T->getUnderlyingExpr()->printPretty(s, 0, Policy); |
| 378 | S = "decltype(" + s.str() + ")" + S; |
| 379 | } |
| 380 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 381 | /// Appends the given scope to the end of a string. |
| 382 | void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) { |
| 383 | if (DC->isTranslationUnit()) return; |
| 384 | AppendScope(DC->getParent(), Buffer); |
| 385 | |
| 386 | unsigned OldSize = Buffer.size(); |
| 387 | |
| 388 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 389 | if (NS->getIdentifier()) |
| 390 | Buffer += NS->getNameAsString(); |
| 391 | else |
| 392 | Buffer += "<anonymous>"; |
| 393 | } else if (ClassTemplateSpecializationDecl *Spec |
| 394 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
| 395 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 396 | std::string TemplateArgsStr |
| 397 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 398 | TemplateArgs.getFlatArgumentList(), |
| 399 | TemplateArgs.flat_size(), |
| 400 | Policy); |
| 401 | Buffer += Spec->getIdentifier()->getName(); |
| 402 | Buffer += TemplateArgsStr; |
| 403 | } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { |
| 404 | if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl()) |
| 405 | Buffer += Typedef->getIdentifier()->getName(); |
| 406 | else if (Tag->getIdentifier()) |
| 407 | Buffer += Tag->getIdentifier()->getName(); |
| 408 | } |
| 409 | |
| 410 | if (Buffer.size() != OldSize) |
| 411 | Buffer += "::"; |
| 412 | } |
| 413 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 414 | void TypePrinter::PrintTag(TagDecl *D, std::string &InnerString) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 415 | if (Policy.SuppressTag) |
| 416 | return; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 417 | |
| 418 | std::string Buffer; |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 419 | bool HasKindDecoration = false; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 420 | |
| 421 | // We don't print tags unless this is an elaborated type. |
| 422 | // In C, we just assume every RecordType is an elaborated type. |
| 423 | if (!Policy.LangOpts.CPlusPlus && !D->getTypedefForAnonDecl()) { |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 424 | HasKindDecoration = true; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 425 | Buffer += D->getKindName(); |
| 426 | Buffer += ' '; |
| 427 | } |
| 428 | |
| 429 | if (!Policy.SuppressScope) |
| 430 | // Compute the full nested-name-specifier for this type. In C, |
| 431 | // this will always be empty. |
| 432 | AppendScope(D->getDeclContext(), Buffer); |
| 433 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 434 | if (const IdentifierInfo *II = D->getIdentifier()) |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 435 | Buffer += II->getNameStart(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 436 | else if (TypedefDecl *Typedef = D->getTypedefForAnonDecl()) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 437 | assert(Typedef->getIdentifier() && "Typedef without identifier?"); |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 438 | Buffer += Typedef->getIdentifier()->getNameStart(); |
| 439 | } else { |
| 440 | // Make an unambiguous representation for anonymous types, e.g. |
| 441 | // <anonymous enum at /usr/include/string.h:120:9> |
| 442 | llvm::raw_string_ostream OS(Buffer); |
| 443 | OS << "<anonymous"; |
| 444 | |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 445 | if (Policy.AnonymousTagLocations) { |
| 446 | // Suppress the redundant tag keyword if we just printed one. |
| 447 | // We don't have to worry about ElaboratedTypes here because you can't |
| 448 | // refer to an anonymous type with one. |
| 449 | if (!HasKindDecoration) |
| 450 | OS << " " << D->getKindName(); |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 451 | |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 452 | PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( |
| 453 | D->getLocation()); |
| 454 | OS << " at " << PLoc.getFilename() |
| 455 | << ':' << PLoc.getLine() |
| 456 | << ':' << PLoc.getColumn(); |
| 457 | } |
| 458 | |
| 459 | OS << '>'; |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 460 | OS.flush(); |
| 461 | } |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 463 | // If this is a class template specialization, print the template |
| 464 | // arguments. |
| 465 | if (ClassTemplateSpecializationDecl *Spec |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 466 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 467 | const TemplateArgument *Args; |
| 468 | unsigned NumArgs; |
| 469 | if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { |
| 470 | const TemplateSpecializationType *TST = |
| 471 | cast<TemplateSpecializationType>(TAW->getType()); |
| 472 | Args = TST->getArgs(); |
| 473 | NumArgs = TST->getNumArgs(); |
| 474 | } else { |
| 475 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 476 | Args = TemplateArgs.getFlatArgumentList(); |
| 477 | NumArgs = TemplateArgs.flat_size(); |
| 478 | } |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 479 | Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args, |
| 480 | NumArgs, |
| 481 | Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 482 | } |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 483 | |
| 484 | if (!InnerString.empty()) { |
| 485 | Buffer += ' '; |
| 486 | Buffer += InnerString; |
| 487 | } |
| 488 | |
| 489 | std::swap(Buffer, InnerString); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void TypePrinter::PrintRecord(const RecordType *T, std::string &S) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 493 | PrintTag(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void TypePrinter::PrintEnum(const EnumType *T, std::string &S) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 497 | PrintTag(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 500 | void TypePrinter::PrintTemplateTypeParm(const TemplateTypeParmType *T, |
| 501 | std::string &S) { |
| 502 | if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'. |
| 503 | S = ' ' + S; |
| 504 | |
| 505 | if (!T->getName()) |
| 506 | S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' + |
| 507 | llvm::utostr_32(T->getIndex()) + S; |
| 508 | else |
| 509 | S = T->getName()->getName().str() + S; |
| 510 | } |
| 511 | |
| 512 | void TypePrinter::PrintSubstTemplateTypeParm(const SubstTemplateTypeParmType *T, |
| 513 | std::string &S) { |
| 514 | Print(T->getReplacementType(), S); |
| 515 | } |
| 516 | |
| 517 | void TypePrinter::PrintTemplateSpecialization( |
| 518 | const TemplateSpecializationType *T, |
| 519 | std::string &S) { |
| 520 | std::string SpecString; |
| 521 | |
| 522 | { |
| 523 | llvm::raw_string_ostream OS(SpecString); |
| 524 | T->getTemplateName().print(OS, Policy); |
| 525 | } |
| 526 | |
| 527 | SpecString += TemplateSpecializationType::PrintTemplateArgumentList( |
| 528 | T->getArgs(), |
| 529 | T->getNumArgs(), |
| 530 | Policy); |
| 531 | if (S.empty()) |
| 532 | S.swap(SpecString); |
| 533 | else |
| 534 | S = SpecString + ' ' + S; |
| 535 | } |
| 536 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 537 | void TypePrinter::PrintInjectedClassName(const InjectedClassNameType *T, |
| 538 | std::string &S) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 539 | PrintTemplateSpecialization(T->getInjectedTST(), S); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 542 | void TypePrinter::PrintElaborated(const ElaboratedType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 543 | std::string MyString; |
| 544 | |
| 545 | { |
| 546 | llvm::raw_string_ostream OS(MyString); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 547 | OS << TypeWithKeyword::getKeywordName(T->getKeyword()); |
| 548 | if (T->getKeyword() != ETK_None) |
| 549 | OS << " "; |
| 550 | NestedNameSpecifier* Qualifier = T->getQualifier(); |
| 551 | if (Qualifier) |
| 552 | Qualifier->print(OS, Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | std::string TypeStr; |
| 556 | PrintingPolicy InnerPolicy(Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 557 | InnerPolicy.SuppressScope = true; |
| 558 | TypePrinter(InnerPolicy).Print(T->getNamedType(), TypeStr); |
| 559 | |
| 560 | MyString += TypeStr; |
| 561 | if (S.empty()) |
| 562 | S.swap(MyString); |
| 563 | else |
| 564 | S = MyString + ' ' + S; |
| 565 | } |
| 566 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 567 | void TypePrinter::PrintDependentName(const DependentNameType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 568 | std::string MyString; |
| 569 | |
| 570 | { |
| 571 | llvm::raw_string_ostream OS(MyString); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 572 | OS << TypeWithKeyword::getKeywordName(T->getKeyword()); |
| 573 | if (T->getKeyword() != ETK_None) |
| 574 | OS << " "; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 575 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 576 | T->getQualifier()->print(OS, Policy); |
| 577 | |
| 578 | if (const IdentifierInfo *Ident = T->getIdentifier()) |
| 579 | OS << Ident->getName(); |
| 580 | else if (const TemplateSpecializationType *Spec = T->getTemplateId()) { |
| 581 | Spec->getTemplateName().print(OS, Policy, true); |
| 582 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
| 583 | Spec->getArgs(), |
| 584 | Spec->getNumArgs(), |
| 585 | Policy); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (S.empty()) |
| 590 | S.swap(MyString); |
| 591 | else |
| 592 | S = MyString + ' ' + S; |
| 593 | } |
| 594 | |
| 595 | void TypePrinter::PrintObjCInterface(const ObjCInterfaceType *T, |
| 596 | std::string &S) { |
| 597 | if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 598 | S = ' ' + S; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame^] | 599 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 600 | std::string ObjCQIString = T->getDecl()->getNameAsString(); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 601 | S = ObjCQIString + S; |
| 602 | } |
| 603 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame^] | 604 | void TypePrinter::PrintObjCObject(const ObjCObjectType *T, |
| 605 | std::string &S) { |
| 606 | if (T->qual_empty()) |
| 607 | return Print(T->getBaseType(), S); |
| 608 | |
| 609 | std::string tmp; |
| 610 | Print(T->getBaseType(), tmp); |
| 611 | tmp += '<'; |
| 612 | bool isFirst = true; |
| 613 | for (ObjCObjectType::qual_iterator |
| 614 | I = T->qual_begin(), E = T->qual_end(); I != E; ++I) { |
| 615 | if (isFirst) |
| 616 | isFirst = false; |
| 617 | else |
| 618 | tmp += ','; |
| 619 | tmp += (*I)->getNameAsString(); |
| 620 | } |
| 621 | tmp += '>'; |
| 622 | |
| 623 | if (!S.empty()) { |
| 624 | tmp += ' '; |
| 625 | tmp += S; |
| 626 | } |
| 627 | std::swap(tmp, S); |
| 628 | } |
| 629 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 630 | void TypePrinter::PrintObjCObjectPointer(const ObjCObjectPointerType *T, |
| 631 | std::string &S) { |
| 632 | std::string ObjCQIString; |
| 633 | |
| 634 | if (T->isObjCIdType() || T->isObjCQualifiedIdType()) |
| 635 | ObjCQIString = "id"; |
| 636 | else if (T->isObjCClassType() || T->isObjCQualifiedClassType()) |
| 637 | ObjCQIString = "Class"; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 638 | else if (T->isObjCSelType()) |
| 639 | ObjCQIString = "SEL"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 640 | else |
| 641 | ObjCQIString = T->getInterfaceDecl()->getNameAsString(); |
| 642 | |
| 643 | if (!T->qual_empty()) { |
| 644 | ObjCQIString += '<'; |
| 645 | for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(), |
| 646 | E = T->qual_end(); |
| 647 | I != E; ++I) { |
| 648 | ObjCQIString += (*I)->getNameAsString(); |
| 649 | if (I+1 != E) |
| 650 | ObjCQIString += ','; |
| 651 | } |
| 652 | ObjCQIString += '>'; |
| 653 | } |
| 654 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 655 | T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString, |
| 656 | Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 657 | |
| 658 | if (!T->isObjCIdType() && !T->isObjCQualifiedIdType()) |
| 659 | ObjCQIString += " *"; // Don't forget the implicit pointer. |
| 660 | else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 661 | S = ' ' + S; |
| 662 | |
| 663 | S = ObjCQIString + S; |
| 664 | } |
| 665 | |
| 666 | static void PrintTemplateArgument(std::string &Buffer, |
| 667 | const TemplateArgument &Arg, |
| 668 | const PrintingPolicy &Policy) { |
| 669 | switch (Arg.getKind()) { |
| 670 | case TemplateArgument::Null: |
| 671 | assert(false && "Null template argument"); |
| 672 | break; |
| 673 | |
| 674 | case TemplateArgument::Type: |
| 675 | Arg.getAsType().getAsStringInternal(Buffer, Policy); |
| 676 | break; |
| 677 | |
| 678 | case TemplateArgument::Declaration: |
| 679 | Buffer = cast<NamedDecl>(Arg.getAsDecl())->getNameAsString(); |
| 680 | break; |
| 681 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 682 | case TemplateArgument::Template: { |
| 683 | llvm::raw_string_ostream s(Buffer); |
| 684 | Arg.getAsTemplate().print(s, Policy); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 685 | break; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 688 | case TemplateArgument::Integral: |
| 689 | Buffer = Arg.getAsIntegral()->toString(10, true); |
| 690 | break; |
| 691 | |
| 692 | case TemplateArgument::Expression: { |
| 693 | llvm::raw_string_ostream s(Buffer); |
| 694 | Arg.getAsExpr()->printPretty(s, 0, Policy); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | case TemplateArgument::Pack: |
| 699 | assert(0 && "FIXME: Implement!"); |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 704 | std::string TemplateSpecializationType:: |
| 705 | PrintTemplateArgumentList(const TemplateArgumentListInfo &Args, |
| 706 | const PrintingPolicy &Policy) { |
| 707 | return PrintTemplateArgumentList(Args.getArgumentArray(), |
| 708 | Args.size(), |
| 709 | Policy); |
| 710 | } |
| 711 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 712 | std::string |
| 713 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 714 | const TemplateArgument *Args, |
| 715 | unsigned NumArgs, |
| 716 | const PrintingPolicy &Policy) { |
| 717 | std::string SpecString; |
| 718 | SpecString += '<'; |
| 719 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 720 | if (Arg) |
| 721 | SpecString += ", "; |
| 722 | |
| 723 | // Print the argument into a string. |
| 724 | std::string ArgString; |
| 725 | PrintTemplateArgument(ArgString, Args[Arg], Policy); |
| 726 | |
| 727 | // If this is the first argument and its string representation |
| 728 | // begins with the global scope specifier ('::foo'), add a space |
| 729 | // to avoid printing the diagraph '<:'. |
| 730 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 731 | SpecString += ' '; |
| 732 | |
| 733 | SpecString += ArgString; |
| 734 | } |
| 735 | |
| 736 | // If the last character of our string is '>', add another space to |
| 737 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 738 | // C++0x, but it's still good hygiene. |
| 739 | if (SpecString[SpecString.size() - 1] == '>') |
| 740 | SpecString += ' '; |
| 741 | |
| 742 | SpecString += '>'; |
| 743 | |
| 744 | return SpecString; |
| 745 | } |
| 746 | |
| 747 | // Sadly, repeat all that with TemplateArgLoc. |
| 748 | std::string TemplateSpecializationType:: |
| 749 | PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs, |
| 750 | const PrintingPolicy &Policy) { |
| 751 | std::string SpecString; |
| 752 | SpecString += '<'; |
| 753 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 754 | if (Arg) |
| 755 | SpecString += ", "; |
| 756 | |
| 757 | // Print the argument into a string. |
| 758 | std::string ArgString; |
| 759 | PrintTemplateArgument(ArgString, Args[Arg].getArgument(), Policy); |
| 760 | |
| 761 | // If this is the first argument and its string representation |
| 762 | // begins with the global scope specifier ('::foo'), add a space |
| 763 | // to avoid printing the diagraph '<:'. |
| 764 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 765 | SpecString += ' '; |
| 766 | |
| 767 | SpecString += ArgString; |
| 768 | } |
| 769 | |
| 770 | // If the last character of our string is '>', add another space to |
| 771 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 772 | // C++0x, but it's still good hygiene. |
| 773 | if (SpecString[SpecString.size() - 1] == '>') |
| 774 | SpecString += ' '; |
| 775 | |
| 776 | SpecString += '>'; |
| 777 | |
| 778 | return SpecString; |
| 779 | } |
| 780 | |
| 781 | void QualType::dump(const char *msg) const { |
| 782 | std::string R = "identifier"; |
| 783 | LangOptions LO; |
| 784 | getAsStringInternal(R, PrintingPolicy(LO)); |
| 785 | if (msg) |
Daniel Dunbar | e7cb7e4 | 2009-12-03 09:14:02 +0000 | [diff] [blame] | 786 | llvm::errs() << msg << ": "; |
| 787 | llvm::errs() << R << "\n"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 788 | } |
| 789 | void QualType::dump() const { |
| 790 | dump(""); |
| 791 | } |
| 792 | |
| 793 | void Type::dump() const { |
| 794 | QualType(this, 0).dump(); |
| 795 | } |
| 796 | |
| 797 | std::string Qualifiers::getAsString() const { |
| 798 | LangOptions LO; |
| 799 | return getAsString(PrintingPolicy(LO)); |
| 800 | } |
| 801 | |
| 802 | // Appends qualifiers to the given string, separated by spaces. Will |
| 803 | // prefix a space if the string is non-empty. Will not append a final |
| 804 | // space. |
| 805 | void Qualifiers::getAsStringInternal(std::string &S, |
| 806 | const PrintingPolicy&) const { |
| 807 | AppendTypeQualList(S, getCVRQualifiers()); |
| 808 | if (unsigned AddressSpace = getAddressSpace()) { |
| 809 | if (!S.empty()) S += ' '; |
| 810 | S += "__attribute__((address_space("; |
| 811 | S += llvm::utostr_32(AddressSpace); |
| 812 | S += ")))"; |
| 813 | } |
| 814 | if (Qualifiers::GC GCAttrType = getObjCGCAttr()) { |
| 815 | if (!S.empty()) S += ' '; |
| 816 | S += "__attribute__((objc_gc("; |
| 817 | if (GCAttrType == Qualifiers::Weak) |
| 818 | S += "weak"; |
| 819 | else |
| 820 | S += "strong"; |
| 821 | S += ")))"; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | std::string QualType::getAsString() const { |
| 826 | std::string S; |
| 827 | LangOptions LO; |
| 828 | getAsStringInternal(S, PrintingPolicy(LO)); |
| 829 | return S; |
| 830 | } |
| 831 | |
| 832 | void QualType::getAsStringInternal(std::string &S, |
| 833 | const PrintingPolicy &Policy) const { |
| 834 | TypePrinter Printer(Policy); |
| 835 | Printer.Print(*this, S); |
| 836 | } |