blob: bb3fd9d44b5a59877eab325ae9392ce1c9945c67 [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
Steve Naroff4cdec1c2008-01-15 01:41:59 +000080bool Type::isComplexIntegerType() const {
81 // Check for GCC complex integer extension.
82 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
83 return CT->getElementType()->isIntegerType();
84 return false;
85}
86
87const ComplexType *Type::getAsComplexIntegerType() const {
88 // Are we directly a complex type?
89 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
90 if (CTy->getElementType()->isIntegerType())
91 return CTy;
92 }
93 // If the canonical form of this type isn't the right kind, reject it.
94 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
95 if (!CTy || !CTy->getElementType()->isIntegerType())
96 return 0;
97
98 // If this is a typedef for a complex type, strip the typedef off without
99 // losing all typedef information.
100 return getDesugaredType()->getAsComplexIntegerType();
101}
102
Chris Lattnerdea61462007-10-29 03:41:11 +0000103/// getDesugaredType - Return the specified type with any "sugar" removed from
104/// type type. This takes off typedefs, typeof's etc. If the outer level of
105/// the type is already concrete, it returns it unmodified. This is similar
106/// to getting the canonical type, but it doesn't remove *all* typedefs. For
107/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
108/// concrete.
109const Type *Type::getDesugaredType() const {
110 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
111 return TDT->LookThroughTypedefs().getTypePtr();
112 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
113 return TOE->getUnderlyingExpr()->getType().getTypePtr();
114 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
115 return TOT->getUnderlyingType().getTypePtr();
116 return this;
117}
118
119
Steve Naroff77878cc2007-08-27 04:08:11 +0000120const BuiltinType *Type::getAsBuiltinType() const {
121 // If this is directly a builtin type, return it.
122 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
123 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000124
125 // If the canonical form of this type isn't a builtin type, reject it.
126 if (!isa<BuiltinType>(CanonicalType))
127 return 0;
128
Steve Naroff77878cc2007-08-27 04:08:11 +0000129 // If this is a typedef for a builtin type, strip the typedef off without
130 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000131 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000132}
133
Chris Lattnerc8629632007-07-31 19:29:30 +0000134const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000135 // If this is directly a function type, return it.
136 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
137 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +0000138
Chris Lattnerdea61462007-10-29 03:41:11 +0000139 // If the canonical form of this type isn't the right kind, reject it.
140 if (!isa<FunctionType>(CanonicalType))
141 return 0;
142
Steve Naroff7064f5c2007-07-26 18:32:01 +0000143 // If this is a typedef for a function type, strip the typedef off without
144 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000145 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000146}
147
Chris Lattnerbefee482007-07-31 16:53:04 +0000148const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000149 // If this is directly a pointer type, return it.
150 if (const PointerType *PTy = dyn_cast<PointerType>(this))
151 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000152
Chris Lattnerdea61462007-10-29 03:41:11 +0000153 // If the canonical form of this type isn't the right kind, reject it.
154 if (!isa<PointerType>(CanonicalType))
155 return 0;
156
Chris Lattnera2c77672007-07-16 22:05:22 +0000157 // If this is a typedef for a pointer type, strip the typedef off without
158 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000159 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000160}
161
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000162const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000163 // If this is directly a reference type, return it.
164 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
165 return RTy;
166
Chris Lattnerdea61462007-10-29 03:41:11 +0000167 // If the canonical form of this type isn't the right kind, reject it.
168 if (!isa<ReferenceType>(CanonicalType))
169 return 0;
170
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000171 // If this is a typedef for a reference type, strip the typedef off without
172 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000173 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Chris Lattnerc8629632007-07-31 19:29:30 +0000176const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000177 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000178 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
179 return ATy;
180
Chris Lattnerdea61462007-10-29 03:41:11 +0000181 // If the canonical form of this type isn't the right kind, reject it.
182 if (!isa<ArrayType>(CanonicalType))
183 return 0;
184
Steve Naroff700204c2007-07-24 21:46:40 +0000185 // If this is a typedef for an array type, strip the typedef off without
186 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000187 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000188}
189
Steve Naroffd7444aa2007-08-31 17:20:07 +0000190const ConstantArrayType *Type::getAsConstantArrayType() const {
191 // If this is directly a constant array type, return it.
192 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
193 return ATy;
194
Chris Lattnerdea61462007-10-29 03:41:11 +0000195 // If the canonical form of this type isn't the right kind, reject it.
196 if (!isa<ConstantArrayType>(CanonicalType))
197 return 0;
198
199 // If this is a typedef for a constant array type, strip the typedef off
200 // without losing all typedef information.
201 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000202}
203
204const VariableArrayType *Type::getAsVariableArrayType() const {
205 // If this is directly a variable array type, return it.
206 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
207 return ATy;
208
Chris Lattnerdea61462007-10-29 03:41:11 +0000209 // If the canonical form of this type isn't the right kind, reject it.
210 if (!isa<VariableArrayType>(CanonicalType))
211 return 0;
212
213 // If this is a typedef for a variable array type, strip the typedef off
214 // without losing all typedef information.
215 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000216}
217
218/// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
219/// types that have a non-constant expression. This does not include "[]".
220bool Type::isVariablyModifiedType() const {
221 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
222 if (VAT->getSizeExpr())
223 return true;
224 }
225 return false;
226}
227
228const VariableArrayType *Type::getAsVariablyModifiedType() const {
229 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
230 if (VAT->getSizeExpr())
231 return VAT;
232 }
233 return 0;
234}
235
Chris Lattnerc8629632007-07-31 19:29:30 +0000236const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000237 // If this is directly a reference type, return it.
238 if (const RecordType *RTy = dyn_cast<RecordType>(this))
239 return RTy;
240
Chris Lattnerdea61462007-10-29 03:41:11 +0000241 // If the canonical form of this type isn't the right kind, reject it.
242 if (!isa<RecordType>(CanonicalType))
243 return 0;
244
245 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000246 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000247 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000248}
249
Chris Lattnerc8629632007-07-31 19:29:30 +0000250const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000251 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000252 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
253 if (RT->getDecl()->getKind() == Decl::Struct)
254 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000255 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000256
257 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000258 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000259 if (RT->getDecl()->getKind() != Decl::Struct)
260 return 0;
261
262 // If this is a typedef for a structure type, strip the typedef off without
263 // losing all typedef information.
264 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000266 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000267}
268
Chris Lattnerc8629632007-07-31 19:29:30 +0000269const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000270 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000271 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
272 if (RT->getDecl()->getKind() == Decl::Union)
273 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000274 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000275 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000276 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000277 if (RT->getDecl()->getKind() != Decl::Union)
278 return 0;
279
280 // If this is a typedef for a union type, strip the typedef off without
281 // losing all typedef information.
282 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000284 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000285}
286
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000287const ComplexType *Type::getAsComplexType() const {
288 // Are we directly a complex type?
289 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
290 return CTy;
291
Chris Lattnerdea61462007-10-29 03:41:11 +0000292 // If the canonical form of this type isn't the right kind, reject it.
293 if (!isa<ComplexType>(CanonicalType))
294 return 0;
295
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000296 // If this is a typedef for a complex type, strip the typedef off without
297 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000298 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000299}
300
Chris Lattnerc8629632007-07-31 19:29:30 +0000301const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000302 // Are we directly a vector type?
303 if (const VectorType *VTy = dyn_cast<VectorType>(this))
304 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000305
Chris Lattnerdea61462007-10-29 03:41:11 +0000306 // If the canonical form of this type isn't the right kind, reject it.
307 if (!isa<VectorType>(CanonicalType))
308 return 0;
309
Chris Lattnera2c77672007-07-16 22:05:22 +0000310 // If this is a typedef for a vector type, strip the typedef off without
311 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000312 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000313}
314
Chris Lattnerc8629632007-07-31 19:29:30 +0000315const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000316 // Are we directly an OpenCU vector type?
317 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
318 return VTy;
319
Chris Lattnerdea61462007-10-29 03:41:11 +0000320 // If the canonical form of this type isn't the right kind, reject it.
321 if (!isa<OCUVectorType>(CanonicalType))
322 return 0;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000323
Chris Lattnerdea61462007-10-29 03:41:11 +0000324 // If this is a typedef for an ocuvector type, strip the typedef off without
325 // losing all typedef information.
326 return getDesugaredType()->getAsOCUVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000327}
328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329bool Type::isIntegerType() const {
330 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
331 return BT->getKind() >= BuiltinType::Bool &&
332 BT->getKind() <= BuiltinType::LongLong;
333 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
334 if (TT->getDecl()->getKind() == Decl::Enum)
335 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000336 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
337 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 return false;
339}
340
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000341bool Type::isIntegralType() const {
342 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
343 return BT->getKind() >= BuiltinType::Bool &&
344 BT->getKind() <= BuiltinType::LongLong;
345 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
346 if (TT->getDecl()->getKind() == Decl::Enum)
347 return true;
348 return false;
349}
350
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000351bool Type::isEnumeralType() const {
352 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
353 return TT->getDecl()->getKind() == Decl::Enum;
354 return false;
355}
356
357bool Type::isBooleanType() const {
358 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
359 return BT->getKind() == BuiltinType::Bool;
360 return false;
361}
362
363bool Type::isCharType() const {
364 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
365 return BT->getKind() == BuiltinType::Char_U ||
366 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000367 BT->getKind() == BuiltinType::Char_S ||
368 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000369 return false;
370}
371
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000372/// isSignedIntegerType - Return true if this is an integer type that is
373/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
374/// an enum decl which has a signed representation, or a vector of signed
375/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000376bool Type::isSignedIntegerType() const {
377 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
378 return BT->getKind() >= BuiltinType::Char_S &&
379 BT->getKind() <= BuiltinType::LongLong;
380 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000381
382 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
383 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
384 return ED->getIntegerType()->isSignedIntegerType();
385
Steve Naroffc63b96a2007-07-12 21:46:55 +0000386 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
387 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 return false;
389}
390
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000391/// isUnsignedIntegerType - Return true if this is an integer type that is
392/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
393/// decl which has an unsigned representation, or a vector of unsigned integer
394/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000395bool Type::isUnsignedIntegerType() const {
396 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
397 return BT->getKind() >= BuiltinType::Bool &&
398 BT->getKind() <= BuiltinType::ULongLong;
399 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000400
401 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
402 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
403 return ED->getIntegerType()->isUnsignedIntegerType();
404
Steve Naroffc63b96a2007-07-12 21:46:55 +0000405 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
406 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 return false;
408}
409
410bool Type::isFloatingType() const {
411 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
412 return BT->getKind() >= BuiltinType::Float &&
413 BT->getKind() <= BuiltinType::LongDouble;
414 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000415 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000416 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
417 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 return false;
419}
420
421bool Type::isRealFloatingType() const {
422 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
423 return BT->getKind() >= BuiltinType::Float &&
424 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000425 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
426 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 return false;
428}
429
430bool Type::isRealType() const {
431 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
432 return BT->getKind() >= BuiltinType::Bool &&
433 BT->getKind() <= BuiltinType::LongDouble;
434 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
435 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000436 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
437 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 return false;
439}
440
Reid Spencer5f016e22007-07-11 17:01:13 +0000441bool Type::isArithmeticType() const {
442 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
443 return BT->getKind() != BuiltinType::Void;
444 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
445 if (TT->getDecl()->getKind() == Decl::Enum)
446 return true;
447 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
448}
449
450bool Type::isScalarType() const {
451 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
452 return BT->getKind() != BuiltinType::Void;
453 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
454 if (TT->getDecl()->getKind() == Decl::Enum)
455 return true;
456 return false;
457 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000458 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000459 isa<VectorType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000460 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000461}
462
463bool Type::isAggregateType() const {
464 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
465 if (TT->getDecl()->getKind() == Decl::Struct)
466 return true;
467 return false;
468 }
Steve Narofffb22d962007-08-30 01:06:46 +0000469 return CanonicalType->getTypeClass() == ConstantArray ||
470 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000471}
472
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000473/// isConstantSizeType - Return true if this is not a variable sized type,
474/// according to the rules of C99 6.7.5p3. It is not legal to call this on
475/// incomplete types.
476bool Type::isConstantSizeType(ASTContext &Ctx) const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000477 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000478 // The VAT must have a size, as it is known to be complete.
479 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000480}
481
482/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
483/// - a type that can describe objects, but which lacks information needed to
484/// determine its size.
485bool Type::isIncompleteType() const {
486 switch (CanonicalType->getTypeClass()) {
487 default: return false;
488 case Builtin:
489 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
490 // be completed.
491 return isVoidType();
492 case Tagged:
493 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
494 // forward declaration, but not a full definition (C99 6.2.5p22).
495 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000496 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000498 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 }
500}
501
502bool Type::isPromotableIntegerType() const {
503 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
504 if (!BT) return false;
505 switch (BT->getKind()) {
506 case BuiltinType::Bool:
507 case BuiltinType::Char_S:
508 case BuiltinType::Char_U:
509 case BuiltinType::SChar:
510 case BuiltinType::UChar:
511 case BuiltinType::Short:
512 case BuiltinType::UShort:
513 return true;
514 default:
515 return false;
516 }
517}
518
519const char *BuiltinType::getName() const {
520 switch (getKind()) {
521 default: assert(0 && "Unknown builtin type!");
522 case Void: return "void";
523 case Bool: return "_Bool";
524 case Char_S: return "char";
525 case Char_U: return "char";
526 case SChar: return "signed char";
527 case Short: return "short";
528 case Int: return "int";
529 case Long: return "long";
530 case LongLong: return "long long";
531 case UChar: return "unsigned char";
532 case UShort: return "unsigned short";
533 case UInt: return "unsigned int";
534 case ULong: return "unsigned long";
535 case ULongLong: return "unsigned long long";
536 case Float: return "float";
537 case Double: return "double";
538 case LongDouble: return "long double";
539 }
540}
541
Reid Spencer5f016e22007-07-11 17:01:13 +0000542void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000543 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 unsigned NumArgs, bool isVariadic) {
545 ID.AddPointer(Result.getAsOpaquePtr());
546 for (unsigned i = 0; i != NumArgs; ++i)
547 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
548 ID.AddInteger(isVariadic);
549}
550
551void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000552 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000553}
554
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000555void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
556 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000557 unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000558 for (unsigned i = 0; i != NumProtocols; i++)
559 ID.AddPointer(protocols[i]);
560}
561
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000562void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000563 Profile(ID, &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000564}
565
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000566void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
567 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000568 unsigned NumProtocols) {
569 for (unsigned i = 0; i != NumProtocols; i++)
570 ID.AddPointer(protocols[i]);
571}
572
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000573void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000574 Profile(ID, &Protocols[0], getNumProtocols());
575}
576
Chris Lattnera2c77672007-07-16 22:05:22 +0000577/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
578/// potentially looking through *all* consequtive typedefs. This returns the
579/// sum of the type qualifiers, so if you have:
580/// typedef const int A;
581/// typedef volatile A B;
582/// looking through the typedefs for B will give you "const volatile A".
583///
584QualType TypedefType::LookThroughTypedefs() const {
585 // Usually, there is only a single level of typedefs, be fast in that case.
586 QualType FirstType = getDecl()->getUnderlyingType();
587 if (!isa<TypedefType>(FirstType))
588 return FirstType;
589
590 // Otherwise, do the fully general loop.
591 unsigned TypeQuals = 0;
592 const TypedefType *TDT = this;
593 while (1) {
594 QualType CurType = TDT->getDecl()->getUnderlyingType();
595 TypeQuals |= CurType.getQualifiers();
596
597 TDT = dyn_cast<TypedefType>(CurType);
598 if (TDT == 0)
599 return QualType(CurType.getTypePtr(), TypeQuals);
600 }
601}
Reid Spencer5f016e22007-07-11 17:01:13 +0000602
603bool RecordType::classof(const Type *T) {
604 if (const TagType *TT = dyn_cast<TagType>(T))
605 return isa<RecordDecl>(TT->getDecl());
606 return false;
607}
608
609
610//===----------------------------------------------------------------------===//
611// Type Printing
612//===----------------------------------------------------------------------===//
613
614void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000615 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 getAsStringInternal(R);
617 if (msg)
618 fprintf(stderr, "%s: %s\n", msg, R.c_str());
619 else
620 fprintf(stderr, "%s\n", R.c_str());
621}
622
623static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
624 // Note: funkiness to ensure we get a space only between quals.
625 bool NonePrinted = true;
626 if (TypeQuals & QualType::Const)
627 S += "const", NonePrinted = false;
628 if (TypeQuals & QualType::Volatile)
629 S += (NonePrinted+" volatile"), NonePrinted = false;
630 if (TypeQuals & QualType::Restrict)
631 S += (NonePrinted+" restrict"), NonePrinted = false;
632}
633
634void QualType::getAsStringInternal(std::string &S) const {
635 if (isNull()) {
636 S += "NULL TYPE\n";
637 return;
638 }
639
640 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000641 unsigned TQ = getQualifiers();
642 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 std::string TQS;
644 AppendTypeQualList(TQS, TQ);
645 if (!S.empty())
646 S = TQS + ' ' + S;
647 else
648 S = TQS;
649 }
650
651 getTypePtr()->getAsStringInternal(S);
652}
653
654void BuiltinType::getAsStringInternal(std::string &S) const {
655 if (S.empty()) {
656 S = getName();
657 } else {
658 // Prefix the basic type, e.g. 'int X'.
659 S = ' ' + S;
660 S = getName() + S;
661 }
662}
663
664void ComplexType::getAsStringInternal(std::string &S) const {
665 ElementType->getAsStringInternal(S);
666 S = "_Complex " + S;
667}
668
669void PointerType::getAsStringInternal(std::string &S) const {
670 S = '*' + S;
671
672 // Handle things like 'int (*A)[4];' correctly.
673 // FIXME: this should include vectors, but vectors use attributes I guess.
674 if (isa<ArrayType>(PointeeType.getTypePtr()))
675 S = '(' + S + ')';
676
677 PointeeType.getAsStringInternal(S);
678}
679
680void ReferenceType::getAsStringInternal(std::string &S) const {
681 S = '&' + S;
682
683 // Handle things like 'int (&A)[4];' correctly.
684 // FIXME: this should include vectors, but vectors use attributes I guess.
685 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
686 S = '(' + S + ')';
687
688 ReferenceeType.getAsStringInternal(S);
689}
690
Steve Narofffb22d962007-08-30 01:06:46 +0000691void ConstantArrayType::getAsStringInternal(std::string &S) const {
692 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000693 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000694 S += ']';
695
696 getElementType().getAsStringInternal(S);
697}
698
699void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 S += '[';
701
Steve Naroffc9406122007-08-30 18:10:14 +0000702 if (getIndexTypeQualifier()) {
703 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 S += ' ';
705 }
706
Steve Naroffc9406122007-08-30 18:10:14 +0000707 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000709 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 S += '*';
711
Steve Narofffb22d962007-08-30 01:06:46 +0000712 if (getSizeExpr()) {
713 std::ostringstream s;
714 getSizeExpr()->printPretty(s);
715 S += s.str();
716 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 S += ']';
718
Steve Narofffb22d962007-08-30 01:06:46 +0000719 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000720}
721
722void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000723 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000724 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000726 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 ElementType.getAsStringInternal(S);
728}
729
Steve Naroff31a45842007-07-28 23:10:27 +0000730void OCUVectorType::getAsStringInternal(std::string &S) const {
731 S += " __attribute__((ocu_vector_type(";
732 S += llvm::utostr_32(NumElements);
733 S += ")))";
734 ElementType.getAsStringInternal(S);
735}
736
Steve Naroffd1861fd2007-07-31 12:34:36 +0000737void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000738 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
739 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000740 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000741 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000742 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000743}
744
Steve Naroff363bcff2007-08-01 23:45:51 +0000745void TypeOfType::getAsStringInternal(std::string &InnerString) const {
746 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
747 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000748 std::string Tmp;
749 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000750 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000751}
752
Reid Spencer5f016e22007-07-11 17:01:13 +0000753void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
754 // If needed for precedence reasons, wrap the inner part in grouping parens.
755 if (!S.empty())
756 S = "(" + S + ")";
757
758 S += "()";
759 getResultType().getAsStringInternal(S);
760}
761
762void FunctionTypeProto::getAsStringInternal(std::string &S) const {
763 // If needed for precedence reasons, wrap the inner part in grouping parens.
764 if (!S.empty())
765 S = "(" + S + ")";
766
767 S += "(";
768 std::string Tmp;
769 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
770 if (i) S += ", ";
771 getArgType(i).getAsStringInternal(Tmp);
772 S += Tmp;
773 Tmp.clear();
774 }
775
776 if (isVariadic()) {
777 if (getNumArgs())
778 S += ", ";
779 S += "...";
780 } else if (getNumArgs() == 0) {
781 // Do not emit int() if we have a proto, emit 'int(void)'.
782 S += "void";
783 }
784
785 S += ")";
786 getResultType().getAsStringInternal(S);
787}
788
789
790void TypedefType::getAsStringInternal(std::string &InnerString) const {
791 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
792 InnerString = ' ' + InnerString;
793 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
794}
795
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000796void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000797 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
798 InnerString = ' ' + InnerString;
799 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
800}
801
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000802void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000803 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000804 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
805 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000806 std::string ObjCQIString = getDecl()->getName();
807 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000808 int num = getNumProtocols();
809 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000810 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000811 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000813 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000814 ObjCQIString += '>';
815 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000816}
817
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000819 std::string &InnerString) const {
820 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
821 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000822 std::string ObjCQIString = "id";
823 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000824 int num = getNumProtocols();
825 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000826 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000827 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000829 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000830 ObjCQIString += '>';
831 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000832}
833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834void TagType::getAsStringInternal(std::string &InnerString) const {
835 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
836 InnerString = ' ' + InnerString;
837
838 const char *Kind = getDecl()->getKindName();
839 const char *ID;
840 if (const IdentifierInfo *II = getDecl()->getIdentifier())
841 ID = II->getName();
842 else
843 ID = "<anonymous>";
844
845 InnerString = std::string(Kind) + " " + ID + InnerString;
846}