blob: 13ffb0e220988a2f5cc9531e78df3e1617ee46b1 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer5f016e22007-07-11 17:01:13 +000014#include "clang/AST/Type.h"
15#include "clang/AST/Decl.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/Expr.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000018#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "clang/Basic/TargetInfo.h"
20#include "llvm/Support/Streams.h"
21#include "llvm/ADT/StringExtras.h"
Steve Naroff8d1a3b82007-08-01 17:20:42 +000022#include <sstream>
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26Type::~Type() {}
27
28/// isVoidType - Helper method to determine if this is the 'void' type.
29bool Type::isVoidType() const {
30 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
31 return BT->getKind() == BuiltinType::Void;
32 return false;
33}
34
35bool Type::isObjectType() const {
36 if (isa<FunctionType>(CanonicalType))
37 return false;
38 else if (CanonicalType->isIncompleteType())
39 return false;
40 else
41 return true;
42}
43
44bool Type::isDerivedType() const {
45 switch (CanonicalType->getTypeClass()) {
46 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000047 case VariableArray:
48 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +000049 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000050 case FunctionProto:
51 case FunctionNoProto:
52 case Reference:
53 return true;
54 case Tagged: {
55 const TagType *TT = cast<TagType>(CanonicalType);
56 const Decl::Kind Kind = TT->getDecl()->getKind();
57 return Kind == Decl::Struct || Kind == Decl::Union;
58 }
59 default:
60 return false;
61 }
62}
63
Chris Lattnerc8629632007-07-31 19:29:30 +000064bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000065 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000066 if (RT->getDecl()->getKind() == Decl::Struct)
67 return true;
68 return false;
69}
70bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000071 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000072 if (RT->getDecl()->getKind() == Decl::Union)
73 return true;
74 return false;
75}
Chris Lattnerc8629632007-07-31 19:29:30 +000076
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000077bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +000078 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
79 return CT->getElementType()->isFloatingType();
80 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000081}
82
Steve Naroff4cdec1c2008-01-15 01:41:59 +000083bool Type::isComplexIntegerType() const {
84 // Check for GCC complex integer extension.
85 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
86 return CT->getElementType()->isIntegerType();
87 return false;
88}
89
90const ComplexType *Type::getAsComplexIntegerType() const {
91 // Are we directly a complex type?
92 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
93 if (CTy->getElementType()->isIntegerType())
94 return CTy;
95 }
96 // If the canonical form of this type isn't the right kind, reject it.
97 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
98 if (!CTy || !CTy->getElementType()->isIntegerType())
99 return 0;
100
101 // If this is a typedef for a complex type, strip the typedef off without
102 // losing all typedef information.
103 return getDesugaredType()->getAsComplexIntegerType();
104}
105
Chris Lattnerdea61462007-10-29 03:41:11 +0000106/// getDesugaredType - Return the specified type with any "sugar" removed from
107/// type type. This takes off typedefs, typeof's etc. If the outer level of
108/// the type is already concrete, it returns it unmodified. This is similar
109/// to getting the canonical type, but it doesn't remove *all* typedefs. For
110/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
111/// concrete.
112const Type *Type::getDesugaredType() const {
113 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
114 return TDT->LookThroughTypedefs().getTypePtr();
115 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
116 return TOE->getUnderlyingExpr()->getType().getTypePtr();
117 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
118 return TOT->getUnderlyingType().getTypePtr();
119 return this;
120}
121
122
Steve Naroff77878cc2007-08-27 04:08:11 +0000123const BuiltinType *Type::getAsBuiltinType() const {
124 // If this is directly a builtin type, return it.
125 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
126 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000127
128 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000129 if (!isa<BuiltinType>(CanonicalType)) {
130 // Look through type qualifiers
131 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
132 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000133 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000134 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000135
Steve Naroff77878cc2007-08-27 04:08:11 +0000136 // If this is a typedef for a builtin type, strip the typedef off without
137 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000138 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000139}
140
Chris Lattnerc8629632007-07-31 19:29:30 +0000141const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000142 // If this is directly a function type, return it.
143 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
144 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000145
Chris Lattnerdea61462007-10-29 03:41:11 +0000146 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000147 if (!isa<FunctionType>(CanonicalType)) {
148 // Look through type qualifiers
149 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
150 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000151 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000152 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000153
Steve Naroff7064f5c2007-07-26 18:32:01 +0000154 // If this is a typedef for a function type, strip the typedef off without
155 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000156 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000157}
158
Chris Lattnerbefee482007-07-31 16:53:04 +0000159const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000160 // If this is directly a pointer type, return it.
161 if (const PointerType *PTy = dyn_cast<PointerType>(this))
162 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000163
Chris Lattnerdea61462007-10-29 03:41:11 +0000164 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000165 if (!isa<PointerType>(CanonicalType)) {
166 // Look through type qualifiers
167 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
168 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000169 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000170 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000171
Chris Lattnera2c77672007-07-16 22:05:22 +0000172 // If this is a typedef for a pointer type, strip the typedef off without
173 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000174 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000177const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000178 // If this is directly a reference type, return it.
179 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
180 return RTy;
181
Chris Lattnerdea61462007-10-29 03:41:11 +0000182 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000183 if (!isa<ReferenceType>(CanonicalType)) {
184 // Look through type qualifiers
185 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
186 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000187 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000188 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000189
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000190 // If this is a typedef for a reference type, strip the typedef off without
191 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000192 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000193}
194
Chris Lattnerc8629632007-07-31 19:29:30 +0000195const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000196 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000197 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
198 return ATy;
199
Chris Lattnerdea61462007-10-29 03:41:11 +0000200 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000201 if (!isa<ArrayType>(CanonicalType)) {
202 // Look through type qualifiers
203 if (isa<ArrayType>(CanonicalType.getUnqualifiedType()))
204 return CanonicalType.getUnqualifiedType()->getAsArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000205 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000206 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000207
Steve Naroff700204c2007-07-24 21:46:40 +0000208 // If this is a typedef for an array type, strip the typedef off without
209 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000210 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Steve Naroffd7444aa2007-08-31 17:20:07 +0000213const ConstantArrayType *Type::getAsConstantArrayType() const {
214 // If this is directly a constant array type, return it.
215 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
216 return ATy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000217
Chris Lattnerdea61462007-10-29 03:41:11 +0000218 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000219 if (!isa<ConstantArrayType>(CanonicalType)) {
220 // Look through type qualifiers
221 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()))
222 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000223 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000224 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000225
226 // If this is a typedef for a constant array type, strip the typedef off
227 // without losing all typedef information.
228 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000229}
230
231const VariableArrayType *Type::getAsVariableArrayType() const {
232 // If this is directly a variable array type, return it.
233 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
234 return ATy;
235
Chris Lattnerdea61462007-10-29 03:41:11 +0000236 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000237 if (!isa<VariableArrayType>(CanonicalType)) {
238 // Look through type qualifiers
239 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType()))
240 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000241 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000242 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000243
244 // If this is a typedef for a variable array type, strip the typedef off
245 // without losing all typedef information.
246 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000247}
248
249/// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
250/// types that have a non-constant expression. This does not include "[]".
251bool Type::isVariablyModifiedType() const {
252 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
253 if (VAT->getSizeExpr())
254 return true;
255 }
256 return false;
257}
258
Steve Naroff5c06a692008-01-21 22:59:18 +0000259bool Type::isIncompleteArrayType() const {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000260 return isa<IncompleteArrayType>(CanonicalType);
Steve Naroff5c06a692008-01-21 22:59:18 +0000261}
262
Eli Friedmanc5773c42008-02-15 18:16:39 +0000263const IncompleteArrayType *Type::getAsIncompleteArrayType() const {
264 // If this is directly a variable array type, return it.
265 if (const IncompleteArrayType *ATy = dyn_cast<IncompleteArrayType>(this))
266 return ATy;
267
268 // If the canonical form of this type isn't the right kind, reject it.
269 if (!isa<IncompleteArrayType>(CanonicalType)) {
270 // Look through type qualifiers
271 if (isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()))
272 return CanonicalType.getUnqualifiedType()->getAsIncompleteArrayType();
273 return 0;
Steve Naroff5c06a692008-01-21 22:59:18 +0000274 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000275
276 // If this is a typedef for a variable array type, strip the typedef off
277 // without losing all typedef information.
278 return getDesugaredType()->getAsIncompleteArrayType();
Steve Naroff5c06a692008-01-21 22:59:18 +0000279}
280
Chris Lattnerc8629632007-07-31 19:29:30 +0000281const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000282 // If this is directly a reference type, return it.
283 if (const RecordType *RTy = dyn_cast<RecordType>(this))
284 return RTy;
285
Chris Lattnerdea61462007-10-29 03:41:11 +0000286 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000287 if (!isa<RecordType>(CanonicalType)) {
288 // Look through type qualifiers
289 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
290 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000291 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000292 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000293
294 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000295 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000296 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000297}
298
Chris Lattnerc8629632007-07-31 19:29:30 +0000299const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000300 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000301 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
302 if (RT->getDecl()->getKind() == Decl::Struct)
303 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000304 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000305
306 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000307 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000308 if (RT->getDecl()->getKind() != Decl::Struct)
309 return 0;
310
311 // If this is a typedef for a structure type, strip the typedef off without
312 // losing all typedef information.
313 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000315 // Look through type qualifiers
316 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
317 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000318 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000319}
320
Chris Lattnerc8629632007-07-31 19:29:30 +0000321const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000322 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000323 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
324 if (RT->getDecl()->getKind() == Decl::Union)
325 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000326 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000327
Chris Lattnerdea61462007-10-29 03:41:11 +0000328 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000329 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000330 if (RT->getDecl()->getKind() != Decl::Union)
331 return 0;
332
333 // If this is a typedef for a union type, strip the typedef off without
334 // losing all typedef information.
335 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000337
338 // Look through type qualifiers
339 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
340 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000341 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000342}
343
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000344const ComplexType *Type::getAsComplexType() const {
345 // Are we directly a complex type?
346 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
347 return CTy;
348
Chris Lattnerdea61462007-10-29 03:41:11 +0000349 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000350 if (!isa<ComplexType>(CanonicalType)) {
351 // Look through type qualifiers
352 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
353 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000354 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000355 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000356
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000357 // If this is a typedef for a complex type, strip the typedef off without
358 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000359 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000360}
361
Chris Lattnerc8629632007-07-31 19:29:30 +0000362const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000363 // Are we directly a vector type?
364 if (const VectorType *VTy = dyn_cast<VectorType>(this))
365 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000366
Chris Lattnerdea61462007-10-29 03:41:11 +0000367 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000368 if (!isa<VectorType>(CanonicalType)) {
369 // Look through type qualifiers
370 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
371 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000372 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000373 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000374
Chris Lattnera2c77672007-07-16 22:05:22 +0000375 // If this is a typedef for a vector type, strip the typedef off without
376 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000377 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000378}
379
Chris Lattnerc8629632007-07-31 19:29:30 +0000380const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000381 // Are we directly an OpenCU vector type?
382 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
383 return VTy;
384
Chris Lattnerdea61462007-10-29 03:41:11 +0000385 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000386 if (!isa<OCUVectorType>(CanonicalType)) {
387 // Look through type qualifiers
388 if (isa<OCUVectorType>(CanonicalType.getUnqualifiedType()))
389 return CanonicalType.getUnqualifiedType()->getAsOCUVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000390 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000391 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000392
Chris Lattnerdea61462007-10-29 03:41:11 +0000393 // If this is a typedef for an ocuvector type, strip the typedef off without
394 // losing all typedef information.
395 return getDesugaredType()->getAsOCUVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000396}
397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398bool Type::isIntegerType() const {
399 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
400 return BT->getKind() >= BuiltinType::Bool &&
401 BT->getKind() <= BuiltinType::LongLong;
402 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
403 if (TT->getDecl()->getKind() == Decl::Enum)
404 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000405 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
406 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000407 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
408 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 return false;
410}
411
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000412bool Type::isIntegralType() const {
413 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
414 return BT->getKind() >= BuiltinType::Bool &&
415 BT->getKind() <= BuiltinType::LongLong;
416 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
417 if (TT->getDecl()->getKind() == Decl::Enum)
418 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000419 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
420 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000421 return false;
422}
423
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000424bool Type::isEnumeralType() const {
425 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
426 return TT->getDecl()->getKind() == Decl::Enum;
Christopher Lambebb97e92008-02-04 02:31:56 +0000427 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
428 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000429 return false;
430}
431
432bool Type::isBooleanType() const {
433 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
434 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000435 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
436 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000437 return false;
438}
439
440bool Type::isCharType() const {
441 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
442 return BT->getKind() == BuiltinType::Char_U ||
443 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000444 BT->getKind() == BuiltinType::Char_S ||
445 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000446 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
447 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000448 return false;
449}
450
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000451/// isSignedIntegerType - Return true if this is an integer type that is
452/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
453/// an enum decl which has a signed representation, or a vector of signed
454/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000455bool Type::isSignedIntegerType() const {
456 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
457 return BT->getKind() >= BuiltinType::Char_S &&
458 BT->getKind() <= BuiltinType::LongLong;
459 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000460
461 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
462 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
463 return ED->getIntegerType()->isSignedIntegerType();
464
Steve Naroffc63b96a2007-07-12 21:46:55 +0000465 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
466 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000467 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
468 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 return false;
470}
471
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000472/// isUnsignedIntegerType - Return true if this is an integer type that is
473/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
474/// decl which has an unsigned representation, or a vector of unsigned integer
475/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000476bool Type::isUnsignedIntegerType() const {
477 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
478 return BT->getKind() >= BuiltinType::Bool &&
479 BT->getKind() <= BuiltinType::ULongLong;
480 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000481
482 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
483 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
484 return ED->getIntegerType()->isUnsignedIntegerType();
485
Steve Naroffc63b96a2007-07-12 21:46:55 +0000486 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
487 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000488 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
489 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 return false;
491}
492
493bool Type::isFloatingType() const {
494 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
495 return BT->getKind() >= BuiltinType::Float &&
496 BT->getKind() <= BuiltinType::LongDouble;
497 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000498 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000499 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
500 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000501 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
502 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 return false;
504}
505
506bool Type::isRealFloatingType() const {
507 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
508 return BT->getKind() >= BuiltinType::Float &&
509 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000510 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
511 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000512 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
513 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000514 return false;
515}
516
517bool Type::isRealType() const {
518 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
519 return BT->getKind() >= BuiltinType::Bool &&
520 BT->getKind() <= BuiltinType::LongDouble;
521 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
522 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000523 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
524 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000525 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
526 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 return false;
528}
529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530bool Type::isArithmeticType() const {
531 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
532 return BT->getKind() != BuiltinType::Void;
533 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Steve Naroff67c49e82008-01-16 23:54:22 +0000534 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
535 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
Steve Naroff494783a2008-01-16 23:56:32 +0000536 // If a body isn't seen by the time we get here, return false.
Steve Naroff67c49e82008-01-16 23:54:22 +0000537 return ED->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000538 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
539 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
541}
542
543bool Type::isScalarType() const {
544 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
545 return BT->getKind() != BuiltinType::Void;
546 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
547 if (TT->getDecl()->getKind() == Decl::Enum)
548 return true;
549 return false;
550 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000551 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
552 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000553 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000554 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000555}
556
557bool Type::isAggregateType() const {
558 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
559 if (TT->getDecl()->getKind() == Decl::Struct)
560 return true;
561 return false;
562 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000563 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
564 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000565 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000566}
567
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000568/// isConstantSizeType - Return true if this is not a variable sized type,
569/// according to the rules of C99 6.7.5p3. It is not legal to call this on
570/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000571bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000572 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000573 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000574 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000575 // The VAT must have a size, as it is known to be complete.
576 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000577}
578
579/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
580/// - a type that can describe objects, but which lacks information needed to
581/// determine its size.
582bool Type::isIncompleteType() const {
583 switch (CanonicalType->getTypeClass()) {
584 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000585 case ASQual:
586 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 case Builtin:
588 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
589 // be completed.
590 return isVoidType();
591 case Tagged:
592 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
593 // forward declaration, but not a full definition (C99 6.2.5p22).
594 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000595 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000597 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 }
599}
600
601bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000602 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
603 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
605 if (!BT) return false;
606 switch (BT->getKind()) {
607 case BuiltinType::Bool:
608 case BuiltinType::Char_S:
609 case BuiltinType::Char_U:
610 case BuiltinType::SChar:
611 case BuiltinType::UChar:
612 case BuiltinType::Short:
613 case BuiltinType::UShort:
614 return true;
615 default:
616 return false;
617 }
618}
619
620const char *BuiltinType::getName() const {
621 switch (getKind()) {
622 default: assert(0 && "Unknown builtin type!");
623 case Void: return "void";
624 case Bool: return "_Bool";
625 case Char_S: return "char";
626 case Char_U: return "char";
627 case SChar: return "signed char";
628 case Short: return "short";
629 case Int: return "int";
630 case Long: return "long";
631 case LongLong: return "long long";
632 case UChar: return "unsigned char";
633 case UShort: return "unsigned short";
634 case UInt: return "unsigned int";
635 case ULong: return "unsigned long";
636 case ULongLong: return "unsigned long long";
637 case Float: return "float";
638 case Double: return "double";
639 case LongDouble: return "long double";
640 }
641}
642
Reid Spencer5f016e22007-07-11 17:01:13 +0000643void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000644 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 unsigned NumArgs, bool isVariadic) {
646 ID.AddPointer(Result.getAsOpaquePtr());
647 for (unsigned i = 0; i != NumArgs; ++i)
648 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
649 ID.AddInteger(isVariadic);
650}
651
652void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000653 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000654}
655
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000656void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
657 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000658 unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000659 for (unsigned i = 0; i != NumProtocols; i++)
660 ID.AddPointer(protocols[i]);
661}
662
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000663void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000664 Profile(ID, &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000665}
666
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000667void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
668 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000669 unsigned NumProtocols) {
670 for (unsigned i = 0; i != NumProtocols; i++)
671 ID.AddPointer(protocols[i]);
672}
673
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000674void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000675 Profile(ID, &Protocols[0], getNumProtocols());
676}
677
Chris Lattnera2c77672007-07-16 22:05:22 +0000678/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
679/// potentially looking through *all* consequtive typedefs. This returns the
680/// sum of the type qualifiers, so if you have:
681/// typedef const int A;
682/// typedef volatile A B;
683/// looking through the typedefs for B will give you "const volatile A".
684///
685QualType TypedefType::LookThroughTypedefs() const {
686 // Usually, there is only a single level of typedefs, be fast in that case.
687 QualType FirstType = getDecl()->getUnderlyingType();
688 if (!isa<TypedefType>(FirstType))
689 return FirstType;
690
691 // Otherwise, do the fully general loop.
692 unsigned TypeQuals = 0;
693 const TypedefType *TDT = this;
694 while (1) {
695 QualType CurType = TDT->getDecl()->getUnderlyingType();
696 TypeQuals |= CurType.getQualifiers();
697
698 TDT = dyn_cast<TypedefType>(CurType);
699 if (TDT == 0)
700 return QualType(CurType.getTypePtr(), TypeQuals);
701 }
702}
Reid Spencer5f016e22007-07-11 17:01:13 +0000703
704bool RecordType::classof(const Type *T) {
705 if (const TagType *TT = dyn_cast<TagType>(T))
706 return isa<RecordDecl>(TT->getDecl());
707 return false;
708}
709
710
711//===----------------------------------------------------------------------===//
712// Type Printing
713//===----------------------------------------------------------------------===//
714
715void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000716 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 getAsStringInternal(R);
718 if (msg)
719 fprintf(stderr, "%s: %s\n", msg, R.c_str());
720 else
721 fprintf(stderr, "%s\n", R.c_str());
722}
723
724static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
725 // Note: funkiness to ensure we get a space only between quals.
726 bool NonePrinted = true;
727 if (TypeQuals & QualType::Const)
728 S += "const", NonePrinted = false;
729 if (TypeQuals & QualType::Volatile)
730 S += (NonePrinted+" volatile"), NonePrinted = false;
731 if (TypeQuals & QualType::Restrict)
732 S += (NonePrinted+" restrict"), NonePrinted = false;
733}
734
735void QualType::getAsStringInternal(std::string &S) const {
736 if (isNull()) {
737 S += "NULL TYPE\n";
738 return;
739 }
740
741 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000742 unsigned TQ = getQualifiers();
743 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 std::string TQS;
745 AppendTypeQualList(TQS, TQ);
746 if (!S.empty())
747 S = TQS + ' ' + S;
748 else
749 S = TQS;
750 }
751
752 getTypePtr()->getAsStringInternal(S);
753}
754
755void BuiltinType::getAsStringInternal(std::string &S) const {
756 if (S.empty()) {
757 S = getName();
758 } else {
759 // Prefix the basic type, e.g. 'int X'.
760 S = ' ' + S;
761 S = getName() + S;
762 }
763}
764
765void ComplexType::getAsStringInternal(std::string &S) const {
766 ElementType->getAsStringInternal(S);
767 S = "_Complex " + S;
768}
769
Christopher Lambebb97e92008-02-04 02:31:56 +0000770void ASQualType::getAsStringInternal(std::string &S) const {
771 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
772 BaseType->getAsStringInternal(S);
773}
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775void PointerType::getAsStringInternal(std::string &S) const {
776 S = '*' + S;
777
778 // Handle things like 'int (*A)[4];' correctly.
779 // FIXME: this should include vectors, but vectors use attributes I guess.
780 if (isa<ArrayType>(PointeeType.getTypePtr()))
781 S = '(' + S + ')';
782
783 PointeeType.getAsStringInternal(S);
784}
785
786void ReferenceType::getAsStringInternal(std::string &S) const {
787 S = '&' + S;
788
789 // Handle things like 'int (&A)[4];' correctly.
790 // FIXME: this should include vectors, but vectors use attributes I guess.
791 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
792 S = '(' + S + ')';
793
794 ReferenceeType.getAsStringInternal(S);
795}
796
Steve Narofffb22d962007-08-30 01:06:46 +0000797void ConstantArrayType::getAsStringInternal(std::string &S) const {
798 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000799 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000800 S += ']';
801
802 getElementType().getAsStringInternal(S);
803}
804
Eli Friedmanc5773c42008-02-15 18:16:39 +0000805void IncompleteArrayType::getAsStringInternal(std::string &S) const {
806 S += "[]";
807
808 getElementType().getAsStringInternal(S);
809}
810
Steve Narofffb22d962007-08-30 01:06:46 +0000811void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 S += '[';
813
Steve Naroffc9406122007-08-30 18:10:14 +0000814 if (getIndexTypeQualifier()) {
815 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 S += ' ';
817 }
818
Steve Naroffc9406122007-08-30 18:10:14 +0000819 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000821 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 S += '*';
823
Steve Narofffb22d962007-08-30 01:06:46 +0000824 if (getSizeExpr()) {
825 std::ostringstream s;
826 getSizeExpr()->printPretty(s);
827 S += s.str();
828 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 S += ']';
830
Steve Narofffb22d962007-08-30 01:06:46 +0000831 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000832}
833
834void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000835 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000836 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000838 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 ElementType.getAsStringInternal(S);
840}
841
Steve Naroff31a45842007-07-28 23:10:27 +0000842void OCUVectorType::getAsStringInternal(std::string &S) const {
843 S += " __attribute__((ocu_vector_type(";
844 S += llvm::utostr_32(NumElements);
845 S += ")))";
846 ElementType.getAsStringInternal(S);
847}
848
Steve Naroffd1861fd2007-07-31 12:34:36 +0000849void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000850 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
851 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000852 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000853 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000854 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000855}
856
Steve Naroff363bcff2007-08-01 23:45:51 +0000857void TypeOfType::getAsStringInternal(std::string &InnerString) const {
858 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
859 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000860 std::string Tmp;
861 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000862 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000863}
864
Reid Spencer5f016e22007-07-11 17:01:13 +0000865void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
866 // If needed for precedence reasons, wrap the inner part in grouping parens.
867 if (!S.empty())
868 S = "(" + S + ")";
869
870 S += "()";
871 getResultType().getAsStringInternal(S);
872}
873
874void FunctionTypeProto::getAsStringInternal(std::string &S) const {
875 // If needed for precedence reasons, wrap the inner part in grouping parens.
876 if (!S.empty())
877 S = "(" + S + ")";
878
879 S += "(";
880 std::string Tmp;
881 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
882 if (i) S += ", ";
883 getArgType(i).getAsStringInternal(Tmp);
884 S += Tmp;
885 Tmp.clear();
886 }
887
888 if (isVariadic()) {
889 if (getNumArgs())
890 S += ", ";
891 S += "...";
892 } else if (getNumArgs() == 0) {
893 // Do not emit int() if we have a proto, emit 'int(void)'.
894 S += "void";
895 }
896
897 S += ")";
898 getResultType().getAsStringInternal(S);
899}
900
901
902void TypedefType::getAsStringInternal(std::string &InnerString) const {
903 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
904 InnerString = ' ' + InnerString;
905 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
906}
907
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000908void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000909 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
910 InnerString = ' ' + InnerString;
911 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
912}
913
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000914void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000915 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000916 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
917 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 std::string ObjCQIString = getDecl()->getName();
919 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000920 int num = getNumProtocols();
921 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000922 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000923 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000924 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000925 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926 ObjCQIString += '>';
927 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000928}
929
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000930void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000931 std::string &InnerString) const {
932 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
933 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934 std::string ObjCQIString = "id";
935 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000936 int num = getNumProtocols();
937 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000939 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000940 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000941 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000942 ObjCQIString += '>';
943 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000944}
945
Reid Spencer5f016e22007-07-11 17:01:13 +0000946void TagType::getAsStringInternal(std::string &InnerString) const {
947 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
948 InnerString = ' ' + InnerString;
949
950 const char *Kind = getDecl()->getKindName();
951 const char *ID;
952 if (const IdentifierInfo *II = getDecl()->getIdentifier())
953 ID = II->getName();
954 else
955 ID = "<anonymous>";
956
957 InnerString = std::string(Kind) + " " + ID + InnerString;
958}