blob: af96830a9cb82b5da87cfb524fd9e857d3f9fb39 [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>
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26Type::~Type() {}
27
28/// isVoidType - Helper method to determine if this is the 'void' type.
29bool Type::isVoidType() const {
30 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
31 return BT->getKind() == BuiltinType::Void;
32 return false;
33}
34
35bool Type::isObjectType() const {
36 if (isa<FunctionType>(CanonicalType))
37 return false;
38 else if (CanonicalType->isIncompleteType())
39 return false;
40 else
41 return true;
42}
43
44bool Type::isDerivedType() const {
45 switch (CanonicalType->getTypeClass()) {
46 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000047 case VariableArray:
48 case ConstantArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000049 case FunctionProto:
50 case FunctionNoProto:
51 case Reference:
52 return true;
53 case Tagged: {
54 const TagType *TT = cast<TagType>(CanonicalType);
55 const Decl::Kind Kind = TT->getDecl()->getKind();
56 return Kind == Decl::Struct || Kind == Decl::Union;
57 }
58 default:
59 return false;
60 }
61}
62
Chris Lattnerc8629632007-07-31 19:29:30 +000063bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000064 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000065 if (RT->getDecl()->getKind() == Decl::Struct)
66 return true;
67 return false;
68}
69bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000070 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000071 if (RT->getDecl()->getKind() == Decl::Union)
72 return true;
73 return false;
74}
Chris Lattnerc8629632007-07-31 19:29:30 +000075
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000076bool Type::isComplexType() const {
77 return isa<ComplexType>(CanonicalType);
78}
79
Chris Lattnerdea61462007-10-29 03:41:11 +000080/// getDesugaredType - Return the specified type with any "sugar" removed from
81/// type type. This takes off typedefs, typeof's etc. If the outer level of
82/// the type is already concrete, it returns it unmodified. This is similar
83/// to getting the canonical type, but it doesn't remove *all* typedefs. For
84/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
85/// concrete.
86const Type *Type::getDesugaredType() const {
87 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
88 return TDT->LookThroughTypedefs().getTypePtr();
89 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
90 return TOE->getUnderlyingExpr()->getType().getTypePtr();
91 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
92 return TOT->getUnderlyingType().getTypePtr();
93 return this;
94}
95
96
Steve Naroff77878cc2007-08-27 04:08:11 +000097const BuiltinType *Type::getAsBuiltinType() const {
98 // If this is directly a builtin type, return it.
99 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
100 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000101
102 // If the canonical form of this type isn't a builtin type, reject it.
103 if (!isa<BuiltinType>(CanonicalType))
104 return 0;
105
Steve Naroff77878cc2007-08-27 04:08:11 +0000106 // If this is a typedef for a builtin type, strip the typedef off without
107 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000108 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000109}
110
Chris Lattnerc8629632007-07-31 19:29:30 +0000111const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000112 // If this is directly a function type, return it.
113 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
114 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +0000115
Chris Lattnerdea61462007-10-29 03:41:11 +0000116 // If the canonical form of this type isn't the right kind, reject it.
117 if (!isa<FunctionType>(CanonicalType))
118 return 0;
119
Steve Naroff7064f5c2007-07-26 18:32:01 +0000120 // If this is a typedef for a function type, strip the typedef off without
121 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000122 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000123}
124
Chris Lattnerbefee482007-07-31 16:53:04 +0000125const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000126 // If this is directly a pointer type, return it.
127 if (const PointerType *PTy = dyn_cast<PointerType>(this))
128 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000129
Chris Lattnerdea61462007-10-29 03:41:11 +0000130 // If the canonical form of this type isn't the right kind, reject it.
131 if (!isa<PointerType>(CanonicalType))
132 return 0;
133
Chris Lattnera2c77672007-07-16 22:05:22 +0000134 // If this is a typedef for a pointer type, strip the typedef off without
135 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000136 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000139const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000140 // If this is directly a reference type, return it.
141 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
142 return RTy;
143
Chris Lattnerdea61462007-10-29 03:41:11 +0000144 // If the canonical form of this type isn't the right kind, reject it.
145 if (!isa<ReferenceType>(CanonicalType))
146 return 0;
147
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000148 // If this is a typedef for a reference type, strip the typedef off without
149 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000150 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000151}
152
Chris Lattnerc8629632007-07-31 19:29:30 +0000153const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000154 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000155 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
156 return ATy;
157
Chris Lattnerdea61462007-10-29 03:41:11 +0000158 // If the canonical form of this type isn't the right kind, reject it.
159 if (!isa<ArrayType>(CanonicalType))
160 return 0;
161
Steve Naroff700204c2007-07-24 21:46:40 +0000162 // If this is a typedef for an array type, strip the typedef off without
163 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000164 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000165}
166
Steve Naroffd7444aa2007-08-31 17:20:07 +0000167const ConstantArrayType *Type::getAsConstantArrayType() const {
168 // If this is directly a constant array type, return it.
169 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
170 return ATy;
171
Chris Lattnerdea61462007-10-29 03:41:11 +0000172 // If the canonical form of this type isn't the right kind, reject it.
173 if (!isa<ConstantArrayType>(CanonicalType))
174 return 0;
175
176 // If this is a typedef for a constant array type, strip the typedef off
177 // without losing all typedef information.
178 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000179}
180
181const VariableArrayType *Type::getAsVariableArrayType() const {
182 // If this is directly a variable array type, return it.
183 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
184 return ATy;
185
Chris Lattnerdea61462007-10-29 03:41:11 +0000186 // If the canonical form of this type isn't the right kind, reject it.
187 if (!isa<VariableArrayType>(CanonicalType))
188 return 0;
189
190 // If this is a typedef for a variable array type, strip the typedef off
191 // without losing all typedef information.
192 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000193}
194
195/// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
196/// types that have a non-constant expression. This does not include "[]".
197bool Type::isVariablyModifiedType() const {
198 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
199 if (VAT->getSizeExpr())
200 return true;
201 }
202 return false;
203}
204
205const VariableArrayType *Type::getAsVariablyModifiedType() const {
206 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
207 if (VAT->getSizeExpr())
208 return VAT;
209 }
210 return 0;
211}
212
Chris Lattnerc8629632007-07-31 19:29:30 +0000213const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000214 // If this is directly a reference type, return it.
215 if (const RecordType *RTy = dyn_cast<RecordType>(this))
216 return RTy;
217
Chris Lattnerdea61462007-10-29 03:41:11 +0000218 // If the canonical form of this type isn't the right kind, reject it.
219 if (!isa<RecordType>(CanonicalType))
220 return 0;
221
222 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000223 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000224 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000225}
226
Chris Lattnerc8629632007-07-31 19:29:30 +0000227const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000228 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000229 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
230 if (RT->getDecl()->getKind() == Decl::Struct)
231 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000232 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000233
234 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000235 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000236 if (RT->getDecl()->getKind() != Decl::Struct)
237 return 0;
238
239 // If this is a typedef for a structure type, strip the typedef off without
240 // losing all typedef information.
241 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000243 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Chris Lattnerc8629632007-07-31 19:29:30 +0000246const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000247 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000248 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
249 if (RT->getDecl()->getKind() == Decl::Union)
250 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000251 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000252 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000253 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000254 if (RT->getDecl()->getKind() != Decl::Union)
255 return 0;
256
257 // If this is a typedef for a union type, strip the typedef off without
258 // losing all typedef information.
259 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000261 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000264const ComplexType *Type::getAsComplexType() const {
265 // Are we directly a complex type?
266 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
267 return CTy;
268
Chris Lattnerdea61462007-10-29 03:41:11 +0000269 // If the canonical form of this type isn't the right kind, reject it.
270 if (!isa<ComplexType>(CanonicalType))
271 return 0;
272
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000273 // If this is a typedef for a complex type, strip the typedef off without
274 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000275 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000276}
277
Chris Lattnerc8629632007-07-31 19:29:30 +0000278const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000279 // Are we directly a vector type?
280 if (const VectorType *VTy = dyn_cast<VectorType>(this))
281 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000282
Chris Lattnerdea61462007-10-29 03:41:11 +0000283 // If the canonical form of this type isn't the right kind, reject it.
284 if (!isa<VectorType>(CanonicalType))
285 return 0;
286
Chris Lattnera2c77672007-07-16 22:05:22 +0000287 // If this is a typedef for a vector type, strip the typedef off without
288 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000289 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000290}
291
Chris Lattnerc8629632007-07-31 19:29:30 +0000292const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000293 // Are we directly an OpenCU vector type?
294 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
295 return VTy;
296
Chris Lattnerdea61462007-10-29 03:41:11 +0000297 // If the canonical form of this type isn't the right kind, reject it.
298 if (!isa<OCUVectorType>(CanonicalType))
299 return 0;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000300
Chris Lattnerdea61462007-10-29 03:41:11 +0000301 // If this is a typedef for an ocuvector type, strip the typedef off without
302 // losing all typedef information.
303 return getDesugaredType()->getAsOCUVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000304}
305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306bool Type::isIntegerType() const {
307 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
308 return BT->getKind() >= BuiltinType::Bool &&
309 BT->getKind() <= BuiltinType::LongLong;
310 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
311 if (TT->getDecl()->getKind() == Decl::Enum)
312 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000313 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
314 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 return false;
316}
317
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000318bool Type::isIntegralType() const {
319 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
320 return BT->getKind() >= BuiltinType::Bool &&
321 BT->getKind() <= BuiltinType::LongLong;
322 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
323 if (TT->getDecl()->getKind() == Decl::Enum)
324 return true;
325 return false;
326}
327
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000328bool Type::isEnumeralType() const {
329 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
330 return TT->getDecl()->getKind() == Decl::Enum;
331 return false;
332}
333
334bool Type::isBooleanType() const {
335 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
336 return BT->getKind() == BuiltinType::Bool;
337 return false;
338}
339
340bool Type::isCharType() const {
341 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
342 return BT->getKind() == BuiltinType::Char_U ||
343 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000344 BT->getKind() == BuiltinType::Char_S ||
345 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000346 return false;
347}
348
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000349/// isSignedIntegerType - Return true if this is an integer type that is
350/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
351/// an enum decl which has a signed representation, or a vector of signed
352/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000353bool Type::isSignedIntegerType() const {
354 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
355 return BT->getKind() >= BuiltinType::Char_S &&
356 BT->getKind() <= BuiltinType::LongLong;
357 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000358
359 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
360 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
361 return ED->getIntegerType()->isSignedIntegerType();
362
Steve Naroffc63b96a2007-07-12 21:46:55 +0000363 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
364 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 return false;
366}
367
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000368/// isUnsignedIntegerType - Return true if this is an integer type that is
369/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
370/// decl which has an unsigned representation, or a vector of unsigned integer
371/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000372bool Type::isUnsignedIntegerType() const {
373 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
374 return BT->getKind() >= BuiltinType::Bool &&
375 BT->getKind() <= BuiltinType::ULongLong;
376 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000377
378 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
379 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
380 return ED->getIntegerType()->isUnsignedIntegerType();
381
Steve Naroffc63b96a2007-07-12 21:46:55 +0000382 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
383 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 return false;
385}
386
387bool Type::isFloatingType() const {
388 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
389 return BT->getKind() >= BuiltinType::Float &&
390 BT->getKind() <= BuiltinType::LongDouble;
391 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000392 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000393 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
394 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 return false;
396}
397
398bool Type::isRealFloatingType() const {
399 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
400 return BT->getKind() >= BuiltinType::Float &&
401 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000402 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
403 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 return false;
405}
406
407bool Type::isRealType() const {
408 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
409 return BT->getKind() >= BuiltinType::Bool &&
410 BT->getKind() <= BuiltinType::LongDouble;
411 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
412 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000413 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
414 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 return false;
416}
417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418bool Type::isArithmeticType() const {
419 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
420 return BT->getKind() != BuiltinType::Void;
421 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
422 if (TT->getDecl()->getKind() == Decl::Enum)
423 return true;
424 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
425}
426
427bool Type::isScalarType() const {
428 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
429 return BT->getKind() != BuiltinType::Void;
430 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
431 if (TT->getDecl()->getKind() == Decl::Enum)
432 return true;
433 return false;
434 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000435 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000436 isa<VectorType>(CanonicalType) ||
437 isa<ObjcQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000438}
439
440bool Type::isAggregateType() const {
441 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
442 if (TT->getDecl()->getKind() == Decl::Struct)
443 return true;
444 return false;
445 }
Steve Narofffb22d962007-08-30 01:06:46 +0000446 return CanonicalType->getTypeClass() == ConstantArray ||
447 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000448}
449
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000450/// isConstantSizeType - Return true if this is not a variable sized type,
451/// according to the rules of C99 6.7.5p3. It is not legal to call this on
452/// incomplete types.
453bool Type::isConstantSizeType(ASTContext &Ctx) const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000454 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000455 // The VAT must have a size, as it is known to be complete.
456 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000457}
458
459/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
460/// - a type that can describe objects, but which lacks information needed to
461/// determine its size.
462bool Type::isIncompleteType() const {
463 switch (CanonicalType->getTypeClass()) {
464 default: return false;
465 case Builtin:
466 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
467 // be completed.
468 return isVoidType();
469 case Tagged:
470 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
471 // forward declaration, but not a full definition (C99 6.2.5p22).
472 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000473 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000475 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 }
477}
478
479bool Type::isPromotableIntegerType() const {
480 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
481 if (!BT) return false;
482 switch (BT->getKind()) {
483 case BuiltinType::Bool:
484 case BuiltinType::Char_S:
485 case BuiltinType::Char_U:
486 case BuiltinType::SChar:
487 case BuiltinType::UChar:
488 case BuiltinType::Short:
489 case BuiltinType::UShort:
490 return true;
491 default:
492 return false;
493 }
494}
495
496const char *BuiltinType::getName() const {
497 switch (getKind()) {
498 default: assert(0 && "Unknown builtin type!");
499 case Void: return "void";
500 case Bool: return "_Bool";
501 case Char_S: return "char";
502 case Char_U: return "char";
503 case SChar: return "signed char";
504 case Short: return "short";
505 case Int: return "int";
506 case Long: return "long";
507 case LongLong: return "long long";
508 case UChar: return "unsigned char";
509 case UShort: return "unsigned short";
510 case UInt: return "unsigned int";
511 case ULong: return "unsigned long";
512 case ULongLong: return "unsigned long long";
513 case Float: return "float";
514 case Double: return "double";
515 case LongDouble: return "long double";
516 }
517}
518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000520 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 unsigned NumArgs, bool isVariadic) {
522 ID.AddPointer(Result.getAsOpaquePtr());
523 for (unsigned i = 0; i != NumArgs; ++i)
524 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
525 ID.AddInteger(isVariadic);
526}
527
528void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000529 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000530}
531
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000532void ObjcQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000533 ObjcProtocolDecl **protocols,
534 unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000535 for (unsigned i = 0; i != NumProtocols; i++)
536 ID.AddPointer(protocols[i]);
537}
538
539void ObjcQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000540 Profile(ID, &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000541}
542
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000543void ObjcQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
544 ObjcProtocolDecl **protocols,
545 unsigned NumProtocols) {
546 for (unsigned i = 0; i != NumProtocols; i++)
547 ID.AddPointer(protocols[i]);
548}
549
550void ObjcQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
551 Profile(ID, &Protocols[0], getNumProtocols());
552}
553
Chris Lattnera2c77672007-07-16 22:05:22 +0000554/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
555/// potentially looking through *all* consequtive typedefs. This returns the
556/// sum of the type qualifiers, so if you have:
557/// typedef const int A;
558/// typedef volatile A B;
559/// looking through the typedefs for B will give you "const volatile A".
560///
561QualType TypedefType::LookThroughTypedefs() const {
562 // Usually, there is only a single level of typedefs, be fast in that case.
563 QualType FirstType = getDecl()->getUnderlyingType();
564 if (!isa<TypedefType>(FirstType))
565 return FirstType;
566
567 // Otherwise, do the fully general loop.
568 unsigned TypeQuals = 0;
569 const TypedefType *TDT = this;
570 while (1) {
571 QualType CurType = TDT->getDecl()->getUnderlyingType();
572 TypeQuals |= CurType.getQualifiers();
573
574 TDT = dyn_cast<TypedefType>(CurType);
575 if (TDT == 0)
576 return QualType(CurType.getTypePtr(), TypeQuals);
577 }
578}
Reid Spencer5f016e22007-07-11 17:01:13 +0000579
580bool RecordType::classof(const Type *T) {
581 if (const TagType *TT = dyn_cast<TagType>(T))
582 return isa<RecordDecl>(TT->getDecl());
583 return false;
584}
585
586
587//===----------------------------------------------------------------------===//
588// Type Printing
589//===----------------------------------------------------------------------===//
590
591void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000592 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 getAsStringInternal(R);
594 if (msg)
595 fprintf(stderr, "%s: %s\n", msg, R.c_str());
596 else
597 fprintf(stderr, "%s\n", R.c_str());
598}
599
600static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
601 // Note: funkiness to ensure we get a space only between quals.
602 bool NonePrinted = true;
603 if (TypeQuals & QualType::Const)
604 S += "const", NonePrinted = false;
605 if (TypeQuals & QualType::Volatile)
606 S += (NonePrinted+" volatile"), NonePrinted = false;
607 if (TypeQuals & QualType::Restrict)
608 S += (NonePrinted+" restrict"), NonePrinted = false;
609}
610
611void QualType::getAsStringInternal(std::string &S) const {
612 if (isNull()) {
613 S += "NULL TYPE\n";
614 return;
615 }
616
617 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000618 unsigned TQ = getQualifiers();
619 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 std::string TQS;
621 AppendTypeQualList(TQS, TQ);
622 if (!S.empty())
623 S = TQS + ' ' + S;
624 else
625 S = TQS;
626 }
627
628 getTypePtr()->getAsStringInternal(S);
629}
630
631void BuiltinType::getAsStringInternal(std::string &S) const {
632 if (S.empty()) {
633 S = getName();
634 } else {
635 // Prefix the basic type, e.g. 'int X'.
636 S = ' ' + S;
637 S = getName() + S;
638 }
639}
640
641void ComplexType::getAsStringInternal(std::string &S) const {
642 ElementType->getAsStringInternal(S);
643 S = "_Complex " + S;
644}
645
646void PointerType::getAsStringInternal(std::string &S) const {
647 S = '*' + S;
648
649 // Handle things like 'int (*A)[4];' correctly.
650 // FIXME: this should include vectors, but vectors use attributes I guess.
651 if (isa<ArrayType>(PointeeType.getTypePtr()))
652 S = '(' + S + ')';
653
654 PointeeType.getAsStringInternal(S);
655}
656
657void ReferenceType::getAsStringInternal(std::string &S) const {
658 S = '&' + S;
659
660 // Handle things like 'int (&A)[4];' correctly.
661 // FIXME: this should include vectors, but vectors use attributes I guess.
662 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
663 S = '(' + S + ')';
664
665 ReferenceeType.getAsStringInternal(S);
666}
667
Steve Narofffb22d962007-08-30 01:06:46 +0000668void ConstantArrayType::getAsStringInternal(std::string &S) const {
669 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000670 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000671 S += ']';
672
673 getElementType().getAsStringInternal(S);
674}
675
676void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 S += '[';
678
Steve Naroffc9406122007-08-30 18:10:14 +0000679 if (getIndexTypeQualifier()) {
680 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 S += ' ';
682 }
683
Steve Naroffc9406122007-08-30 18:10:14 +0000684 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000686 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 S += '*';
688
Steve Narofffb22d962007-08-30 01:06:46 +0000689 if (getSizeExpr()) {
690 std::ostringstream s;
691 getSizeExpr()->printPretty(s);
692 S += s.str();
693 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 S += ']';
695
Steve Narofffb22d962007-08-30 01:06:46 +0000696 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000697}
698
699void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000700 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000701 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000703 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 ElementType.getAsStringInternal(S);
705}
706
Steve Naroff31a45842007-07-28 23:10:27 +0000707void OCUVectorType::getAsStringInternal(std::string &S) const {
708 S += " __attribute__((ocu_vector_type(";
709 S += llvm::utostr_32(NumElements);
710 S += ")))";
711 ElementType.getAsStringInternal(S);
712}
713
Steve Naroffd1861fd2007-07-31 12:34:36 +0000714void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000715 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
716 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000717 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000718 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000719 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000720}
721
Steve Naroff363bcff2007-08-01 23:45:51 +0000722void TypeOfType::getAsStringInternal(std::string &InnerString) const {
723 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
724 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000725 std::string Tmp;
726 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000727 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000728}
729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
731 // If needed for precedence reasons, wrap the inner part in grouping parens.
732 if (!S.empty())
733 S = "(" + S + ")";
734
735 S += "()";
736 getResultType().getAsStringInternal(S);
737}
738
739void FunctionTypeProto::getAsStringInternal(std::string &S) const {
740 // If needed for precedence reasons, wrap the inner part in grouping parens.
741 if (!S.empty())
742 S = "(" + S + ")";
743
744 S += "(";
745 std::string Tmp;
746 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
747 if (i) S += ", ";
748 getArgType(i).getAsStringInternal(Tmp);
749 S += Tmp;
750 Tmp.clear();
751 }
752
753 if (isVariadic()) {
754 if (getNumArgs())
755 S += ", ";
756 S += "...";
757 } else if (getNumArgs() == 0) {
758 // Do not emit int() if we have a proto, emit 'int(void)'.
759 S += "void";
760 }
761
762 S += ")";
763 getResultType().getAsStringInternal(S);
764}
765
766
767void TypedefType::getAsStringInternal(std::string &InnerString) const {
768 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
769 InnerString = ' ' + InnerString;
770 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
771}
772
Steve Naroff3536b442007-09-06 21:24:23 +0000773void ObjcInterfaceType::getAsStringInternal(std::string &InnerString) const {
774 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
775 InnerString = ' ' + InnerString;
776 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
777}
778
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000779void ObjcQualifiedInterfaceType::getAsStringInternal(
780 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000781 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
782 InnerString = ' ' + InnerString;
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000783 std::string ObjcQIString = getDecl()->getName();
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000784 ObjcQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000785 int num = getNumProtocols();
786 for (int i = 0; i < num; i++) {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000787 ObjcQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000788 if (i < num-1)
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000789 ObjcQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000790 }
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000791 ObjcQIString += '>';
792 InnerString = ObjcQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000793}
794
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000795void ObjcQualifiedIdType::getAsStringInternal(
796 std::string &InnerString) const {
797 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
798 InnerString = ' ' + InnerString;
799 std::string ObjcQIString = "id";
800 ObjcQIString += '<';
801 int num = getNumProtocols();
802 for (int i = 0; i < num; i++) {
803 ObjcQIString += getProtocols(i)->getName();
804 if (i < num-1)
805 ObjcQIString += ',';
806 }
807 ObjcQIString += '>';
808 InnerString = ObjcQIString + InnerString;
809}
810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811void TagType::getAsStringInternal(std::string &InnerString) const {
812 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
813 InnerString = ' ' + InnerString;
814
815 const char *Kind = getDecl()->getKindName();
816 const char *ID;
817 if (const IdentifierInfo *II = getDecl()->getIdentifier())
818 ID = II->getName();
819 else
820 ID = "<anonymous>";
821
822 InnerString = std::string(Kind) + " " + ID + InnerString;
823}