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 { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 27 | /// \brief RAII object that enables printing of the ARC __strong lifetime |
| 28 | /// qualifier. |
| 29 | class IncludeStrongLifetimeRAII { |
| 30 | PrintingPolicy &Policy; |
| 31 | bool Old; |
| 32 | |
| 33 | public: |
| 34 | explicit IncludeStrongLifetimeRAII(PrintingPolicy &Policy) |
| 35 | : Policy(Policy), Old(Policy.SuppressStrongLifetime) { |
| 36 | Policy.SuppressStrongLifetime = false; |
| 37 | } |
| 38 | |
| 39 | ~IncludeStrongLifetimeRAII() { |
| 40 | Policy.SuppressStrongLifetime = Old; |
| 41 | } |
| 42 | }; |
| 43 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 44 | class TypePrinter { |
| 45 | PrintingPolicy Policy; |
| 46 | |
| 47 | public: |
| 48 | explicit TypePrinter(const PrintingPolicy &Policy) : Policy(Policy) { } |
Abramo Bagnara | 493ec51 | 2010-04-27 21:08:25 +0000 | [diff] [blame] | 49 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 50 | void print(const Type *ty, Qualifiers qs, std::string &buffer); |
| 51 | void print(QualType T, std::string &S); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 52 | void AppendScope(DeclContext *DC, std::string &S); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 53 | void printTag(TagDecl *T, std::string &S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 54 | #define ABSTRACT_TYPE(CLASS, PARENT) |
| 55 | #define TYPE(CLASS, PARENT) \ |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 56 | void print##CLASS(const CLASS##Type *T, std::string &S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 57 | #include "clang/AST/TypeNodes.def" |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | static void AppendTypeQualList(std::string &S, unsigned TypeQuals) { |
| 62 | if (TypeQuals & Qualifiers::Const) { |
| 63 | if (!S.empty()) S += ' '; |
| 64 | S += "const"; |
| 65 | } |
| 66 | if (TypeQuals & Qualifiers::Volatile) { |
| 67 | if (!S.empty()) S += ' '; |
| 68 | S += "volatile"; |
| 69 | } |
| 70 | if (TypeQuals & Qualifiers::Restrict) { |
| 71 | if (!S.empty()) S += ' '; |
| 72 | S += "restrict"; |
| 73 | } |
| 74 | } |
| 75 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 76 | void TypePrinter::print(QualType t, std::string &buffer) { |
| 77 | SplitQualType split = t.split(); |
| 78 | print(split.first, split.second, buffer); |
| 79 | } |
| 80 | |
| 81 | void TypePrinter::print(const Type *T, Qualifiers Quals, std::string &buffer) { |
| 82 | if (!T) { |
| 83 | buffer += "NULL TYPE"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (Policy.SuppressSpecifiers && T->isSpecifierType()) |
| 88 | return; |
| 89 | |
| 90 | // Print qualifiers as appropriate. |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 91 | |
| 92 | // CanPrefixQualifiers - We prefer to print type qualifiers before the type, |
| 93 | // so that we get "const int" instead of "int const", but we can't do this if |
| 94 | // the type is complex. For example if the type is "int*", we *must* print |
| 95 | // "int * const", printing "const int *" is different. Only do this when the |
| 96 | // type expands to a simple string. |
Douglas Gregor | c6daf0b | 2011-02-17 06:52:25 +0000 | [diff] [blame] | 97 | bool CanPrefixQualifiers = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 98 | bool NeedARCStrongQualifier = false; |
Douglas Gregor | c6daf0b | 2011-02-17 06:52:25 +0000 | [diff] [blame] | 99 | Type::TypeClass TC = T->getTypeClass(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 100 | if (const AutoType *AT = dyn_cast<AutoType>(T)) |
| 101 | TC = AT->desugar()->getTypeClass(); |
Douglas Gregor | c6daf0b | 2011-02-17 06:52:25 +0000 | [diff] [blame] | 102 | if (const SubstTemplateTypeParmType *Subst |
| 103 | = dyn_cast<SubstTemplateTypeParmType>(T)) |
| 104 | TC = Subst->getReplacementType()->getTypeClass(); |
| 105 | |
| 106 | switch (TC) { |
| 107 | case Type::Builtin: |
| 108 | case Type::Complex: |
| 109 | case Type::UnresolvedUsing: |
| 110 | case Type::Typedef: |
| 111 | case Type::TypeOfExpr: |
| 112 | case Type::TypeOf: |
| 113 | case Type::Decltype: |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 114 | case Type::UnaryTransform: |
Douglas Gregor | c6daf0b | 2011-02-17 06:52:25 +0000 | [diff] [blame] | 115 | case Type::Record: |
| 116 | case Type::Enum: |
| 117 | case Type::Elaborated: |
| 118 | case Type::TemplateTypeParm: |
| 119 | case Type::SubstTemplateTypeParmPack: |
| 120 | case Type::TemplateSpecialization: |
| 121 | case Type::InjectedClassName: |
| 122 | case Type::DependentName: |
| 123 | case Type::DependentTemplateSpecialization: |
| 124 | case Type::ObjCObject: |
| 125 | case Type::ObjCInterface: |
| 126 | CanPrefixQualifiers = true; |
| 127 | break; |
| 128 | |
| 129 | case Type::ObjCObjectPointer: |
| 130 | CanPrefixQualifiers = T->isObjCIdType() || T->isObjCClassType() || |
| 131 | T->isObjCQualifiedIdType() || T->isObjCQualifiedClassType(); |
| 132 | break; |
| 133 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 134 | case Type::ConstantArray: |
| 135 | case Type::IncompleteArray: |
| 136 | case Type::VariableArray: |
| 137 | case Type::DependentSizedArray: |
| 138 | NeedARCStrongQualifier = true; |
| 139 | // Fall through |
| 140 | |
Douglas Gregor | c6daf0b | 2011-02-17 06:52:25 +0000 | [diff] [blame] | 141 | case Type::Pointer: |
| 142 | case Type::BlockPointer: |
| 143 | case Type::LValueReference: |
| 144 | case Type::RValueReference: |
| 145 | case Type::MemberPointer: |
Douglas Gregor | c6daf0b | 2011-02-17 06:52:25 +0000 | [diff] [blame] | 146 | case Type::DependentSizedExtVector: |
| 147 | case Type::Vector: |
| 148 | case Type::ExtVector: |
| 149 | case Type::FunctionProto: |
| 150 | case Type::FunctionNoProto: |
| 151 | case Type::Paren: |
| 152 | case Type::Attributed: |
| 153 | case Type::PackExpansion: |
| 154 | case Type::SubstTemplateTypeParm: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 155 | case Type::Auto: |
Douglas Gregor | c6daf0b | 2011-02-17 06:52:25 +0000 | [diff] [blame] | 156 | CanPrefixQualifiers = false; |
| 157 | break; |
| 158 | } |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 159 | |
| 160 | if (!CanPrefixQualifiers && !Quals.empty()) { |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 161 | std::string qualsBuffer; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 162 | if (NeedARCStrongQualifier) { |
| 163 | IncludeStrongLifetimeRAII Strong(Policy); |
| 164 | Quals.getAsStringInternal(qualsBuffer, Policy); |
| 165 | } else { |
| 166 | Quals.getAsStringInternal(qualsBuffer, Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 167 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 168 | |
| 169 | if (!qualsBuffer.empty()) { |
| 170 | if (!buffer.empty()) { |
| 171 | qualsBuffer += ' '; |
| 172 | qualsBuffer += buffer; |
| 173 | } |
| 174 | std::swap(buffer, qualsBuffer); |
| 175 | } |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | switch (T->getTypeClass()) { |
| 179 | #define ABSTRACT_TYPE(CLASS, PARENT) |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 180 | #define TYPE(CLASS, PARENT) case Type::CLASS: \ |
| 181 | print##CLASS(cast<CLASS##Type>(T), buffer); \ |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 182 | break; |
| 183 | #include "clang/AST/TypeNodes.def" |
| 184 | } |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 185 | |
| 186 | // If we're adding the qualifiers as a prefix, do it now. |
| 187 | if (CanPrefixQualifiers && !Quals.empty()) { |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 188 | std::string qualsBuffer; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 189 | if (NeedARCStrongQualifier) { |
| 190 | IncludeStrongLifetimeRAII Strong(Policy); |
| 191 | Quals.getAsStringInternal(qualsBuffer, Policy); |
| 192 | } else { |
| 193 | Quals.getAsStringInternal(qualsBuffer, Policy); |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 194 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 195 | |
| 196 | if (!qualsBuffer.empty()) { |
| 197 | if (!buffer.empty()) { |
| 198 | qualsBuffer += ' '; |
| 199 | qualsBuffer += buffer; |
| 200 | } |
| 201 | std::swap(buffer, qualsBuffer); |
| 202 | } |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 203 | } |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 204 | } |
| 205 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 206 | void TypePrinter::printBuiltin(const BuiltinType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 207 | if (S.empty()) { |
| 208 | S = T->getName(Policy.LangOpts); |
| 209 | } else { |
| 210 | // Prefix the basic type, e.g. 'int X'. |
Chris Lattner | 59742de | 2010-09-04 23:43:40 +0000 | [diff] [blame] | 211 | S = ' ' + S; |
| 212 | S = T->getName(Policy.LangOpts) + S; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 216 | void TypePrinter::printComplex(const ComplexType *T, std::string &S) { |
| 217 | print(T->getElementType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 218 | S = "_Complex " + S; |
| 219 | } |
| 220 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 221 | void TypePrinter::printPointer(const PointerType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 222 | S = '*' + S; |
| 223 | |
| 224 | // Handle things like 'int (*A)[4];' correctly. |
| 225 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 226 | if (isa<ArrayType>(T->getPointeeType())) |
| 227 | S = '(' + S + ')'; |
| 228 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 229 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 230 | print(T->getPointeeType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 231 | } |
| 232 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 233 | void TypePrinter::printBlockPointer(const BlockPointerType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 234 | S = '^' + S; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 235 | print(T->getPointeeType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 236 | } |
| 237 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 238 | void TypePrinter::printLValueReference(const LValueReferenceType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 239 | std::string &S) { |
| 240 | S = '&' + S; |
| 241 | |
| 242 | // Handle things like 'int (&A)[4];' correctly. |
| 243 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 244 | if (isa<ArrayType>(T->getPointeeTypeAsWritten())) |
| 245 | S = '(' + S + ')'; |
| 246 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 247 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 248 | print(T->getPointeeTypeAsWritten(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 249 | } |
| 250 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 251 | void TypePrinter::printRValueReference(const RValueReferenceType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 252 | std::string &S) { |
| 253 | S = "&&" + S; |
| 254 | |
| 255 | // Handle things like 'int (&&A)[4];' correctly. |
| 256 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 257 | if (isa<ArrayType>(T->getPointeeTypeAsWritten())) |
| 258 | S = '(' + S + ')'; |
| 259 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 260 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 261 | print(T->getPointeeTypeAsWritten(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 262 | } |
| 263 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 264 | void TypePrinter::printMemberPointer(const MemberPointerType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 265 | std::string &S) { |
| 266 | std::string C; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 267 | print(QualType(T->getClass(), 0), C); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 268 | C += "::*"; |
| 269 | S = C + S; |
| 270 | |
| 271 | // Handle things like 'int (Cls::*A)[4];' correctly. |
| 272 | // FIXME: this should include vectors, but vectors use attributes I guess. |
| 273 | if (isa<ArrayType>(T->getPointeeType())) |
| 274 | S = '(' + S + ')'; |
| 275 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 276 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 277 | print(T->getPointeeType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 278 | } |
| 279 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 280 | void TypePrinter::printConstantArray(const ConstantArrayType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 281 | std::string &S) { |
| 282 | S += '['; |
| 283 | S += llvm::utostr(T->getSize().getZExtValue()); |
| 284 | S += ']'; |
| 285 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 286 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 287 | print(T->getElementType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 288 | } |
| 289 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 290 | void TypePrinter::printIncompleteArray(const IncompleteArrayType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 291 | std::string &S) { |
| 292 | S += "[]"; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 293 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 294 | print(T->getElementType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 295 | } |
| 296 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 297 | void TypePrinter::printVariableArray(const VariableArrayType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 298 | std::string &S) { |
| 299 | S += '['; |
| 300 | |
| 301 | if (T->getIndexTypeQualifiers().hasQualifiers()) { |
| 302 | AppendTypeQualList(S, T->getIndexTypeCVRQualifiers()); |
| 303 | S += ' '; |
| 304 | } |
| 305 | |
| 306 | if (T->getSizeModifier() == VariableArrayType::Static) |
| 307 | S += "static"; |
| 308 | else if (T->getSizeModifier() == VariableArrayType::Star) |
| 309 | S += '*'; |
| 310 | |
| 311 | if (T->getSizeExpr()) { |
| 312 | std::string SStr; |
| 313 | llvm::raw_string_ostream s(SStr); |
| 314 | T->getSizeExpr()->printPretty(s, 0, Policy); |
| 315 | S += s.str(); |
| 316 | } |
| 317 | S += ']'; |
| 318 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 319 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 320 | print(T->getElementType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 321 | } |
| 322 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 323 | void TypePrinter::printDependentSizedArray(const DependentSizedArrayType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 324 | std::string &S) { |
| 325 | S += '['; |
| 326 | |
| 327 | if (T->getSizeExpr()) { |
| 328 | std::string SStr; |
| 329 | llvm::raw_string_ostream s(SStr); |
| 330 | T->getSizeExpr()->printPretty(s, 0, Policy); |
| 331 | S += s.str(); |
| 332 | } |
| 333 | S += ']'; |
| 334 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 335 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 336 | print(T->getElementType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 337 | } |
| 338 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 339 | void TypePrinter::printDependentSizedExtVector( |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 340 | const DependentSizedExtVectorType *T, |
| 341 | std::string &S) { |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 342 | print(T->getElementType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 343 | |
| 344 | S += " __attribute__((ext_vector_type("; |
| 345 | if (T->getSizeExpr()) { |
| 346 | std::string SStr; |
| 347 | llvm::raw_string_ostream s(SStr); |
| 348 | T->getSizeExpr()->printPretty(s, 0, Policy); |
| 349 | S += s.str(); |
| 350 | } |
| 351 | S += ")))"; |
| 352 | } |
| 353 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 354 | void TypePrinter::printVector(const VectorType *T, std::string &S) { |
Bob Wilson | ec33cbe | 2010-11-16 00:32:26 +0000 | [diff] [blame] | 355 | switch (T->getVectorKind()) { |
| 356 | case VectorType::AltiVecPixel: |
| 357 | S = "__vector __pixel " + S; |
| 358 | break; |
| 359 | case VectorType::AltiVecBool: |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 360 | print(T->getElementType(), S); |
Bob Wilson | ec33cbe | 2010-11-16 00:32:26 +0000 | [diff] [blame] | 361 | S = "__vector __bool " + S; |
| 362 | break; |
| 363 | case VectorType::AltiVecVector: |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 364 | print(T->getElementType(), S); |
Bob Wilson | ec33cbe | 2010-11-16 00:32:26 +0000 | [diff] [blame] | 365 | S = "__vector " + S; |
| 366 | break; |
| 367 | case VectorType::NeonVector: |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 368 | print(T->getElementType(), S); |
Bob Wilson | ec33cbe | 2010-11-16 00:32:26 +0000 | [diff] [blame] | 369 | S = ("__attribute__((neon_vector_type(" + |
| 370 | llvm::utostr_32(T->getNumElements()) + "))) " + S); |
| 371 | break; |
| 372 | case VectorType::NeonPolyVector: |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 373 | print(T->getElementType(), S); |
Bob Wilson | ec33cbe | 2010-11-16 00:32:26 +0000 | [diff] [blame] | 374 | S = ("__attribute__((neon_polyvector_type(" + |
| 375 | llvm::utostr_32(T->getNumElements()) + "))) " + S); |
| 376 | break; |
| 377 | case VectorType::GenericVector: { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 378 | // FIXME: We prefer to print the size directly here, but have no way |
| 379 | // to get the size of the type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 380 | print(T->getElementType(), S); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 381 | std::string V = "__attribute__((__vector_size__("; |
| 382 | V += llvm::utostr_32(T->getNumElements()); // convert back to bytes. |
| 383 | std::string ET; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 384 | print(T->getElementType(), ET); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 385 | V += " * sizeof(" + ET + ")))) "; |
| 386 | S = V + S; |
Bob Wilson | ec33cbe | 2010-11-16 00:32:26 +0000 | [diff] [blame] | 387 | break; |
| 388 | } |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 389 | } |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 390 | } |
| 391 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 392 | void TypePrinter::printExtVector(const ExtVectorType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 393 | S += " __attribute__((ext_vector_type("; |
| 394 | S += llvm::utostr_32(T->getNumElements()); |
| 395 | S += ")))"; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 396 | print(T->getElementType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 397 | } |
| 398 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 399 | void TypePrinter::printFunctionProto(const FunctionProtoType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 400 | std::string &S) { |
| 401 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 402 | if (!S.empty()) |
| 403 | S = "(" + S + ")"; |
| 404 | |
| 405 | S += "("; |
| 406 | std::string Tmp; |
| 407 | PrintingPolicy ParamPolicy(Policy); |
| 408 | ParamPolicy.SuppressSpecifiers = false; |
| 409 | for (unsigned i = 0, e = T->getNumArgs(); i != e; ++i) { |
| 410 | if (i) S += ", "; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 411 | print(T->getArgType(i), Tmp); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 412 | S += Tmp; |
| 413 | Tmp.clear(); |
| 414 | } |
| 415 | |
| 416 | if (T->isVariadic()) { |
| 417 | if (T->getNumArgs()) |
| 418 | S += ", "; |
| 419 | S += "..."; |
| 420 | } else if (T->getNumArgs() == 0 && !Policy.LangOpts.CPlusPlus) { |
| 421 | // Do not emit int() if we have a proto, emit 'int(void)'. |
| 422 | S += "void"; |
| 423 | } |
| 424 | |
| 425 | S += ")"; |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 426 | |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 427 | FunctionType::ExtInfo Info = T->getExtInfo(); |
| 428 | switch(Info.getCC()) { |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 429 | case CC_Default: |
| 430 | default: break; |
| 431 | case CC_C: |
| 432 | S += " __attribute__((cdecl))"; |
| 433 | break; |
| 434 | case CC_X86StdCall: |
| 435 | S += " __attribute__((stdcall))"; |
| 436 | break; |
| 437 | case CC_X86FastCall: |
| 438 | S += " __attribute__((fastcall))"; |
| 439 | break; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 440 | case CC_X86ThisCall: |
| 441 | S += " __attribute__((thiscall))"; |
| 442 | break; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 443 | case CC_X86Pascal: |
| 444 | S += " __attribute__((pascal))"; |
| 445 | break; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 446 | case CC_AAPCS: |
| 447 | S += " __attribute__((pcs(\"aapcs\")))"; |
| 448 | break; |
| 449 | case CC_AAPCS_VFP: |
| 450 | S += " __attribute__((pcs(\"aapcs-vfp\")))"; |
| 451 | break; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 452 | } |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 453 | if (Info.getNoReturn()) |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 454 | S += " __attribute__((noreturn))"; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 455 | if (Info.getRegParm()) |
| 456 | S += " __attribute__((regparm (" + |
| 457 | llvm::utostr_32(Info.getRegParm()) + ")))"; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 459 | AppendTypeQualList(S, T->getTypeQuals()); |
| 460 | |
| 461 | switch (T->getRefQualifier()) { |
| 462 | case RQ_None: |
| 463 | break; |
| 464 | |
| 465 | case RQ_LValue: |
| 466 | S += " &"; |
| 467 | break; |
| 468 | |
| 469 | case RQ_RValue: |
| 470 | S += " &&"; |
| 471 | break; |
| 472 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 473 | |
| 474 | if (T->hasDynamicExceptionSpec()) { |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 475 | S += " throw("; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 476 | if (T->getExceptionSpecType() == EST_MSAny) |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 477 | S += "..."; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 478 | else |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 479 | for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) { |
| 480 | if (I) |
| 481 | S += ", "; |
| 482 | |
| 483 | std::string ExceptionType; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 484 | print(T->getExceptionType(I), ExceptionType); |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 485 | S += ExceptionType; |
| 486 | } |
| 487 | S += ")"; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 488 | } else if (isNoexceptExceptionSpec(T->getExceptionSpecType())) { |
| 489 | S += " noexcept"; |
| 490 | if (T->getExceptionSpecType() == EST_ComputedNoexcept) { |
| 491 | S += "("; |
| 492 | llvm::raw_string_ostream EOut(S); |
| 493 | T->getNoexceptExpr()->printPretty(EOut, 0, Policy); |
| 494 | EOut.flush(); |
| 495 | S += EOut.str(); |
| 496 | S += ")"; |
| 497 | } |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 498 | } |
| 499 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 500 | print(T->getResultType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 501 | } |
| 502 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 503 | void TypePrinter::printFunctionNoProto(const FunctionNoProtoType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 504 | std::string &S) { |
| 505 | // If needed for precedence reasons, wrap the inner part in grouping parens. |
| 506 | if (!S.empty()) |
| 507 | S = "(" + S + ")"; |
| 508 | |
| 509 | S += "()"; |
| 510 | if (T->getNoReturnAttr()) |
| 511 | S += " __attribute__((noreturn))"; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 512 | print(T->getResultType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 513 | } |
| 514 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 515 | static void printTypeSpec(const NamedDecl *D, std::string &S) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 516 | IdentifierInfo *II = D->getIdentifier(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 517 | if (S.empty()) |
| 518 | S = II->getName().str(); |
| 519 | else |
| 520 | S = II->getName().str() + ' ' + S; |
| 521 | } |
| 522 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 523 | void TypePrinter::printUnresolvedUsing(const UnresolvedUsingType *T, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 524 | std::string &S) { |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 525 | printTypeSpec(T->getDecl(), S); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 526 | } |
| 527 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 528 | void TypePrinter::printTypedef(const TypedefType *T, std::string &S) { |
| 529 | printTypeSpec(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 530 | } |
| 531 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 532 | void TypePrinter::printTypeOfExpr(const TypeOfExprType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 533 | if (!S.empty()) // Prefix the basic type, e.g. 'typeof(e) X'. |
| 534 | S = ' ' + S; |
| 535 | std::string Str; |
| 536 | llvm::raw_string_ostream s(Str); |
| 537 | T->getUnderlyingExpr()->printPretty(s, 0, Policy); |
| 538 | S = "typeof " + s.str() + S; |
| 539 | } |
| 540 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 541 | void TypePrinter::printTypeOf(const TypeOfType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 542 | if (!S.empty()) // Prefix the basic type, e.g. 'typeof(t) X'. |
| 543 | S = ' ' + S; |
| 544 | std::string Tmp; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 545 | print(T->getUnderlyingType(), Tmp); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 546 | S = "typeof(" + Tmp + ")" + S; |
| 547 | } |
| 548 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 549 | void TypePrinter::printDecltype(const DecltypeType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 550 | if (!S.empty()) // Prefix the basic type, e.g. 'decltype(t) X'. |
| 551 | S = ' ' + S; |
| 552 | std::string Str; |
| 553 | llvm::raw_string_ostream s(Str); |
| 554 | T->getUnderlyingExpr()->printPretty(s, 0, Policy); |
| 555 | S = "decltype(" + s.str() + ")" + S; |
| 556 | } |
| 557 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 558 | void TypePrinter::printUnaryTransform(const UnaryTransformType *T, |
| 559 | std::string &S) { |
| 560 | if (!S.empty()) |
| 561 | S = ' ' + S; |
| 562 | std::string Str; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 563 | IncludeStrongLifetimeRAII Strong(Policy); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 564 | print(T->getBaseType(), Str); |
| 565 | |
| 566 | switch (T->getUTTKind()) { |
| 567 | case UnaryTransformType::EnumUnderlyingType: |
| 568 | S = "__underlying_type(" + Str + ")" + S; |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 573 | void TypePrinter::printAuto(const AutoType *T, std::string &S) { |
| 574 | // If the type has been deduced, do not print 'auto'. |
| 575 | if (T->isDeduced()) { |
| 576 | print(T->getDeducedType(), S); |
| 577 | } else { |
| 578 | if (!S.empty()) // Prefix the basic type, e.g. 'auto X'. |
| 579 | S = ' ' + S; |
| 580 | S = "auto" + S; |
| 581 | } |
| 582 | } |
| 583 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 584 | /// Appends the given scope to the end of a string. |
| 585 | void TypePrinter::AppendScope(DeclContext *DC, std::string &Buffer) { |
| 586 | if (DC->isTranslationUnit()) return; |
| 587 | AppendScope(DC->getParent(), Buffer); |
| 588 | |
| 589 | unsigned OldSize = Buffer.size(); |
| 590 | |
| 591 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(DC)) { |
| 592 | if (NS->getIdentifier()) |
| 593 | Buffer += NS->getNameAsString(); |
| 594 | else |
| 595 | Buffer += "<anonymous>"; |
| 596 | } else if (ClassTemplateSpecializationDecl *Spec |
| 597 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 598 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 599 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 600 | std::string TemplateArgsStr |
| 601 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 602 | TemplateArgs.data(), |
| 603 | TemplateArgs.size(), |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 604 | Policy); |
| 605 | Buffer += Spec->getIdentifier()->getName(); |
| 606 | Buffer += TemplateArgsStr; |
| 607 | } else if (TagDecl *Tag = dyn_cast<TagDecl>(DC)) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 608 | if (TypedefNameDecl *Typedef = Tag->getTypedefNameForAnonDecl()) |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 609 | Buffer += Typedef->getIdentifier()->getName(); |
| 610 | else if (Tag->getIdentifier()) |
| 611 | Buffer += Tag->getIdentifier()->getName(); |
| 612 | } |
| 613 | |
| 614 | if (Buffer.size() != OldSize) |
| 615 | Buffer += "::"; |
| 616 | } |
| 617 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 618 | void TypePrinter::printTag(TagDecl *D, std::string &InnerString) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 619 | if (Policy.SuppressTag) |
| 620 | return; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 621 | |
| 622 | std::string Buffer; |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 623 | bool HasKindDecoration = false; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 624 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 625 | // bool SuppressTagKeyword |
| 626 | // = Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword; |
| 627 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 628 | // We don't print tags unless this is an elaborated type. |
| 629 | // In C, we just assume every RecordType is an elaborated type. |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 630 | if (!(Policy.LangOpts.CPlusPlus || Policy.SuppressTagKeyword || |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 631 | D->getTypedefNameForAnonDecl())) { |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 632 | HasKindDecoration = true; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 633 | Buffer += D->getKindName(); |
| 634 | Buffer += ' '; |
| 635 | } |
| 636 | |
Chris Lattner | c7acc66 | 2010-08-28 16:26:18 +0000 | [diff] [blame] | 637 | // Compute the full nested-name-specifier for this type. |
| 638 | // In C, this will always be empty except when the type |
| 639 | // being printed is anonymous within other Record. |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 640 | if (!Policy.SuppressScope) |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 641 | AppendScope(D->getDeclContext(), Buffer); |
| 642 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 643 | if (const IdentifierInfo *II = D->getIdentifier()) |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 644 | Buffer += II->getNameStart(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 645 | else if (TypedefNameDecl *Typedef = D->getTypedefNameForAnonDecl()) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 646 | assert(Typedef->getIdentifier() && "Typedef without identifier?"); |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 647 | Buffer += Typedef->getIdentifier()->getNameStart(); |
| 648 | } else { |
| 649 | // Make an unambiguous representation for anonymous types, e.g. |
| 650 | // <anonymous enum at /usr/include/string.h:120:9> |
| 651 | llvm::raw_string_ostream OS(Buffer); |
| 652 | OS << "<anonymous"; |
| 653 | |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 654 | if (Policy.AnonymousTagLocations) { |
| 655 | // Suppress the redundant tag keyword if we just printed one. |
| 656 | // We don't have to worry about ElaboratedTypes here because you can't |
| 657 | // refer to an anonymous type with one. |
| 658 | if (!HasKindDecoration) |
| 659 | OS << " " << D->getKindName(); |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 661 | PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( |
Douglas Gregor | 16834e8 | 2010-05-28 21:47:04 +0000 | [diff] [blame] | 662 | D->getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 663 | if (PLoc.isValid()) { |
Douglas Gregor | 16834e8 | 2010-05-28 21:47:04 +0000 | [diff] [blame] | 664 | OS << " at " << PLoc.getFilename() |
| 665 | << ':' << PLoc.getLine() |
| 666 | << ':' << PLoc.getColumn(); |
| 667 | } |
Douglas Gregor | 84139d6 | 2010-04-05 21:25:31 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | OS << '>'; |
John McCall | 73061d0 | 2010-03-19 07:56:44 +0000 | [diff] [blame] | 671 | } |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 672 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 673 | // If this is a class template specialization, print the template |
| 674 | // arguments. |
| 675 | if (ClassTemplateSpecializationDecl *Spec |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 676 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 677 | const TemplateArgument *Args; |
| 678 | unsigned NumArgs; |
| 679 | if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) { |
| 680 | const TemplateSpecializationType *TST = |
| 681 | cast<TemplateSpecializationType>(TAW->getType()); |
| 682 | Args = TST->getArgs(); |
| 683 | NumArgs = TST->getNumArgs(); |
| 684 | } else { |
| 685 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 686 | Args = TemplateArgs.data(); |
| 687 | NumArgs = TemplateArgs.size(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 688 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 689 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 690 | Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args, |
| 691 | NumArgs, |
| 692 | Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 693 | } |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 694 | |
| 695 | if (!InnerString.empty()) { |
| 696 | Buffer += ' '; |
| 697 | Buffer += InnerString; |
| 698 | } |
| 699 | |
| 700 | std::swap(Buffer, InnerString); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 701 | } |
| 702 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 703 | void TypePrinter::printRecord(const RecordType *T, std::string &S) { |
| 704 | printTag(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 705 | } |
| 706 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 707 | void TypePrinter::printEnum(const EnumType *T, std::string &S) { |
| 708 | printTag(T->getDecl(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 709 | } |
| 710 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 711 | void TypePrinter::printTemplateTypeParm(const TemplateTypeParmType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 712 | std::string &S) { |
| 713 | if (!S.empty()) // Prefix the basic type, e.g. 'parmname X'. |
| 714 | S = ' ' + S; |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 715 | |
Chandler Carruth | b7efff4 | 2011-05-01 01:05:51 +0000 | [diff] [blame] | 716 | if (IdentifierInfo *Id = T->getIdentifier()) |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 717 | S = Id->getName().str() + S; |
| 718 | else |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 719 | S = "type-parameter-" + llvm::utostr_32(T->getDepth()) + '-' + |
| 720 | llvm::utostr_32(T->getIndex()) + S; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 721 | } |
| 722 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 723 | void TypePrinter::printSubstTemplateTypeParm(const SubstTemplateTypeParmType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 724 | std::string &S) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 725 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 726 | print(T->getReplacementType(), S); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 729 | void TypePrinter::printSubstTemplateTypeParmPack( |
| 730 | const SubstTemplateTypeParmPackType *T, |
| 731 | std::string &S) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 732 | IncludeStrongLifetimeRAII Strong(Policy); |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 733 | printTemplateTypeParm(T->getReplacedParameter(), S); |
| 734 | } |
| 735 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 736 | void TypePrinter::printTemplateSpecialization( |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 737 | const TemplateSpecializationType *T, |
| 738 | std::string &S) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 739 | IncludeStrongLifetimeRAII Strong(Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 740 | std::string SpecString; |
| 741 | |
| 742 | { |
| 743 | llvm::raw_string_ostream OS(SpecString); |
| 744 | T->getTemplateName().print(OS, Policy); |
| 745 | } |
| 746 | |
| 747 | SpecString += TemplateSpecializationType::PrintTemplateArgumentList( |
| 748 | T->getArgs(), |
| 749 | T->getNumArgs(), |
| 750 | Policy); |
| 751 | if (S.empty()) |
| 752 | S.swap(SpecString); |
| 753 | else |
| 754 | S = SpecString + ' ' + S; |
| 755 | } |
| 756 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 757 | void TypePrinter::printInjectedClassName(const InjectedClassNameType *T, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 758 | std::string &S) { |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 759 | printTemplateSpecialization(T->getInjectedTST(), S); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 760 | } |
| 761 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 762 | void TypePrinter::printElaborated(const ElaboratedType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 763 | std::string MyString; |
| 764 | |
| 765 | { |
| 766 | llvm::raw_string_ostream OS(MyString); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 767 | OS << TypeWithKeyword::getKeywordName(T->getKeyword()); |
| 768 | if (T->getKeyword() != ETK_None) |
| 769 | OS << " "; |
| 770 | NestedNameSpecifier* Qualifier = T->getQualifier(); |
| 771 | if (Qualifier) |
| 772 | Qualifier->print(OS, Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | std::string TypeStr; |
| 776 | PrintingPolicy InnerPolicy(Policy); |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 777 | InnerPolicy.SuppressTagKeyword = true; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 778 | InnerPolicy.SuppressScope = true; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 779 | TypePrinter(InnerPolicy).print(T->getNamedType(), TypeStr); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 780 | |
| 781 | MyString += TypeStr; |
| 782 | if (S.empty()) |
| 783 | S.swap(MyString); |
| 784 | else |
| 785 | S = MyString + ' ' + S; |
| 786 | } |
| 787 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 788 | void TypePrinter::printParen(const ParenType *T, std::string &S) { |
| 789 | if (!S.empty() && !isa<FunctionType>(T->getInnerType())) |
| 790 | S = '(' + S + ')'; |
| 791 | print(T->getInnerType(), S); |
| 792 | } |
| 793 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 794 | void TypePrinter::printDependentName(const DependentNameType *T, std::string &S) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 795 | std::string MyString; |
| 796 | |
| 797 | { |
| 798 | llvm::raw_string_ostream OS(MyString); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 799 | OS << TypeWithKeyword::getKeywordName(T->getKeyword()); |
| 800 | if (T->getKeyword() != ETK_None) |
| 801 | OS << " "; |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 802 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 803 | T->getQualifier()->print(OS, Policy); |
| 804 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 805 | OS << T->getIdentifier()->getName(); |
| 806 | } |
| 807 | |
| 808 | if (S.empty()) |
| 809 | S.swap(MyString); |
| 810 | else |
| 811 | S = MyString + ' ' + S; |
| 812 | } |
| 813 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 814 | void TypePrinter::printDependentTemplateSpecialization( |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 815 | const DependentTemplateSpecializationType *T, std::string &S) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 816 | IncludeStrongLifetimeRAII Strong(Policy); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 817 | std::string MyString; |
| 818 | { |
| 819 | llvm::raw_string_ostream OS(MyString); |
| 820 | |
| 821 | OS << TypeWithKeyword::getKeywordName(T->getKeyword()); |
| 822 | if (T->getKeyword() != ETK_None) |
| 823 | OS << " "; |
| 824 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 825 | if (T->getQualifier()) |
| 826 | T->getQualifier()->print(OS, Policy); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 827 | OS << T->getIdentifier()->getName(); |
| 828 | OS << TemplateSpecializationType::PrintTemplateArgumentList( |
| 829 | T->getArgs(), |
| 830 | T->getNumArgs(), |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 831 | Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | if (S.empty()) |
| 835 | S.swap(MyString); |
| 836 | else |
| 837 | S = MyString + ' ' + S; |
| 838 | } |
| 839 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 840 | void TypePrinter::printPackExpansion(const PackExpansionType *T, |
| 841 | std::string &S) { |
| 842 | print(T->getPattern(), S); |
| 843 | S += "..."; |
| 844 | } |
| 845 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 846 | void TypePrinter::printAttributed(const AttributedType *T, |
| 847 | std::string &S) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 848 | // Prefer the macro forms of the GC and lifetime qualifiers. |
| 849 | if (T->getAttrKind() == AttributedType::attr_objc_gc || |
| 850 | T->getAttrKind() == AttributedType::attr_objc_lifetime) |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 851 | return print(T->getEquivalentType(), S); |
| 852 | |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 853 | print(T->getModifiedType(), S); |
| 854 | |
| 855 | // TODO: not all attributes are GCC-style attributes. |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 856 | S += " __attribute__(("; |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 857 | switch (T->getAttrKind()) { |
Francois Pichet | 170464b | 2011-01-07 03:17:31 +0000 | [diff] [blame] | 858 | case AttributedType::attr_address_space: |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 859 | S += "address_space("; |
| 860 | S += T->getEquivalentType().getAddressSpace(); |
| 861 | S += ")"; |
| 862 | break; |
| 863 | |
Francois Pichet | 170464b | 2011-01-07 03:17:31 +0000 | [diff] [blame] | 864 | case AttributedType::attr_vector_size: { |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 865 | S += "__vector_size__("; |
| 866 | if (const VectorType *vector =T->getEquivalentType()->getAs<VectorType>()) { |
| 867 | S += vector->getNumElements(); |
| 868 | S += " * sizeof("; |
| 869 | |
| 870 | std::string tmp; |
| 871 | print(vector->getElementType(), tmp); |
| 872 | S += tmp; |
| 873 | S += ")"; |
| 874 | } |
| 875 | S += ")"; |
| 876 | break; |
| 877 | } |
| 878 | |
Francois Pichet | 170464b | 2011-01-07 03:17:31 +0000 | [diff] [blame] | 879 | case AttributedType::attr_neon_vector_type: |
| 880 | case AttributedType::attr_neon_polyvector_type: { |
| 881 | if (T->getAttrKind() == AttributedType::attr_neon_vector_type) |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 882 | S += "neon_vector_type("; |
| 883 | else |
| 884 | S += "neon_polyvector_type("; |
| 885 | const VectorType *vector = T->getEquivalentType()->getAs<VectorType>(); |
| 886 | S += llvm::utostr_32(vector->getNumElements()); |
| 887 | S += ")"; |
| 888 | break; |
| 889 | } |
| 890 | |
Francois Pichet | 170464b | 2011-01-07 03:17:31 +0000 | [diff] [blame] | 891 | case AttributedType::attr_regparm: { |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 892 | S += "regparm("; |
| 893 | QualType t = T->getEquivalentType(); |
| 894 | while (!t->isFunctionType()) |
| 895 | t = t->getPointeeType(); |
| 896 | S += t->getAs<FunctionType>()->getRegParmType(); |
| 897 | S += ")"; |
| 898 | break; |
| 899 | } |
| 900 | |
Francois Pichet | 170464b | 2011-01-07 03:17:31 +0000 | [diff] [blame] | 901 | case AttributedType::attr_objc_gc: { |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 902 | S += "objc_gc("; |
| 903 | |
| 904 | QualType tmp = T->getEquivalentType(); |
| 905 | while (tmp.getObjCGCAttr() == Qualifiers::GCNone) { |
| 906 | QualType next = tmp->getPointeeType(); |
| 907 | if (next == tmp) break; |
| 908 | tmp = next; |
| 909 | } |
| 910 | |
| 911 | if (tmp.isObjCGCWeak()) |
| 912 | S += "weak"; |
| 913 | else |
| 914 | S += "strong"; |
| 915 | S += ")"; |
| 916 | break; |
| 917 | } |
| 918 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 919 | case AttributedType::attr_objc_lifetime: |
| 920 | S += "objc_lifetime("; |
| 921 | switch (T->getEquivalentType().getObjCLifetime()) { |
| 922 | case Qualifiers::OCL_None: llvm_unreachable("no lifetime!"); break; |
| 923 | case Qualifiers::OCL_ExplicitNone: S += "none"; break; |
| 924 | case Qualifiers::OCL_Strong: S += "strong"; break; |
| 925 | case Qualifiers::OCL_Weak: S += "weak"; break; |
| 926 | case Qualifiers::OCL_Autoreleasing: S += "autoreleasing"; break; |
| 927 | } |
| 928 | S += ")"; |
| 929 | break; |
| 930 | |
Francois Pichet | 3cd47e7 | 2011-01-07 02:58:13 +0000 | [diff] [blame] | 931 | case AttributedType::attr_noreturn: S += "noreturn"; break; |
| 932 | case AttributedType::attr_cdecl: S += "cdecl"; break; |
| 933 | case AttributedType::attr_fastcall: S += "fastcall"; break; |
| 934 | case AttributedType::attr_stdcall: S += "stdcall"; break; |
| 935 | case AttributedType::attr_thiscall: S += "thiscall"; break; |
| 936 | case AttributedType::attr_pascal: S += "pascal"; break; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 937 | case AttributedType::attr_pcs: { |
| 938 | S += "pcs("; |
| 939 | QualType t = T->getEquivalentType(); |
| 940 | while (!t->isFunctionType()) |
| 941 | t = t->getPointeeType(); |
| 942 | S += (t->getAs<FunctionType>()->getCallConv() == CC_AAPCS ? |
| 943 | "\"aapcs\"" : "\"aapcs-vfp\""); |
| 944 | S += ")"; |
| 945 | break; |
| 946 | } |
John McCall | 9d156a7 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 947 | } |
| 948 | S += "))"; |
| 949 | } |
| 950 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 951 | void TypePrinter::printObjCInterface(const ObjCInterfaceType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 952 | std::string &S) { |
| 953 | if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 954 | S = ' ' + S; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 955 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 956 | std::string ObjCQIString = T->getDecl()->getNameAsString(); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 957 | S = ObjCQIString + S; |
| 958 | } |
| 959 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 960 | void TypePrinter::printObjCObject(const ObjCObjectType *T, |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 961 | std::string &S) { |
| 962 | if (T->qual_empty()) |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 963 | return print(T->getBaseType(), S); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 964 | |
| 965 | std::string tmp; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 966 | print(T->getBaseType(), tmp); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 967 | tmp += '<'; |
| 968 | bool isFirst = true; |
| 969 | for (ObjCObjectType::qual_iterator |
| 970 | I = T->qual_begin(), E = T->qual_end(); I != E; ++I) { |
| 971 | if (isFirst) |
| 972 | isFirst = false; |
| 973 | else |
| 974 | tmp += ','; |
| 975 | tmp += (*I)->getNameAsString(); |
| 976 | } |
| 977 | tmp += '>'; |
| 978 | |
| 979 | if (!S.empty()) { |
| 980 | tmp += ' '; |
| 981 | tmp += S; |
| 982 | } |
| 983 | std::swap(tmp, S); |
| 984 | } |
| 985 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 986 | void TypePrinter::printObjCObjectPointer(const ObjCObjectPointerType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 987 | std::string &S) { |
| 988 | std::string ObjCQIString; |
| 989 | |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 990 | T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString, |
| 991 | Policy); |
| 992 | if (!ObjCQIString.empty()) |
| 993 | ObjCQIString += ' '; |
| 994 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 995 | if (T->isObjCIdType() || T->isObjCQualifiedIdType()) |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 996 | ObjCQIString += "id"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 997 | else if (T->isObjCClassType() || T->isObjCQualifiedClassType()) |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 998 | ObjCQIString += "Class"; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 999 | else if (T->isObjCSelType()) |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 1000 | ObjCQIString += "SEL"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1001 | else |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 1002 | ObjCQIString += T->getInterfaceDecl()->getNameAsString(); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1003 | |
| 1004 | if (!T->qual_empty()) { |
| 1005 | ObjCQIString += '<'; |
| 1006 | for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(), |
| 1007 | E = T->qual_end(); |
| 1008 | I != E; ++I) { |
| 1009 | ObjCQIString += (*I)->getNameAsString(); |
| 1010 | if (I+1 != E) |
| 1011 | ObjCQIString += ','; |
| 1012 | } |
| 1013 | ObjCQIString += '>'; |
| 1014 | } |
| 1015 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1016 | if (!T->isObjCIdType() && !T->isObjCQualifiedIdType()) |
| 1017 | ObjCQIString += " *"; // Don't forget the implicit pointer. |
| 1018 | else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 1019 | S = ' ' + S; |
| 1020 | |
| 1021 | S = ObjCQIString + S; |
| 1022 | } |
| 1023 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1024 | std::string TemplateSpecializationType:: |
| 1025 | PrintTemplateArgumentList(const TemplateArgumentListInfo &Args, |
| 1026 | const PrintingPolicy &Policy) { |
| 1027 | return PrintTemplateArgumentList(Args.getArgumentArray(), |
| 1028 | Args.size(), |
| 1029 | Policy); |
| 1030 | } |
| 1031 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1032 | std::string |
| 1033 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 1034 | const TemplateArgument *Args, |
| 1035 | unsigned NumArgs, |
Douglas Gregor | dace95b | 2010-12-20 22:28:59 +0000 | [diff] [blame] | 1036 | const PrintingPolicy &Policy, |
| 1037 | bool SkipBrackets) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1038 | std::string SpecString; |
Douglas Gregor | dace95b | 2010-12-20 22:28:59 +0000 | [diff] [blame] | 1039 | if (!SkipBrackets) |
| 1040 | SpecString += '<'; |
| 1041 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1042 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
Francois Pichet | 319b4a6 | 2011-05-25 16:07:10 +0000 | [diff] [blame] | 1043 | if (SpecString.size() > unsigned(!SkipBrackets)) |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1044 | SpecString += ", "; |
| 1045 | |
| 1046 | // Print the argument into a string. |
| 1047 | std::string ArgString; |
Douglas Gregor | dace95b | 2010-12-20 22:28:59 +0000 | [diff] [blame] | 1048 | if (Args[Arg].getKind() == TemplateArgument::Pack) { |
| 1049 | ArgString = PrintTemplateArgumentList(Args[Arg].pack_begin(), |
| 1050 | Args[Arg].pack_size(), |
| 1051 | Policy, true); |
| 1052 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1053 | llvm::raw_string_ostream ArgOut(ArgString); |
| 1054 | Args[Arg].print(Policy, ArgOut); |
| 1055 | } |
| 1056 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1057 | // If this is the first argument and its string representation |
| 1058 | // begins with the global scope specifier ('::foo'), add a space |
| 1059 | // to avoid printing the diagraph '<:'. |
| 1060 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 1061 | SpecString += ' '; |
| 1062 | |
| 1063 | SpecString += ArgString; |
| 1064 | } |
| 1065 | |
| 1066 | // If the last character of our string is '>', add another space to |
| 1067 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 1068 | // C++0x, but it's still good hygiene. |
Francois Pichet | b5bc7d0 | 2011-01-09 22:32:25 +0000 | [diff] [blame] | 1069 | if (!SpecString.empty() && SpecString[SpecString.size() - 1] == '>') |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1070 | SpecString += ' '; |
| 1071 | |
Douglas Gregor | dace95b | 2010-12-20 22:28:59 +0000 | [diff] [blame] | 1072 | if (!SkipBrackets) |
| 1073 | SpecString += '>'; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1074 | |
| 1075 | return SpecString; |
| 1076 | } |
| 1077 | |
| 1078 | // Sadly, repeat all that with TemplateArgLoc. |
| 1079 | std::string TemplateSpecializationType:: |
| 1080 | PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs, |
| 1081 | const PrintingPolicy &Policy) { |
| 1082 | std::string SpecString; |
| 1083 | SpecString += '<'; |
| 1084 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
Douglas Gregor | dace95b | 2010-12-20 22:28:59 +0000 | [diff] [blame] | 1085 | if (SpecString.size() > 1) |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1086 | SpecString += ", "; |
| 1087 | |
| 1088 | // Print the argument into a string. |
| 1089 | std::string ArgString; |
Douglas Gregor | dace95b | 2010-12-20 22:28:59 +0000 | [diff] [blame] | 1090 | if (Args[Arg].getArgument().getKind() == TemplateArgument::Pack) { |
| 1091 | ArgString = PrintTemplateArgumentList( |
| 1092 | Args[Arg].getArgument().pack_begin(), |
| 1093 | Args[Arg].getArgument().pack_size(), |
| 1094 | Policy, true); |
| 1095 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 1096 | llvm::raw_string_ostream ArgOut(ArgString); |
| 1097 | Args[Arg].getArgument().print(Policy, ArgOut); |
| 1098 | } |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1099 | |
| 1100 | // If this is the first argument and its string representation |
| 1101 | // begins with the global scope specifier ('::foo'), add a space |
| 1102 | // to avoid printing the diagraph '<:'. |
| 1103 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 1104 | SpecString += ' '; |
| 1105 | |
| 1106 | SpecString += ArgString; |
| 1107 | } |
| 1108 | |
| 1109 | // If the last character of our string is '>', add another space to |
| 1110 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 1111 | // C++0x, but it's still good hygiene. |
| 1112 | if (SpecString[SpecString.size() - 1] == '>') |
| 1113 | SpecString += ' '; |
| 1114 | |
| 1115 | SpecString += '>'; |
| 1116 | |
| 1117 | return SpecString; |
| 1118 | } |
| 1119 | |
| 1120 | void QualType::dump(const char *msg) const { |
| 1121 | std::string R = "identifier"; |
| 1122 | LangOptions LO; |
| 1123 | getAsStringInternal(R, PrintingPolicy(LO)); |
| 1124 | if (msg) |
Daniel Dunbar | e7cb7e4 | 2009-12-03 09:14:02 +0000 | [diff] [blame] | 1125 | llvm::errs() << msg << ": "; |
| 1126 | llvm::errs() << R << "\n"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1127 | } |
| 1128 | void QualType::dump() const { |
| 1129 | dump(""); |
| 1130 | } |
| 1131 | |
| 1132 | void Type::dump() const { |
| 1133 | QualType(this, 0).dump(); |
| 1134 | } |
| 1135 | |
| 1136 | std::string Qualifiers::getAsString() const { |
| 1137 | LangOptions LO; |
| 1138 | return getAsString(PrintingPolicy(LO)); |
| 1139 | } |
| 1140 | |
| 1141 | // Appends qualifiers to the given string, separated by spaces. Will |
| 1142 | // prefix a space if the string is non-empty. Will not append a final |
| 1143 | // space. |
| 1144 | void Qualifiers::getAsStringInternal(std::string &S, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 1145 | const PrintingPolicy& Policy) const { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1146 | AppendTypeQualList(S, getCVRQualifiers()); |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 1147 | if (unsigned addrspace = getAddressSpace()) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1148 | if (!S.empty()) S += ' '; |
| 1149 | S += "__attribute__((address_space("; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 1150 | S += llvm::utostr_32(addrspace); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1151 | S += ")))"; |
| 1152 | } |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 1153 | if (Qualifiers::GC gc = getObjCGCAttr()) { |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1154 | if (!S.empty()) S += ' '; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 1155 | if (gc == Qualifiers::Weak) |
| 1156 | S += "__weak"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1157 | else |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 1158 | S += "__strong"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1159 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame^] | 1160 | if (Qualifiers::ObjCLifetime lifetime = getObjCLifetime()) { |
| 1161 | if (!S.empty() && |
| 1162 | !(lifetime == Qualifiers::OCL_Strong && Policy.SuppressStrongLifetime)) |
| 1163 | S += ' '; |
| 1164 | |
| 1165 | switch (lifetime) { |
| 1166 | case Qualifiers::OCL_None: llvm_unreachable("none but true"); |
| 1167 | case Qualifiers::OCL_ExplicitNone: S += "__unsafe_unretained"; break; |
| 1168 | case Qualifiers::OCL_Strong: |
| 1169 | if (!Policy.SuppressStrongLifetime) |
| 1170 | S += "__strong"; |
| 1171 | break; |
| 1172 | |
| 1173 | case Qualifiers::OCL_Weak: S += "__weak"; break; |
| 1174 | case Qualifiers::OCL_Autoreleasing: S += "__autoreleasing"; break; |
| 1175 | } |
| 1176 | } |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1179 | std::string QualType::getAsString(const Type *ty, Qualifiers qs) { |
| 1180 | std::string buffer; |
| 1181 | LangOptions options; |
| 1182 | getAsStringInternal(ty, qs, buffer, PrintingPolicy(options)); |
| 1183 | return buffer; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1186 | void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, |
| 1187 | std::string &buffer, |
| 1188 | const PrintingPolicy &policy) { |
| 1189 | TypePrinter(policy).print(ty, qs, buffer); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 1190 | } |