blob: 2c5a3f41bf9fec6e5ac30817036d93cd5a696c0e [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"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.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"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/StringExtras.h"
Steve Naroff8d1a3b82007-08-01 17:20:42 +000019#include <sstream>
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000022void Type::Destroy(ASTContext& C) { delete this; }
23
24void FunctionTypeProto::Destroy(ASTContext& C) {
25 // Destroy the object, but don't call delete. These are malloc'd.
26 this->~FunctionTypeProto();
27 free(this);
28}
29
30void VariableArrayType::Destroy(ASTContext& C) {
31 SizeExpr->Destroy(C);
32 delete this;
33}
Reid Spencer5f016e22007-07-11 17:01:13 +000034
Chris Lattnerc63a1f22008-08-04 07:31:14 +000035
36/// getArrayElementTypeNoTypeQual - If this is an array type, return the
37/// element type of the array, potentially with type qualifiers missing.
38/// This method should never be used when type qualifiers are meaningful.
39const Type *Type::getArrayElementTypeNoTypeQual() const {
40 // If this is directly an array type, return it.
41 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
42 return ATy->getElementType().getTypePtr();
43
44 // If the canonical form of this type isn't the right kind, reject it.
45 if (!isa<ArrayType>(CanonicalType)) {
46 // Look through type qualifiers
47 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
48 return AT->getElementType().getTypePtr();
49 return 0;
50 }
51
52 // If this is a typedef for an array type, strip the typedef off without
53 // losing all typedef information.
54 return getDesugaredType()->getArrayElementTypeNoTypeQual();
55}
56
57/// getDesugaredType - Return the specified type with any "sugar" removed from
58/// type type. This takes off typedefs, typeof's etc. If the outer level of
59/// the type is already concrete, it returns it unmodified. This is similar
60/// to getting the canonical type, but it doesn't remove *all* typedefs. For
61/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
62/// concrete.
63QualType Type::getDesugaredType() const {
64 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
65 return TDT->LookThroughTypedefs();
66 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
67 return TOE->getUnderlyingExpr()->getType();
68 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
69 return TOT->getUnderlyingType();
70 // FIXME: remove this cast.
71 return QualType(const_cast<Type*>(this), 0);
72}
73
Reid Spencer5f016e22007-07-11 17:01:13 +000074/// isVoidType - Helper method to determine if this is the 'void' type.
75bool Type::isVoidType() const {
76 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
77 return BT->getKind() == BuiltinType::Void;
78 return false;
79}
80
81bool Type::isObjectType() const {
82 if (isa<FunctionType>(CanonicalType))
83 return false;
84 else if (CanonicalType->isIncompleteType())
85 return false;
86 else
87 return true;
88}
89
90bool Type::isDerivedType() const {
91 switch (CanonicalType->getTypeClass()) {
92 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000093 case VariableArray:
94 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +000095 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000096 case FunctionProto:
97 case FunctionNoProto:
98 case Reference:
99 return true;
100 case Tagged: {
101 const TagType *TT = cast<TagType>(CanonicalType);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000102 return !TT->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 }
104 default:
105 return false;
106 }
107}
108
Chris Lattner99dc9142008-04-13 18:59:07 +0000109bool Type::isClassType() const {
110 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000111 if (RT->getDecl()->isClass())
Chris Lattner99dc9142008-04-13 18:59:07 +0000112 return true;
113 return false;
114}
Chris Lattnerc8629632007-07-31 19:29:30 +0000115bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000116 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000117 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000118 return true;
119 return false;
120}
121bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000122 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000123 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000124 return true;
125 return false;
126}
Chris Lattnerc8629632007-07-31 19:29:30 +0000127
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000128bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000129 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
130 return CT->getElementType()->isFloatingType();
131 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000132}
133
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000134bool Type::isComplexIntegerType() const {
135 // Check for GCC complex integer extension.
136 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
137 return CT->getElementType()->isIntegerType();
138 return false;
139}
140
141const ComplexType *Type::getAsComplexIntegerType() const {
142 // Are we directly a complex type?
143 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
144 if (CTy->getElementType()->isIntegerType())
145 return CTy;
146 }
147 // If the canonical form of this type isn't the right kind, reject it.
148 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
149 if (!CTy || !CTy->getElementType()->isIntegerType())
150 return 0;
151
152 // If this is a typedef for a complex type, strip the typedef off without
153 // losing all typedef information.
154 return getDesugaredType()->getAsComplexIntegerType();
155}
156
Steve Naroff77878cc2007-08-27 04:08:11 +0000157const BuiltinType *Type::getAsBuiltinType() const {
158 // If this is directly a builtin type, return it.
159 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
160 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000161
162 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000163 if (!isa<BuiltinType>(CanonicalType)) {
164 // Look through type qualifiers
165 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
166 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000167 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000168 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000169
Steve Naroff77878cc2007-08-27 04:08:11 +0000170 // If this is a typedef for a builtin type, strip the typedef off without
171 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000172 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000173}
174
Chris Lattnerc8629632007-07-31 19:29:30 +0000175const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000176 // If this is directly a function type, return it.
177 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
178 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000179
Chris Lattnerdea61462007-10-29 03:41:11 +0000180 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000181 if (!isa<FunctionType>(CanonicalType)) {
182 // Look through type qualifiers
183 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
184 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000185 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000186 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000187
Steve Naroff7064f5c2007-07-26 18:32:01 +0000188 // If this is a typedef for a function type, strip the typedef off without
189 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000190 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000191}
192
Chris Lattnerb77792e2008-07-26 22:17:49 +0000193const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
194 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
195}
196
197
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000198const PointerLikeType *Type::getAsPointerLikeType() const {
199 // If this is directly a pointer-like type, return it.
200 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
201 return PTy;
202
203 // If the canonical form of this type isn't the right kind, reject it.
204 if (!isa<PointerLikeType>(CanonicalType)) {
205 // Look through type qualifiers
206 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
207 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
208 return 0;
209 }
210
211 // If this is a typedef for a pointer type, strip the typedef off without
212 // losing all typedef information.
213 return getDesugaredType()->getAsPointerLikeType();
214}
215
Chris Lattnerbefee482007-07-31 16:53:04 +0000216const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000217 // If this is directly a pointer type, return it.
218 if (const PointerType *PTy = dyn_cast<PointerType>(this))
219 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000220
Chris Lattnerdea61462007-10-29 03:41:11 +0000221 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000222 if (!isa<PointerType>(CanonicalType)) {
223 // Look through type qualifiers
224 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
225 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000226 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000227 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000228
Chris Lattnera2c77672007-07-16 22:05:22 +0000229 // If this is a typedef for a pointer type, strip the typedef off without
230 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000231 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000232}
233
Steve Naroff5618bd42008-08-27 16:04:49 +0000234const BlockPointerType *Type::getAsBlockPointerType() const {
235 // If this is directly a block pointer type, return it.
236 if (const BlockPointerType *PTy = dyn_cast<BlockPointerType>(this))
237 return PTy;
238
239 // If the canonical form of this type isn't the right kind, reject it.
240 if (!isa<BlockPointerType>(CanonicalType))
241 return 0;
242
243 // If this is a typedef for a block pointer type, strip the typedef off
244 // without losing all typedef information.
245 return getDesugaredType()->getAsBlockPointerType();
246}
247
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000248const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000249 // If this is directly a reference type, return it.
250 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
251 return RTy;
252
Chris Lattnerdea61462007-10-29 03:41:11 +0000253 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000254 if (!isa<ReferenceType>(CanonicalType)) {
255 // Look through type qualifiers
256 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
257 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000258 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000259 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000260
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000261 // If this is a typedef for a reference type, strip the typedef off without
262 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000263 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000264}
265
Eli Friedmand3f2f792008-02-17 00:59:11 +0000266/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
267/// array types and types that contain variable array types in their
268/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000269bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000270 // A VLA is a variably modified type.
271 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000272 return true;
273
274 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000275 if (const Type *T = getArrayElementTypeNoTypeQual())
276 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000277
278 // A pointer can point to a variably modified type
279 if (const PointerType* PT = getAsPointerType())
280 return PT->getPointeeType()->isVariablyModifiedType();
281
282 // A function can return a variably modified type
283 // This one isn't completely obvious, but it follows from the
284 // definition in C99 6.7.5p3. Because of this rule, it's
285 // illegal to declare a function returning a variably modified type.
286 if (const FunctionType* FT = getAsFunctionType())
287 return FT->getResultType()->isVariablyModifiedType();
288
Steve Naroffd7444aa2007-08-31 17:20:07 +0000289 return false;
290}
291
Chris Lattnerc8629632007-07-31 19:29:30 +0000292const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000293 // If this is directly a reference type, return it.
294 if (const RecordType *RTy = dyn_cast<RecordType>(this))
295 return RTy;
296
Chris Lattnerdea61462007-10-29 03:41:11 +0000297 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000298 if (!isa<RecordType>(CanonicalType)) {
299 // Look through type qualifiers
300 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
301 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000302 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000303 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000304
305 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000306 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000307 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000308}
309
Chris Lattnerc8629632007-07-31 19:29:30 +0000310const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000311 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000312 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000313 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000314 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000315 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000316
317 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000318 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000319 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000320 return 0;
321
322 // If this is a typedef for a structure type, strip the typedef off without
323 // losing all typedef information.
324 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000326 // Look through type qualifiers
327 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
328 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000329 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000330}
331
Chris Lattnerc8629632007-07-31 19:29:30 +0000332const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000333 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000334 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000335 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000336 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000337 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000338
Chris Lattnerdea61462007-10-29 03:41:11 +0000339 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000340 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000341 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000342 return 0;
343
344 // If this is a typedef for a union type, strip the typedef off without
345 // losing all typedef information.
346 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000348
349 // Look through type qualifiers
350 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
351 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000352 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000353}
354
Eli Friedmanad74a752008-06-28 06:23:08 +0000355const EnumType *Type::getAsEnumType() const {
356 // Check the canonicalized unqualified type directly; the more complex
357 // version is unnecessary because there isn't any typedef information
358 // to preserve.
359 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
360}
361
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000362const ComplexType *Type::getAsComplexType() const {
363 // Are we directly a complex type?
364 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
365 return CTy;
366
Chris Lattnerdea61462007-10-29 03:41:11 +0000367 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000368 if (!isa<ComplexType>(CanonicalType)) {
369 // Look through type qualifiers
370 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
371 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000372 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000373 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000374
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000375 // If this is a typedef for a complex type, strip the typedef off without
376 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000377 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000378}
379
Chris Lattnerc8629632007-07-31 19:29:30 +0000380const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000381 // Are we directly a vector type?
382 if (const VectorType *VTy = dyn_cast<VectorType>(this))
383 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000384
Chris Lattnerdea61462007-10-29 03:41:11 +0000385 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000386 if (!isa<VectorType>(CanonicalType)) {
387 // Look through type qualifiers
388 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
389 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000390 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000391 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000392
Chris Lattnera2c77672007-07-16 22:05:22 +0000393 // If this is a typedef for a vector type, strip the typedef off without
394 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000395 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000396}
397
Nate Begeman213541a2008-04-18 23:10:10 +0000398const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000399 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000400 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000401 return VTy;
402
Chris Lattnerdea61462007-10-29 03:41:11 +0000403 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000404 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000405 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000406 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
407 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000408 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000409 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000410
Nate Begeman213541a2008-04-18 23:10:10 +0000411 // If this is a typedef for an extended vector type, strip the typedef off
412 // without losing all typedef information.
413 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000414}
415
Chris Lattner368eefa2008-04-07 00:27:04 +0000416const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000417 // There is no sugar for ObjCInterfaceType's, just return the canonical
418 // type pointer if it is the right class.
419 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000420}
421
422const ObjCQualifiedInterfaceType *
423Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000424 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
425 // type pointer if it is the right class.
426 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
427}
428
429const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
430 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
431 // type pointer if it is the right class.
432 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000433}
434
435
Reid Spencer5f016e22007-07-11 17:01:13 +0000436bool Type::isIntegerType() const {
437 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
438 return BT->getKind() >= BuiltinType::Bool &&
439 BT->getKind() <= BuiltinType::LongLong;
440 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000441 // Incomplete enum types are not treated as integer types.
442 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000444 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
445 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000446 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
447 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 return false;
449}
450
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000451bool Type::isIntegralType() const {
452 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
453 return BT->getKind() >= BuiltinType::Bool &&
454 BT->getKind() <= BuiltinType::LongLong;
455 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000456 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
457 return true; // Complete enum types are integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000458 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
459 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000460 return false;
461}
462
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000463bool Type::isEnumeralType() const {
464 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000465 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000466 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
467 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000468 return false;
469}
470
471bool Type::isBooleanType() const {
472 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
473 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000474 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
475 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000476 return false;
477}
478
479bool Type::isCharType() const {
480 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
481 return BT->getKind() == BuiltinType::Char_U ||
482 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000483 BT->getKind() == BuiltinType::Char_S ||
484 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000485 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
486 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000487 return false;
488}
489
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000490/// isSignedIntegerType - Return true if this is an integer type that is
491/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
492/// an enum decl which has a signed representation, or a vector of signed
493/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000494bool Type::isSignedIntegerType() const {
495 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
496 return BT->getKind() >= BuiltinType::Char_S &&
497 BT->getKind() <= BuiltinType::LongLong;
498 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000499
Chris Lattner37c1b782008-04-06 22:29:16 +0000500 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
501 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000502
Steve Naroffc63b96a2007-07-12 21:46:55 +0000503 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
504 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000505 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
506 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 return false;
508}
509
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000510/// isUnsignedIntegerType - Return true if this is an integer type that is
511/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
512/// decl which has an unsigned representation, or a vector of unsigned integer
513/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000514bool Type::isUnsignedIntegerType() const {
515 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
516 return BT->getKind() >= BuiltinType::Bool &&
517 BT->getKind() <= BuiltinType::ULongLong;
518 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000519
Chris Lattner37c1b782008-04-06 22:29:16 +0000520 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
521 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000522
Steve Naroffc63b96a2007-07-12 21:46:55 +0000523 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
524 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000525 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
526 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 return false;
528}
529
530bool Type::isFloatingType() const {
531 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
532 return BT->getKind() >= BuiltinType::Float &&
533 BT->getKind() <= BuiltinType::LongDouble;
534 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000535 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000536 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
537 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000538 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
539 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 return false;
541}
542
543bool Type::isRealFloatingType() const {
544 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
545 return BT->getKind() >= BuiltinType::Float &&
546 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000547 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
548 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000549 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
550 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 return false;
552}
553
554bool Type::isRealType() const {
555 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
556 return BT->getKind() >= BuiltinType::Bool &&
557 BT->getKind() <= BuiltinType::LongDouble;
558 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000559 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000560 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
561 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000562 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
563 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 return false;
565}
566
Reid Spencer5f016e22007-07-11 17:01:13 +0000567bool Type::isArithmeticType() const {
568 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
569 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000570 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
571 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
572 // If a body isn't seen by the time we get here, return false.
573 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000574 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
575 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
577}
578
579bool Type::isScalarType() const {
580 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
581 return BT->getKind() != BuiltinType::Void;
582 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000583 // Enums are scalar types, but only if they are defined. Incomplete enums
584 // are not treated as scalar types.
585 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 return true;
587 return false;
588 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000589 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
590 return ASQT->getBaseType()->isScalarType();
Steve Naroff5618bd42008-08-27 16:04:49 +0000591 return isa<PointerType>(CanonicalType) ||
592 isa<BlockPointerType>(CanonicalType) ||
593 isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000594 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000595}
596
597bool Type::isAggregateType() const {
598 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000599 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 return true;
601 return false;
602 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000603 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
604 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000605 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606}
607
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000608/// isConstantSizeType - Return true if this is not a variable sized type,
609/// according to the rules of C99 6.7.5p3. It is not legal to call this on
610/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000611bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000612 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000613 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000614 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000615 // The VAT must have a size, as it is known to be complete.
616 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000617}
618
619/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
620/// - a type that can describe objects, but which lacks information needed to
621/// determine its size.
622bool Type::isIncompleteType() const {
623 switch (CanonicalType->getTypeClass()) {
624 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000625 case ASQual:
626 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 case Builtin:
628 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
629 // be completed.
630 return isVoidType();
631 case Tagged:
632 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
633 // forward declaration, but not a full definition (C99 6.2.5p22).
634 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000635 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000637 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 }
639}
640
641bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000642 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
643 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
645 if (!BT) return false;
646 switch (BT->getKind()) {
647 case BuiltinType::Bool:
648 case BuiltinType::Char_S:
649 case BuiltinType::Char_U:
650 case BuiltinType::SChar:
651 case BuiltinType::UChar:
652 case BuiltinType::Short:
653 case BuiltinType::UShort:
654 return true;
655 default:
656 return false;
657 }
658}
659
660const char *BuiltinType::getName() const {
661 switch (getKind()) {
662 default: assert(0 && "Unknown builtin type!");
663 case Void: return "void";
664 case Bool: return "_Bool";
665 case Char_S: return "char";
666 case Char_U: return "char";
667 case SChar: return "signed char";
668 case Short: return "short";
669 case Int: return "int";
670 case Long: return "long";
671 case LongLong: return "long long";
672 case UChar: return "unsigned char";
673 case UShort: return "unsigned short";
674 case UInt: return "unsigned int";
675 case ULong: return "unsigned long";
676 case ULongLong: return "unsigned long long";
677 case Float: return "float";
678 case Double: return "double";
679 case LongDouble: return "long double";
Argyrios Kyrtzidis46713ef2008-08-09 17:11:33 +0000680 case WChar: return "wchar_t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 }
682}
683
Reid Spencer5f016e22007-07-11 17:01:13 +0000684void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000685 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 unsigned NumArgs, bool isVariadic) {
687 ID.AddPointer(Result.getAsOpaquePtr());
688 for (unsigned i = 0; i != NumArgs; ++i)
689 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
690 ID.AddInteger(isVariadic);
691}
692
693void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000694 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000695}
696
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000697void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000698 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000699 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000700 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000701 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000702 for (unsigned i = 0; i != NumProtocols; i++)
703 ID.AddPointer(protocols[i]);
704}
705
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000706void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000707 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000708}
709
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000710void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000711 ObjCProtocolDecl **protocols,
712 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000713 for (unsigned i = 0; i != NumProtocols; i++)
714 ID.AddPointer(protocols[i]);
715}
716
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000717void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000718 Profile(ID, &Protocols[0], getNumProtocols());
719}
720
Chris Lattnera2c77672007-07-16 22:05:22 +0000721/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
722/// potentially looking through *all* consequtive typedefs. This returns the
723/// sum of the type qualifiers, so if you have:
724/// typedef const int A;
725/// typedef volatile A B;
726/// looking through the typedefs for B will give you "const volatile A".
727///
728QualType TypedefType::LookThroughTypedefs() const {
729 // Usually, there is only a single level of typedefs, be fast in that case.
730 QualType FirstType = getDecl()->getUnderlyingType();
731 if (!isa<TypedefType>(FirstType))
732 return FirstType;
733
734 // Otherwise, do the fully general loop.
735 unsigned TypeQuals = 0;
736 const TypedefType *TDT = this;
737 while (1) {
738 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000739
740
741 /// FIXME:
742 /// FIXME: This is incorrect for ASQuals!
743 /// FIXME:
744 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000745
746 TDT = dyn_cast<TypedefType>(CurType);
747 if (TDT == 0)
748 return QualType(CurType.getTypePtr(), TypeQuals);
749 }
750}
Reid Spencer5f016e22007-07-11 17:01:13 +0000751
Chris Lattner2daa5df2008-04-06 22:04:54 +0000752bool RecordType::classof(const TagType *TT) {
753 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000754}
755
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000756bool CXXRecordType::classof(const TagType *TT) {
757 return isa<CXXRecordDecl>(TT->getDecl());
758}
759
Chris Lattner2daa5df2008-04-06 22:04:54 +0000760bool EnumType::classof(const TagType *TT) {
761 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000762}
763
Reid Spencer5f016e22007-07-11 17:01:13 +0000764
765//===----------------------------------------------------------------------===//
766// Type Printing
767//===----------------------------------------------------------------------===//
768
769void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000770 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 getAsStringInternal(R);
772 if (msg)
773 fprintf(stderr, "%s: %s\n", msg, R.c_str());
774 else
775 fprintf(stderr, "%s\n", R.c_str());
776}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000777void QualType::dump() const {
778 dump("");
779}
780
781void Type::dump() const {
782 std::string S = "identifier";
783 getAsStringInternal(S);
784 fprintf(stderr, "%s\n", S.c_str());
785}
786
787
Reid Spencer5f016e22007-07-11 17:01:13 +0000788
789static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
790 // Note: funkiness to ensure we get a space only between quals.
791 bool NonePrinted = true;
792 if (TypeQuals & QualType::Const)
793 S += "const", NonePrinted = false;
794 if (TypeQuals & QualType::Volatile)
795 S += (NonePrinted+" volatile"), NonePrinted = false;
796 if (TypeQuals & QualType::Restrict)
797 S += (NonePrinted+" restrict"), NonePrinted = false;
798}
799
800void QualType::getAsStringInternal(std::string &S) const {
801 if (isNull()) {
802 S += "NULL TYPE\n";
803 return;
804 }
805
806 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000807 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000809 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 if (!S.empty())
811 S = TQS + ' ' + S;
812 else
813 S = TQS;
814 }
815
816 getTypePtr()->getAsStringInternal(S);
817}
818
819void BuiltinType::getAsStringInternal(std::string &S) const {
820 if (S.empty()) {
821 S = getName();
822 } else {
823 // Prefix the basic type, e.g. 'int X'.
824 S = ' ' + S;
825 S = getName() + S;
826 }
827}
828
829void ComplexType::getAsStringInternal(std::string &S) const {
830 ElementType->getAsStringInternal(S);
831 S = "_Complex " + S;
832}
833
Christopher Lambebb97e92008-02-04 02:31:56 +0000834void ASQualType::getAsStringInternal(std::string &S) const {
835 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
836 BaseType->getAsStringInternal(S);
837}
838
Reid Spencer5f016e22007-07-11 17:01:13 +0000839void PointerType::getAsStringInternal(std::string &S) const {
840 S = '*' + S;
841
842 // Handle things like 'int (*A)[4];' correctly.
843 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000844 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 S = '(' + S + ')';
846
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000847 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848}
849
Steve Naroff5618bd42008-08-27 16:04:49 +0000850void BlockPointerType::getAsStringInternal(std::string &S) const {
851 S = '^' + S;
852 PointeeType.getAsStringInternal(S);
853}
854
Reid Spencer5f016e22007-07-11 17:01:13 +0000855void ReferenceType::getAsStringInternal(std::string &S) const {
856 S = '&' + S;
857
858 // Handle things like 'int (&A)[4];' correctly.
859 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000860 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 S = '(' + S + ')';
862
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000863 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000864}
865
Steve Narofffb22d962007-08-30 01:06:46 +0000866void ConstantArrayType::getAsStringInternal(std::string &S) const {
867 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000868 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000869 S += ']';
870
871 getElementType().getAsStringInternal(S);
872}
873
Eli Friedmanc5773c42008-02-15 18:16:39 +0000874void IncompleteArrayType::getAsStringInternal(std::string &S) const {
875 S += "[]";
876
877 getElementType().getAsStringInternal(S);
878}
879
Steve Narofffb22d962007-08-30 01:06:46 +0000880void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 S += '[';
882
Steve Naroffc9406122007-08-30 18:10:14 +0000883 if (getIndexTypeQualifier()) {
884 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 S += ' ';
886 }
887
Steve Naroffc9406122007-08-30 18:10:14 +0000888 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000890 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 S += '*';
892
Steve Narofffb22d962007-08-30 01:06:46 +0000893 if (getSizeExpr()) {
894 std::ostringstream s;
895 getSizeExpr()->printPretty(s);
896 S += s.str();
897 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 S += ']';
899
Steve Narofffb22d962007-08-30 01:06:46 +0000900 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000901}
902
903void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000904 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000905 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000907 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 ElementType.getAsStringInternal(S);
909}
910
Nate Begeman213541a2008-04-18 23:10:10 +0000911void ExtVectorType::getAsStringInternal(std::string &S) const {
912 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000913 S += llvm::utostr_32(NumElements);
914 S += ")))";
915 ElementType.getAsStringInternal(S);
916}
917
Steve Naroffd1861fd2007-07-31 12:34:36 +0000918void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000919 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
920 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000921 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000922 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000923 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000924}
925
Steve Naroff363bcff2007-08-01 23:45:51 +0000926void TypeOfType::getAsStringInternal(std::string &InnerString) const {
927 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
928 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000929 std::string Tmp;
930 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000931 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000932}
933
Reid Spencer5f016e22007-07-11 17:01:13 +0000934void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
935 // If needed for precedence reasons, wrap the inner part in grouping parens.
936 if (!S.empty())
937 S = "(" + S + ")";
938
939 S += "()";
940 getResultType().getAsStringInternal(S);
941}
942
943void FunctionTypeProto::getAsStringInternal(std::string &S) const {
944 // If needed for precedence reasons, wrap the inner part in grouping parens.
945 if (!S.empty())
946 S = "(" + S + ")";
947
948 S += "(";
949 std::string Tmp;
950 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
951 if (i) S += ", ";
952 getArgType(i).getAsStringInternal(Tmp);
953 S += Tmp;
954 Tmp.clear();
955 }
956
957 if (isVariadic()) {
958 if (getNumArgs())
959 S += ", ";
960 S += "...";
961 } else if (getNumArgs() == 0) {
962 // Do not emit int() if we have a proto, emit 'int(void)'.
963 S += "void";
964 }
965
966 S += ")";
967 getResultType().getAsStringInternal(S);
968}
969
970
971void TypedefType::getAsStringInternal(std::string &InnerString) const {
972 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
973 InnerString = ' ' + InnerString;
974 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
975}
976
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000977void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000978 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
979 InnerString = ' ' + InnerString;
980 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
981}
982
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000984 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000985 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
986 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000987 std::string ObjCQIString = getDecl()->getName();
988 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +0000989 bool isFirst = true;
990 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
991 if (isFirst)
992 isFirst = false;
993 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994 ObjCQIString += ',';
Chris Lattnercdce6d12008-07-21 05:19:23 +0000995 ObjCQIString += (*I)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000996 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000997 ObjCQIString += '>';
998 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000999}
1000
Chris Lattnere8e4f922008-07-25 23:07:18 +00001001void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001002 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1003 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004 std::string ObjCQIString = "id";
1005 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001006 int num = getNumProtocols();
1007 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001009 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001010 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001011 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001012 ObjCQIString += '>';
1013 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001014}
1015
Reid Spencer5f016e22007-07-11 17:01:13 +00001016void TagType::getAsStringInternal(std::string &InnerString) const {
1017 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1018 InnerString = ' ' + InnerString;
1019
1020 const char *Kind = getDecl()->getKindName();
1021 const char *ID;
1022 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1023 ID = II->getName();
1024 else
1025 ID = "<anonymous>";
1026
1027 InnerString = std::string(Kind) + " " + ID + InnerString;
1028}