blob: d5bef07ca96d39716a443a11a501f7e26b4dcd2d [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 Lattnerb77792e2008-07-26 22:17:49 +0000174const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
175 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
176}
177
178
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000179const PointerLikeType *Type::getAsPointerLikeType() const {
180 // If this is directly a pointer-like type, return it.
181 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
182 return PTy;
183
184 // If the canonical form of this type isn't the right kind, reject it.
185 if (!isa<PointerLikeType>(CanonicalType)) {
186 // Look through type qualifiers
187 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
188 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
189 return 0;
190 }
191
192 // If this is a typedef for a pointer type, strip the typedef off without
193 // losing all typedef information.
194 return getDesugaredType()->getAsPointerLikeType();
195}
196
Chris Lattnerbefee482007-07-31 16:53:04 +0000197const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000198 // If this is directly a pointer type, return it.
199 if (const PointerType *PTy = dyn_cast<PointerType>(this))
200 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000201
Chris Lattnerdea61462007-10-29 03:41:11 +0000202 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000203 if (!isa<PointerType>(CanonicalType)) {
204 // Look through type qualifiers
205 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
206 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000207 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000208 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000209
Chris Lattnera2c77672007-07-16 22:05:22 +0000210 // If this is a typedef for a pointer type, strip the typedef off without
211 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000212 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000213}
214
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000215const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000216 // If this is directly a reference type, return it.
217 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
218 return RTy;
219
Chris Lattnerdea61462007-10-29 03:41:11 +0000220 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000221 if (!isa<ReferenceType>(CanonicalType)) {
222 // Look through type qualifiers
223 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
224 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000225 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000226 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000227
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000228 // If this is a typedef for a reference type, strip the typedef off without
229 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000230 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000231}
232
Chris Lattnerc8629632007-07-31 19:29:30 +0000233const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000234 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000235 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
236 return ATy;
237
Chris Lattnerdea61462007-10-29 03:41:11 +0000238 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000239 if (!isa<ArrayType>(CanonicalType)) {
240 // Look through type qualifiers
241 if (isa<ArrayType>(CanonicalType.getUnqualifiedType()))
242 return CanonicalType.getUnqualifiedType()->getAsArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000243 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000244 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000245
Steve Naroff700204c2007-07-24 21:46:40 +0000246 // If this is a typedef for an array type, strip the typedef off without
247 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000248 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000249}
250
Steve Naroffd7444aa2007-08-31 17:20:07 +0000251const ConstantArrayType *Type::getAsConstantArrayType() const {
252 // If this is directly a constant array type, return it.
253 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
254 return ATy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000255
Chris Lattnerdea61462007-10-29 03:41:11 +0000256 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000257 if (!isa<ConstantArrayType>(CanonicalType)) {
258 // Look through type qualifiers
259 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()))
260 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000261 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000262 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000263
264 // If this is a typedef for a constant array type, strip the typedef off
265 // without losing all typedef information.
266 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000267}
268
269const VariableArrayType *Type::getAsVariableArrayType() const {
270 // If this is directly a variable array type, return it.
271 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
272 return ATy;
273
Chris Lattnerdea61462007-10-29 03:41:11 +0000274 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000275 if (!isa<VariableArrayType>(CanonicalType)) {
276 // Look through type qualifiers
277 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType()))
278 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000279 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000280 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000281
282 // If this is a typedef for a variable array type, strip the typedef off
283 // without losing all typedef information.
284 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000285}
286
Eli Friedmand3f2f792008-02-17 00:59:11 +0000287/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
288/// array types and types that contain variable array types in their
289/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000290bool Type::isVariablyModifiedType() const {
Eli Friedmand3f2f792008-02-17 00:59:11 +0000291 // A VLA is a veriably modified type
292 if (getAsVariableArrayType())
293 return true;
294
295 // An array can contain a variably modified type
296 if (const ArrayType* AT = getAsArrayType())
297 return AT->getElementType()->isVariablyModifiedType();
298
299 // A pointer can point to a variably modified type
300 if (const PointerType* PT = getAsPointerType())
301 return PT->getPointeeType()->isVariablyModifiedType();
302
303 // A function can return a variably modified type
304 // This one isn't completely obvious, but it follows from the
305 // definition in C99 6.7.5p3. Because of this rule, it's
306 // illegal to declare a function returning a variably modified type.
307 if (const FunctionType* FT = getAsFunctionType())
308 return FT->getResultType()->isVariablyModifiedType();
309
Steve Naroffd7444aa2007-08-31 17:20:07 +0000310 return false;
311}
312
Steve Naroff5c06a692008-01-21 22:59:18 +0000313bool Type::isIncompleteArrayType() const {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000314 return isa<IncompleteArrayType>(CanonicalType);
Steve Naroff5c06a692008-01-21 22:59:18 +0000315}
316
Eli Friedmanc5773c42008-02-15 18:16:39 +0000317const IncompleteArrayType *Type::getAsIncompleteArrayType() const {
318 // If this is directly a variable array type, return it.
319 if (const IncompleteArrayType *ATy = dyn_cast<IncompleteArrayType>(this))
320 return ATy;
321
322 // If the canonical form of this type isn't the right kind, reject it.
323 if (!isa<IncompleteArrayType>(CanonicalType)) {
324 // Look through type qualifiers
325 if (isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()))
326 return CanonicalType.getUnqualifiedType()->getAsIncompleteArrayType();
327 return 0;
Steve Naroff5c06a692008-01-21 22:59:18 +0000328 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000329
330 // If this is a typedef for a variable array type, strip the typedef off
331 // without losing all typedef information.
332 return getDesugaredType()->getAsIncompleteArrayType();
Steve Naroff5c06a692008-01-21 22:59:18 +0000333}
334
Chris Lattnerc8629632007-07-31 19:29:30 +0000335const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000336 // If this is directly a reference type, return it.
337 if (const RecordType *RTy = dyn_cast<RecordType>(this))
338 return RTy;
339
Chris Lattnerdea61462007-10-29 03:41:11 +0000340 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000341 if (!isa<RecordType>(CanonicalType)) {
342 // Look through type qualifiers
343 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
344 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000345 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000346 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000347
348 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000349 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000350 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000351}
352
Chris Lattnerc8629632007-07-31 19:29:30 +0000353const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000354 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000355 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000356 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000357 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000358 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000359
360 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000361 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000362 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000363 return 0;
364
365 // If this is a typedef for a structure type, strip the typedef off without
366 // losing all typedef information.
367 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000369 // Look through type qualifiers
370 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
371 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000372 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000373}
374
Chris Lattnerc8629632007-07-31 19:29:30 +0000375const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000376 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000377 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000378 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000379 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000380 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000381
Chris Lattnerdea61462007-10-29 03:41:11 +0000382 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000383 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000384 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000385 return 0;
386
387 // If this is a typedef for a union type, strip the typedef off without
388 // losing all typedef information.
389 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000391
392 // Look through type qualifiers
393 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
394 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000395 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000396}
397
Eli Friedmanad74a752008-06-28 06:23:08 +0000398const EnumType *Type::getAsEnumType() const {
399 // Check the canonicalized unqualified type directly; the more complex
400 // version is unnecessary because there isn't any typedef information
401 // to preserve.
402 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
403}
404
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000405const ComplexType *Type::getAsComplexType() const {
406 // Are we directly a complex type?
407 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
408 return CTy;
409
Chris Lattnerdea61462007-10-29 03:41:11 +0000410 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000411 if (!isa<ComplexType>(CanonicalType)) {
412 // Look through type qualifiers
413 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
414 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000415 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000416 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000417
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000418 // If this is a typedef for a complex type, strip the typedef off without
419 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000420 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000421}
422
Chris Lattnerc8629632007-07-31 19:29:30 +0000423const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000424 // Are we directly a vector type?
425 if (const VectorType *VTy = dyn_cast<VectorType>(this))
426 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000427
Chris Lattnerdea61462007-10-29 03:41:11 +0000428 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000429 if (!isa<VectorType>(CanonicalType)) {
430 // Look through type qualifiers
431 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
432 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000433 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000434 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000435
Chris Lattnera2c77672007-07-16 22:05:22 +0000436 // If this is a typedef for a vector type, strip the typedef off without
437 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000438 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000439}
440
Nate Begeman213541a2008-04-18 23:10:10 +0000441const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000442 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000443 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000444 return VTy;
445
Chris Lattnerdea61462007-10-29 03:41:11 +0000446 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000447 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000448 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000449 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
450 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000451 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000452 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000453
Nate Begeman213541a2008-04-18 23:10:10 +0000454 // If this is a typedef for an extended vector type, strip the typedef off
455 // without losing all typedef information.
456 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000457}
458
Chris Lattner368eefa2008-04-07 00:27:04 +0000459const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000460 // There is no sugar for ObjCInterfaceType's, just return the canonical
461 // type pointer if it is the right class.
462 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000463}
464
465const ObjCQualifiedInterfaceType *
466Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000467 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
468 // type pointer if it is the right class.
469 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
470}
471
472const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
473 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
474 // type pointer if it is the right class.
475 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000476}
477
478
Reid Spencer5f016e22007-07-11 17:01:13 +0000479bool Type::isIntegerType() const {
480 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
481 return BT->getKind() >= BuiltinType::Bool &&
482 BT->getKind() <= BuiltinType::LongLong;
483 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000484 // Incomplete enum types are not treated as integer types.
485 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000487 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
488 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000489 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
490 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 return false;
492}
493
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000494bool Type::isIntegralType() const {
495 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
496 return BT->getKind() >= BuiltinType::Bool &&
497 BT->getKind() <= BuiltinType::LongLong;
498 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000499 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
500 return true; // Complete enum types are integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000501 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
502 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000503 return false;
504}
505
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000506bool Type::isEnumeralType() const {
507 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000508 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000509 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
510 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000511 return false;
512}
513
514bool Type::isBooleanType() const {
515 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
516 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000517 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
518 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000519 return false;
520}
521
522bool Type::isCharType() const {
523 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
524 return BT->getKind() == BuiltinType::Char_U ||
525 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000526 BT->getKind() == BuiltinType::Char_S ||
527 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000528 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
529 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000530 return false;
531}
532
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000533/// isSignedIntegerType - Return true if this is an integer type that is
534/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
535/// an enum decl which has a signed representation, or a vector of signed
536/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000537bool Type::isSignedIntegerType() const {
538 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
539 return BT->getKind() >= BuiltinType::Char_S &&
540 BT->getKind() <= BuiltinType::LongLong;
541 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000542
Chris Lattner37c1b782008-04-06 22:29:16 +0000543 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
544 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000545
Steve Naroffc63b96a2007-07-12 21:46:55 +0000546 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
547 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000548 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
549 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 return false;
551}
552
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000553/// isUnsignedIntegerType - Return true if this is an integer type that is
554/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
555/// decl which has an unsigned representation, or a vector of unsigned integer
556/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000557bool Type::isUnsignedIntegerType() const {
558 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
559 return BT->getKind() >= BuiltinType::Bool &&
560 BT->getKind() <= BuiltinType::ULongLong;
561 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000562
Chris Lattner37c1b782008-04-06 22:29:16 +0000563 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
564 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000565
Steve Naroffc63b96a2007-07-12 21:46:55 +0000566 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
567 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000568 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
569 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 return false;
571}
572
573bool Type::isFloatingType() const {
574 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
575 return BT->getKind() >= BuiltinType::Float &&
576 BT->getKind() <= BuiltinType::LongDouble;
577 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000578 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000579 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
580 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000581 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
582 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 return false;
584}
585
586bool Type::isRealFloatingType() const {
587 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
588 return BT->getKind() >= BuiltinType::Float &&
589 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000590 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
591 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000592 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
593 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 return false;
595}
596
597bool Type::isRealType() const {
598 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
599 return BT->getKind() >= BuiltinType::Bool &&
600 BT->getKind() <= BuiltinType::LongDouble;
601 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000602 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000603 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
604 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000605 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
606 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 return false;
608}
609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610bool Type::isArithmeticType() const {
611 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
612 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000613 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
614 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
615 // If a body isn't seen by the time we get here, return false.
616 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000617 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
618 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
620}
621
622bool Type::isScalarType() const {
623 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
624 return BT->getKind() != BuiltinType::Void;
625 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000626 // Enums are scalar types, but only if they are defined. Incomplete enums
627 // are not treated as scalar types.
628 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 return true;
630 return false;
631 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000632 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
633 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000634 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000635 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000636}
637
638bool Type::isAggregateType() const {
639 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000640 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 return true;
642 return false;
643 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000644 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
645 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000646 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000647}
648
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000649/// isConstantSizeType - Return true if this is not a variable sized type,
650/// according to the rules of C99 6.7.5p3. It is not legal to call this on
651/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000652bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000653 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000654 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000655 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000656 // The VAT must have a size, as it is known to be complete.
657 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000658}
659
660/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
661/// - a type that can describe objects, but which lacks information needed to
662/// determine its size.
663bool Type::isIncompleteType() const {
664 switch (CanonicalType->getTypeClass()) {
665 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000666 case ASQual:
667 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 case Builtin:
669 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
670 // be completed.
671 return isVoidType();
672 case Tagged:
673 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
674 // forward declaration, but not a full definition (C99 6.2.5p22).
675 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000676 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000678 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 }
680}
681
682bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000683 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
684 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
686 if (!BT) return false;
687 switch (BT->getKind()) {
688 case BuiltinType::Bool:
689 case BuiltinType::Char_S:
690 case BuiltinType::Char_U:
691 case BuiltinType::SChar:
692 case BuiltinType::UChar:
693 case BuiltinType::Short:
694 case BuiltinType::UShort:
695 return true;
696 default:
697 return false;
698 }
699}
700
701const char *BuiltinType::getName() const {
702 switch (getKind()) {
703 default: assert(0 && "Unknown builtin type!");
704 case Void: return "void";
705 case Bool: return "_Bool";
706 case Char_S: return "char";
707 case Char_U: return "char";
708 case SChar: return "signed char";
709 case Short: return "short";
710 case Int: return "int";
711 case Long: return "long";
712 case LongLong: return "long long";
713 case UChar: return "unsigned char";
714 case UShort: return "unsigned short";
715 case UInt: return "unsigned int";
716 case ULong: return "unsigned long";
717 case ULongLong: return "unsigned long long";
718 case Float: return "float";
719 case Double: return "double";
720 case LongDouble: return "long double";
721 }
722}
723
Reid Spencer5f016e22007-07-11 17:01:13 +0000724void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000725 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 unsigned NumArgs, bool isVariadic) {
727 ID.AddPointer(Result.getAsOpaquePtr());
728 for (unsigned i = 0; i != NumArgs; ++i)
729 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
730 ID.AddInteger(isVariadic);
731}
732
733void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000734 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000735}
736
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000737void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000738 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000739 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000740 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000741 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000742 for (unsigned i = 0; i != NumProtocols; i++)
743 ID.AddPointer(protocols[i]);
744}
745
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000746void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000747 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000748}
749
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000750void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000751 ObjCProtocolDecl **protocols,
752 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000753 for (unsigned i = 0; i != NumProtocols; i++)
754 ID.AddPointer(protocols[i]);
755}
756
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000757void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000758 Profile(ID, &Protocols[0], getNumProtocols());
759}
760
Chris Lattnera2c77672007-07-16 22:05:22 +0000761/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
762/// potentially looking through *all* consequtive typedefs. This returns the
763/// sum of the type qualifiers, so if you have:
764/// typedef const int A;
765/// typedef volatile A B;
766/// looking through the typedefs for B will give you "const volatile A".
767///
768QualType TypedefType::LookThroughTypedefs() const {
769 // Usually, there is only a single level of typedefs, be fast in that case.
770 QualType FirstType = getDecl()->getUnderlyingType();
771 if (!isa<TypedefType>(FirstType))
772 return FirstType;
773
774 // Otherwise, do the fully general loop.
775 unsigned TypeQuals = 0;
776 const TypedefType *TDT = this;
777 while (1) {
778 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000779
780
781 /// FIXME:
782 /// FIXME: This is incorrect for ASQuals!
783 /// FIXME:
784 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000785
786 TDT = dyn_cast<TypedefType>(CurType);
787 if (TDT == 0)
788 return QualType(CurType.getTypePtr(), TypeQuals);
789 }
790}
Reid Spencer5f016e22007-07-11 17:01:13 +0000791
Chris Lattner2daa5df2008-04-06 22:04:54 +0000792bool RecordType::classof(const TagType *TT) {
793 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000794}
795
Chris Lattner2daa5df2008-04-06 22:04:54 +0000796bool EnumType::classof(const TagType *TT) {
797 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000798}
799
Reid Spencer5f016e22007-07-11 17:01:13 +0000800
801//===----------------------------------------------------------------------===//
802// Type Printing
803//===----------------------------------------------------------------------===//
804
805void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000806 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 getAsStringInternal(R);
808 if (msg)
809 fprintf(stderr, "%s: %s\n", msg, R.c_str());
810 else
811 fprintf(stderr, "%s\n", R.c_str());
812}
813
814static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
815 // Note: funkiness to ensure we get a space only between quals.
816 bool NonePrinted = true;
817 if (TypeQuals & QualType::Const)
818 S += "const", NonePrinted = false;
819 if (TypeQuals & QualType::Volatile)
820 S += (NonePrinted+" volatile"), NonePrinted = false;
821 if (TypeQuals & QualType::Restrict)
822 S += (NonePrinted+" restrict"), NonePrinted = false;
823}
824
825void QualType::getAsStringInternal(std::string &S) const {
826 if (isNull()) {
827 S += "NULL TYPE\n";
828 return;
829 }
830
831 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000832 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000834 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 if (!S.empty())
836 S = TQS + ' ' + S;
837 else
838 S = TQS;
839 }
840
841 getTypePtr()->getAsStringInternal(S);
842}
843
844void BuiltinType::getAsStringInternal(std::string &S) const {
845 if (S.empty()) {
846 S = getName();
847 } else {
848 // Prefix the basic type, e.g. 'int X'.
849 S = ' ' + S;
850 S = getName() + S;
851 }
852}
853
854void ComplexType::getAsStringInternal(std::string &S) const {
855 ElementType->getAsStringInternal(S);
856 S = "_Complex " + S;
857}
858
Christopher Lambebb97e92008-02-04 02:31:56 +0000859void ASQualType::getAsStringInternal(std::string &S) const {
860 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
861 BaseType->getAsStringInternal(S);
862}
863
Reid Spencer5f016e22007-07-11 17:01:13 +0000864void PointerType::getAsStringInternal(std::string &S) const {
865 S = '*' + S;
866
867 // Handle things like 'int (*A)[4];' correctly.
868 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000869 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 S = '(' + S + ')';
871
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000872 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000873}
874
875void ReferenceType::getAsStringInternal(std::string &S) const {
876 S = '&' + S;
877
878 // Handle things like 'int (&A)[4];' correctly.
879 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000880 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 S = '(' + S + ')';
882
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000883 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000884}
885
Steve Narofffb22d962007-08-30 01:06:46 +0000886void ConstantArrayType::getAsStringInternal(std::string &S) const {
887 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000888 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000889 S += ']';
890
891 getElementType().getAsStringInternal(S);
892}
893
Eli Friedmanc5773c42008-02-15 18:16:39 +0000894void IncompleteArrayType::getAsStringInternal(std::string &S) const {
895 S += "[]";
896
897 getElementType().getAsStringInternal(S);
898}
899
Steve Narofffb22d962007-08-30 01:06:46 +0000900void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 S += '[';
902
Steve Naroffc9406122007-08-30 18:10:14 +0000903 if (getIndexTypeQualifier()) {
904 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 S += ' ';
906 }
907
Steve Naroffc9406122007-08-30 18:10:14 +0000908 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000910 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 S += '*';
912
Steve Narofffb22d962007-08-30 01:06:46 +0000913 if (getSizeExpr()) {
914 std::ostringstream s;
915 getSizeExpr()->printPretty(s);
916 S += s.str();
917 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 S += ']';
919
Steve Narofffb22d962007-08-30 01:06:46 +0000920 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000921}
922
923void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000924 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000925 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000927 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 ElementType.getAsStringInternal(S);
929}
930
Nate Begeman213541a2008-04-18 23:10:10 +0000931void ExtVectorType::getAsStringInternal(std::string &S) const {
932 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000933 S += llvm::utostr_32(NumElements);
934 S += ")))";
935 ElementType.getAsStringInternal(S);
936}
937
Steve Naroffd1861fd2007-07-31 12:34:36 +0000938void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000939 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
940 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000941 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000942 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000943 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000944}
945
Steve Naroff363bcff2007-08-01 23:45:51 +0000946void TypeOfType::getAsStringInternal(std::string &InnerString) const {
947 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
948 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000949 std::string Tmp;
950 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000951 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000952}
953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
955 // If needed for precedence reasons, wrap the inner part in grouping parens.
956 if (!S.empty())
957 S = "(" + S + ")";
958
959 S += "()";
960 getResultType().getAsStringInternal(S);
961}
962
963void FunctionTypeProto::getAsStringInternal(std::string &S) const {
964 // If needed for precedence reasons, wrap the inner part in grouping parens.
965 if (!S.empty())
966 S = "(" + S + ")";
967
968 S += "(";
969 std::string Tmp;
970 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
971 if (i) S += ", ";
972 getArgType(i).getAsStringInternal(Tmp);
973 S += Tmp;
974 Tmp.clear();
975 }
976
977 if (isVariadic()) {
978 if (getNumArgs())
979 S += ", ";
980 S += "...";
981 } else if (getNumArgs() == 0) {
982 // Do not emit int() if we have a proto, emit 'int(void)'.
983 S += "void";
984 }
985
986 S += ")";
987 getResultType().getAsStringInternal(S);
988}
989
990
991void TypedefType::getAsStringInternal(std::string &InnerString) const {
992 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
993 InnerString = ' ' + InnerString;
994 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
995}
996
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000997void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000998 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
999 InnerString = ' ' + InnerString;
1000 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
1001}
1002
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001003void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001004 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001005 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1006 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001007 std::string ObjCQIString = getDecl()->getName();
1008 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001009 bool isFirst = true;
1010 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1011 if (isFirst)
1012 isFirst = false;
1013 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001014 ObjCQIString += ',';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001015 ObjCQIString += (*I)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001016 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001017 ObjCQIString += '>';
1018 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001019}
1020
Chris Lattnere8e4f922008-07-25 23:07:18 +00001021void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001022 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1023 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001024 std::string ObjCQIString = "id";
1025 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001026 int num = getNumProtocols();
1027 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001029 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001030 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001031 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001032 ObjCQIString += '>';
1033 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001034}
1035
Reid Spencer5f016e22007-07-11 17:01:13 +00001036void TagType::getAsStringInternal(std::string &InnerString) const {
1037 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1038 InnerString = ' ' + InnerString;
1039
1040 const char *Kind = getDecl()->getKindName();
1041 const char *ID;
1042 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1043 ID = II->getName();
1044 else
1045 ID = "<anonymous>";
1046
1047 InnerString = std::string(Kind) + " " + ID + InnerString;
1048}