blob: 275406eabacacca27ae8172632d0e350cda3baf6 [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))
Chris Lattner834a72a2008-07-25 23:18:17 +0000479 // Incomplete enum types are not treated as integer types.
480 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000482 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
483 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000484 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
485 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 return false;
487}
488
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000489bool Type::isIntegralType() const {
490 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
491 return BT->getKind() >= BuiltinType::Bool &&
492 BT->getKind() <= BuiltinType::LongLong;
493 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000494 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
495 return true; // Complete enum types are integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000496 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
497 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000498 return false;
499}
500
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000501bool Type::isEnumeralType() const {
502 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000503 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000504 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
505 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000506 return false;
507}
508
509bool Type::isBooleanType() const {
510 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
511 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000512 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
513 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000514 return false;
515}
516
517bool Type::isCharType() const {
518 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
519 return BT->getKind() == BuiltinType::Char_U ||
520 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000521 BT->getKind() == BuiltinType::Char_S ||
522 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000523 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
524 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000525 return false;
526}
527
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000528/// isSignedIntegerType - Return true if this is an integer type that is
529/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
530/// an enum decl which has a signed representation, or a vector of signed
531/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000532bool Type::isSignedIntegerType() const {
533 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
534 return BT->getKind() >= BuiltinType::Char_S &&
535 BT->getKind() <= BuiltinType::LongLong;
536 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000537
Chris Lattner37c1b782008-04-06 22:29:16 +0000538 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
539 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000540
Steve Naroffc63b96a2007-07-12 21:46:55 +0000541 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
542 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000543 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
544 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 return false;
546}
547
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000548/// isUnsignedIntegerType - Return true if this is an integer type that is
549/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
550/// decl which has an unsigned representation, or a vector of unsigned integer
551/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000552bool Type::isUnsignedIntegerType() const {
553 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
554 return BT->getKind() >= BuiltinType::Bool &&
555 BT->getKind() <= BuiltinType::ULongLong;
556 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000557
Chris Lattner37c1b782008-04-06 22:29:16 +0000558 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
559 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000560
Steve Naroffc63b96a2007-07-12 21:46:55 +0000561 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
562 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000563 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
564 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 return false;
566}
567
568bool Type::isFloatingType() const {
569 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
570 return BT->getKind() >= BuiltinType::Float &&
571 BT->getKind() <= BuiltinType::LongDouble;
572 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000573 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000574 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
575 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000576 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
577 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 return false;
579}
580
581bool Type::isRealFloatingType() const {
582 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
583 return BT->getKind() >= BuiltinType::Float &&
584 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000585 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
586 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000587 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
588 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 return false;
590}
591
592bool Type::isRealType() const {
593 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
594 return BT->getKind() >= BuiltinType::Bool &&
595 BT->getKind() <= BuiltinType::LongDouble;
596 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000597 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000598 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
599 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000600 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
601 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 return false;
603}
604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605bool Type::isArithmeticType() const {
606 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
607 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000608 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
609 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
610 // If a body isn't seen by the time we get here, return false.
611 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000612 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
613 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
615}
616
617bool Type::isScalarType() const {
618 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
619 return BT->getKind() != BuiltinType::Void;
620 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000621 // Enums are scalar types, but only if they are defined. Incomplete enums
622 // are not treated as scalar types.
623 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 return true;
625 return false;
626 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000627 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
628 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000629 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000630 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000631}
632
633bool Type::isAggregateType() const {
634 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000635 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 return true;
637 return false;
638 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000639 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
640 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000641 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000642}
643
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000644/// isConstantSizeType - Return true if this is not a variable sized type,
645/// according to the rules of C99 6.7.5p3. It is not legal to call this on
646/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000647bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000648 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000649 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000650 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000651 // The VAT must have a size, as it is known to be complete.
652 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000653}
654
655/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
656/// - a type that can describe objects, but which lacks information needed to
657/// determine its size.
658bool Type::isIncompleteType() const {
659 switch (CanonicalType->getTypeClass()) {
660 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000661 case ASQual:
662 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000663 case Builtin:
664 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
665 // be completed.
666 return isVoidType();
667 case Tagged:
668 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
669 // forward declaration, but not a full definition (C99 6.2.5p22).
670 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000671 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000673 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 }
675}
676
677bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000678 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
679 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
681 if (!BT) return false;
682 switch (BT->getKind()) {
683 case BuiltinType::Bool:
684 case BuiltinType::Char_S:
685 case BuiltinType::Char_U:
686 case BuiltinType::SChar:
687 case BuiltinType::UChar:
688 case BuiltinType::Short:
689 case BuiltinType::UShort:
690 return true;
691 default:
692 return false;
693 }
694}
695
696const char *BuiltinType::getName() const {
697 switch (getKind()) {
698 default: assert(0 && "Unknown builtin type!");
699 case Void: return "void";
700 case Bool: return "_Bool";
701 case Char_S: return "char";
702 case Char_U: return "char";
703 case SChar: return "signed char";
704 case Short: return "short";
705 case Int: return "int";
706 case Long: return "long";
707 case LongLong: return "long long";
708 case UChar: return "unsigned char";
709 case UShort: return "unsigned short";
710 case UInt: return "unsigned int";
711 case ULong: return "unsigned long";
712 case ULongLong: return "unsigned long long";
713 case Float: return "float";
714 case Double: return "double";
715 case LongDouble: return "long double";
716 }
717}
718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000720 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 unsigned NumArgs, bool isVariadic) {
722 ID.AddPointer(Result.getAsOpaquePtr());
723 for (unsigned i = 0; i != NumArgs; ++i)
724 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
725 ID.AddInteger(isVariadic);
726}
727
728void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000729 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000730}
731
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000732void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000733 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000734 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000735 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000736 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000737 for (unsigned i = 0; i != NumProtocols; i++)
738 ID.AddPointer(protocols[i]);
739}
740
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000741void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000742 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000743}
744
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000745void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
746 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000747 unsigned NumProtocols) {
748 for (unsigned i = 0; i != NumProtocols; i++)
749 ID.AddPointer(protocols[i]);
750}
751
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000752void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000753 Profile(ID, &Protocols[0], getNumProtocols());
754}
755
Chris Lattnera2c77672007-07-16 22:05:22 +0000756/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
757/// potentially looking through *all* consequtive typedefs. This returns the
758/// sum of the type qualifiers, so if you have:
759/// typedef const int A;
760/// typedef volatile A B;
761/// looking through the typedefs for B will give you "const volatile A".
762///
763QualType TypedefType::LookThroughTypedefs() const {
764 // Usually, there is only a single level of typedefs, be fast in that case.
765 QualType FirstType = getDecl()->getUnderlyingType();
766 if (!isa<TypedefType>(FirstType))
767 return FirstType;
768
769 // Otherwise, do the fully general loop.
770 unsigned TypeQuals = 0;
771 const TypedefType *TDT = this;
772 while (1) {
773 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000774
775
776 /// FIXME:
777 /// FIXME: This is incorrect for ASQuals!
778 /// FIXME:
779 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000780
781 TDT = dyn_cast<TypedefType>(CurType);
782 if (TDT == 0)
783 return QualType(CurType.getTypePtr(), TypeQuals);
784 }
785}
Reid Spencer5f016e22007-07-11 17:01:13 +0000786
Chris Lattner2daa5df2008-04-06 22:04:54 +0000787bool RecordType::classof(const TagType *TT) {
788 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000789}
790
Chris Lattner2daa5df2008-04-06 22:04:54 +0000791bool EnumType::classof(const TagType *TT) {
792 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000793}
794
Reid Spencer5f016e22007-07-11 17:01:13 +0000795
796//===----------------------------------------------------------------------===//
797// Type Printing
798//===----------------------------------------------------------------------===//
799
800void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000801 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 getAsStringInternal(R);
803 if (msg)
804 fprintf(stderr, "%s: %s\n", msg, R.c_str());
805 else
806 fprintf(stderr, "%s\n", R.c_str());
807}
808
809static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
810 // Note: funkiness to ensure we get a space only between quals.
811 bool NonePrinted = true;
812 if (TypeQuals & QualType::Const)
813 S += "const", NonePrinted = false;
814 if (TypeQuals & QualType::Volatile)
815 S += (NonePrinted+" volatile"), NonePrinted = false;
816 if (TypeQuals & QualType::Restrict)
817 S += (NonePrinted+" restrict"), NonePrinted = false;
818}
819
820void QualType::getAsStringInternal(std::string &S) const {
821 if (isNull()) {
822 S += "NULL TYPE\n";
823 return;
824 }
825
826 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000827 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000829 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 if (!S.empty())
831 S = TQS + ' ' + S;
832 else
833 S = TQS;
834 }
835
836 getTypePtr()->getAsStringInternal(S);
837}
838
839void BuiltinType::getAsStringInternal(std::string &S) const {
840 if (S.empty()) {
841 S = getName();
842 } else {
843 // Prefix the basic type, e.g. 'int X'.
844 S = ' ' + S;
845 S = getName() + S;
846 }
847}
848
849void ComplexType::getAsStringInternal(std::string &S) const {
850 ElementType->getAsStringInternal(S);
851 S = "_Complex " + S;
852}
853
Christopher Lambebb97e92008-02-04 02:31:56 +0000854void ASQualType::getAsStringInternal(std::string &S) const {
855 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
856 BaseType->getAsStringInternal(S);
857}
858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859void PointerType::getAsStringInternal(std::string &S) const {
860 S = '*' + S;
861
862 // Handle things like 'int (*A)[4];' correctly.
863 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000864 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 S = '(' + S + ')';
866
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000867 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000868}
869
870void ReferenceType::getAsStringInternal(std::string &S) const {
871 S = '&' + S;
872
873 // Handle things like 'int (&A)[4];' correctly.
874 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000875 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 S = '(' + S + ')';
877
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000878 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000879}
880
Steve Narofffb22d962007-08-30 01:06:46 +0000881void ConstantArrayType::getAsStringInternal(std::string &S) const {
882 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000883 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000884 S += ']';
885
886 getElementType().getAsStringInternal(S);
887}
888
Eli Friedmanc5773c42008-02-15 18:16:39 +0000889void IncompleteArrayType::getAsStringInternal(std::string &S) const {
890 S += "[]";
891
892 getElementType().getAsStringInternal(S);
893}
894
Steve Narofffb22d962007-08-30 01:06:46 +0000895void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 S += '[';
897
Steve Naroffc9406122007-08-30 18:10:14 +0000898 if (getIndexTypeQualifier()) {
899 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 S += ' ';
901 }
902
Steve Naroffc9406122007-08-30 18:10:14 +0000903 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000905 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 S += '*';
907
Steve Narofffb22d962007-08-30 01:06:46 +0000908 if (getSizeExpr()) {
909 std::ostringstream s;
910 getSizeExpr()->printPretty(s);
911 S += s.str();
912 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 S += ']';
914
Steve Narofffb22d962007-08-30 01:06:46 +0000915 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000916}
917
918void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000919 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000920 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000922 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 ElementType.getAsStringInternal(S);
924}
925
Nate Begeman213541a2008-04-18 23:10:10 +0000926void ExtVectorType::getAsStringInternal(std::string &S) const {
927 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000928 S += llvm::utostr_32(NumElements);
929 S += ")))";
930 ElementType.getAsStringInternal(S);
931}
932
Steve Naroffd1861fd2007-07-31 12:34:36 +0000933void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000934 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
935 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000936 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000937 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000938 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000939}
940
Steve Naroff363bcff2007-08-01 23:45:51 +0000941void TypeOfType::getAsStringInternal(std::string &InnerString) const {
942 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
943 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000944 std::string Tmp;
945 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000946 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000947}
948
Reid Spencer5f016e22007-07-11 17:01:13 +0000949void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
950 // If needed for precedence reasons, wrap the inner part in grouping parens.
951 if (!S.empty())
952 S = "(" + S + ")";
953
954 S += "()";
955 getResultType().getAsStringInternal(S);
956}
957
958void FunctionTypeProto::getAsStringInternal(std::string &S) const {
959 // If needed for precedence reasons, wrap the inner part in grouping parens.
960 if (!S.empty())
961 S = "(" + S + ")";
962
963 S += "(";
964 std::string Tmp;
965 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
966 if (i) S += ", ";
967 getArgType(i).getAsStringInternal(Tmp);
968 S += Tmp;
969 Tmp.clear();
970 }
971
972 if (isVariadic()) {
973 if (getNumArgs())
974 S += ", ";
975 S += "...";
976 } else if (getNumArgs() == 0) {
977 // Do not emit int() if we have a proto, emit 'int(void)'.
978 S += "void";
979 }
980
981 S += ")";
982 getResultType().getAsStringInternal(S);
983}
984
985
986void TypedefType::getAsStringInternal(std::string &InnerString) const {
987 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
988 InnerString = ' ' + InnerString;
989 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
990}
991
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000992void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000993 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
994 InnerString = ' ' + InnerString;
995 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
996}
997
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000999 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +00001000 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1001 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001002 std::string ObjCQIString = getDecl()->getName();
1003 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001004 bool isFirst = true;
1005 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
1006 if (isFirst)
1007 isFirst = false;
1008 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001009 ObjCQIString += ',';
Chris Lattnercdce6d12008-07-21 05:19:23 +00001010 ObjCQIString += (*I)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001011 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001012 ObjCQIString += '>';
1013 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001014}
1015
Chris Lattnere8e4f922008-07-25 23:07:18 +00001016void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001017 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1018 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001019 std::string ObjCQIString = "id";
1020 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001021 int num = getNumProtocols();
1022 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001023 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001024 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001025 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001026 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001027 ObjCQIString += '>';
1028 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001029}
1030
Reid Spencer5f016e22007-07-11 17:01:13 +00001031void TagType::getAsStringInternal(std::string &InnerString) const {
1032 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1033 InnerString = ' ' + InnerString;
1034
1035 const char *Kind = getDecl()->getKindName();
1036 const char *ID;
1037 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1038 ID = II->getName();
1039 else
1040 ID = "<anonymous>";
1041
1042 InnerString = std::string(Kind) + " " + ID + InnerString;
1043}