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) { } |
| 32 | |
| 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 | |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 285 | switch(T->getCallConv()) { |
| 286 | case CC_Default: |
| 287 | default: break; |
| 288 | case CC_C: |
| 289 | S += " __attribute__((cdecl))"; |
| 290 | break; |
| 291 | case CC_X86StdCall: |
| 292 | S += " __attribute__((stdcall))"; |
| 293 | break; |
| 294 | case CC_X86FastCall: |
| 295 | S += " __attribute__((fastcall))"; |
| 296 | break; |
| 297 | } |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 298 | if (T->getNoReturnAttr()) |
| 299 | S += " __attribute__((noreturn))"; |
| 300 | |
| 301 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 302 | if (T->hasExceptionSpec()) { |
| 303 | S += " throw("; |
| 304 | if (T->hasAnyExceptionSpec()) |
| 305 | S += "..."; |
| 306 | else |
| 307 | for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) { |
| 308 | if (I) |
| 309 | S += ", "; |
| 310 | |
| 311 | std::string ExceptionType; |
| 312 | Print(T->getExceptionType(I), ExceptionType); |
| 313 | S += ExceptionType; |
| 314 | } |
| 315 | S += ")"; |
| 316 | } |
| 317 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 318 | AppendTypeQualList(S, T->getTypeQuals()); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 319 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 320 | Print(T->getResultType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void TypePrinter::PrintFunctionNoProto(const FunctionNoProtoType *T, |
| 324 | std::string &S) { |
| 325 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 326 | if (!S.empty()) |
| 327 | S = "(" + S + ")"; |
| 328 | |
| 329 | S += "()"; |
| 330 | if (T->getNoReturnAttr()) |
| 331 | S += " __attribute__((noreturn))"; |
| 332 | Print(T->getResultType(), S); |
| 333 | } |
| 334 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 335 | static void PrintTypeSpec(const NamedDecl *D, std::string &S) { |
| 336 | IdentifierInfo *II = D->getIdentifier(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 337 | if (S.empty()) |
| 338 | S = II->getName().str(); |
| 339 | else |
| 340 | S = II->getName().str() + ' ' + S; |
| 341 | } |
| 342 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 343 | void TypePrinter::PrintUnresolvedUsing(const UnresolvedUsingType *T, |
| 344 | std::string &S) { |
| 345 | PrintTypeSpec(T->getDecl(), S); |
| 346 | } |
| 347 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 348 | void TypePrinter::PrintTypedef(const TypedefType *T, std::string &S) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 349 | PrintTypeSpec(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void TypePrinter::PrintTypeOfExpr(const TypeOfExprType *T, std::string &S) { |
| 353 | if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. |
| 354 | S = ' ' + S; |
| 355 | std::string Str; |
| 356 | llvm::raw_string_ostream s(Str); |
| 357 | T->getUnderlyingExpr()->printPretty(s, 0, Policy); |
| 358 | S = "typeof " + s.str() + S; |
| 359 | } |
| 360 | |
| 361 | void TypePrinter::PrintTypeOf(const TypeOfType *T, std::string &S) { |
| 362 | if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. |
| 363 | S = ' ' + S; |
| 364 | std::string Tmp; |
| 365 | Print(T->getUnderlyingType(), Tmp); |
| 366 | S = "typeof(" + Tmp + ")" + S; |
| 367 | } |
| 368 | |
| 369 | void TypePrinter::PrintDecltype(const DecltypeType *T, std::string &S) { |
| 370 | if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'. |
| 371 | S = ' ' + S; |
| 372 | std::string Str; |
| 373 | llvm::raw_string_ostream s(Str); |
| 374 | T->getUnderlyingExpr()->printPretty(s, 0, Policy); |
| 375 | S = "decltype(" + s.str() + ")" + S; |
| 376 | } |
| 377 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 378 | /// Appends the given scope to the end of a string. |
| 379 | void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) { |
| 380 | if (DC->isTranslationUnit()) return; |
| 381 | AppendScope(DC->getParent(), Buffer); |
| 382 | |
| 383 | unsigned OldSize = Buffer.size(); |
| 384 | |
| 385 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 386 | if (NS->getIdentifier()) |
| 387 | Buffer += NS->getNameAsString(); |
| 388 | else |
| 389 | Buffer += "<anonymous>"; |
| 390 | } else if (ClassTemplateSpecializationDecl *Spec |
| 391 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
| 392 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 393 | std::string TemplateArgsStr |
| 394 | = TemplateSpecializationType::PrintTemplateArgumentList( |
| 395 | TemplateArgs.getFlatArgumentList(), |
| 396 | TemplateArgs.flat_size(), |
| 397 | Policy); |
| 398 | Buffer += Spec->getIdentifier()->getName(); |
| 399 | Buffer += TemplateArgsStr; |
| 400 | } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { |
| 401 | if (TypedefDecl *Typedef = Tag->getTypedefForAnonDecl()) |
| 402 | Buffer += Typedef->getIdentifier()->getName(); |
| 403 | else if (Tag->getIdentifier()) |
| 404 | Buffer += Tag->getIdentifier()->getName(); |
| 405 | } |
| 406 | |
| 407 | if (Buffer.size() != OldSize) |
| 408 | Buffer += "::"; |
| 409 | } |
| 410 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 411 | void TypePrinter::PrintTag(TagDecl *D, std::string &InnerString) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 412 | if (Policy.SuppressTag) |
| 413 | return; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 414 | |
| 415 | std::string Buffer; |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame^] | 416 | bool HasKindDecoration = false; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 417 | |
| 418 | // We don't print tags unless this is an elaborated type. |
| 419 | // In C, we just assume every RecordType is an elaborated type. |
| 420 | if (!Policy.LangOpts.CPlusPlus && !D->getTypedefForAnonDecl()) { |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame^] | 421 | HasKindDecoration = true; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 422 | Buffer += D->getKindName(); |
| 423 | Buffer += ' '; |
| 424 | } |
| 425 | |
| 426 | if (!Policy.SuppressScope) |
| 427 | // Compute the full nested-name-specifier for this type. In C, |
| 428 | // this will always be empty. |
| 429 | AppendScope(D->getDeclContext(), Buffer); |
| 430 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 431 | if (const IdentifierInfo *II = D->getIdentifier()) |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame^] | 432 | Buffer += II->getNameStart(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 433 | else if (TypedefDecl *Typedef = D->getTypedefForAnonDecl()) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 434 | assert(Typedef->getIdentifier() && "Typedef without identifier?"); |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame^] | 435 | Buffer += Typedef->getIdentifier()->getNameStart(); |
| 436 | } else { |
| 437 | // Make an unambiguous representation for anonymous types, e.g. |
| 438 | // <anonymous enum at /usr/include/string.h:120:9> |
| 439 | llvm::raw_string_ostream OS(Buffer); |
| 440 | OS << "<anonymous"; |
| 441 | |
| 442 | // Suppress the redundant tag keyword if we just printed one. |
| 443 | // We don't have to worry about ElaboratedTypes here because you can't |
| 444 | // refer to an anonymous type with one. |
| 445 | if (!HasKindDecoration) |
| 446 | OS << " " << D->getKindName(); |
| 447 | |
| 448 | PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( |
| 449 | D->getLocation()); |
| 450 | OS << " at " << PLoc.getFilename() |
| 451 | << ':' << PLoc.getLine() |
| 452 | << ':' << PLoc.getColumn() |
| 453 | << '>'; |
| 454 | OS.flush(); |
| 455 | } |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 456 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 457 | // If this is a class template specialization, print the template |
| 458 | // arguments. |
| 459 | if (ClassTemplateSpecializationDecl *Spec |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 460 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 461 | const TemplateArgument *Args; |
| 462 | unsigned NumArgs; |
| 463 | if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { |
| 464 | const TemplateSpecializationType *TST = |
| 465 | cast<TemplateSpecializationType>(TAW->getType()); |
| 466 | Args = TST->getArgs(); |
| 467 | NumArgs = TST->getNumArgs(); |
| 468 | } else { |
| 469 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 470 | Args = TemplateArgs.getFlatArgumentList(); |
| 471 | NumArgs = TemplateArgs.flat_size(); |
| 472 | } |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 473 | Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args, |
| 474 | NumArgs, |
| 475 | Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 476 | } |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 477 | |
| 478 | if (!InnerString.empty()) { |
| 479 | Buffer += ' '; |
| 480 | Buffer += InnerString; |
| 481 | } |
| 482 | |
| 483 | std::swap(Buffer, InnerString); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void TypePrinter::PrintRecord(const RecordType *T, std::string &S) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 487 | PrintTag(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void TypePrinter::PrintEnum(const EnumType *T, std::string &S) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 491 | PrintTag(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void TypePrinter::PrintElaborated(const ElaboratedType *T, std::string &S) { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 495 | Print(T->getUnderlyingType(), S); |
John McCall | 327fb2d | 2010-03-10 21:05:46 +0000 | [diff] [blame] | 496 | |
| 497 | // We don't actually make these in C, but the language options |
| 498 | // sometimes lie to us -- for example, if someone calls |
| 499 | // QualType::getAsString(). Just suppress the redundant tag if so. |
| 500 | if (Policy.LangOpts.CPlusPlus) |
| 501 | S = std::string(T->getNameForTagKind(T->getTagKind())) + ' ' + S; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void TypePrinter::PrintTemplateTypeParm(const TemplateTypeParmType *T, |
| 505 | std::string &S) { |
| 506 | if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'. |
| 507 | S = ' ' + S; |
| 508 | |
| 509 | if (!T->getName()) |
| 510 | S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' + |
| 511 | llvm::utostr_32(T->getIndex()) + S; |
| 512 | else |
| 513 | S = T->getName()->getName().str() + S; |
| 514 | } |
| 515 | |
| 516 | void TypePrinter::PrintSubstTemplateTypeParm(const SubstTemplateTypeParmType *T, |
| 517 | std::string &S) { |
| 518 | Print(T->getReplacementType(), S); |
| 519 | } |
| 520 | |
| 521 | void TypePrinter::PrintTemplateSpecialization( |
| 522 | const TemplateSpecializationType *T, |
| 523 | std::string &S) { |
| 524 | std::string SpecString; |
| 525 | |
| 526 | { |
| 527 | llvm::raw_string_ostream OS(SpecString); |
| 528 | T->getTemplateName().print(OS, Policy); |
| 529 | } |
| 530 | |
| 531 | SpecString += TemplateSpecializationType::PrintTemplateArgumentList( |
| 532 | T->getArgs(), |
| 533 | T->getNumArgs(), |
| 534 | Policy); |
| 535 | if (S.empty()) |
| 536 | S.swap(SpecString); |
| 537 | else |
| 538 | S = SpecString + ' ' + S; |
| 539 | } |
| 540 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 541 | void TypePrinter::PrintInjectedClassName(const InjectedClassNameType *T, |
| 542 | std::string &S) { |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 543 | PrintTemplateSpecialization(T->getUnderlyingTST(), S); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 546 | void TypePrinter::PrintQualifiedName(const QualifiedNameType *T, |
| 547 | std::string &S) { |
| 548 | std::string MyString; |
| 549 | |
| 550 | { |
| 551 | llvm::raw_string_ostream OS(MyString); |
| 552 | T->getQualifier()->print(OS, Policy); |
| 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 | |
| 567 | void TypePrinter::PrintTypename(const TypenameType *T, std::string &S) { |
| 568 | std::string MyString; |
| 569 | |
| 570 | { |
| 571 | llvm::raw_string_ostream OS(MyString); |
| 572 | OS << "typename "; |
| 573 | T->getQualifier()->print(OS, Policy); |
| 574 | |
| 575 | if (const IdentifierInfo *Ident = T->getIdentifier()) |
| 576 | OS << Ident->getName(); |
| 577 | else if (const TemplateSpecializationType *Spec = T->getTemplateId()) { |
| 578 | Spec->getTemplateName().print(OS, Policy, true); |
| 579 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
| 580 | Spec->getArgs(), |
| 581 | Spec->getNumArgs(), |
| 582 | Policy); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (S.empty()) |
| 587 | S.swap(MyString); |
| 588 | else |
| 589 | S = MyString + ' ' + S; |
| 590 | } |
| 591 | |
| 592 | void TypePrinter::PrintObjCInterface(const ObjCInterfaceType *T, |
| 593 | std::string &S) { |
| 594 | if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 595 | S = ' ' + S; |
| 596 | |
| 597 | std::string ObjCQIString = T->getDecl()->getNameAsString(); |
| 598 | if (T->getNumProtocols()) { |
| 599 | ObjCQIString += '<'; |
| 600 | bool isFirst = true; |
| 601 | for (ObjCInterfaceType::qual_iterator I = T->qual_begin(), |
| 602 | E = T->qual_end(); |
| 603 | I != E; ++I) { |
| 604 | if (isFirst) |
| 605 | isFirst = false; |
| 606 | else |
| 607 | ObjCQIString += ','; |
| 608 | ObjCQIString += (*I)->getNameAsString(); |
| 609 | } |
| 610 | ObjCQIString += '>'; |
| 611 | } |
| 612 | S = ObjCQIString + S; |
| 613 | } |
| 614 | |
| 615 | void TypePrinter::PrintObjCObjectPointer(const ObjCObjectPointerType *T, |
| 616 | std::string &S) { |
| 617 | std::string ObjCQIString; |
| 618 | |
| 619 | if (T->isObjCIdType() || T->isObjCQualifiedIdType()) |
| 620 | ObjCQIString = "id"; |
| 621 | else if (T->isObjCClassType() || T->isObjCQualifiedClassType()) |
| 622 | ObjCQIString = "Class"; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 623 | else if (T->isObjCSelType()) |
| 624 | ObjCQIString = "SEL"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 625 | else |
| 626 | ObjCQIString = T->getInterfaceDecl()->getNameAsString(); |
| 627 | |
| 628 | if (!T->qual_empty()) { |
| 629 | ObjCQIString += '<'; |
| 630 | for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(), |
| 631 | E = T->qual_end(); |
| 632 | I != E; ++I) { |
| 633 | ObjCQIString += (*I)->getNameAsString(); |
| 634 | if (I+1 != E) |
| 635 | ObjCQIString += ','; |
| 636 | } |
| 637 | ObjCQIString += '>'; |
| 638 | } |
| 639 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 640 | T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString, |
| 641 | Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 642 | |
| 643 | if (!T->isObjCIdType() && !T->isObjCQualifiedIdType()) |
| 644 | ObjCQIString += " *"; // Don't forget the implicit pointer. |
| 645 | else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 646 | S = ' ' + S; |
| 647 | |
| 648 | S = ObjCQIString + S; |
| 649 | } |
| 650 | |
| 651 | static void PrintTemplateArgument(std::string &Buffer, |
| 652 | const TemplateArgument &Arg, |
| 653 | const PrintingPolicy &Policy) { |
| 654 | switch (Arg.getKind()) { |
| 655 | case TemplateArgument::Null: |
| 656 | assert(false && "Null template argument"); |
| 657 | break; |
| 658 | |
| 659 | case TemplateArgument::Type: |
| 660 | Arg.getAsType().getAsStringInternal(Buffer, Policy); |
| 661 | break; |
| 662 | |
| 663 | case TemplateArgument::Declaration: |
| 664 | Buffer = cast<NamedDecl>(Arg.getAsDecl())->getNameAsString(); |
| 665 | break; |
| 666 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 667 | case TemplateArgument::Template: { |
| 668 | llvm::raw_string_ostream s(Buffer); |
| 669 | Arg.getAsTemplate().print(s, Policy); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 670 | break; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 673 | case TemplateArgument::Integral: |
| 674 | Buffer = Arg.getAsIntegral()->toString(10, true); |
| 675 | break; |
| 676 | |
| 677 | case TemplateArgument::Expression: { |
| 678 | llvm::raw_string_ostream s(Buffer); |
| 679 | Arg.getAsExpr()->printPretty(s, 0, Policy); |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | case TemplateArgument::Pack: |
| 684 | assert(0 && "FIXME: Implement!"); |
| 685 | break; |
| 686 | } |
| 687 | } |
| 688 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 689 | std::string TemplateSpecializationType:: |
| 690 | PrintTemplateArgumentList(const TemplateArgumentListInfo &Args, |
| 691 | const PrintingPolicy &Policy) { |
| 692 | return PrintTemplateArgumentList(Args.getArgumentArray(), |
| 693 | Args.size(), |
| 694 | Policy); |
| 695 | } |
| 696 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 697 | std::string |
| 698 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 699 | const TemplateArgument *Args, |
| 700 | unsigned NumArgs, |
| 701 | const PrintingPolicy &Policy) { |
| 702 | std::string SpecString; |
| 703 | SpecString += '<'; |
| 704 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 705 | if (Arg) |
| 706 | SpecString += ", "; |
| 707 | |
| 708 | // Print the argument into a string. |
| 709 | std::string ArgString; |
| 710 | PrintTemplateArgument(ArgString, Args[Arg], Policy); |
| 711 | |
| 712 | // If this is the first argument and its string representation |
| 713 | // begins with the global scope specifier ('::foo'), add a space |
| 714 | // to avoid printing the diagraph '<:'. |
| 715 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 716 | SpecString += ' '; |
| 717 | |
| 718 | SpecString += ArgString; |
| 719 | } |
| 720 | |
| 721 | // If the last character of our string is '>', add another space to |
| 722 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 723 | // C++0x, but it's still good hygiene. |
| 724 | if (SpecString[SpecString.size() - 1] == '>') |
| 725 | SpecString += ' '; |
| 726 | |
| 727 | SpecString += '>'; |
| 728 | |
| 729 | return SpecString; |
| 730 | } |
| 731 | |
| 732 | // Sadly, repeat all that with TemplateArgLoc. |
| 733 | std::string TemplateSpecializationType:: |
| 734 | PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs, |
| 735 | const PrintingPolicy &Policy) { |
| 736 | std::string SpecString; |
| 737 | SpecString += '<'; |
| 738 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 739 | if (Arg) |
| 740 | SpecString += ", "; |
| 741 | |
| 742 | // Print the argument into a string. |
| 743 | std::string ArgString; |
| 744 | PrintTemplateArgument(ArgString, Args[Arg].getArgument(), Policy); |
| 745 | |
| 746 | // If this is the first argument and its string representation |
| 747 | // begins with the global scope specifier ('::foo'), add a space |
| 748 | // to avoid printing the diagraph '<:'. |
| 749 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 750 | SpecString += ' '; |
| 751 | |
| 752 | SpecString += ArgString; |
| 753 | } |
| 754 | |
| 755 | // If the last character of our string is '>', add another space to |
| 756 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 757 | // C++0x, but it's still good hygiene. |
| 758 | if (SpecString[SpecString.size() - 1] == '>') |
| 759 | SpecString += ' '; |
| 760 | |
| 761 | SpecString += '>'; |
| 762 | |
| 763 | return SpecString; |
| 764 | } |
| 765 | |
| 766 | void QualType::dump(const char *msg) const { |
| 767 | std::string R = "identifier"; |
| 768 | LangOptions LO; |
| 769 | getAsStringInternal(R, PrintingPolicy(LO)); |
| 770 | if (msg) |
Daniel Dunbar | e7cb7e4 | 2009-12-03 09:14:02 +0000 | [diff] [blame] | 771 | llvm::errs() << msg << ": "; |
| 772 | llvm::errs() << R << "\n"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 773 | } |
| 774 | void QualType::dump() const { |
| 775 | dump(""); |
| 776 | } |
| 777 | |
| 778 | void Type::dump() const { |
| 779 | QualType(this, 0).dump(); |
| 780 | } |
| 781 | |
| 782 | std::string Qualifiers::getAsString() const { |
| 783 | LangOptions LO; |
| 784 | return getAsString(PrintingPolicy(LO)); |
| 785 | } |
| 786 | |
| 787 | // Appends qualifiers to the given string, separated by spaces. Will |
| 788 | // prefix a space if the string is non-empty. Will not append a final |
| 789 | // space. |
| 790 | void Qualifiers::getAsStringInternal(std::string &S, |
| 791 | const PrintingPolicy&) const { |
| 792 | AppendTypeQualList(S, getCVRQualifiers()); |
| 793 | if (unsigned AddressSpace = getAddressSpace()) { |
| 794 | if (!S.empty()) S += ' '; |
| 795 | S += "__attribute__((address_space("; |
| 796 | S += llvm::utostr_32(AddressSpace); |
| 797 | S += ")))"; |
| 798 | } |
| 799 | if (Qualifiers::GC GCAttrType = getObjCGCAttr()) { |
| 800 | if (!S.empty()) S += ' '; |
| 801 | S += "__attribute__((objc_gc("; |
| 802 | if (GCAttrType == Qualifiers::Weak) |
| 803 | S += "weak"; |
| 804 | else |
| 805 | S += "strong"; |
| 806 | S += ")))"; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | std::string QualType::getAsString() const { |
| 811 | std::string S; |
| 812 | LangOptions LO; |
| 813 | getAsStringInternal(S, PrintingPolicy(LO)); |
| 814 | return S; |
| 815 | } |
| 816 | |
| 817 | void QualType::getAsStringInternal(std::string &S, |
| 818 | const PrintingPolicy &Policy) const { |
| 819 | TypePrinter Printer(Policy); |
| 820 | Printer.Print(*this, S); |
| 821 | } |
| 822 | |