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 | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 677 | void TypePrinter::printObjCInterface(const ObjCInterfaceType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 678 | std::string &S) { |
| 679 | if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 680 | S = ' ' + S; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 682 | std::string ObjCQIString = T->getDecl()->getNameAsString(); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 683 | S = ObjCQIString + S; |
| 684 | } |
| 685 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 686 | void TypePrinter::printObjCObject(const ObjCObjectType *T, |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 687 | std::string &S) { |
| 688 | if (T->qual_empty()) |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 689 | return print(T->getBaseType(), S); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 690 | |
| 691 | std::string tmp; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 692 | print(T->getBaseType(), tmp); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 693 | tmp += '<'; |
| 694 | bool isFirst = true; |
| 695 | for (ObjCObjectType::qual_iterator |
| 696 | I = T->qual_begin(), E = T->qual_end(); I != E; ++I) { |
| 697 | if (isFirst) |
| 698 | isFirst = false; |
| 699 | else |
| 700 | tmp += ','; |
| 701 | tmp += (*I)->getNameAsString(); |
| 702 | } |
| 703 | tmp += '>'; |
| 704 | |
| 705 | if (!S.empty()) { |
| 706 | tmp += ' '; |
| 707 | tmp += S; |
| 708 | } |
| 709 | std::swap(tmp, S); |
| 710 | } |
| 711 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 712 | void TypePrinter::printObjCObjectPointer(const ObjCObjectPointerType *T, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 713 | std::string &S) { |
| 714 | std::string ObjCQIString; |
| 715 | |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 716 | T->getPointeeType().getLocalQualifiers().getAsStringInternal(ObjCQIString, |
| 717 | Policy); |
| 718 | if (!ObjCQIString.empty()) |
| 719 | ObjCQIString += ' '; |
| 720 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 721 | if (T->isObjCIdType() || T->isObjCQualifiedIdType()) |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 722 | ObjCQIString += "id"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 723 | else if (T->isObjCClassType() || T->isObjCQualifiedClassType()) |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 724 | ObjCQIString += "Class"; |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 725 | else if (T->isObjCSelType()) |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 726 | ObjCQIString += "SEL"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 727 | else |
Chris Lattner | d7e52b8 | 2010-09-05 00:43:21 +0000 | [diff] [blame] | 728 | ObjCQIString += T->getInterfaceDecl()->getNameAsString(); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 729 | |
| 730 | if (!T->qual_empty()) { |
| 731 | ObjCQIString += '<'; |
| 732 | for (ObjCObjectPointerType::qual_iterator I = T->qual_begin(), |
| 733 | E = T->qual_end(); |
| 734 | I != E; ++I) { |
| 735 | ObjCQIString += (*I)->getNameAsString(); |
| 736 | if (I+1 != E) |
| 737 | ObjCQIString += ','; |
| 738 | } |
| 739 | ObjCQIString += '>'; |
| 740 | } |
| 741 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 742 | if (!T->isObjCIdType() && !T->isObjCQualifiedIdType()) |
| 743 | ObjCQIString += " *"; // Don't forget the implicit pointer. |
| 744 | else if (!S.empty()) // Prefix the basic type, e.g. 'typedefname X'. |
| 745 | S = ' ' + S; |
| 746 | |
| 747 | S = ObjCQIString + S; |
| 748 | } |
| 749 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 750 | static void printTemplateArgument(std::string &Buffer, |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 751 | const TemplateArgument &Arg, |
| 752 | const PrintingPolicy &Policy) { |
| 753 | switch (Arg.getKind()) { |
| 754 | case TemplateArgument::Null: |
| 755 | assert(false && "Null template argument"); |
| 756 | break; |
| 757 | |
| 758 | case TemplateArgument::Type: |
| 759 | Arg.getAsType().getAsStringInternal(Buffer, Policy); |
| 760 | break; |
| 761 | |
| 762 | case TemplateArgument::Declaration: |
| 763 | Buffer = cast<NamedDecl>(Arg.getAsDecl())->getNameAsString(); |
| 764 | break; |
| 765 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 766 | case TemplateArgument::Template: { |
| 767 | llvm::raw_string_ostream s(Buffer); |
| 768 | Arg.getAsTemplate().print(s, Policy); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 769 | break; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 772 | case TemplateArgument::Integral: |
| 773 | Buffer = Arg.getAsIntegral()->toString(10, true); |
| 774 | break; |
| 775 | |
| 776 | case TemplateArgument::Expression: { |
| 777 | llvm::raw_string_ostream s(Buffer); |
| 778 | Arg.getAsExpr()->printPretty(s, 0, Policy); |
| 779 | break; |
| 780 | } |
| 781 | |
| 782 | case TemplateArgument::Pack: |
| 783 | assert(0 && "FIXME: Implement!"); |
| 784 | break; |
| 785 | } |
| 786 | } |
| 787 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 788 | std::string TemplateSpecializationType:: |
| 789 | PrintTemplateArgumentList(const TemplateArgumentListInfo &Args, |
| 790 | const PrintingPolicy &Policy) { |
| 791 | return PrintTemplateArgumentList(Args.getArgumentArray(), |
| 792 | Args.size(), |
| 793 | Policy); |
| 794 | } |
| 795 | |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 796 | std::string |
| 797 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 798 | const TemplateArgument *Args, |
| 799 | unsigned NumArgs, |
| 800 | const PrintingPolicy &Policy) { |
| 801 | std::string SpecString; |
| 802 | SpecString += '<'; |
| 803 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 804 | if (Arg) |
| 805 | SpecString += ", "; |
| 806 | |
| 807 | // Print the argument into a string. |
| 808 | std::string ArgString; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 809 | printTemplateArgument(ArgString, Args[Arg], Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 810 | |
| 811 | // If this is the first argument and its string representation |
| 812 | // begins with the global scope specifier ('::foo'), add a space |
| 813 | // to avoid printing the diagraph '<:'. |
| 814 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 815 | SpecString += ' '; |
| 816 | |
| 817 | SpecString += ArgString; |
| 818 | } |
| 819 | |
| 820 | // If the last character of our string is '>', add another space to |
| 821 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 822 | // C++0x, but it's still good hygiene. |
| 823 | if (SpecString[SpecString.size() - 1] == '>') |
| 824 | SpecString += ' '; |
| 825 | |
| 826 | SpecString += '>'; |
| 827 | |
| 828 | return SpecString; |
| 829 | } |
| 830 | |
| 831 | // Sadly, repeat all that with TemplateArgLoc. |
| 832 | std::string TemplateSpecializationType:: |
| 833 | PrintTemplateArgumentList(const TemplateArgumentLoc *Args, unsigned NumArgs, |
| 834 | const PrintingPolicy &Policy) { |
| 835 | std::string SpecString; |
| 836 | SpecString += '<'; |
| 837 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) { |
| 838 | if (Arg) |
| 839 | SpecString += ", "; |
| 840 | |
| 841 | // Print the argument into a string. |
| 842 | std::string ArgString; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 843 | printTemplateArgument(ArgString, Args[Arg].getArgument(), Policy); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 844 | |
| 845 | // If this is the first argument and its string representation |
| 846 | // begins with the global scope specifier ('::foo'), add a space |
| 847 | // to avoid printing the diagraph '<:'. |
| 848 | if (!Arg && !ArgString.empty() && ArgString[0] == ':') |
| 849 | SpecString += ' '; |
| 850 | |
| 851 | SpecString += ArgString; |
| 852 | } |
| 853 | |
| 854 | // If the last character of our string is '>', add another space to |
| 855 | // keep the two '>''s separate tokens. We don't *have* to do this in |
| 856 | // C++0x, but it's still good hygiene. |
| 857 | if (SpecString[SpecString.size() - 1] == '>') |
| 858 | SpecString += ' '; |
| 859 | |
| 860 | SpecString += '>'; |
| 861 | |
| 862 | return SpecString; |
| 863 | } |
| 864 | |
| 865 | void QualType::dump(const char *msg) const { |
| 866 | std::string R = "identifier"; |
| 867 | LangOptions LO; |
| 868 | getAsStringInternal(R, PrintingPolicy(LO)); |
| 869 | if (msg) |
Daniel Dunbar | e7cb7e4 | 2009-12-03 09:14:02 +0000 | [diff] [blame] | 870 | llvm::errs() << msg << ": "; |
| 871 | llvm::errs() << R << "\n"; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 872 | } |
| 873 | void QualType::dump() const { |
| 874 | dump(""); |
| 875 | } |
| 876 | |
| 877 | void Type::dump() const { |
| 878 | QualType(this, 0).dump(); |
| 879 | } |
| 880 | |
| 881 | std::string Qualifiers::getAsString() const { |
| 882 | LangOptions LO; |
| 883 | return getAsString(PrintingPolicy(LO)); |
| 884 | } |
| 885 | |
| 886 | // Appends qualifiers to the given string, separated by spaces. Will |
| 887 | // prefix a space if the string is non-empty. Will not append a final |
| 888 | // space. |
| 889 | void Qualifiers::getAsStringInternal(std::string &S, |
| 890 | const PrintingPolicy&) const { |
| 891 | AppendTypeQualList(S, getCVRQualifiers()); |
| 892 | if (unsigned AddressSpace = getAddressSpace()) { |
| 893 | if (!S.empty()) S += ' '; |
| 894 | S += "__attribute__((address_space("; |
| 895 | S += llvm::utostr_32(AddressSpace); |
| 896 | S += ")))"; |
| 897 | } |
| 898 | if (Qualifiers::GC GCAttrType = getObjCGCAttr()) { |
| 899 | if (!S.empty()) S += ' '; |
| 900 | S += "__attribute__((objc_gc("; |
| 901 | if (GCAttrType == Qualifiers::Weak) |
| 902 | S += "weak"; |
| 903 | else |
| 904 | S += "strong"; |
| 905 | S += ")))"; |
| 906 | } |
| 907 | } |
| 908 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 909 | std::string QualType::getAsString(const Type *ty, Qualifiers qs) { |
| 910 | std::string buffer; |
| 911 | LangOptions options; |
| 912 | getAsStringInternal(ty, qs, buffer, PrintingPolicy(options)); |
| 913 | return buffer; |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 914 | } |
| 915 | |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 916 | void QualType::getAsStringInternal(const Type *ty, Qualifiers qs, |
| 917 | std::string &buffer, |
| 918 | const PrintingPolicy &policy) { |
| 919 | TypePrinter(policy).print(ty, qs, buffer); |
Douglas Gregor | fee8a3c | 2009-11-10 00:39:07 +0000 | [diff] [blame] | 920 | } |