blob: fd57fb236f55be28b868a9b8aaa4dd26c8267827 [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);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000066 return !TT->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +000067 }
68 default:
69 return false;
70 }
71}
72
Chris Lattner99dc9142008-04-13 18:59:07 +000073bool Type::isClassType() const {
74 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000075 if (RT->getDecl()->isClass())
Chris Lattner99dc9142008-04-13 18:59:07 +000076 return true;
77 return false;
78}
Chris Lattnerc8629632007-07-31 19:29:30 +000079bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000080 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000081 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +000082 return true;
83 return false;
84}
85bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000086 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +000087 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +000088 return true;
89 return false;
90}
Chris Lattnerc8629632007-07-31 19:29:30 +000091
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000092bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +000093 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
94 return CT->getElementType()->isFloatingType();
95 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000096}
97
Steve Naroff4cdec1c2008-01-15 01:41:59 +000098bool Type::isComplexIntegerType() const {
99 // Check for GCC complex integer extension.
100 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
101 return CT->getElementType()->isIntegerType();
102 return false;
103}
104
105const ComplexType *Type::getAsComplexIntegerType() const {
106 // Are we directly a complex type?
107 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
108 if (CTy->getElementType()->isIntegerType())
109 return CTy;
110 }
111 // If the canonical form of this type isn't the right kind, reject it.
112 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
113 if (!CTy || !CTy->getElementType()->isIntegerType())
114 return 0;
115
116 // If this is a typedef for a complex type, strip the typedef off without
117 // losing all typedef information.
118 return getDesugaredType()->getAsComplexIntegerType();
119}
120
Chris Lattnerdea61462007-10-29 03:41:11 +0000121/// getDesugaredType - Return the specified type with any "sugar" removed from
122/// type type. This takes off typedefs, typeof's etc. If the outer level of
123/// the type is already concrete, it returns it unmodified. This is similar
124/// to getting the canonical type, but it doesn't remove *all* typedefs. For
125/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
126/// concrete.
127const Type *Type::getDesugaredType() const {
128 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
129 return TDT->LookThroughTypedefs().getTypePtr();
130 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
131 return TOE->getUnderlyingExpr()->getType().getTypePtr();
132 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
133 return TOT->getUnderlyingType().getTypePtr();
134 return this;
135}
136
137
Steve Naroff77878cc2007-08-27 04:08:11 +0000138const BuiltinType *Type::getAsBuiltinType() const {
139 // If this is directly a builtin type, return it.
140 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
141 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000142
143 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000144 if (!isa<BuiltinType>(CanonicalType)) {
145 // Look through type qualifiers
146 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
147 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000148 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000149 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000150
Steve Naroff77878cc2007-08-27 04:08:11 +0000151 // If this is a typedef for a builtin type, strip the typedef off without
152 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000153 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000154}
155
Chris Lattnerc8629632007-07-31 19:29:30 +0000156const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000157 // If this is directly a function type, return it.
158 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
159 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000160
Chris Lattnerdea61462007-10-29 03:41:11 +0000161 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000162 if (!isa<FunctionType>(CanonicalType)) {
163 // Look through type qualifiers
164 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
165 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000166 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000167 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000168
Steve Naroff7064f5c2007-07-26 18:32:01 +0000169 // If this is a typedef for a function type, strip the typedef off without
170 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000171 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000172}
173
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000174const PointerLikeType *Type::getAsPointerLikeType() const {
175 // If this is directly a pointer-like type, return it.
176 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
177 return PTy;
178
179 // If the canonical form of this type isn't the right kind, reject it.
180 if (!isa<PointerLikeType>(CanonicalType)) {
181 // Look through type qualifiers
182 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
183 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
184 return 0;
185 }
186
187 // If this is a typedef for a pointer type, strip the typedef off without
188 // losing all typedef information.
189 return getDesugaredType()->getAsPointerLikeType();
190}
191
Chris Lattnerbefee482007-07-31 16:53:04 +0000192const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000193 // If this is directly a pointer type, return it.
194 if (const PointerType *PTy = dyn_cast<PointerType>(this))
195 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000196
Chris Lattnerdea61462007-10-29 03:41:11 +0000197 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000198 if (!isa<PointerType>(CanonicalType)) {
199 // Look through type qualifiers
200 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
201 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000202 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000203 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000204
Chris Lattnera2c77672007-07-16 22:05:22 +0000205 // If this is a typedef for a pointer type, strip the typedef off without
206 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000207 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000208}
209
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000210const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000211 // If this is directly a reference type, return it.
212 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
213 return RTy;
214
Chris Lattnerdea61462007-10-29 03:41:11 +0000215 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000216 if (!isa<ReferenceType>(CanonicalType)) {
217 // Look through type qualifiers
218 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
219 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000220 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000221 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000222
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000223 // If this is a typedef for a reference type, strip the typedef off without
224 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000225 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000226}
227
Chris Lattnerc8629632007-07-31 19:29:30 +0000228const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000229 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000230 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
231 return ATy;
232
Chris Lattnerdea61462007-10-29 03:41:11 +0000233 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000234 if (!isa<ArrayType>(CanonicalType)) {
235 // Look through type qualifiers
236 if (isa<ArrayType>(CanonicalType.getUnqualifiedType()))
237 return CanonicalType.getUnqualifiedType()->getAsArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000238 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000239 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000240
Steve Naroff700204c2007-07-24 21:46:40 +0000241 // If this is a typedef for an array type, strip the typedef off without
242 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000243 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Steve Naroffd7444aa2007-08-31 17:20:07 +0000246const ConstantArrayType *Type::getAsConstantArrayType() const {
247 // If this is directly a constant array type, return it.
248 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
249 return ATy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000250
Chris Lattnerdea61462007-10-29 03:41:11 +0000251 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000252 if (!isa<ConstantArrayType>(CanonicalType)) {
253 // Look through type qualifiers
254 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()))
255 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000256 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000257 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000258
259 // If this is a typedef for a constant array type, strip the typedef off
260 // without losing all typedef information.
261 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000262}
263
264const VariableArrayType *Type::getAsVariableArrayType() const {
265 // If this is directly a variable array type, return it.
266 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
267 return ATy;
268
Chris Lattnerdea61462007-10-29 03:41:11 +0000269 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000270 if (!isa<VariableArrayType>(CanonicalType)) {
271 // Look through type qualifiers
272 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType()))
273 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000274 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000275 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000276
277 // If this is a typedef for a variable array type, strip the typedef off
278 // without losing all typedef information.
279 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000280}
281
Eli Friedmand3f2f792008-02-17 00:59:11 +0000282/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
283/// array types and types that contain variable array types in their
284/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000285bool Type::isVariablyModifiedType() const {
Eli Friedmand3f2f792008-02-17 00:59:11 +0000286 // A VLA is a veriably modified type
287 if (getAsVariableArrayType())
288 return true;
289
290 // An array can contain a variably modified type
291 if (const ArrayType* AT = getAsArrayType())
292 return AT->getElementType()->isVariablyModifiedType();
293
294 // A pointer can point to a variably modified type
295 if (const PointerType* PT = getAsPointerType())
296 return PT->getPointeeType()->isVariablyModifiedType();
297
298 // A function can return a variably modified type
299 // This one isn't completely obvious, but it follows from the
300 // definition in C99 6.7.5p3. Because of this rule, it's
301 // illegal to declare a function returning a variably modified type.
302 if (const FunctionType* FT = getAsFunctionType())
303 return FT->getResultType()->isVariablyModifiedType();
304
Steve Naroffd7444aa2007-08-31 17:20:07 +0000305 return false;
306}
307
Steve Naroff5c06a692008-01-21 22:59:18 +0000308bool Type::isIncompleteArrayType() const {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000309 return isa<IncompleteArrayType>(CanonicalType);
Steve Naroff5c06a692008-01-21 22:59:18 +0000310}
311
Eli Friedmanc5773c42008-02-15 18:16:39 +0000312const IncompleteArrayType *Type::getAsIncompleteArrayType() const {
313 // If this is directly a variable array type, return it.
314 if (const IncompleteArrayType *ATy = dyn_cast<IncompleteArrayType>(this))
315 return ATy;
316
317 // If the canonical form of this type isn't the right kind, reject it.
318 if (!isa<IncompleteArrayType>(CanonicalType)) {
319 // Look through type qualifiers
320 if (isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()))
321 return CanonicalType.getUnqualifiedType()->getAsIncompleteArrayType();
322 return 0;
Steve Naroff5c06a692008-01-21 22:59:18 +0000323 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000324
325 // If this is a typedef for a variable array type, strip the typedef off
326 // without losing all typedef information.
327 return getDesugaredType()->getAsIncompleteArrayType();
Steve Naroff5c06a692008-01-21 22:59:18 +0000328}
329
Chris Lattnerc8629632007-07-31 19:29:30 +0000330const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000331 // If this is directly a reference type, return it.
332 if (const RecordType *RTy = dyn_cast<RecordType>(this))
333 return RTy;
334
Chris Lattnerdea61462007-10-29 03:41:11 +0000335 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000336 if (!isa<RecordType>(CanonicalType)) {
337 // Look through type qualifiers
338 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
339 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000340 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000341 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000342
343 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000344 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000345 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000346}
347
Chris Lattnerc8629632007-07-31 19:29:30 +0000348const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000349 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000350 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000351 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000352 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000353 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000354
355 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000356 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000357 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000358 return 0;
359
360 // If this is a typedef for a structure type, strip the typedef off without
361 // losing all typedef information.
362 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000364 // Look through type qualifiers
365 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
366 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000367 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000368}
369
Chris Lattnerc8629632007-07-31 19:29:30 +0000370const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000371 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000372 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000373 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000374 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000375 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000376
Chris Lattnerdea61462007-10-29 03:41:11 +0000377 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000378 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000379 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000380 return 0;
381
382 // If this is a typedef for a union type, strip the typedef off without
383 // losing all typedef information.
384 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000386
387 // Look through type qualifiers
388 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
389 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000390 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000391}
392
Eli Friedmanad74a752008-06-28 06:23:08 +0000393const EnumType *Type::getAsEnumType() const {
394 // Check the canonicalized unqualified type directly; the more complex
395 // version is unnecessary because there isn't any typedef information
396 // to preserve.
397 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
398}
399
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000400const ComplexType *Type::getAsComplexType() const {
401 // Are we directly a complex type?
402 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
403 return CTy;
404
Chris Lattnerdea61462007-10-29 03:41:11 +0000405 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000406 if (!isa<ComplexType>(CanonicalType)) {
407 // Look through type qualifiers
408 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
409 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000410 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000411 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000412
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000413 // If this is a typedef for a complex type, strip the typedef off without
414 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000415 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000416}
417
Chris Lattnerc8629632007-07-31 19:29:30 +0000418const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000419 // Are we directly a vector type?
420 if (const VectorType *VTy = dyn_cast<VectorType>(this))
421 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000422
Chris Lattnerdea61462007-10-29 03:41:11 +0000423 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000424 if (!isa<VectorType>(CanonicalType)) {
425 // Look through type qualifiers
426 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
427 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000428 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000429 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000430
Chris Lattnera2c77672007-07-16 22:05:22 +0000431 // If this is a typedef for a vector type, strip the typedef off without
432 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000433 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000434}
435
Nate Begeman213541a2008-04-18 23:10:10 +0000436const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000437 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000438 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000439 return VTy;
440
Chris Lattnerdea61462007-10-29 03:41:11 +0000441 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000442 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000443 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000444 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
445 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000446 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000447 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000448
Nate Begeman213541a2008-04-18 23:10:10 +0000449 // If this is a typedef for an extended vector type, strip the typedef off
450 // without losing all typedef information.
451 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000452}
453
Chris Lattner368eefa2008-04-07 00:27:04 +0000454const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000455 // There is no sugar for ObjCInterfaceType's, just return the canonical
456 // type pointer if it is the right class.
457 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000458}
459
460const ObjCQualifiedInterfaceType *
461Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000462 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
463 // type pointer if it is the right class.
464 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
465}
466
467const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
468 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
469 // type pointer if it is the right class.
470 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000471}
472
473
Reid Spencer5f016e22007-07-11 17:01:13 +0000474bool Type::isIntegerType() const {
475 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
476 return BT->getKind() >= BuiltinType::Bool &&
477 BT->getKind() <= BuiltinType::LongLong;
478 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000479 if (TT->getDecl()->isEnum())
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000481 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
482 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000483 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
484 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 return false;
486}
487
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000488bool Type::isIntegralType() const {
489 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
490 return BT->getKind() >= BuiltinType::Bool &&
491 BT->getKind() <= BuiltinType::LongLong;
492 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000493 if (TT->getDecl()->isEnum())
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000494 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000495 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
496 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000497 return false;
498}
499
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000500bool Type::isEnumeralType() const {
501 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000502 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000503 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
504 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000505 return false;
506}
507
508bool Type::isBooleanType() const {
509 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
510 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000511 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
512 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000513 return false;
514}
515
516bool Type::isCharType() const {
517 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
518 return BT->getKind() == BuiltinType::Char_U ||
519 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000520 BT->getKind() == BuiltinType::Char_S ||
521 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000522 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
523 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000524 return false;
525}
526
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000527/// isSignedIntegerType - Return true if this is an integer type that is
528/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
529/// an enum decl which has a signed representation, or a vector of signed
530/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000531bool Type::isSignedIntegerType() const {
532 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
533 return BT->getKind() >= BuiltinType::Char_S &&
534 BT->getKind() <= BuiltinType::LongLong;
535 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000536
Chris Lattner37c1b782008-04-06 22:29:16 +0000537 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
538 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000539
Steve Naroffc63b96a2007-07-12 21:46:55 +0000540 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
541 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000542 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
543 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 return false;
545}
546
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000547/// isUnsignedIntegerType - Return true if this is an integer type that is
548/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
549/// decl which has an unsigned representation, or a vector of unsigned integer
550/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000551bool Type::isUnsignedIntegerType() const {
552 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
553 return BT->getKind() >= BuiltinType::Bool &&
554 BT->getKind() <= BuiltinType::ULongLong;
555 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000556
Chris Lattner37c1b782008-04-06 22:29:16 +0000557 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
558 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000559
Steve Naroffc63b96a2007-07-12 21:46:55 +0000560 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
561 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000562 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
563 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 return false;
565}
566
567bool Type::isFloatingType() const {
568 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
569 return BT->getKind() >= BuiltinType::Float &&
570 BT->getKind() <= BuiltinType::LongDouble;
571 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000572 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000573 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
574 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000575 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
576 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 return false;
578}
579
580bool Type::isRealFloatingType() const {
581 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
582 return BT->getKind() >= BuiltinType::Float &&
583 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000584 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
585 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000586 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
587 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 return false;
589}
590
591bool Type::isRealType() const {
592 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
593 return BT->getKind() >= BuiltinType::Bool &&
594 BT->getKind() <= BuiltinType::LongDouble;
595 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000596 return TT->getDecl()->isEnum();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000597 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
598 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000599 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
600 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 return false;
602}
603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604bool Type::isArithmeticType() const {
605 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
606 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000607 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
608 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
609 // If a body isn't seen by the time we get here, return false.
610 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000611 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
612 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
614}
615
616bool Type::isScalarType() const {
617 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
618 return BT->getKind() != BuiltinType::Void;
619 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000620 if (TT->getDecl()->isEnum())
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 return true;
622 return false;
623 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000624 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
625 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000626 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000628}
629
630bool Type::isAggregateType() const {
631 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000632 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 return true;
634 return false;
635 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000636 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
637 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000638 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000639}
640
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000641/// isConstantSizeType - Return true if this is not a variable sized type,
642/// according to the rules of C99 6.7.5p3. It is not legal to call this on
643/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000644bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000645 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000646 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000647 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000648 // The VAT must have a size, as it is known to be complete.
649 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000650}
651
652/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
653/// - a type that can describe objects, but which lacks information needed to
654/// determine its size.
655bool Type::isIncompleteType() const {
656 switch (CanonicalType->getTypeClass()) {
657 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000658 case ASQual:
659 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 case Builtin:
661 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
662 // be completed.
663 return isVoidType();
664 case Tagged:
665 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
666 // forward declaration, but not a full definition (C99 6.2.5p22).
667 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000668 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000670 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 }
672}
673
674bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000675 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
676 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
678 if (!BT) return false;
679 switch (BT->getKind()) {
680 case BuiltinType::Bool:
681 case BuiltinType::Char_S:
682 case BuiltinType::Char_U:
683 case BuiltinType::SChar:
684 case BuiltinType::UChar:
685 case BuiltinType::Short:
686 case BuiltinType::UShort:
687 return true;
688 default:
689 return false;
690 }
691}
692
693const char *BuiltinType::getName() const {
694 switch (getKind()) {
695 default: assert(0 && "Unknown builtin type!");
696 case Void: return "void";
697 case Bool: return "_Bool";
698 case Char_S: return "char";
699 case Char_U: return "char";
700 case SChar: return "signed char";
701 case Short: return "short";
702 case Int: return "int";
703 case Long: return "long";
704 case LongLong: return "long long";
705 case UChar: return "unsigned char";
706 case UShort: return "unsigned short";
707 case UInt: return "unsigned int";
708 case ULong: return "unsigned long";
709 case ULongLong: return "unsigned long long";
710 case Float: return "float";
711 case Double: return "double";
712 case LongDouble: return "long double";
713 }
714}
715
Reid Spencer5f016e22007-07-11 17:01:13 +0000716void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000717 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 unsigned NumArgs, bool isVariadic) {
719 ID.AddPointer(Result.getAsOpaquePtr());
720 for (unsigned i = 0; i != NumArgs; ++i)
721 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
722 ID.AddInteger(isVariadic);
723}
724
725void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000726 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000727}
728
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000729void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000730 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000732 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000733 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000734 for (unsigned i = 0; i != NumProtocols; i++)
735 ID.AddPointer(protocols[i]);
736}
737
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000738void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000739 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000740}
741
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000742void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
743 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000744 unsigned NumProtocols) {
745 for (unsigned i = 0; i != NumProtocols; i++)
746 ID.AddPointer(protocols[i]);
747}
748
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000749void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000750 Profile(ID, &Protocols[0], getNumProtocols());
751}
752
Chris Lattnera2c77672007-07-16 22:05:22 +0000753/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
754/// potentially looking through *all* consequtive typedefs. This returns the
755/// sum of the type qualifiers, so if you have:
756/// typedef const int A;
757/// typedef volatile A B;
758/// looking through the typedefs for B will give you "const volatile A".
759///
760QualType TypedefType::LookThroughTypedefs() const {
761 // Usually, there is only a single level of typedefs, be fast in that case.
762 QualType FirstType = getDecl()->getUnderlyingType();
763 if (!isa<TypedefType>(FirstType))
764 return FirstType;
765
766 // Otherwise, do the fully general loop.
767 unsigned TypeQuals = 0;
768 const TypedefType *TDT = this;
769 while (1) {
770 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000771
772
773 /// FIXME:
774 /// FIXME: This is incorrect for ASQuals!
775 /// FIXME:
776 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000777
778 TDT = dyn_cast<TypedefType>(CurType);
779 if (TDT == 0)
780 return QualType(CurType.getTypePtr(), TypeQuals);
781 }
782}
Reid Spencer5f016e22007-07-11 17:01:13 +0000783
Chris Lattner2daa5df2008-04-06 22:04:54 +0000784bool RecordType::classof(const TagType *TT) {
785 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000786}
787
Chris Lattner2daa5df2008-04-06 22:04:54 +0000788bool EnumType::classof(const TagType *TT) {
789 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000790}
791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792
793//===----------------------------------------------------------------------===//
794// Type Printing
795//===----------------------------------------------------------------------===//
796
797void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000798 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 getAsStringInternal(R);
800 if (msg)
801 fprintf(stderr, "%s: %s\n", msg, R.c_str());
802 else
803 fprintf(stderr, "%s\n", R.c_str());
804}
805
806static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
807 // Note: funkiness to ensure we get a space only between quals.
808 bool NonePrinted = true;
809 if (TypeQuals & QualType::Const)
810 S += "const", NonePrinted = false;
811 if (TypeQuals & QualType::Volatile)
812 S += (NonePrinted+" volatile"), NonePrinted = false;
813 if (TypeQuals & QualType::Restrict)
814 S += (NonePrinted+" restrict"), NonePrinted = false;
815}
816
817void QualType::getAsStringInternal(std::string &S) const {
818 if (isNull()) {
819 S += "NULL TYPE\n";
820 return;
821 }
822
823 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000824 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000826 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 if (!S.empty())
828 S = TQS + ' ' + S;
829 else
830 S = TQS;
831 }
832
833 getTypePtr()->getAsStringInternal(S);
834}
835
836void BuiltinType::getAsStringInternal(std::string &S) const {
837 if (S.empty()) {
838 S = getName();
839 } else {
840 // Prefix the basic type, e.g. 'int X'.
841 S = ' ' + S;
842 S = getName() + S;
843 }
844}
845
846void ComplexType::getAsStringInternal(std::string &S) const {
847 ElementType->getAsStringInternal(S);
848 S = "_Complex " + S;
849}
850
Christopher Lambebb97e92008-02-04 02:31:56 +0000851void ASQualType::getAsStringInternal(std::string &S) const {
852 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
853 BaseType->getAsStringInternal(S);
854}
855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856void PointerType::getAsStringInternal(std::string &S) const {
857 S = '*' + S;
858
859 // Handle things like 'int (*A)[4];' correctly.
860 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000861 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 S = '(' + S + ')';
863
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000864 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000865}
866
867void ReferenceType::getAsStringInternal(std::string &S) const {
868 S = '&' + S;
869
870 // Handle things like 'int (&A)[4];' correctly.
871 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000872 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 S = '(' + S + ')';
874
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000875 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000876}
877
Steve Narofffb22d962007-08-30 01:06:46 +0000878void ConstantArrayType::getAsStringInternal(std::string &S) const {
879 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000880 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000881 S += ']';
882
883 getElementType().getAsStringInternal(S);
884}
885
Eli Friedmanc5773c42008-02-15 18:16:39 +0000886void IncompleteArrayType::getAsStringInternal(std::string &S) const {
887 S += "[]";
888
889 getElementType().getAsStringInternal(S);
890}
891
Steve Narofffb22d962007-08-30 01:06:46 +0000892void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 S += '[';
894
Steve Naroffc9406122007-08-30 18:10:14 +0000895 if (getIndexTypeQualifier()) {
896 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 S += ' ';
898 }
899
Steve Naroffc9406122007-08-30 18:10:14 +0000900 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000902 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 S += '*';
904
Steve Narofffb22d962007-08-30 01:06:46 +0000905 if (getSizeExpr()) {
906 std::ostringstream s;
907 getSizeExpr()->printPretty(s);
908 S += s.str();
909 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 S += ']';
911
Steve Narofffb22d962007-08-30 01:06:46 +0000912 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000913}
914
915void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000916 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000917 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000919 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 ElementType.getAsStringInternal(S);
921}
922
Nate Begeman213541a2008-04-18 23:10:10 +0000923void ExtVectorType::getAsStringInternal(std::string &S) const {
924 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000925 S += llvm::utostr_32(NumElements);
926 S += ")))";
927 ElementType.getAsStringInternal(S);
928}
929
Steve Naroffd1861fd2007-07-31 12:34:36 +0000930void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000931 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
932 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000933 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000934 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000935 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000936}
937
Steve Naroff363bcff2007-08-01 23:45:51 +0000938void TypeOfType::getAsStringInternal(std::string &InnerString) const {
939 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
940 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000941 std::string Tmp;
942 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000943 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000944}
945
Reid Spencer5f016e22007-07-11 17:01:13 +0000946void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
947 // If needed for precedence reasons, wrap the inner part in grouping parens.
948 if (!S.empty())
949 S = "(" + S + ")";
950
951 S += "()";
952 getResultType().getAsStringInternal(S);
953}
954
955void FunctionTypeProto::getAsStringInternal(std::string &S) const {
956 // If needed for precedence reasons, wrap the inner part in grouping parens.
957 if (!S.empty())
958 S = "(" + S + ")";
959
960 S += "(";
961 std::string Tmp;
962 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
963 if (i) S += ", ";
964 getArgType(i).getAsStringInternal(Tmp);
965 S += Tmp;
966 Tmp.clear();
967 }
968
969 if (isVariadic()) {
970 if (getNumArgs())
971 S += ", ";
972 S += "...";
973 } else if (getNumArgs() == 0) {
974 // Do not emit int() if we have a proto, emit 'int(void)'.
975 S += "void";
976 }
977
978 S += ")";
979 getResultType().getAsStringInternal(S);
980}
981
982
983void TypedefType::getAsStringInternal(std::string &InnerString) const {
984 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 ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000990 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
991 InnerString = ' ' + InnerString;
992 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
993}
994
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000995void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000996 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000997 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
998 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000999 std::string ObjCQIString = getDecl()->getName();
1000 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001001 bool isFirst = true;
1002 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1003 if (isFirst)
1004 isFirst = false;
1005 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006 ObjCQIString += ',';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001007 ObjCQIString += (*I)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001008 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009 ObjCQIString += '>';
1010 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001011}
1012
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001013void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001014 std::string &InnerString) const {
1015 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1016 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001017 std::string ObjCQIString = "id";
1018 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001019 int num = getNumProtocols();
1020 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001021 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001022 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001023 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001024 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001025 ObjCQIString += '>';
1026 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001027}
1028
Reid Spencer5f016e22007-07-11 17:01:13 +00001029void TagType::getAsStringInternal(std::string &InnerString) const {
1030 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1031 InnerString = ' ' + InnerString;
1032
1033 const char *Kind = getDecl()->getKindName();
1034 const char *ID;
1035 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1036 ID = II->getName();
1037 else
1038 ID = "<anonymous>";
1039
1040 InnerString = std::string(Kind) + " " + ID + InnerString;
1041}