blob: ccd4630843d7496f3bf2f4ec60d7d88d56a88e62 [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 Lattnerd52a4572007-12-18 07:03:30 +0000452bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *Loc) const {
453 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
454 if (const VariableArrayType *VAT =dyn_cast<VariableArrayType>(CanonicalType)){
455 // The VAT must have a size, as it is known to be complete.
456 if (Loc) *Loc = VAT->getSizeExpr()->getLocStart();
Steve Narofffb22d962007-08-30 01:06:46 +0000457 return false;
Chris Lattnerd52a4572007-12-18 07:03:30 +0000458 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 return true;
460}
461
462/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
463/// - a type that can describe objects, but which lacks information needed to
464/// determine its size.
465bool Type::isIncompleteType() const {
466 switch (CanonicalType->getTypeClass()) {
467 default: return false;
468 case Builtin:
469 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
470 // be completed.
471 return isVoidType();
472 case Tagged:
473 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
474 // forward declaration, but not a full definition (C99 6.2.5p22).
475 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000476 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000478 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 }
480}
481
482bool Type::isPromotableIntegerType() const {
483 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
484 if (!BT) return false;
485 switch (BT->getKind()) {
486 case BuiltinType::Bool:
487 case BuiltinType::Char_S:
488 case BuiltinType::Char_U:
489 case BuiltinType::SChar:
490 case BuiltinType::UChar:
491 case BuiltinType::Short:
492 case BuiltinType::UShort:
493 return true;
494 default:
495 return false;
496 }
497}
498
499const char *BuiltinType::getName() const {
500 switch (getKind()) {
501 default: assert(0 && "Unknown builtin type!");
502 case Void: return "void";
503 case Bool: return "_Bool";
504 case Char_S: return "char";
505 case Char_U: return "char";
506 case SChar: return "signed char";
507 case Short: return "short";
508 case Int: return "int";
509 case Long: return "long";
510 case LongLong: return "long long";
511 case UChar: return "unsigned char";
512 case UShort: return "unsigned short";
513 case UInt: return "unsigned int";
514 case ULong: return "unsigned long";
515 case ULongLong: return "unsigned long long";
516 case Float: return "float";
517 case Double: return "double";
518 case LongDouble: return "long double";
519 }
520}
521
Reid Spencer5f016e22007-07-11 17:01:13 +0000522void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000523 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 unsigned NumArgs, bool isVariadic) {
525 ID.AddPointer(Result.getAsOpaquePtr());
526 for (unsigned i = 0; i != NumArgs; ++i)
527 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
528 ID.AddInteger(isVariadic);
529}
530
531void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000532 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000533}
534
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000535void ObjcQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000536 ObjcProtocolDecl **protocols,
537 unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000538 for (unsigned i = 0; i != NumProtocols; i++)
539 ID.AddPointer(protocols[i]);
540}
541
542void ObjcQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000543 Profile(ID, &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000544}
545
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000546void ObjcQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
547 ObjcProtocolDecl **protocols,
548 unsigned NumProtocols) {
549 for (unsigned i = 0; i != NumProtocols; i++)
550 ID.AddPointer(protocols[i]);
551}
552
553void ObjcQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
554 Profile(ID, &Protocols[0], getNumProtocols());
555}
556
Chris Lattnera2c77672007-07-16 22:05:22 +0000557/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
558/// potentially looking through *all* consequtive typedefs. This returns the
559/// sum of the type qualifiers, so if you have:
560/// typedef const int A;
561/// typedef volatile A B;
562/// looking through the typedefs for B will give you "const volatile A".
563///
564QualType TypedefType::LookThroughTypedefs() const {
565 // Usually, there is only a single level of typedefs, be fast in that case.
566 QualType FirstType = getDecl()->getUnderlyingType();
567 if (!isa<TypedefType>(FirstType))
568 return FirstType;
569
570 // Otherwise, do the fully general loop.
571 unsigned TypeQuals = 0;
572 const TypedefType *TDT = this;
573 while (1) {
574 QualType CurType = TDT->getDecl()->getUnderlyingType();
575 TypeQuals |= CurType.getQualifiers();
576
577 TDT = dyn_cast<TypedefType>(CurType);
578 if (TDT == 0)
579 return QualType(CurType.getTypePtr(), TypeQuals);
580 }
581}
Reid Spencer5f016e22007-07-11 17:01:13 +0000582
583bool RecordType::classof(const Type *T) {
584 if (const TagType *TT = dyn_cast<TagType>(T))
585 return isa<RecordDecl>(TT->getDecl());
586 return false;
587}
588
589
590//===----------------------------------------------------------------------===//
591// Type Printing
592//===----------------------------------------------------------------------===//
593
594void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000595 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 getAsStringInternal(R);
597 if (msg)
598 fprintf(stderr, "%s: %s\n", msg, R.c_str());
599 else
600 fprintf(stderr, "%s\n", R.c_str());
601}
602
603static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
604 // Note: funkiness to ensure we get a space only between quals.
605 bool NonePrinted = true;
606 if (TypeQuals & QualType::Const)
607 S += "const", NonePrinted = false;
608 if (TypeQuals & QualType::Volatile)
609 S += (NonePrinted+" volatile"), NonePrinted = false;
610 if (TypeQuals & QualType::Restrict)
611 S += (NonePrinted+" restrict"), NonePrinted = false;
612}
613
614void QualType::getAsStringInternal(std::string &S) const {
615 if (isNull()) {
616 S += "NULL TYPE\n";
617 return;
618 }
619
620 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000621 unsigned TQ = getQualifiers();
622 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 std::string TQS;
624 AppendTypeQualList(TQS, TQ);
625 if (!S.empty())
626 S = TQS + ' ' + S;
627 else
628 S = TQS;
629 }
630
631 getTypePtr()->getAsStringInternal(S);
632}
633
634void BuiltinType::getAsStringInternal(std::string &S) const {
635 if (S.empty()) {
636 S = getName();
637 } else {
638 // Prefix the basic type, e.g. 'int X'.
639 S = ' ' + S;
640 S = getName() + S;
641 }
642}
643
644void ComplexType::getAsStringInternal(std::string &S) const {
645 ElementType->getAsStringInternal(S);
646 S = "_Complex " + S;
647}
648
649void PointerType::getAsStringInternal(std::string &S) const {
650 S = '*' + S;
651
652 // Handle things like 'int (*A)[4];' correctly.
653 // FIXME: this should include vectors, but vectors use attributes I guess.
654 if (isa<ArrayType>(PointeeType.getTypePtr()))
655 S = '(' + S + ')';
656
657 PointeeType.getAsStringInternal(S);
658}
659
660void ReferenceType::getAsStringInternal(std::string &S) const {
661 S = '&' + S;
662
663 // Handle things like 'int (&A)[4];' correctly.
664 // FIXME: this should include vectors, but vectors use attributes I guess.
665 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
666 S = '(' + S + ')';
667
668 ReferenceeType.getAsStringInternal(S);
669}
670
Steve Narofffb22d962007-08-30 01:06:46 +0000671void ConstantArrayType::getAsStringInternal(std::string &S) const {
672 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000673 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000674 S += ']';
675
676 getElementType().getAsStringInternal(S);
677}
678
679void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 S += '[';
681
Steve Naroffc9406122007-08-30 18:10:14 +0000682 if (getIndexTypeQualifier()) {
683 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 S += ' ';
685 }
686
Steve Naroffc9406122007-08-30 18:10:14 +0000687 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000689 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 S += '*';
691
Steve Narofffb22d962007-08-30 01:06:46 +0000692 if (getSizeExpr()) {
693 std::ostringstream s;
694 getSizeExpr()->printPretty(s);
695 S += s.str();
696 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 S += ']';
698
Steve Narofffb22d962007-08-30 01:06:46 +0000699 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000700}
701
702void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000703 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000704 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000706 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 ElementType.getAsStringInternal(S);
708}
709
Steve Naroff31a45842007-07-28 23:10:27 +0000710void OCUVectorType::getAsStringInternal(std::string &S) const {
711 S += " __attribute__((ocu_vector_type(";
712 S += llvm::utostr_32(NumElements);
713 S += ")))";
714 ElementType.getAsStringInternal(S);
715}
716
Steve Naroffd1861fd2007-07-31 12:34:36 +0000717void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000718 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
719 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000720 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000721 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000722 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000723}
724
Steve Naroff363bcff2007-08-01 23:45:51 +0000725void TypeOfType::getAsStringInternal(std::string &InnerString) const {
726 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
727 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000728 std::string Tmp;
729 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000730 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000731}
732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
734 // If needed for precedence reasons, wrap the inner part in grouping parens.
735 if (!S.empty())
736 S = "(" + S + ")";
737
738 S += "()";
739 getResultType().getAsStringInternal(S);
740}
741
742void FunctionTypeProto::getAsStringInternal(std::string &S) const {
743 // If needed for precedence reasons, wrap the inner part in grouping parens.
744 if (!S.empty())
745 S = "(" + S + ")";
746
747 S += "(";
748 std::string Tmp;
749 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
750 if (i) S += ", ";
751 getArgType(i).getAsStringInternal(Tmp);
752 S += Tmp;
753 Tmp.clear();
754 }
755
756 if (isVariadic()) {
757 if (getNumArgs())
758 S += ", ";
759 S += "...";
760 } else if (getNumArgs() == 0) {
761 // Do not emit int() if we have a proto, emit 'int(void)'.
762 S += "void";
763 }
764
765 S += ")";
766 getResultType().getAsStringInternal(S);
767}
768
769
770void TypedefType::getAsStringInternal(std::string &InnerString) const {
771 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
772 InnerString = ' ' + InnerString;
773 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
774}
775
Steve Naroff3536b442007-09-06 21:24:23 +0000776void ObjcInterfaceType::getAsStringInternal(std::string &InnerString) const {
777 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
778 InnerString = ' ' + InnerString;
779 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
780}
781
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000782void ObjcQualifiedInterfaceType::getAsStringInternal(
783 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000784 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
785 InnerString = ' ' + InnerString;
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000786 std::string ObjcQIString = getDecl()->getName();
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000787 ObjcQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000788 int num = getNumProtocols();
789 for (int i = 0; i < num; i++) {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000790 ObjcQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000791 if (i < num-1)
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000792 ObjcQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000793 }
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000794 ObjcQIString += '>';
795 InnerString = ObjcQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000796}
797
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000798void ObjcQualifiedIdType::getAsStringInternal(
799 std::string &InnerString) const {
800 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
801 InnerString = ' ' + InnerString;
802 std::string ObjcQIString = "id";
803 ObjcQIString += '<';
804 int num = getNumProtocols();
805 for (int i = 0; i < num; i++) {
806 ObjcQIString += getProtocols(i)->getName();
807 if (i < num-1)
808 ObjcQIString += ',';
809 }
810 ObjcQIString += '>';
811 InnerString = ObjcQIString + InnerString;
812}
813
Reid Spencer5f016e22007-07-11 17:01:13 +0000814void TagType::getAsStringInternal(std::string &InnerString) const {
815 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
816 InnerString = ' ' + InnerString;
817
818 const char *Kind = getDecl()->getKindName();
819 const char *ID;
820 if (const IdentifierInfo *II = getDecl()->getIdentifier())
821 ID = II->getName();
822 else
823 ID = "<anonymous>";
824
825 InnerString = std::string(Kind) + " " + ID + InnerString;
826}