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