blob: 78e0dbbb27193f31eae07d4b9b566d22c0cfae58 [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 {
Steve Naroff02f62a92008-01-15 19:36:10 +000077 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
78 return CT->getElementType()->isFloatingType();
79 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000080}
81
Steve Naroff4cdec1c2008-01-15 01:41:59 +000082bool Type::isComplexIntegerType() const {
83 // Check for GCC complex integer extension.
84 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
85 return CT->getElementType()->isIntegerType();
86 return false;
87}
88
89const ComplexType *Type::getAsComplexIntegerType() const {
90 // Are we directly a complex type?
91 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
92 if (CTy->getElementType()->isIntegerType())
93 return CTy;
94 }
95 // If the canonical form of this type isn't the right kind, reject it.
96 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
97 if (!CTy || !CTy->getElementType()->isIntegerType())
98 return 0;
99
100 // If this is a typedef for a complex type, strip the typedef off without
101 // losing all typedef information.
102 return getDesugaredType()->getAsComplexIntegerType();
103}
104
Chris Lattnerdea61462007-10-29 03:41:11 +0000105/// getDesugaredType - Return the specified type with any "sugar" removed from
106/// type type. This takes off typedefs, typeof's etc. If the outer level of
107/// the type is already concrete, it returns it unmodified. This is similar
108/// to getting the canonical type, but it doesn't remove *all* typedefs. For
109/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
110/// concrete.
111const Type *Type::getDesugaredType() const {
112 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
113 return TDT->LookThroughTypedefs().getTypePtr();
114 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
115 return TOE->getUnderlyingExpr()->getType().getTypePtr();
116 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
117 return TOT->getUnderlyingType().getTypePtr();
118 return this;
119}
120
121
Steve Naroff77878cc2007-08-27 04:08:11 +0000122const BuiltinType *Type::getAsBuiltinType() const {
123 // If this is directly a builtin type, return it.
124 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
125 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000126
127 // If the canonical form of this type isn't a builtin type, reject it.
128 if (!isa<BuiltinType>(CanonicalType))
129 return 0;
130
Steve Naroff77878cc2007-08-27 04:08:11 +0000131 // If this is a typedef for a builtin type, strip the typedef off without
132 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000133 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000134}
135
Chris Lattnerc8629632007-07-31 19:29:30 +0000136const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000137 // If this is directly a function type, return it.
138 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
139 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +0000140
Chris Lattnerdea61462007-10-29 03:41:11 +0000141 // If the canonical form of this type isn't the right kind, reject it.
142 if (!isa<FunctionType>(CanonicalType))
143 return 0;
144
Steve Naroff7064f5c2007-07-26 18:32:01 +0000145 // If this is a typedef for a function type, strip the typedef off without
146 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000147 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000148}
149
Chris Lattnerbefee482007-07-31 16:53:04 +0000150const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000151 // If this is directly a pointer type, return it.
152 if (const PointerType *PTy = dyn_cast<PointerType>(this))
153 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000154
Chris Lattnerdea61462007-10-29 03:41:11 +0000155 // If the canonical form of this type isn't the right kind, reject it.
156 if (!isa<PointerType>(CanonicalType))
157 return 0;
158
Chris Lattnera2c77672007-07-16 22:05:22 +0000159 // If this is a typedef for a pointer type, strip the typedef off without
160 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000161 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000162}
163
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000164const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000165 // If this is directly a reference type, return it.
166 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
167 return RTy;
168
Chris Lattnerdea61462007-10-29 03:41:11 +0000169 // If the canonical form of this type isn't the right kind, reject it.
170 if (!isa<ReferenceType>(CanonicalType))
171 return 0;
172
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000173 // If this is a typedef for a reference type, strip the typedef off without
174 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000175 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
Chris Lattnerc8629632007-07-31 19:29:30 +0000178const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000179 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000180 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
181 return ATy;
182
Chris Lattnerdea61462007-10-29 03:41:11 +0000183 // If the canonical form of this type isn't the right kind, reject it.
184 if (!isa<ArrayType>(CanonicalType))
185 return 0;
186
Steve Naroff700204c2007-07-24 21:46:40 +0000187 // If this is a typedef for an array type, strip the typedef off without
188 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000189 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000190}
191
Steve Naroffd7444aa2007-08-31 17:20:07 +0000192const ConstantArrayType *Type::getAsConstantArrayType() const {
193 // If this is directly a constant array type, return it.
194 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
195 return ATy;
196
Chris Lattnerdea61462007-10-29 03:41:11 +0000197 // If the canonical form of this type isn't the right kind, reject it.
198 if (!isa<ConstantArrayType>(CanonicalType))
199 return 0;
200
201 // If this is a typedef for a constant array type, strip the typedef off
202 // without losing all typedef information.
203 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000204}
205
206const VariableArrayType *Type::getAsVariableArrayType() const {
207 // If this is directly a variable array type, return it.
208 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
209 return ATy;
210
Chris Lattnerdea61462007-10-29 03:41:11 +0000211 // If the canonical form of this type isn't the right kind, reject it.
212 if (!isa<VariableArrayType>(CanonicalType))
213 return 0;
214
215 // If this is a typedef for a variable array type, strip the typedef off
216 // without losing all typedef information.
217 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000218}
219
220/// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
221/// types that have a non-constant expression. This does not include "[]".
222bool Type::isVariablyModifiedType() const {
223 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
224 if (VAT->getSizeExpr())
225 return true;
226 }
227 return false;
228}
229
230const VariableArrayType *Type::getAsVariablyModifiedType() const {
231 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
232 if (VAT->getSizeExpr())
233 return VAT;
234 }
235 return 0;
236}
237
Chris Lattnerc8629632007-07-31 19:29:30 +0000238const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000239 // If this is directly a reference type, return it.
240 if (const RecordType *RTy = dyn_cast<RecordType>(this))
241 return RTy;
242
Chris Lattnerdea61462007-10-29 03:41:11 +0000243 // If the canonical form of this type isn't the right kind, reject it.
244 if (!isa<RecordType>(CanonicalType))
245 return 0;
246
247 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000248 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000249 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000250}
251
Chris Lattnerc8629632007-07-31 19:29:30 +0000252const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000253 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000254 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
255 if (RT->getDecl()->getKind() == Decl::Struct)
256 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000257 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000258
259 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000260 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000261 if (RT->getDecl()->getKind() != Decl::Struct)
262 return 0;
263
264 // If this is a typedef for a structure type, strip the typedef off without
265 // losing all typedef information.
266 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000268 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000269}
270
Chris Lattnerc8629632007-07-31 19:29:30 +0000271const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000272 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000273 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
274 if (RT->getDecl()->getKind() == Decl::Union)
275 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000276 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000277 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000278 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000279 if (RT->getDecl()->getKind() != Decl::Union)
280 return 0;
281
282 // If this is a typedef for a union type, strip the typedef off without
283 // losing all typedef information.
284 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000286 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000287}
288
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000289const ComplexType *Type::getAsComplexType() const {
290 // Are we directly a complex type?
291 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
292 return CTy;
293
Chris Lattnerdea61462007-10-29 03:41:11 +0000294 // If the canonical form of this type isn't the right kind, reject it.
295 if (!isa<ComplexType>(CanonicalType))
296 return 0;
297
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000298 // If this is a typedef for a complex type, strip the typedef off without
299 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000300 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000301}
302
Chris Lattnerc8629632007-07-31 19:29:30 +0000303const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000304 // Are we directly a vector type?
305 if (const VectorType *VTy = dyn_cast<VectorType>(this))
306 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000307
Chris Lattnerdea61462007-10-29 03:41:11 +0000308 // If the canonical form of this type isn't the right kind, reject it.
309 if (!isa<VectorType>(CanonicalType))
310 return 0;
311
Chris Lattnera2c77672007-07-16 22:05:22 +0000312 // If this is a typedef for a vector type, strip the typedef off without
313 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000314 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000315}
316
Chris Lattnerc8629632007-07-31 19:29:30 +0000317const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000318 // Are we directly an OpenCU vector type?
319 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
320 return VTy;
321
Chris Lattnerdea61462007-10-29 03:41:11 +0000322 // If the canonical form of this type isn't the right kind, reject it.
323 if (!isa<OCUVectorType>(CanonicalType))
324 return 0;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000325
Chris Lattnerdea61462007-10-29 03:41:11 +0000326 // If this is a typedef for an ocuvector type, strip the typedef off without
327 // losing all typedef information.
328 return getDesugaredType()->getAsOCUVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000329}
330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331bool Type::isIntegerType() const {
332 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
333 return BT->getKind() >= BuiltinType::Bool &&
334 BT->getKind() <= BuiltinType::LongLong;
335 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
336 if (TT->getDecl()->getKind() == Decl::Enum)
337 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000338 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
339 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 return false;
341}
342
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000343bool Type::isIntegralType() const {
344 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
345 return BT->getKind() >= BuiltinType::Bool &&
346 BT->getKind() <= BuiltinType::LongLong;
347 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
348 if (TT->getDecl()->getKind() == Decl::Enum)
349 return true;
350 return false;
351}
352
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000353bool Type::isEnumeralType() const {
354 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
355 return TT->getDecl()->getKind() == Decl::Enum;
356 return false;
357}
358
359bool Type::isBooleanType() const {
360 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
361 return BT->getKind() == BuiltinType::Bool;
362 return false;
363}
364
365bool Type::isCharType() const {
366 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
367 return BT->getKind() == BuiltinType::Char_U ||
368 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000369 BT->getKind() == BuiltinType::Char_S ||
370 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000371 return false;
372}
373
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000374/// isSignedIntegerType - Return true if this is an integer type that is
375/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
376/// an enum decl which has a signed representation, or a vector of signed
377/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000378bool Type::isSignedIntegerType() const {
379 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
380 return BT->getKind() >= BuiltinType::Char_S &&
381 BT->getKind() <= BuiltinType::LongLong;
382 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000383
384 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
385 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
386 return ED->getIntegerType()->isSignedIntegerType();
387
Steve Naroffc63b96a2007-07-12 21:46:55 +0000388 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
389 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 return false;
391}
392
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000393/// isUnsignedIntegerType - Return true if this is an integer type that is
394/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
395/// decl which has an unsigned representation, or a vector of unsigned integer
396/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000397bool Type::isUnsignedIntegerType() const {
398 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
399 return BT->getKind() >= BuiltinType::Bool &&
400 BT->getKind() <= BuiltinType::ULongLong;
401 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000402
403 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
404 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
405 return ED->getIntegerType()->isUnsignedIntegerType();
406
Steve Naroffc63b96a2007-07-12 21:46:55 +0000407 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
408 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 return false;
410}
411
412bool Type::isFloatingType() const {
413 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
414 return BT->getKind() >= BuiltinType::Float &&
415 BT->getKind() <= BuiltinType::LongDouble;
416 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000417 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000418 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
419 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 return false;
421}
422
423bool Type::isRealFloatingType() const {
424 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
425 return BT->getKind() >= BuiltinType::Float &&
426 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000427 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
428 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 return false;
430}
431
432bool Type::isRealType() const {
433 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
434 return BT->getKind() >= BuiltinType::Bool &&
435 BT->getKind() <= BuiltinType::LongDouble;
436 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
437 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000438 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
439 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 return false;
441}
442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443bool Type::isArithmeticType() const {
444 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
445 return BT->getKind() != BuiltinType::Void;
446 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
447 if (TT->getDecl()->getKind() == Decl::Enum)
448 return true;
449 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
450}
451
452bool Type::isScalarType() const {
453 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
454 return BT->getKind() != BuiltinType::Void;
455 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
456 if (TT->getDecl()->getKind() == Decl::Enum)
457 return true;
458 return false;
459 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000460 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Fariborz Jahaniand58fabf2007-12-18 21:33:44 +0000461 isa<VectorType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000462 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000463}
464
465bool Type::isAggregateType() const {
466 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
467 if (TT->getDecl()->getKind() == Decl::Struct)
468 return true;
469 return false;
470 }
Steve Narofffb22d962007-08-30 01:06:46 +0000471 return CanonicalType->getTypeClass() == ConstantArray ||
472 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000473}
474
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000475/// isConstantSizeType - Return true if this is not a variable sized type,
476/// according to the rules of C99 6.7.5p3. It is not legal to call this on
477/// incomplete types.
478bool Type::isConstantSizeType(ASTContext &Ctx) const {
Chris Lattnerd52a4572007-12-18 07:03:30 +0000479 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000480 // The VAT must have a size, as it is known to be complete.
481 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482}
483
484/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
485/// - a type that can describe objects, but which lacks information needed to
486/// determine its size.
487bool Type::isIncompleteType() const {
488 switch (CanonicalType->getTypeClass()) {
489 default: return false;
490 case Builtin:
491 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
492 // be completed.
493 return isVoidType();
494 case Tagged:
495 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
496 // forward declaration, but not a full definition (C99 6.2.5p22).
497 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000498 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000500 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 }
502}
503
504bool Type::isPromotableIntegerType() const {
505 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
506 if (!BT) return false;
507 switch (BT->getKind()) {
508 case BuiltinType::Bool:
509 case BuiltinType::Char_S:
510 case BuiltinType::Char_U:
511 case BuiltinType::SChar:
512 case BuiltinType::UChar:
513 case BuiltinType::Short:
514 case BuiltinType::UShort:
515 return true;
516 default:
517 return false;
518 }
519}
520
521const char *BuiltinType::getName() const {
522 switch (getKind()) {
523 default: assert(0 && "Unknown builtin type!");
524 case Void: return "void";
525 case Bool: return "_Bool";
526 case Char_S: return "char";
527 case Char_U: return "char";
528 case SChar: return "signed char";
529 case Short: return "short";
530 case Int: return "int";
531 case Long: return "long";
532 case LongLong: return "long long";
533 case UChar: return "unsigned char";
534 case UShort: return "unsigned short";
535 case UInt: return "unsigned int";
536 case ULong: return "unsigned long";
537 case ULongLong: return "unsigned long long";
538 case Float: return "float";
539 case Double: return "double";
540 case LongDouble: return "long double";
541 }
542}
543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000545 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 unsigned NumArgs, bool isVariadic) {
547 ID.AddPointer(Result.getAsOpaquePtr());
548 for (unsigned i = 0; i != NumArgs; ++i)
549 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
550 ID.AddInteger(isVariadic);
551}
552
553void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000554 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000555}
556
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000557void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
558 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000559 unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000560 for (unsigned i = 0; i != NumProtocols; i++)
561 ID.AddPointer(protocols[i]);
562}
563
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000565 Profile(ID, &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000566}
567
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000568void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
569 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000570 unsigned NumProtocols) {
571 for (unsigned i = 0; i != NumProtocols; i++)
572 ID.AddPointer(protocols[i]);
573}
574
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000575void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000576 Profile(ID, &Protocols[0], getNumProtocols());
577}
578
Chris Lattnera2c77672007-07-16 22:05:22 +0000579/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
580/// potentially looking through *all* consequtive typedefs. This returns the
581/// sum of the type qualifiers, so if you have:
582/// typedef const int A;
583/// typedef volatile A B;
584/// looking through the typedefs for B will give you "const volatile A".
585///
586QualType TypedefType::LookThroughTypedefs() const {
587 // Usually, there is only a single level of typedefs, be fast in that case.
588 QualType FirstType = getDecl()->getUnderlyingType();
589 if (!isa<TypedefType>(FirstType))
590 return FirstType;
591
592 // Otherwise, do the fully general loop.
593 unsigned TypeQuals = 0;
594 const TypedefType *TDT = this;
595 while (1) {
596 QualType CurType = TDT->getDecl()->getUnderlyingType();
597 TypeQuals |= CurType.getQualifiers();
598
599 TDT = dyn_cast<TypedefType>(CurType);
600 if (TDT == 0)
601 return QualType(CurType.getTypePtr(), TypeQuals);
602 }
603}
Reid Spencer5f016e22007-07-11 17:01:13 +0000604
605bool RecordType::classof(const Type *T) {
606 if (const TagType *TT = dyn_cast<TagType>(T))
607 return isa<RecordDecl>(TT->getDecl());
608 return false;
609}
610
611
612//===----------------------------------------------------------------------===//
613// Type Printing
614//===----------------------------------------------------------------------===//
615
616void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000617 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 getAsStringInternal(R);
619 if (msg)
620 fprintf(stderr, "%s: %s\n", msg, R.c_str());
621 else
622 fprintf(stderr, "%s\n", R.c_str());
623}
624
625static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
626 // Note: funkiness to ensure we get a space only between quals.
627 bool NonePrinted = true;
628 if (TypeQuals & QualType::Const)
629 S += "const", NonePrinted = false;
630 if (TypeQuals & QualType::Volatile)
631 S += (NonePrinted+" volatile"), NonePrinted = false;
632 if (TypeQuals & QualType::Restrict)
633 S += (NonePrinted+" restrict"), NonePrinted = false;
634}
635
636void QualType::getAsStringInternal(std::string &S) const {
637 if (isNull()) {
638 S += "NULL TYPE\n";
639 return;
640 }
641
642 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000643 unsigned TQ = getQualifiers();
644 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 std::string TQS;
646 AppendTypeQualList(TQS, TQ);
647 if (!S.empty())
648 S = TQS + ' ' + S;
649 else
650 S = TQS;
651 }
652
653 getTypePtr()->getAsStringInternal(S);
654}
655
656void BuiltinType::getAsStringInternal(std::string &S) const {
657 if (S.empty()) {
658 S = getName();
659 } else {
660 // Prefix the basic type, e.g. 'int X'.
661 S = ' ' + S;
662 S = getName() + S;
663 }
664}
665
666void ComplexType::getAsStringInternal(std::string &S) const {
667 ElementType->getAsStringInternal(S);
668 S = "_Complex " + S;
669}
670
671void PointerType::getAsStringInternal(std::string &S) const {
672 S = '*' + S;
673
674 // Handle things like 'int (*A)[4];' correctly.
675 // FIXME: this should include vectors, but vectors use attributes I guess.
676 if (isa<ArrayType>(PointeeType.getTypePtr()))
677 S = '(' + S + ')';
678
679 PointeeType.getAsStringInternal(S);
680}
681
682void ReferenceType::getAsStringInternal(std::string &S) const {
683 S = '&' + S;
684
685 // Handle things like 'int (&A)[4];' correctly.
686 // FIXME: this should include vectors, but vectors use attributes I guess.
687 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
688 S = '(' + S + ')';
689
690 ReferenceeType.getAsStringInternal(S);
691}
692
Steve Narofffb22d962007-08-30 01:06:46 +0000693void ConstantArrayType::getAsStringInternal(std::string &S) const {
694 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000695 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000696 S += ']';
697
698 getElementType().getAsStringInternal(S);
699}
700
701void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 S += '[';
703
Steve Naroffc9406122007-08-30 18:10:14 +0000704 if (getIndexTypeQualifier()) {
705 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000706 S += ' ';
707 }
708
Steve Naroffc9406122007-08-30 18:10:14 +0000709 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000711 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 S += '*';
713
Steve Narofffb22d962007-08-30 01:06:46 +0000714 if (getSizeExpr()) {
715 std::ostringstream s;
716 getSizeExpr()->printPretty(s);
717 S += s.str();
718 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 S += ']';
720
Steve Narofffb22d962007-08-30 01:06:46 +0000721 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000722}
723
724void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000725 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000726 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000728 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 ElementType.getAsStringInternal(S);
730}
731
Steve Naroff31a45842007-07-28 23:10:27 +0000732void OCUVectorType::getAsStringInternal(std::string &S) const {
733 S += " __attribute__((ocu_vector_type(";
734 S += llvm::utostr_32(NumElements);
735 S += ")))";
736 ElementType.getAsStringInternal(S);
737}
738
Steve Naroffd1861fd2007-07-31 12:34:36 +0000739void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000740 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
741 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000742 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000743 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000744 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000745}
746
Steve Naroff363bcff2007-08-01 23:45:51 +0000747void TypeOfType::getAsStringInternal(std::string &InnerString) const {
748 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
749 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000750 std::string Tmp;
751 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000752 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000753}
754
Reid Spencer5f016e22007-07-11 17:01:13 +0000755void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
756 // If needed for precedence reasons, wrap the inner part in grouping parens.
757 if (!S.empty())
758 S = "(" + S + ")";
759
760 S += "()";
761 getResultType().getAsStringInternal(S);
762}
763
764void FunctionTypeProto::getAsStringInternal(std::string &S) const {
765 // If needed for precedence reasons, wrap the inner part in grouping parens.
766 if (!S.empty())
767 S = "(" + S + ")";
768
769 S += "(";
770 std::string Tmp;
771 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
772 if (i) S += ", ";
773 getArgType(i).getAsStringInternal(Tmp);
774 S += Tmp;
775 Tmp.clear();
776 }
777
778 if (isVariadic()) {
779 if (getNumArgs())
780 S += ", ";
781 S += "...";
782 } else if (getNumArgs() == 0) {
783 // Do not emit int() if we have a proto, emit 'int(void)'.
784 S += "void";
785 }
786
787 S += ")";
788 getResultType().getAsStringInternal(S);
789}
790
791
792void TypedefType::getAsStringInternal(std::string &InnerString) const {
793 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
794 InnerString = ' ' + InnerString;
795 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
796}
797
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000798void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000799 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
800 InnerString = ' ' + InnerString;
801 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
802}
803
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000804void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000805 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000806 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
807 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000808 std::string ObjCQIString = getDecl()->getName();
809 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000810 int num = getNumProtocols();
811 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000812 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000813 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000814 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000815 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000816 ObjCQIString += '>';
817 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000818}
819
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000820void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000821 std::string &InnerString) const {
822 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
823 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000824 std::string ObjCQIString = "id";
825 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000826 int num = getNumProtocols();
827 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000828 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000829 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000830 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000831 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000832 ObjCQIString += '>';
833 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000834}
835
Reid Spencer5f016e22007-07-11 17:01:13 +0000836void TagType::getAsStringInternal(std::string &InnerString) const {
837 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
838 InnerString = ' ' + InnerString;
839
840 const char *Kind = getDecl()->getKindName();
841 const char *ID;
842 if (const IdentifierInfo *II = getDecl()->getIdentifier())
843 ID = II->getName();
844 else
845 ID = "<anonymous>";
846
847 InnerString = std::string(Kind) + " " + ID + InnerString;
848}