blob: f562376de659d54d1db57f1e9565ad36ea69d100 [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 {
64 if (const RecordType *RT = dyn_cast<RecordType>(this))
65 if (RT->getDecl()->getKind() == Decl::Struct)
66 return true;
67 return false;
68}
69bool Type::isUnionType() const {
70 if (const RecordType *RT = dyn_cast<RecordType>(this))
71 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
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000318bool Type::isEnumeralType() const {
319 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
320 return TT->getDecl()->getKind() == Decl::Enum;
321 return false;
322}
323
324bool Type::isBooleanType() const {
325 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
326 return BT->getKind() == BuiltinType::Bool;
327 return false;
328}
329
330bool Type::isCharType() const {
331 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
332 return BT->getKind() == BuiltinType::Char_U ||
333 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000334 BT->getKind() == BuiltinType::Char_S ||
335 BT->getKind() == BuiltinType::SChar;
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000336 return false;
337}
338
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000339/// isSignedIntegerType - Return true if this is an integer type that is
340/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
341/// an enum decl which has a signed representation, or a vector of signed
342/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000343bool Type::isSignedIntegerType() const {
344 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
345 return BT->getKind() >= BuiltinType::Char_S &&
346 BT->getKind() <= BuiltinType::LongLong;
347 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000348
349 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
350 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
351 return ED->getIntegerType()->isSignedIntegerType();
352
Steve Naroffc63b96a2007-07-12 21:46:55 +0000353 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
354 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 return false;
356}
357
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000358/// isUnsignedIntegerType - Return true if this is an integer type that is
359/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
360/// decl which has an unsigned representation, or a vector of unsigned integer
361/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000362bool Type::isUnsignedIntegerType() const {
363 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
364 return BT->getKind() >= BuiltinType::Bool &&
365 BT->getKind() <= BuiltinType::ULongLong;
366 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000367
368 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
369 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
370 return ED->getIntegerType()->isUnsignedIntegerType();
371
Steve Naroffc63b96a2007-07-12 21:46:55 +0000372 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
373 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 return false;
375}
376
377bool Type::isFloatingType() const {
378 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
379 return BT->getKind() >= BuiltinType::Float &&
380 BT->getKind() <= BuiltinType::LongDouble;
381 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000382 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000383 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
384 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 return false;
386}
387
388bool Type::isRealFloatingType() const {
389 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
390 return BT->getKind() >= BuiltinType::Float &&
391 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000392 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
393 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 return false;
395}
396
397bool Type::isRealType() const {
398 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
399 return BT->getKind() >= BuiltinType::Bool &&
400 BT->getKind() <= BuiltinType::LongDouble;
401 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
402 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000403 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
404 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 return false;
406}
407
Reid Spencer5f016e22007-07-11 17:01:13 +0000408bool Type::isArithmeticType() const {
409 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
410 return BT->getKind() != BuiltinType::Void;
411 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
412 if (TT->getDecl()->getKind() == Decl::Enum)
413 return true;
414 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
415}
416
417bool Type::isScalarType() const {
418 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
419 return BT->getKind() != BuiltinType::Void;
420 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
421 if (TT->getDecl()->getKind() == Decl::Enum)
422 return true;
423 return false;
424 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000425 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
426 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000427}
428
429bool Type::isAggregateType() const {
430 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
431 if (TT->getDecl()->getKind() == Decl::Struct)
432 return true;
433 return false;
434 }
Steve Narofffb22d962007-08-30 01:06:46 +0000435 return CanonicalType->getTypeClass() == ConstantArray ||
436 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000437}
438
439// The only variable size types are auto arrays within a function. Structures
440// cannot contain a VLA member. They can have a flexible array member, however
441// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000442bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Steve Narofffb22d962007-08-30 01:06:46 +0000443 if (isa<VariableArrayType>(CanonicalType))
444 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 return true;
446}
447
448/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
449/// - a type that can describe objects, but which lacks information needed to
450/// determine its size.
451bool Type::isIncompleteType() const {
452 switch (CanonicalType->getTypeClass()) {
453 default: return false;
454 case Builtin:
455 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
456 // be completed.
457 return isVoidType();
458 case Tagged:
459 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
460 // forward declaration, but not a full definition (C99 6.2.5p22).
461 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000462 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000464 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 }
466}
467
468bool Type::isPromotableIntegerType() const {
469 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
470 if (!BT) return false;
471 switch (BT->getKind()) {
472 case BuiltinType::Bool:
473 case BuiltinType::Char_S:
474 case BuiltinType::Char_U:
475 case BuiltinType::SChar:
476 case BuiltinType::UChar:
477 case BuiltinType::Short:
478 case BuiltinType::UShort:
479 return true;
480 default:
481 return false;
482 }
483}
484
485const char *BuiltinType::getName() const {
486 switch (getKind()) {
487 default: assert(0 && "Unknown builtin type!");
488 case Void: return "void";
489 case Bool: return "_Bool";
490 case Char_S: return "char";
491 case Char_U: return "char";
492 case SChar: return "signed char";
493 case Short: return "short";
494 case Int: return "int";
495 case Long: return "long";
496 case LongLong: return "long long";
497 case UChar: return "unsigned char";
498 case UShort: return "unsigned short";
499 case UInt: return "unsigned int";
500 case ULong: return "unsigned long";
501 case ULongLong: return "unsigned long long";
502 case Float: return "float";
503 case Double: return "double";
504 case LongDouble: return "long double";
505 }
506}
507
Reid Spencer5f016e22007-07-11 17:01:13 +0000508void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000509 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000510 unsigned NumArgs, bool isVariadic) {
511 ID.AddPointer(Result.getAsOpaquePtr());
512 for (unsigned i = 0; i != NumArgs; ++i)
513 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
514 ID.AddInteger(isVariadic);
515}
516
517void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000518 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000519}
520
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000521void ObjcQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
522 ObjcInterfaceType *interfaceType,
523 ObjcProtocolDecl **protocols,
524 unsigned NumProtocols) {
525 ID.AddPointer(interfaceType);
526 for (unsigned i = 0; i != NumProtocols; i++)
527 ID.AddPointer(protocols[i]);
528}
529
530void ObjcQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
531 Profile(ID, getInterfaceType(), &Protocols[0], getNumProtocols());
532}
533
Chris Lattnera2c77672007-07-16 22:05:22 +0000534/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
535/// potentially looking through *all* consequtive typedefs. This returns the
536/// sum of the type qualifiers, so if you have:
537/// typedef const int A;
538/// typedef volatile A B;
539/// looking through the typedefs for B will give you "const volatile A".
540///
541QualType TypedefType::LookThroughTypedefs() const {
542 // Usually, there is only a single level of typedefs, be fast in that case.
543 QualType FirstType = getDecl()->getUnderlyingType();
544 if (!isa<TypedefType>(FirstType))
545 return FirstType;
546
547 // Otherwise, do the fully general loop.
548 unsigned TypeQuals = 0;
549 const TypedefType *TDT = this;
550 while (1) {
551 QualType CurType = TDT->getDecl()->getUnderlyingType();
552 TypeQuals |= CurType.getQualifiers();
553
554 TDT = dyn_cast<TypedefType>(CurType);
555 if (TDT == 0)
556 return QualType(CurType.getTypePtr(), TypeQuals);
557 }
558}
Reid Spencer5f016e22007-07-11 17:01:13 +0000559
560bool RecordType::classof(const Type *T) {
561 if (const TagType *TT = dyn_cast<TagType>(T))
562 return isa<RecordDecl>(TT->getDecl());
563 return false;
564}
565
566
567//===----------------------------------------------------------------------===//
568// Type Printing
569//===----------------------------------------------------------------------===//
570
571void QualType::dump(const char *msg) const {
572 std::string R = "foo";
573 getAsStringInternal(R);
574 if (msg)
575 fprintf(stderr, "%s: %s\n", msg, R.c_str());
576 else
577 fprintf(stderr, "%s\n", R.c_str());
578}
579
580static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
581 // Note: funkiness to ensure we get a space only between quals.
582 bool NonePrinted = true;
583 if (TypeQuals & QualType::Const)
584 S += "const", NonePrinted = false;
585 if (TypeQuals & QualType::Volatile)
586 S += (NonePrinted+" volatile"), NonePrinted = false;
587 if (TypeQuals & QualType::Restrict)
588 S += (NonePrinted+" restrict"), NonePrinted = false;
589}
590
591void QualType::getAsStringInternal(std::string &S) const {
592 if (isNull()) {
593 S += "NULL TYPE\n";
594 return;
595 }
596
597 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000598 unsigned TQ = getQualifiers();
599 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 std::string TQS;
601 AppendTypeQualList(TQS, TQ);
602 if (!S.empty())
603 S = TQS + ' ' + S;
604 else
605 S = TQS;
606 }
607
608 getTypePtr()->getAsStringInternal(S);
609}
610
611void BuiltinType::getAsStringInternal(std::string &S) const {
612 if (S.empty()) {
613 S = getName();
614 } else {
615 // Prefix the basic type, e.g. 'int X'.
616 S = ' ' + S;
617 S = getName() + S;
618 }
619}
620
621void ComplexType::getAsStringInternal(std::string &S) const {
622 ElementType->getAsStringInternal(S);
623 S = "_Complex " + S;
624}
625
626void PointerType::getAsStringInternal(std::string &S) const {
627 S = '*' + S;
628
629 // Handle things like 'int (*A)[4];' correctly.
630 // FIXME: this should include vectors, but vectors use attributes I guess.
631 if (isa<ArrayType>(PointeeType.getTypePtr()))
632 S = '(' + S + ')';
633
634 PointeeType.getAsStringInternal(S);
635}
636
637void ReferenceType::getAsStringInternal(std::string &S) const {
638 S = '&' + S;
639
640 // Handle things like 'int (&A)[4];' correctly.
641 // FIXME: this should include vectors, but vectors use attributes I guess.
642 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
643 S = '(' + S + ')';
644
645 ReferenceeType.getAsStringInternal(S);
646}
647
Steve Narofffb22d962007-08-30 01:06:46 +0000648void ConstantArrayType::getAsStringInternal(std::string &S) const {
649 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000650 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000651 S += ']';
652
653 getElementType().getAsStringInternal(S);
654}
655
656void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 S += '[';
658
Steve Naroffc9406122007-08-30 18:10:14 +0000659 if (getIndexTypeQualifier()) {
660 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 S += ' ';
662 }
663
Steve Naroffc9406122007-08-30 18:10:14 +0000664 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000666 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 S += '*';
668
Steve Narofffb22d962007-08-30 01:06:46 +0000669 if (getSizeExpr()) {
670 std::ostringstream s;
671 getSizeExpr()->printPretty(s);
672 S += s.str();
673 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 S += ']';
675
Steve Narofffb22d962007-08-30 01:06:46 +0000676 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000677}
678
679void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000680 S += " __attribute__((vector_size(";
681 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000683 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 ElementType.getAsStringInternal(S);
685}
686
Steve Naroff31a45842007-07-28 23:10:27 +0000687void OCUVectorType::getAsStringInternal(std::string &S) const {
688 S += " __attribute__((ocu_vector_type(";
689 S += llvm::utostr_32(NumElements);
690 S += ")))";
691 ElementType.getAsStringInternal(S);
692}
693
Steve Naroffd1861fd2007-07-31 12:34:36 +0000694void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000695 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
696 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000697 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000698 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000699 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000700}
701
Steve Naroff363bcff2007-08-01 23:45:51 +0000702void TypeOfType::getAsStringInternal(std::string &InnerString) const {
703 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
704 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000705 std::string Tmp;
706 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000707 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000708}
709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
711 // If needed for precedence reasons, wrap the inner part in grouping parens.
712 if (!S.empty())
713 S = "(" + S + ")";
714
715 S += "()";
716 getResultType().getAsStringInternal(S);
717}
718
719void FunctionTypeProto::getAsStringInternal(std::string &S) const {
720 // If needed for precedence reasons, wrap the inner part in grouping parens.
721 if (!S.empty())
722 S = "(" + S + ")";
723
724 S += "(";
725 std::string Tmp;
726 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
727 if (i) S += ", ";
728 getArgType(i).getAsStringInternal(Tmp);
729 S += Tmp;
730 Tmp.clear();
731 }
732
733 if (isVariadic()) {
734 if (getNumArgs())
735 S += ", ";
736 S += "...";
737 } else if (getNumArgs() == 0) {
738 // Do not emit int() if we have a proto, emit 'int(void)'.
739 S += "void";
740 }
741
742 S += ")";
743 getResultType().getAsStringInternal(S);
744}
745
746
747void TypedefType::getAsStringInternal(std::string &InnerString) const {
748 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
749 InnerString = ' ' + InnerString;
750 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
751}
752
Steve Naroff3536b442007-09-06 21:24:23 +0000753void ObjcInterfaceType::getAsStringInternal(std::string &InnerString) const {
754 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
755 InnerString = ' ' + InnerString;
756 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
757}
758
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000759void ObjcQualifiedInterfaceType::getAsStringInternal(
760 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000761 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
762 InnerString = ' ' + InnerString;
763 std::string ObjcQIString = getInterfaceType()->getDecl()->getName();
764 ObjcQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000765 int num = getNumProtocols();
766 for (int i = 0; i < num; i++) {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000767 ObjcQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000768 if (i < num-1)
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000769 ObjcQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000770 }
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000771 ObjcQIString += '>';
772 InnerString = ObjcQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000773}
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775void TagType::getAsStringInternal(std::string &InnerString) const {
776 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
777 InnerString = ' ' + InnerString;
778
779 const char *Kind = getDecl()->getKindName();
780 const char *ID;
781 if (const IdentifierInfo *II = getDecl()->getIdentifier())
782 ID = II->getName();
783 else
784 ID = "<anonymous>";
785
786 InnerString = std::string(Kind) + " " + ID + InnerString;
787}