blob: 7e09bb1ae37d6d1bf23d3317f841f94c4d8b27ab [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
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000393const ComplexType *Type::getAsComplexType() const {
394 // Are we directly a complex type?
395 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
396 return CTy;
397
Chris Lattnerdea61462007-10-29 03:41:11 +0000398 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000399 if (!isa<ComplexType>(CanonicalType)) {
400 // Look through type qualifiers
401 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
402 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000403 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000404 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000405
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000406 // If this is a typedef for a complex type, strip the typedef off without
407 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000408 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000409}
410
Chris Lattnerc8629632007-07-31 19:29:30 +0000411const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000412 // Are we directly a vector type?
413 if (const VectorType *VTy = dyn_cast<VectorType>(this))
414 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000415
Chris Lattnerdea61462007-10-29 03:41:11 +0000416 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000417 if (!isa<VectorType>(CanonicalType)) {
418 // Look through type qualifiers
419 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
420 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000421 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000422 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000423
Chris Lattnera2c77672007-07-16 22:05:22 +0000424 // If this is a typedef for a vector type, strip the typedef off without
425 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000426 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000427}
428
Nate Begeman213541a2008-04-18 23:10:10 +0000429const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000430 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000431 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000432 return VTy;
433
Chris Lattnerdea61462007-10-29 03:41:11 +0000434 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000435 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000436 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000437 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
438 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000439 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000440 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000441
Nate Begeman213541a2008-04-18 23:10:10 +0000442 // If this is a typedef for an extended vector type, strip the typedef off
443 // without losing all typedef information.
444 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000445}
446
Chris Lattner368eefa2008-04-07 00:27:04 +0000447const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000448 // There is no sugar for ObjCInterfaceType's, just return the canonical
449 // type pointer if it is the right class.
450 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000451}
452
453const ObjCQualifiedInterfaceType *
454Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000455 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
456 // type pointer if it is the right class.
457 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
458}
459
460const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
461 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
462 // type pointer if it is the right class.
463 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000464}
465
466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467bool Type::isIntegerType() const {
468 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
469 return BT->getKind() >= BuiltinType::Bool &&
470 BT->getKind() <= BuiltinType::LongLong;
471 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000472 if (TT->getDecl()->isEnum())
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000474 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
475 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000476 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
477 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 return false;
479}
480
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000481bool Type::isIntegralType() const {
482 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
483 return BT->getKind() >= BuiltinType::Bool &&
484 BT->getKind() <= BuiltinType::LongLong;
485 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000486 if (TT->getDecl()->isEnum())
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000487 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000488 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
489 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000490 return false;
491}
492
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000493bool Type::isEnumeralType() const {
494 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000495 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000496 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
497 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000498 return false;
499}
500
501bool Type::isBooleanType() const {
502 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
503 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000504 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
505 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000506 return false;
507}
508
509bool Type::isCharType() const {
510 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
511 return BT->getKind() == BuiltinType::Char_U ||
512 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000513 BT->getKind() == BuiltinType::Char_S ||
514 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000515 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
516 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000517 return false;
518}
519
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000520/// isSignedIntegerType - Return true if this is an integer type that is
521/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
522/// an enum decl which has a signed representation, or a vector of signed
523/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000524bool Type::isSignedIntegerType() const {
525 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
526 return BT->getKind() >= BuiltinType::Char_S &&
527 BT->getKind() <= BuiltinType::LongLong;
528 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000529
Chris Lattner37c1b782008-04-06 22:29:16 +0000530 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
531 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000532
Steve Naroffc63b96a2007-07-12 21:46:55 +0000533 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
534 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000535 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
536 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 return false;
538}
539
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000540/// isUnsignedIntegerType - Return true if this is an integer type that is
541/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
542/// decl which has an unsigned representation, or a vector of unsigned integer
543/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000544bool Type::isUnsignedIntegerType() const {
545 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
546 return BT->getKind() >= BuiltinType::Bool &&
547 BT->getKind() <= BuiltinType::ULongLong;
548 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000549
Chris Lattner37c1b782008-04-06 22:29:16 +0000550 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
551 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000552
Steve Naroffc63b96a2007-07-12 21:46:55 +0000553 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
554 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000555 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
556 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 return false;
558}
559
560bool Type::isFloatingType() const {
561 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
562 return BT->getKind() >= BuiltinType::Float &&
563 BT->getKind() <= BuiltinType::LongDouble;
564 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000565 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000566 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
567 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000568 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
569 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 return false;
571}
572
573bool Type::isRealFloatingType() const {
574 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
575 return BT->getKind() >= BuiltinType::Float &&
576 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000577 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
578 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000579 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
580 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 return false;
582}
583
584bool Type::isRealType() const {
585 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
586 return BT->getKind() >= BuiltinType::Bool &&
587 BT->getKind() <= BuiltinType::LongDouble;
588 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000589 return TT->getDecl()->isEnum();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000590 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
591 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000592 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
593 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 return false;
595}
596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597bool Type::isArithmeticType() const {
598 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
599 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000600 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
601 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
602 // If a body isn't seen by the time we get here, return false.
603 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000604 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
605 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
607}
608
609bool Type::isScalarType() const {
610 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
611 return BT->getKind() != BuiltinType::Void;
612 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000613 if (TT->getDecl()->isEnum())
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 return true;
615 return false;
616 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000617 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
618 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000619 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000620 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000621}
622
623bool Type::isAggregateType() const {
624 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000625 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 return true;
627 return false;
628 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000629 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
630 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000631 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000632}
633
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000634/// isConstantSizeType - Return true if this is not a variable sized type,
635/// according to the rules of C99 6.7.5p3. It is not legal to call this on
636/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000637bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000638 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000639 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000640 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000641 // The VAT must have a size, as it is known to be complete.
642 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000643}
644
645/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
646/// - a type that can describe objects, but which lacks information needed to
647/// determine its size.
648bool Type::isIncompleteType() const {
649 switch (CanonicalType->getTypeClass()) {
650 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000651 case ASQual:
652 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 case Builtin:
654 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
655 // be completed.
656 return isVoidType();
657 case Tagged:
658 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
659 // forward declaration, but not a full definition (C99 6.2.5p22).
660 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000661 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000663 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 }
665}
666
667bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000668 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
669 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
671 if (!BT) return false;
672 switch (BT->getKind()) {
673 case BuiltinType::Bool:
674 case BuiltinType::Char_S:
675 case BuiltinType::Char_U:
676 case BuiltinType::SChar:
677 case BuiltinType::UChar:
678 case BuiltinType::Short:
679 case BuiltinType::UShort:
680 return true;
681 default:
682 return false;
683 }
684}
685
686const char *BuiltinType::getName() const {
687 switch (getKind()) {
688 default: assert(0 && "Unknown builtin type!");
689 case Void: return "void";
690 case Bool: return "_Bool";
691 case Char_S: return "char";
692 case Char_U: return "char";
693 case SChar: return "signed char";
694 case Short: return "short";
695 case Int: return "int";
696 case Long: return "long";
697 case LongLong: return "long long";
698 case UChar: return "unsigned char";
699 case UShort: return "unsigned short";
700 case UInt: return "unsigned int";
701 case ULong: return "unsigned long";
702 case ULongLong: return "unsigned long long";
703 case Float: return "float";
704 case Double: return "double";
705 case LongDouble: return "long double";
706 }
707}
708
Reid Spencer5f016e22007-07-11 17:01:13 +0000709void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000710 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 unsigned NumArgs, bool isVariadic) {
712 ID.AddPointer(Result.getAsOpaquePtr());
713 for (unsigned i = 0; i != NumArgs; ++i)
714 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
715 ID.AddInteger(isVariadic);
716}
717
718void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000719 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000720}
721
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000722void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000723 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000724 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000725 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000726 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000727 for (unsigned i = 0; i != NumProtocols; i++)
728 ID.AddPointer(protocols[i]);
729}
730
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000731void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000732 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000733}
734
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000735void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
736 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000737 unsigned NumProtocols) {
738 for (unsigned i = 0; i != NumProtocols; i++)
739 ID.AddPointer(protocols[i]);
740}
741
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000742void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000743 Profile(ID, &Protocols[0], getNumProtocols());
744}
745
Chris Lattnera2c77672007-07-16 22:05:22 +0000746/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
747/// potentially looking through *all* consequtive typedefs. This returns the
748/// sum of the type qualifiers, so if you have:
749/// typedef const int A;
750/// typedef volatile A B;
751/// looking through the typedefs for B will give you "const volatile A".
752///
753QualType TypedefType::LookThroughTypedefs() const {
754 // Usually, there is only a single level of typedefs, be fast in that case.
755 QualType FirstType = getDecl()->getUnderlyingType();
756 if (!isa<TypedefType>(FirstType))
757 return FirstType;
758
759 // Otherwise, do the fully general loop.
760 unsigned TypeQuals = 0;
761 const TypedefType *TDT = this;
762 while (1) {
763 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000764
765
766 /// FIXME:
767 /// FIXME: This is incorrect for ASQuals!
768 /// FIXME:
769 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000770
771 TDT = dyn_cast<TypedefType>(CurType);
772 if (TDT == 0)
773 return QualType(CurType.getTypePtr(), TypeQuals);
774 }
775}
Reid Spencer5f016e22007-07-11 17:01:13 +0000776
Chris Lattner2daa5df2008-04-06 22:04:54 +0000777bool RecordType::classof(const TagType *TT) {
778 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000779}
780
Chris Lattner2daa5df2008-04-06 22:04:54 +0000781bool EnumType::classof(const TagType *TT) {
782 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000783}
784
Reid Spencer5f016e22007-07-11 17:01:13 +0000785
786//===----------------------------------------------------------------------===//
787// Type Printing
788//===----------------------------------------------------------------------===//
789
790void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000791 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 getAsStringInternal(R);
793 if (msg)
794 fprintf(stderr, "%s: %s\n", msg, R.c_str());
795 else
796 fprintf(stderr, "%s\n", R.c_str());
797}
798
799static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
800 // Note: funkiness to ensure we get a space only between quals.
801 bool NonePrinted = true;
802 if (TypeQuals & QualType::Const)
803 S += "const", NonePrinted = false;
804 if (TypeQuals & QualType::Volatile)
805 S += (NonePrinted+" volatile"), NonePrinted = false;
806 if (TypeQuals & QualType::Restrict)
807 S += (NonePrinted+" restrict"), NonePrinted = false;
808}
809
810void QualType::getAsStringInternal(std::string &S) const {
811 if (isNull()) {
812 S += "NULL TYPE\n";
813 return;
814 }
815
816 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000817 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000819 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 if (!S.empty())
821 S = TQS + ' ' + S;
822 else
823 S = TQS;
824 }
825
826 getTypePtr()->getAsStringInternal(S);
827}
828
829void BuiltinType::getAsStringInternal(std::string &S) const {
830 if (S.empty()) {
831 S = getName();
832 } else {
833 // Prefix the basic type, e.g. 'int X'.
834 S = ' ' + S;
835 S = getName() + S;
836 }
837}
838
839void ComplexType::getAsStringInternal(std::string &S) const {
840 ElementType->getAsStringInternal(S);
841 S = "_Complex " + S;
842}
843
Christopher Lambebb97e92008-02-04 02:31:56 +0000844void ASQualType::getAsStringInternal(std::string &S) const {
845 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
846 BaseType->getAsStringInternal(S);
847}
848
Reid Spencer5f016e22007-07-11 17:01:13 +0000849void PointerType::getAsStringInternal(std::string &S) const {
850 S = '*' + S;
851
852 // Handle things like 'int (*A)[4];' correctly.
853 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000854 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 S = '(' + S + ')';
856
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000857 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000858}
859
860void ReferenceType::getAsStringInternal(std::string &S) const {
861 S = '&' + S;
862
863 // Handle things like 'int (&A)[4];' correctly.
864 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000865 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 S = '(' + S + ')';
867
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000868 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000869}
870
Steve Narofffb22d962007-08-30 01:06:46 +0000871void ConstantArrayType::getAsStringInternal(std::string &S) const {
872 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000873 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000874 S += ']';
875
876 getElementType().getAsStringInternal(S);
877}
878
Eli Friedmanc5773c42008-02-15 18:16:39 +0000879void IncompleteArrayType::getAsStringInternal(std::string &S) const {
880 S += "[]";
881
882 getElementType().getAsStringInternal(S);
883}
884
Steve Narofffb22d962007-08-30 01:06:46 +0000885void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 S += '[';
887
Steve Naroffc9406122007-08-30 18:10:14 +0000888 if (getIndexTypeQualifier()) {
889 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 S += ' ';
891 }
892
Steve Naroffc9406122007-08-30 18:10:14 +0000893 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000895 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 S += '*';
897
Steve Narofffb22d962007-08-30 01:06:46 +0000898 if (getSizeExpr()) {
899 std::ostringstream s;
900 getSizeExpr()->printPretty(s);
901 S += s.str();
902 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 S += ']';
904
Steve Narofffb22d962007-08-30 01:06:46 +0000905 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906}
907
908void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000909 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000910 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000912 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 ElementType.getAsStringInternal(S);
914}
915
Nate Begeman213541a2008-04-18 23:10:10 +0000916void ExtVectorType::getAsStringInternal(std::string &S) const {
917 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000918 S += llvm::utostr_32(NumElements);
919 S += ")))";
920 ElementType.getAsStringInternal(S);
921}
922
Steve Naroffd1861fd2007-07-31 12:34:36 +0000923void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000924 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
925 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000926 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000927 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000928 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000929}
930
Steve Naroff363bcff2007-08-01 23:45:51 +0000931void TypeOfType::getAsStringInternal(std::string &InnerString) const {
932 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
933 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000934 std::string Tmp;
935 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000936 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000937}
938
Reid Spencer5f016e22007-07-11 17:01:13 +0000939void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
940 // If needed for precedence reasons, wrap the inner part in grouping parens.
941 if (!S.empty())
942 S = "(" + S + ")";
943
944 S += "()";
945 getResultType().getAsStringInternal(S);
946}
947
948void FunctionTypeProto::getAsStringInternal(std::string &S) const {
949 // If needed for precedence reasons, wrap the inner part in grouping parens.
950 if (!S.empty())
951 S = "(" + S + ")";
952
953 S += "(";
954 std::string Tmp;
955 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
956 if (i) S += ", ";
957 getArgType(i).getAsStringInternal(Tmp);
958 S += Tmp;
959 Tmp.clear();
960 }
961
962 if (isVariadic()) {
963 if (getNumArgs())
964 S += ", ";
965 S += "...";
966 } else if (getNumArgs() == 0) {
967 // Do not emit int() if we have a proto, emit 'int(void)'.
968 S += "void";
969 }
970
971 S += ")";
972 getResultType().getAsStringInternal(S);
973}
974
975
976void TypedefType::getAsStringInternal(std::string &InnerString) const {
977 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
978 InnerString = ' ' + InnerString;
979 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
980}
981
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000982void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000983 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
984 InnerString = ' ' + InnerString;
985 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
986}
987
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000988void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000989 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000990 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
991 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000992 std::string ObjCQIString = getDecl()->getName();
993 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000994 int num = getNumProtocols();
995 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000997 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000999 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001000 ObjCQIString += '>';
1001 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001002}
1003
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001005 std::string &InnerString) const {
1006 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1007 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 std::string ObjCQIString = "id";
1009 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001010 int num = getNumProtocols();
1011 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001012 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001013 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001014 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001015 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001016 ObjCQIString += '>';
1017 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001018}
1019
Reid Spencer5f016e22007-07-11 17:01:13 +00001020void TagType::getAsStringInternal(std::string &InnerString) const {
1021 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1022 InnerString = ' ' + InnerString;
1023
1024 const char *Kind = getDecl()->getKindName();
1025 const char *ID;
1026 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1027 ID = II->getName();
1028 else
1029 ID = "<anonymous>";
1030
1031 InnerString = std::string(Kind) + " " + ID + InnerString;
1032}