blob: e1d20831fcaff19dc663ec60c680627a3b5f3180 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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) ||
436 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000437}
438
439bool Type::isAggregateType() const {
440 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
441 if (TT->getDecl()->getKind() == Decl::Struct)
442 return true;
443 return false;
444 }
Steve Narofffb22d962007-08-30 01:06:46 +0000445 return CanonicalType->getTypeClass() == ConstantArray ||
446 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000447}
448
449// The only variable size types are auto arrays within a function. Structures
450// cannot contain a VLA member. They can have a flexible array member, however
451// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000452bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Steve Narofffb22d962007-08-30 01:06:46 +0000453 if (isa<VariableArrayType>(CanonicalType))
454 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 return true;
456}
457
458/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
459/// - a type that can describe objects, but which lacks information needed to
460/// determine its size.
461bool Type::isIncompleteType() const {
462 switch (CanonicalType->getTypeClass()) {
463 default: return false;
464 case Builtin:
465 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
466 // be completed.
467 return isVoidType();
468 case Tagged:
469 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
470 // forward declaration, but not a full definition (C99 6.2.5p22).
471 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000472 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000474 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 }
476}
477
478bool Type::isPromotableIntegerType() const {
479 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
480 if (!BT) return false;
481 switch (BT->getKind()) {
482 case BuiltinType::Bool:
483 case BuiltinType::Char_S:
484 case BuiltinType::Char_U:
485 case BuiltinType::SChar:
486 case BuiltinType::UChar:
487 case BuiltinType::Short:
488 case BuiltinType::UShort:
489 return true;
490 default:
491 return false;
492 }
493}
494
495const char *BuiltinType::getName() const {
496 switch (getKind()) {
497 default: assert(0 && "Unknown builtin type!");
498 case Void: return "void";
499 case Bool: return "_Bool";
500 case Char_S: return "char";
501 case Char_U: return "char";
502 case SChar: return "signed char";
503 case Short: return "short";
504 case Int: return "int";
505 case Long: return "long";
506 case LongLong: return "long long";
507 case UChar: return "unsigned char";
508 case UShort: return "unsigned short";
509 case UInt: return "unsigned int";
510 case ULong: return "unsigned long";
511 case ULongLong: return "unsigned long long";
512 case Float: return "float";
513 case Double: return "double";
514 case LongDouble: return "long double";
515 }
516}
517
Reid Spencer5f016e22007-07-11 17:01:13 +0000518void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000519 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 unsigned NumArgs, bool isVariadic) {
521 ID.AddPointer(Result.getAsOpaquePtr());
522 for (unsigned i = 0; i != NumArgs; ++i)
523 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
524 ID.AddInteger(isVariadic);
525}
526
527void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000528 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000529}
530
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000531void ObjcQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
532 ObjcInterfaceType *interfaceType,
533 ObjcProtocolDecl **protocols,
534 unsigned NumProtocols) {
535 ID.AddPointer(interfaceType);
536 for (unsigned i = 0; i != NumProtocols; i++)
537 ID.AddPointer(protocols[i]);
538}
539
540void ObjcQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
541 Profile(ID, getInterfaceType(), &Protocols[0], getNumProtocols());
542}
543
Chris Lattnera2c77672007-07-16 22:05:22 +0000544/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
545/// potentially looking through *all* consequtive typedefs. This returns the
546/// sum of the type qualifiers, so if you have:
547/// typedef const int A;
548/// typedef volatile A B;
549/// looking through the typedefs for B will give you "const volatile A".
550///
551QualType TypedefType::LookThroughTypedefs() const {
552 // Usually, there is only a single level of typedefs, be fast in that case.
553 QualType FirstType = getDecl()->getUnderlyingType();
554 if (!isa<TypedefType>(FirstType))
555 return FirstType;
556
557 // Otherwise, do the fully general loop.
558 unsigned TypeQuals = 0;
559 const TypedefType *TDT = this;
560 while (1) {
561 QualType CurType = TDT->getDecl()->getUnderlyingType();
562 TypeQuals |= CurType.getQualifiers();
563
564 TDT = dyn_cast<TypedefType>(CurType);
565 if (TDT == 0)
566 return QualType(CurType.getTypePtr(), TypeQuals);
567 }
568}
Reid Spencer5f016e22007-07-11 17:01:13 +0000569
570bool RecordType::classof(const Type *T) {
571 if (const TagType *TT = dyn_cast<TagType>(T))
572 return isa<RecordDecl>(TT->getDecl());
573 return false;
574}
575
576
577//===----------------------------------------------------------------------===//
578// Type Printing
579//===----------------------------------------------------------------------===//
580
581void QualType::dump(const char *msg) const {
582 std::string R = "foo";
583 getAsStringInternal(R);
584 if (msg)
585 fprintf(stderr, "%s: %s\n", msg, R.c_str());
586 else
587 fprintf(stderr, "%s\n", R.c_str());
588}
589
590static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
591 // Note: funkiness to ensure we get a space only between quals.
592 bool NonePrinted = true;
593 if (TypeQuals & QualType::Const)
594 S += "const", NonePrinted = false;
595 if (TypeQuals & QualType::Volatile)
596 S += (NonePrinted+" volatile"), NonePrinted = false;
597 if (TypeQuals & QualType::Restrict)
598 S += (NonePrinted+" restrict"), NonePrinted = false;
599}
600
601void QualType::getAsStringInternal(std::string &S) const {
602 if (isNull()) {
603 S += "NULL TYPE\n";
604 return;
605 }
606
607 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000608 unsigned TQ = getQualifiers();
609 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 std::string TQS;
611 AppendTypeQualList(TQS, TQ);
612 if (!S.empty())
613 S = TQS + ' ' + S;
614 else
615 S = TQS;
616 }
617
618 getTypePtr()->getAsStringInternal(S);
619}
620
621void BuiltinType::getAsStringInternal(std::string &S) const {
622 if (S.empty()) {
623 S = getName();
624 } else {
625 // Prefix the basic type, e.g. 'int X'.
626 S = ' ' + S;
627 S = getName() + S;
628 }
629}
630
631void ComplexType::getAsStringInternal(std::string &S) const {
632 ElementType->getAsStringInternal(S);
633 S = "_Complex " + S;
634}
635
636void PointerType::getAsStringInternal(std::string &S) const {
637 S = '*' + S;
638
639 // Handle things like 'int (*A)[4];' correctly.
640 // FIXME: this should include vectors, but vectors use attributes I guess.
641 if (isa<ArrayType>(PointeeType.getTypePtr()))
642 S = '(' + S + ')';
643
644 PointeeType.getAsStringInternal(S);
645}
646
647void ReferenceType::getAsStringInternal(std::string &S) const {
648 S = '&' + S;
649
650 // Handle things like 'int (&A)[4];' correctly.
651 // FIXME: this should include vectors, but vectors use attributes I guess.
652 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
653 S = '(' + S + ')';
654
655 ReferenceeType.getAsStringInternal(S);
656}
657
Steve Narofffb22d962007-08-30 01:06:46 +0000658void ConstantArrayType::getAsStringInternal(std::string &S) const {
659 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000660 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000661 S += ']';
662
663 getElementType().getAsStringInternal(S);
664}
665
666void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 S += '[';
668
Steve Naroffc9406122007-08-30 18:10:14 +0000669 if (getIndexTypeQualifier()) {
670 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 S += ' ';
672 }
673
Steve Naroffc9406122007-08-30 18:10:14 +0000674 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000676 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 S += '*';
678
Steve Narofffb22d962007-08-30 01:06:46 +0000679 if (getSizeExpr()) {
680 std::ostringstream s;
681 getSizeExpr()->printPretty(s);
682 S += s.str();
683 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 S += ']';
685
Steve Narofffb22d962007-08-30 01:06:46 +0000686 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000687}
688
689void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000690 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000691 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000693 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 ElementType.getAsStringInternal(S);
695}
696
Steve Naroff31a45842007-07-28 23:10:27 +0000697void OCUVectorType::getAsStringInternal(std::string &S) const {
698 S += " __attribute__((ocu_vector_type(";
699 S += llvm::utostr_32(NumElements);
700 S += ")))";
701 ElementType.getAsStringInternal(S);
702}
703
Steve Naroffd1861fd2007-07-31 12:34:36 +0000704void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000705 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
706 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000707 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000708 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000709 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000710}
711
Steve Naroff363bcff2007-08-01 23:45:51 +0000712void TypeOfType::getAsStringInternal(std::string &InnerString) const {
713 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
714 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000715 std::string Tmp;
716 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000717 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000718}
719
Reid Spencer5f016e22007-07-11 17:01:13 +0000720void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
721 // If needed for precedence reasons, wrap the inner part in grouping parens.
722 if (!S.empty())
723 S = "(" + S + ")";
724
725 S += "()";
726 getResultType().getAsStringInternal(S);
727}
728
729void FunctionTypeProto::getAsStringInternal(std::string &S) const {
730 // If needed for precedence reasons, wrap the inner part in grouping parens.
731 if (!S.empty())
732 S = "(" + S + ")";
733
734 S += "(";
735 std::string Tmp;
736 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
737 if (i) S += ", ";
738 getArgType(i).getAsStringInternal(Tmp);
739 S += Tmp;
740 Tmp.clear();
741 }
742
743 if (isVariadic()) {
744 if (getNumArgs())
745 S += ", ";
746 S += "...";
747 } else if (getNumArgs() == 0) {
748 // Do not emit int() if we have a proto, emit 'int(void)'.
749 S += "void";
750 }
751
752 S += ")";
753 getResultType().getAsStringInternal(S);
754}
755
756
757void TypedefType::getAsStringInternal(std::string &InnerString) const {
758 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
759 InnerString = ' ' + InnerString;
760 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
761}
762
Steve Naroff3536b442007-09-06 21:24:23 +0000763void ObjcInterfaceType::getAsStringInternal(std::string &InnerString) const {
764 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
765 InnerString = ' ' + InnerString;
766 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
767}
768
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000769void ObjcQualifiedInterfaceType::getAsStringInternal(
770 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000771 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
772 InnerString = ' ' + InnerString;
773 std::string ObjcQIString = getInterfaceType()->getDecl()->getName();
774 ObjcQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000775 int num = getNumProtocols();
776 for (int i = 0; i < num; i++) {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000777 ObjcQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000778 if (i < num-1)
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000779 ObjcQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000780 }
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000781 ObjcQIString += '>';
782 InnerString = ObjcQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000783}
784
Reid Spencer5f016e22007-07-11 17:01:13 +0000785void TagType::getAsStringInternal(std::string &InnerString) const {
786 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
787 InnerString = ' ' + InnerString;
788
789 const char *Kind = getDecl()->getKindName();
790 const char *ID;
791 if (const IdentifierInfo *II = getDecl()->getIdentifier())
792 ID = II->getName();
793 else
794 ID = "<anonymous>";
795
796 InnerString = std::string(Kind) + " " + ID + InnerString;
797}