blob: e561a1074cea650f304e4aee310c181d73ede948 [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>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000025void Type::Destroy(ASTContext& C) { delete this; }
26
27void FunctionTypeProto::Destroy(ASTContext& C) {
28 // Destroy the object, but don't call delete. These are malloc'd.
29 this->~FunctionTypeProto();
30 free(this);
31}
32
33void VariableArrayType::Destroy(ASTContext& C) {
34 SizeExpr->Destroy(C);
35 delete this;
36}
Reid Spencer5f016e22007-07-11 17:01:13 +000037
38/// isVoidType - Helper method to determine if this is the 'void' type.
39bool Type::isVoidType() const {
40 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
41 return BT->getKind() == BuiltinType::Void;
42 return false;
43}
44
45bool Type::isObjectType() const {
46 if (isa<FunctionType>(CanonicalType))
47 return false;
48 else if (CanonicalType->isIncompleteType())
49 return false;
50 else
51 return true;
52}
53
54bool Type::isDerivedType() const {
55 switch (CanonicalType->getTypeClass()) {
56 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000057 case VariableArray:
58 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +000059 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000060 case FunctionProto:
61 case FunctionNoProto:
62 case Reference:
63 return true;
64 case Tagged: {
65 const TagType *TT = cast<TagType>(CanonicalType);
66 const Decl::Kind Kind = TT->getDecl()->getKind();
67 return Kind == Decl::Struct || Kind == Decl::Union;
68 }
69 default:
70 return false;
71 }
72}
73
Chris Lattner99dc9142008-04-13 18:59:07 +000074bool Type::isClassType() const {
75 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
76 if (RT->getDecl()->getKind() == Decl::Class)
77 return true;
78 return false;
79}
Chris Lattnerc8629632007-07-31 19:29:30 +000080bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000081 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000082 if (RT->getDecl()->getKind() == Decl::Struct)
83 return true;
84 return false;
85}
86bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000087 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000088 if (RT->getDecl()->getKind() == Decl::Union)
89 return true;
90 return false;
91}
Chris Lattnerc8629632007-07-31 19:29:30 +000092
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000093bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +000094 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
95 return CT->getElementType()->isFloatingType();
96 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000097}
98
Steve Naroff4cdec1c2008-01-15 01:41:59 +000099bool Type::isComplexIntegerType() const {
100 // Check for GCC complex integer extension.
101 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
102 return CT->getElementType()->isIntegerType();
103 return false;
104}
105
106const ComplexType *Type::getAsComplexIntegerType() const {
107 // Are we directly a complex type?
108 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
109 if (CTy->getElementType()->isIntegerType())
110 return CTy;
111 }
112 // If the canonical form of this type isn't the right kind, reject it.
113 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
114 if (!CTy || !CTy->getElementType()->isIntegerType())
115 return 0;
116
117 // If this is a typedef for a complex type, strip the typedef off without
118 // losing all typedef information.
119 return getDesugaredType()->getAsComplexIntegerType();
120}
121
Chris Lattnerdea61462007-10-29 03:41:11 +0000122/// getDesugaredType - Return the specified type with any "sugar" removed from
123/// type type. This takes off typedefs, typeof's etc. If the outer level of
124/// the type is already concrete, it returns it unmodified. This is similar
125/// to getting the canonical type, but it doesn't remove *all* typedefs. For
126/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
127/// concrete.
128const Type *Type::getDesugaredType() const {
129 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
130 return TDT->LookThroughTypedefs().getTypePtr();
131 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
132 return TOE->getUnderlyingExpr()->getType().getTypePtr();
133 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
134 return TOT->getUnderlyingType().getTypePtr();
135 return this;
136}
137
138
Steve Naroff77878cc2007-08-27 04:08:11 +0000139const BuiltinType *Type::getAsBuiltinType() const {
140 // If this is directly a builtin type, return it.
141 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
142 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000143
144 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000145 if (!isa<BuiltinType>(CanonicalType)) {
146 // Look through type qualifiers
147 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
148 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000149 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000150 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000151
Steve Naroff77878cc2007-08-27 04:08:11 +0000152 // If this is a typedef for a builtin type, strip the typedef off without
153 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000154 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000155}
156
Chris Lattnerc8629632007-07-31 19:29:30 +0000157const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000158 // If this is directly a function type, return it.
159 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
160 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000161
Chris Lattnerdea61462007-10-29 03:41:11 +0000162 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000163 if (!isa<FunctionType>(CanonicalType)) {
164 // Look through type qualifiers
165 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
166 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000167 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000168 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000169
Steve Naroff7064f5c2007-07-26 18:32:01 +0000170 // If this is a typedef for a function type, strip the typedef off without
171 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000172 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000173}
174
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000175const PointerLikeType *Type::getAsPointerLikeType() const {
176 // If this is directly a pointer-like type, return it.
177 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
178 return PTy;
179
180 // If the canonical form of this type isn't the right kind, reject it.
181 if (!isa<PointerLikeType>(CanonicalType)) {
182 // Look through type qualifiers
183 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
184 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
185 return 0;
186 }
187
188 // If this is a typedef for a pointer type, strip the typedef off without
189 // losing all typedef information.
190 return getDesugaredType()->getAsPointerLikeType();
191}
192
Chris Lattnerbefee482007-07-31 16:53:04 +0000193const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000194 // If this is directly a pointer type, return it.
195 if (const PointerType *PTy = dyn_cast<PointerType>(this))
196 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000197
Chris Lattnerdea61462007-10-29 03:41:11 +0000198 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000199 if (!isa<PointerType>(CanonicalType)) {
200 // Look through type qualifiers
201 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
202 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000203 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000204 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000205
Chris Lattnera2c77672007-07-16 22:05:22 +0000206 // If this is a typedef for a pointer type, strip the typedef off without
207 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000208 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000209}
210
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000211const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000212 // If this is directly a reference type, return it.
213 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
214 return RTy;
215
Chris Lattnerdea61462007-10-29 03:41:11 +0000216 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000217 if (!isa<ReferenceType>(CanonicalType)) {
218 // Look through type qualifiers
219 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
220 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000221 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000222 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000223
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000224 // If this is a typedef for a reference type, strip the typedef off without
225 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000226 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000227}
228
Chris Lattnerc8629632007-07-31 19:29:30 +0000229const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000230 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000231 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
232 return ATy;
233
Chris Lattnerdea61462007-10-29 03:41:11 +0000234 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000235 if (!isa<ArrayType>(CanonicalType)) {
236 // Look through type qualifiers
237 if (isa<ArrayType>(CanonicalType.getUnqualifiedType()))
238 return CanonicalType.getUnqualifiedType()->getAsArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000239 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000240 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000241
Steve Naroff700204c2007-07-24 21:46:40 +0000242 // If this is a typedef for an array type, strip the typedef off without
243 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000244 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000245}
246
Steve Naroffd7444aa2007-08-31 17:20:07 +0000247const ConstantArrayType *Type::getAsConstantArrayType() const {
248 // If this is directly a constant array type, return it.
249 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
250 return ATy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000251
Chris Lattnerdea61462007-10-29 03:41:11 +0000252 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000253 if (!isa<ConstantArrayType>(CanonicalType)) {
254 // Look through type qualifiers
255 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()))
256 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000257 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000258 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000259
260 // If this is a typedef for a constant array type, strip the typedef off
261 // without losing all typedef information.
262 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000263}
264
265const VariableArrayType *Type::getAsVariableArrayType() const {
266 // If this is directly a variable array type, return it.
267 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
268 return ATy;
269
Chris Lattnerdea61462007-10-29 03:41:11 +0000270 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000271 if (!isa<VariableArrayType>(CanonicalType)) {
272 // Look through type qualifiers
273 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType()))
274 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000275 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000276 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000277
278 // If this is a typedef for a variable array type, strip the typedef off
279 // without losing all typedef information.
280 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000281}
282
Eli Friedmand3f2f792008-02-17 00:59:11 +0000283/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
284/// array types and types that contain variable array types in their
285/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000286bool Type::isVariablyModifiedType() const {
Eli Friedmand3f2f792008-02-17 00:59:11 +0000287 // A VLA is a veriably modified type
288 if (getAsVariableArrayType())
289 return true;
290
291 // An array can contain a variably modified type
292 if (const ArrayType* AT = getAsArrayType())
293 return AT->getElementType()->isVariablyModifiedType();
294
295 // A pointer can point to a variably modified type
296 if (const PointerType* PT = getAsPointerType())
297 return PT->getPointeeType()->isVariablyModifiedType();
298
299 // A function can return a variably modified type
300 // This one isn't completely obvious, but it follows from the
301 // definition in C99 6.7.5p3. Because of this rule, it's
302 // illegal to declare a function returning a variably modified type.
303 if (const FunctionType* FT = getAsFunctionType())
304 return FT->getResultType()->isVariablyModifiedType();
305
Steve Naroffd7444aa2007-08-31 17:20:07 +0000306 return false;
307}
308
Steve Naroff5c06a692008-01-21 22:59:18 +0000309bool Type::isIncompleteArrayType() const {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000310 return isa<IncompleteArrayType>(CanonicalType);
Steve Naroff5c06a692008-01-21 22:59:18 +0000311}
312
Eli Friedmanc5773c42008-02-15 18:16:39 +0000313const IncompleteArrayType *Type::getAsIncompleteArrayType() const {
314 // If this is directly a variable array type, return it.
315 if (const IncompleteArrayType *ATy = dyn_cast<IncompleteArrayType>(this))
316 return ATy;
317
318 // If the canonical form of this type isn't the right kind, reject it.
319 if (!isa<IncompleteArrayType>(CanonicalType)) {
320 // Look through type qualifiers
321 if (isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()))
322 return CanonicalType.getUnqualifiedType()->getAsIncompleteArrayType();
323 return 0;
Steve Naroff5c06a692008-01-21 22:59:18 +0000324 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000325
326 // If this is a typedef for a variable array type, strip the typedef off
327 // without losing all typedef information.
328 return getDesugaredType()->getAsIncompleteArrayType();
Steve Naroff5c06a692008-01-21 22:59:18 +0000329}
330
Chris Lattnerc8629632007-07-31 19:29:30 +0000331const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000332 // If this is directly a reference type, return it.
333 if (const RecordType *RTy = dyn_cast<RecordType>(this))
334 return RTy;
335
Chris Lattnerdea61462007-10-29 03:41:11 +0000336 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000337 if (!isa<RecordType>(CanonicalType)) {
338 // Look through type qualifiers
339 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
340 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000341 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000342 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000343
344 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000345 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000346 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000347}
348
Chris Lattnerc8629632007-07-31 19:29:30 +0000349const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000350 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000351 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
352 if (RT->getDecl()->getKind() == Decl::Struct)
353 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000354 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000355
356 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000357 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000358 if (RT->getDecl()->getKind() != Decl::Struct)
359 return 0;
360
361 // If this is a typedef for a structure type, strip the typedef off without
362 // losing all typedef information.
363 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000365 // Look through type qualifiers
366 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
367 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000368 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000369}
370
Chris Lattnerc8629632007-07-31 19:29:30 +0000371const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000372 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000373 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
374 if (RT->getDecl()->getKind() == Decl::Union)
375 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000376 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000377
Chris Lattnerdea61462007-10-29 03:41:11 +0000378 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000379 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000380 if (RT->getDecl()->getKind() != Decl::Union)
381 return 0;
382
383 // If this is a typedef for a union type, strip the typedef off without
384 // losing all typedef information.
385 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000387
388 // Look through type qualifiers
389 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
390 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000391 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000392}
393
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000394const ComplexType *Type::getAsComplexType() const {
395 // Are we directly a complex type?
396 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
397 return CTy;
398
Chris Lattnerdea61462007-10-29 03:41:11 +0000399 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000400 if (!isa<ComplexType>(CanonicalType)) {
401 // Look through type qualifiers
402 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
403 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000404 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000405 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000406
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000407 // If this is a typedef for a complex type, strip the typedef off without
408 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000409 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000410}
411
Chris Lattnerc8629632007-07-31 19:29:30 +0000412const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000413 // Are we directly a vector type?
414 if (const VectorType *VTy = dyn_cast<VectorType>(this))
415 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000416
Chris Lattnerdea61462007-10-29 03:41:11 +0000417 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000418 if (!isa<VectorType>(CanonicalType)) {
419 // Look through type qualifiers
420 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
421 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000422 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000423 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000424
Chris Lattnera2c77672007-07-16 22:05:22 +0000425 // If this is a typedef for a vector type, strip the typedef off without
426 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000427 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000428}
429
Nate Begeman213541a2008-04-18 23:10:10 +0000430const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000431 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000432 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000433 return VTy;
434
Chris Lattnerdea61462007-10-29 03:41:11 +0000435 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000436 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000437 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000438 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
439 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000440 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000441 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000442
Nate Begeman213541a2008-04-18 23:10:10 +0000443 // If this is a typedef for an extended vector type, strip the typedef off
444 // without losing all typedef information.
445 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000446}
447
Chris Lattner368eefa2008-04-07 00:27:04 +0000448const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000449 // There is no sugar for ObjCInterfaceType's, just return the canonical
450 // type pointer if it is the right class.
451 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000452}
453
454const ObjCQualifiedInterfaceType *
455Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000456 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
457 // type pointer if it is the right class.
458 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
459}
460
461const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
462 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
463 // type pointer if it is the right class.
464 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000465}
466
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468bool Type::isIntegerType() const {
469 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
470 return BT->getKind() >= BuiltinType::Bool &&
471 BT->getKind() <= BuiltinType::LongLong;
472 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
473 if (TT->getDecl()->getKind() == Decl::Enum)
474 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000475 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
476 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000477 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
478 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 return false;
480}
481
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000482bool Type::isIntegralType() const {
483 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
484 return BT->getKind() >= BuiltinType::Bool &&
485 BT->getKind() <= BuiltinType::LongLong;
486 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
487 if (TT->getDecl()->getKind() == Decl::Enum)
488 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000489 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
490 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000491 return false;
492}
493
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000494bool Type::isEnumeralType() const {
495 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
496 return TT->getDecl()->getKind() == Decl::Enum;
Christopher Lambebb97e92008-02-04 02:31:56 +0000497 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
498 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000499 return false;
500}
501
502bool Type::isBooleanType() const {
503 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
504 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000505 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
506 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000507 return false;
508}
509
510bool Type::isCharType() const {
511 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
512 return BT->getKind() == BuiltinType::Char_U ||
513 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000514 BT->getKind() == BuiltinType::Char_S ||
515 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000516 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
517 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000518 return false;
519}
520
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000521/// isSignedIntegerType - Return true if this is an integer type that is
522/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
523/// an enum decl which has a signed representation, or a vector of signed
524/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000525bool Type::isSignedIntegerType() const {
526 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
527 return BT->getKind() >= BuiltinType::Char_S &&
528 BT->getKind() <= BuiltinType::LongLong;
529 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000530
Chris Lattner37c1b782008-04-06 22:29:16 +0000531 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
532 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000533
Steve Naroffc63b96a2007-07-12 21:46:55 +0000534 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
535 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000536 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
537 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 return false;
539}
540
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000541/// isUnsignedIntegerType - Return true if this is an integer type that is
542/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
543/// decl which has an unsigned representation, or a vector of unsigned integer
544/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000545bool Type::isUnsignedIntegerType() const {
546 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
547 return BT->getKind() >= BuiltinType::Bool &&
548 BT->getKind() <= BuiltinType::ULongLong;
549 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000550
Chris Lattner37c1b782008-04-06 22:29:16 +0000551 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
552 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000553
Steve Naroffc63b96a2007-07-12 21:46:55 +0000554 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
555 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000556 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
557 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 return false;
559}
560
561bool Type::isFloatingType() const {
562 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
563 return BT->getKind() >= BuiltinType::Float &&
564 BT->getKind() <= BuiltinType::LongDouble;
565 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000566 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000567 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
568 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000569 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
570 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 return false;
572}
573
574bool Type::isRealFloatingType() const {
575 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
576 return BT->getKind() >= BuiltinType::Float &&
577 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000578 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
579 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000580 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
581 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 return false;
583}
584
585bool Type::isRealType() const {
586 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
587 return BT->getKind() >= BuiltinType::Bool &&
588 BT->getKind() <= BuiltinType::LongDouble;
589 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
590 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000591 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
592 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000593 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
594 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 return false;
596}
597
Reid Spencer5f016e22007-07-11 17:01:13 +0000598bool Type::isArithmeticType() const {
599 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
600 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000601 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
602 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
603 // If a body isn't seen by the time we get here, return false.
604 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000605 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
606 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
608}
609
610bool Type::isScalarType() const {
611 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
612 return BT->getKind() != BuiltinType::Void;
613 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
614 if (TT->getDecl()->getKind() == Decl::Enum)
615 return true;
616 return false;
617 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000618 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
619 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000620 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000621 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000622}
623
624bool Type::isAggregateType() const {
625 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
626 if (TT->getDecl()->getKind() == Decl::Struct)
627 return true;
628 return false;
629 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000630 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
631 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000632 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633}
634
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000635/// isConstantSizeType - Return true if this is not a variable sized type,
636/// according to the rules of C99 6.7.5p3. It is not legal to call this on
637/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000638bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000639 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000640 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000641 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000642 // The VAT must have a size, as it is known to be complete.
643 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000644}
645
646/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
647/// - a type that can describe objects, but which lacks information needed to
648/// determine its size.
649bool Type::isIncompleteType() const {
650 switch (CanonicalType->getTypeClass()) {
651 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000652 case ASQual:
653 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 case Builtin:
655 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
656 // be completed.
657 return isVoidType();
658 case Tagged:
659 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
660 // forward declaration, but not a full definition (C99 6.2.5p22).
661 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000662 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000664 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 }
666}
667
668bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000669 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
670 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
672 if (!BT) return false;
673 switch (BT->getKind()) {
674 case BuiltinType::Bool:
675 case BuiltinType::Char_S:
676 case BuiltinType::Char_U:
677 case BuiltinType::SChar:
678 case BuiltinType::UChar:
679 case BuiltinType::Short:
680 case BuiltinType::UShort:
681 return true;
682 default:
683 return false;
684 }
685}
686
687const char *BuiltinType::getName() const {
688 switch (getKind()) {
689 default: assert(0 && "Unknown builtin type!");
690 case Void: return "void";
691 case Bool: return "_Bool";
692 case Char_S: return "char";
693 case Char_U: return "char";
694 case SChar: return "signed char";
695 case Short: return "short";
696 case Int: return "int";
697 case Long: return "long";
698 case LongLong: return "long long";
699 case UChar: return "unsigned char";
700 case UShort: return "unsigned short";
701 case UInt: return "unsigned int";
702 case ULong: return "unsigned long";
703 case ULongLong: return "unsigned long long";
704 case Float: return "float";
705 case Double: return "double";
706 case LongDouble: return "long double";
707 }
708}
709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000711 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 unsigned NumArgs, bool isVariadic) {
713 ID.AddPointer(Result.getAsOpaquePtr());
714 for (unsigned i = 0; i != NumArgs; ++i)
715 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
716 ID.AddInteger(isVariadic);
717}
718
719void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000720 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000721}
722
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000723void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000724 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000725 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000726 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000727 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000728 for (unsigned i = 0; i != NumProtocols; i++)
729 ID.AddPointer(protocols[i]);
730}
731
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000732void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000733 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000734}
735
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000736void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
737 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000738 unsigned NumProtocols) {
739 for (unsigned i = 0; i != NumProtocols; i++)
740 ID.AddPointer(protocols[i]);
741}
742
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000743void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000744 Profile(ID, &Protocols[0], getNumProtocols());
745}
746
Chris Lattnera2c77672007-07-16 22:05:22 +0000747/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
748/// potentially looking through *all* consequtive typedefs. This returns the
749/// sum of the type qualifiers, so if you have:
750/// typedef const int A;
751/// typedef volatile A B;
752/// looking through the typedefs for B will give you "const volatile A".
753///
754QualType TypedefType::LookThroughTypedefs() const {
755 // Usually, there is only a single level of typedefs, be fast in that case.
756 QualType FirstType = getDecl()->getUnderlyingType();
757 if (!isa<TypedefType>(FirstType))
758 return FirstType;
759
760 // Otherwise, do the fully general loop.
761 unsigned TypeQuals = 0;
762 const TypedefType *TDT = this;
763 while (1) {
764 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000765
766
767 /// FIXME:
768 /// FIXME: This is incorrect for ASQuals!
769 /// FIXME:
770 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000771
772 TDT = dyn_cast<TypedefType>(CurType);
773 if (TDT == 0)
774 return QualType(CurType.getTypePtr(), TypeQuals);
775 }
776}
Reid Spencer5f016e22007-07-11 17:01:13 +0000777
Chris Lattner2daa5df2008-04-06 22:04:54 +0000778bool RecordType::classof(const TagType *TT) {
779 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000780}
781
Chris Lattner2daa5df2008-04-06 22:04:54 +0000782bool EnumType::classof(const TagType *TT) {
783 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000784}
785
Reid Spencer5f016e22007-07-11 17:01:13 +0000786
787//===----------------------------------------------------------------------===//
788// Type Printing
789//===----------------------------------------------------------------------===//
790
791void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000792 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 getAsStringInternal(R);
794 if (msg)
795 fprintf(stderr, "%s: %s\n", msg, R.c_str());
796 else
797 fprintf(stderr, "%s\n", R.c_str());
798}
799
800static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
801 // Note: funkiness to ensure we get a space only between quals.
802 bool NonePrinted = true;
803 if (TypeQuals & QualType::Const)
804 S += "const", NonePrinted = false;
805 if (TypeQuals & QualType::Volatile)
806 S += (NonePrinted+" volatile"), NonePrinted = false;
807 if (TypeQuals & QualType::Restrict)
808 S += (NonePrinted+" restrict"), NonePrinted = false;
809}
810
811void QualType::getAsStringInternal(std::string &S) const {
812 if (isNull()) {
813 S += "NULL TYPE\n";
814 return;
815 }
816
817 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000818 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000820 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 if (!S.empty())
822 S = TQS + ' ' + S;
823 else
824 S = TQS;
825 }
826
827 getTypePtr()->getAsStringInternal(S);
828}
829
830void BuiltinType::getAsStringInternal(std::string &S) const {
831 if (S.empty()) {
832 S = getName();
833 } else {
834 // Prefix the basic type, e.g. 'int X'.
835 S = ' ' + S;
836 S = getName() + S;
837 }
838}
839
840void ComplexType::getAsStringInternal(std::string &S) const {
841 ElementType->getAsStringInternal(S);
842 S = "_Complex " + S;
843}
844
Christopher Lambebb97e92008-02-04 02:31:56 +0000845void ASQualType::getAsStringInternal(std::string &S) const {
846 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
847 BaseType->getAsStringInternal(S);
848}
849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850void PointerType::getAsStringInternal(std::string &S) const {
851 S = '*' + S;
852
853 // Handle things like 'int (*A)[4];' correctly.
854 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000855 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 S = '(' + S + ')';
857
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000858 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859}
860
861void ReferenceType::getAsStringInternal(std::string &S) const {
862 S = '&' + S;
863
864 // Handle things like 'int (&A)[4];' correctly.
865 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000866 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 S = '(' + S + ')';
868
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000869 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000870}
871
Steve Narofffb22d962007-08-30 01:06:46 +0000872void ConstantArrayType::getAsStringInternal(std::string &S) const {
873 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000874 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000875 S += ']';
876
877 getElementType().getAsStringInternal(S);
878}
879
Eli Friedmanc5773c42008-02-15 18:16:39 +0000880void IncompleteArrayType::getAsStringInternal(std::string &S) const {
881 S += "[]";
882
883 getElementType().getAsStringInternal(S);
884}
885
Steve Narofffb22d962007-08-30 01:06:46 +0000886void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 S += '[';
888
Steve Naroffc9406122007-08-30 18:10:14 +0000889 if (getIndexTypeQualifier()) {
890 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 S += ' ';
892 }
893
Steve Naroffc9406122007-08-30 18:10:14 +0000894 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000896 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 S += '*';
898
Steve Narofffb22d962007-08-30 01:06:46 +0000899 if (getSizeExpr()) {
900 std::ostringstream s;
901 getSizeExpr()->printPretty(s);
902 S += s.str();
903 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 S += ']';
905
Steve Narofffb22d962007-08-30 01:06:46 +0000906 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000907}
908
909void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000910 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000911 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000913 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 ElementType.getAsStringInternal(S);
915}
916
Nate Begeman213541a2008-04-18 23:10:10 +0000917void ExtVectorType::getAsStringInternal(std::string &S) const {
918 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000919 S += llvm::utostr_32(NumElements);
920 S += ")))";
921 ElementType.getAsStringInternal(S);
922}
923
Steve Naroffd1861fd2007-07-31 12:34:36 +0000924void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000925 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
926 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000927 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000928 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000929 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000930}
931
Steve Naroff363bcff2007-08-01 23:45:51 +0000932void TypeOfType::getAsStringInternal(std::string &InnerString) const {
933 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
934 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000935 std::string Tmp;
936 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000937 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000938}
939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
941 // If needed for precedence reasons, wrap the inner part in grouping parens.
942 if (!S.empty())
943 S = "(" + S + ")";
944
945 S += "()";
946 getResultType().getAsStringInternal(S);
947}
948
949void FunctionTypeProto::getAsStringInternal(std::string &S) const {
950 // If needed for precedence reasons, wrap the inner part in grouping parens.
951 if (!S.empty())
952 S = "(" + S + ")";
953
954 S += "(";
955 std::string Tmp;
956 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
957 if (i) S += ", ";
958 getArgType(i).getAsStringInternal(Tmp);
959 S += Tmp;
960 Tmp.clear();
961 }
962
963 if (isVariadic()) {
964 if (getNumArgs())
965 S += ", ";
966 S += "...";
967 } else if (getNumArgs() == 0) {
968 // Do not emit int() if we have a proto, emit 'int(void)'.
969 S += "void";
970 }
971
972 S += ")";
973 getResultType().getAsStringInternal(S);
974}
975
976
977void TypedefType::getAsStringInternal(std::string &InnerString) const {
978 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
979 InnerString = ' ' + InnerString;
980 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
981}
982
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000984 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
985 InnerString = ' ' + InnerString;
986 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
987}
988
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000990 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000991 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
992 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000993 std::string ObjCQIString = getDecl()->getName();
994 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000995 int num = getNumProtocols();
996 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000997 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000998 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000999 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001000 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001001 ObjCQIString += '>';
1002 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001003}
1004
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001005void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001006 std::string &InnerString) const {
1007 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1008 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009 std::string ObjCQIString = "id";
1010 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001011 int num = getNumProtocols();
1012 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001013 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001014 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001015 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001016 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001017 ObjCQIString += '>';
1018 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001019}
1020
Reid Spencer5f016e22007-07-11 17:01:13 +00001021void TagType::getAsStringInternal(std::string &InnerString) const {
1022 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1023 InnerString = ' ' + InnerString;
1024
1025 const char *Kind = getDecl()->getKindName();
1026 const char *ID;
1027 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1028 ID = II->getName();
1029 else
1030 ID = "<anonymous>";
1031
1032 InnerString = std::string(Kind) + " " + ID + InnerString;
1033}