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