blob: f9f6ecac071786c880fe9fc16aa1339caaa708a8 [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>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000025void Type::Destroy(ASTContext& C) { delete this; }
26
27void FunctionTypeProto::Destroy(ASTContext& C) {
28 // Destroy the object, but don't call delete. These are malloc'd.
29 this->~FunctionTypeProto();
30 free(this);
31}
32
33void VariableArrayType::Destroy(ASTContext& C) {
34 SizeExpr->Destroy(C);
35 delete this;
36}
Reid Spencer5f016e22007-07-11 17:01:13 +000037
Chris Lattnerc63a1f22008-08-04 07:31:14 +000038
39/// getArrayElementTypeNoTypeQual - If this is an array type, return the
40/// element type of the array, potentially with type qualifiers missing.
41/// This method should never be used when type qualifiers are meaningful.
42const Type *Type::getArrayElementTypeNoTypeQual() const {
43 // If this is directly an array type, return it.
44 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
45 return ATy->getElementType().getTypePtr();
46
47 // If the canonical form of this type isn't the right kind, reject it.
48 if (!isa<ArrayType>(CanonicalType)) {
49 // Look through type qualifiers
50 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
51 return AT->getElementType().getTypePtr();
52 return 0;
53 }
54
55 // If this is a typedef for an array type, strip the typedef off without
56 // losing all typedef information.
57 return getDesugaredType()->getArrayElementTypeNoTypeQual();
58}
59
60/// getDesugaredType - Return the specified type with any "sugar" removed from
61/// type type. This takes off typedefs, typeof's etc. If the outer level of
62/// the type is already concrete, it returns it unmodified. This is similar
63/// to getting the canonical type, but it doesn't remove *all* typedefs. For
64/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
65/// concrete.
66QualType Type::getDesugaredType() const {
67 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
68 return TDT->LookThroughTypedefs();
69 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
70 return TOE->getUnderlyingExpr()->getType();
71 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
72 return TOT->getUnderlyingType();
73 // FIXME: remove this cast.
74 return QualType(const_cast<Type*>(this), 0);
75}
76
Reid Spencer5f016e22007-07-11 17:01:13 +000077/// isVoidType - Helper method to determine if this is the 'void' type.
78bool Type::isVoidType() const {
79 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
80 return BT->getKind() == BuiltinType::Void;
81 return false;
82}
83
84bool Type::isObjectType() const {
85 if (isa<FunctionType>(CanonicalType))
86 return false;
87 else if (CanonicalType->isIncompleteType())
88 return false;
89 else
90 return true;
91}
92
93bool Type::isDerivedType() const {
94 switch (CanonicalType->getTypeClass()) {
95 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000096 case VariableArray:
97 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +000098 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000099 case FunctionProto:
100 case FunctionNoProto:
101 case Reference:
102 return true;
103 case Tagged: {
104 const TagType *TT = cast<TagType>(CanonicalType);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000105 return !TT->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 }
107 default:
108 return false;
109 }
110}
111
Chris Lattner99dc9142008-04-13 18:59:07 +0000112bool Type::isClassType() const {
113 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000114 if (RT->getDecl()->isClass())
Chris Lattner99dc9142008-04-13 18:59:07 +0000115 return true;
116 return false;
117}
Chris Lattnerc8629632007-07-31 19:29:30 +0000118bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000119 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000120 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000121 return true;
122 return false;
123}
124bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000125 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000126 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000127 return true;
128 return false;
129}
Chris Lattnerc8629632007-07-31 19:29:30 +0000130
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000131bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000132 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
133 return CT->getElementType()->isFloatingType();
134 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000135}
136
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000137bool Type::isComplexIntegerType() const {
138 // Check for GCC complex integer extension.
139 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
140 return CT->getElementType()->isIntegerType();
141 return false;
142}
143
144const ComplexType *Type::getAsComplexIntegerType() const {
145 // Are we directly a complex type?
146 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
147 if (CTy->getElementType()->isIntegerType())
148 return CTy;
149 }
150 // If the canonical form of this type isn't the right kind, reject it.
151 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
152 if (!CTy || !CTy->getElementType()->isIntegerType())
153 return 0;
154
155 // If this is a typedef for a complex type, strip the typedef off without
156 // losing all typedef information.
157 return getDesugaredType()->getAsComplexIntegerType();
158}
159
Steve Naroff77878cc2007-08-27 04:08:11 +0000160const BuiltinType *Type::getAsBuiltinType() const {
161 // If this is directly a builtin type, return it.
162 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
163 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000164
165 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000166 if (!isa<BuiltinType>(CanonicalType)) {
167 // Look through type qualifiers
168 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
169 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000170 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000171 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000172
Steve Naroff77878cc2007-08-27 04:08:11 +0000173 // If this is a typedef for a builtin type, strip the typedef off without
174 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000175 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000176}
177
Chris Lattnerc8629632007-07-31 19:29:30 +0000178const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000179 // If this is directly a function type, return it.
180 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
181 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000182
Chris Lattnerdea61462007-10-29 03:41:11 +0000183 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000184 if (!isa<FunctionType>(CanonicalType)) {
185 // Look through type qualifiers
186 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
187 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000188 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000189 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000190
Steve Naroff7064f5c2007-07-26 18:32:01 +0000191 // If this is a typedef for a function type, strip the typedef off without
192 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000193 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000194}
195
Chris Lattnerb77792e2008-07-26 22:17:49 +0000196const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
197 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
198}
199
200
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000201const PointerLikeType *Type::getAsPointerLikeType() const {
202 // If this is directly a pointer-like type, return it.
203 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
204 return PTy;
205
206 // If the canonical form of this type isn't the right kind, reject it.
207 if (!isa<PointerLikeType>(CanonicalType)) {
208 // Look through type qualifiers
209 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
210 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
211 return 0;
212 }
213
214 // If this is a typedef for a pointer type, strip the typedef off without
215 // losing all typedef information.
216 return getDesugaredType()->getAsPointerLikeType();
217}
218
Chris Lattnerbefee482007-07-31 16:53:04 +0000219const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000220 // If this is directly a pointer type, return it.
221 if (const PointerType *PTy = dyn_cast<PointerType>(this))
222 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000223
Chris Lattnerdea61462007-10-29 03:41:11 +0000224 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000225 if (!isa<PointerType>(CanonicalType)) {
226 // Look through type qualifiers
227 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
228 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000229 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000230 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000231
Chris Lattnera2c77672007-07-16 22:05:22 +0000232 // If this is a typedef for a pointer type, strip the typedef off without
233 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000234 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000235}
236
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000237const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000238 // If this is directly a reference type, return it.
239 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
240 return RTy;
241
Chris Lattnerdea61462007-10-29 03:41:11 +0000242 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000243 if (!isa<ReferenceType>(CanonicalType)) {
244 // Look through type qualifiers
245 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
246 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000247 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000248 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000249
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000250 // If this is a typedef for a reference type, strip the typedef off without
251 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000252 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000253}
254
Eli Friedmand3f2f792008-02-17 00:59:11 +0000255/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
256/// array types and types that contain variable array types in their
257/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000258bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000259 // A VLA is a variably modified type.
260 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000261 return true;
262
263 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000264 if (const Type *T = getArrayElementTypeNoTypeQual())
265 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000266
267 // A pointer can point to a variably modified type
268 if (const PointerType* PT = getAsPointerType())
269 return PT->getPointeeType()->isVariablyModifiedType();
270
271 // A function can return a variably modified type
272 // This one isn't completely obvious, but it follows from the
273 // definition in C99 6.7.5p3. Because of this rule, it's
274 // illegal to declare a function returning a variably modified type.
275 if (const FunctionType* FT = getAsFunctionType())
276 return FT->getResultType()->isVariablyModifiedType();
277
Steve Naroffd7444aa2007-08-31 17:20:07 +0000278 return false;
279}
280
Chris Lattnerc8629632007-07-31 19:29:30 +0000281const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000282 // If this is directly a reference type, return it.
283 if (const RecordType *RTy = dyn_cast<RecordType>(this))
284 return RTy;
285
Chris Lattnerdea61462007-10-29 03:41:11 +0000286 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000287 if (!isa<RecordType>(CanonicalType)) {
288 // Look through type qualifiers
289 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
290 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000291 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000292 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000293
294 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000295 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000296 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000297}
298
Chris Lattnerc8629632007-07-31 19:29:30 +0000299const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000300 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000301 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000302 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000303 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000304 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000305
306 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000307 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000308 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000309 return 0;
310
311 // If this is a typedef for a structure type, strip the typedef off without
312 // losing all typedef information.
313 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000315 // Look through type qualifiers
316 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
317 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000318 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000319}
320
Chris Lattnerc8629632007-07-31 19:29:30 +0000321const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000322 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000323 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000324 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000325 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000326 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000327
Chris Lattnerdea61462007-10-29 03:41:11 +0000328 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000329 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000330 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000331 return 0;
332
333 // If this is a typedef for a union type, strip the typedef off without
334 // losing all typedef information.
335 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000337
338 // Look through type qualifiers
339 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
340 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000341 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000342}
343
Eli Friedmanad74a752008-06-28 06:23:08 +0000344const EnumType *Type::getAsEnumType() const {
345 // Check the canonicalized unqualified type directly; the more complex
346 // version is unnecessary because there isn't any typedef information
347 // to preserve.
348 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
349}
350
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000351const ComplexType *Type::getAsComplexType() const {
352 // Are we directly a complex type?
353 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
354 return CTy;
355
Chris Lattnerdea61462007-10-29 03:41:11 +0000356 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000357 if (!isa<ComplexType>(CanonicalType)) {
358 // Look through type qualifiers
359 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
360 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000361 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000362 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000363
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000364 // If this is a typedef for a complex type, strip the typedef off without
365 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000366 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000367}
368
Chris Lattnerc8629632007-07-31 19:29:30 +0000369const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000370 // Are we directly a vector type?
371 if (const VectorType *VTy = dyn_cast<VectorType>(this))
372 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000373
Chris Lattnerdea61462007-10-29 03:41:11 +0000374 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000375 if (!isa<VectorType>(CanonicalType)) {
376 // Look through type qualifiers
377 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
378 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000379 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000380 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000381
Chris Lattnera2c77672007-07-16 22:05:22 +0000382 // If this is a typedef for a vector type, strip the typedef off without
383 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000384 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000385}
386
Nate Begeman213541a2008-04-18 23:10:10 +0000387const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000388 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000389 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000390 return VTy;
391
Chris Lattnerdea61462007-10-29 03:41:11 +0000392 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000393 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000394 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000395 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
396 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000397 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000398 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000399
Nate Begeman213541a2008-04-18 23:10:10 +0000400 // If this is a typedef for an extended vector type, strip the typedef off
401 // without losing all typedef information.
402 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000403}
404
Chris Lattner368eefa2008-04-07 00:27:04 +0000405const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000406 // There is no sugar for ObjCInterfaceType's, just return the canonical
407 // type pointer if it is the right class.
408 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000409}
410
411const ObjCQualifiedInterfaceType *
412Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000413 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
414 // type pointer if it is the right class.
415 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
416}
417
418const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
419 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
420 // type pointer if it is the right class.
421 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000422}
423
424
Reid Spencer5f016e22007-07-11 17:01:13 +0000425bool Type::isIntegerType() const {
426 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
427 return BT->getKind() >= BuiltinType::Bool &&
428 BT->getKind() <= BuiltinType::LongLong;
429 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000430 // Incomplete enum types are not treated as integer types.
431 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000433 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
434 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000435 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
436 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 return false;
438}
439
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000440bool Type::isIntegralType() const {
441 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
442 return BT->getKind() >= BuiltinType::Bool &&
443 BT->getKind() <= BuiltinType::LongLong;
444 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000445 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
446 return true; // Complete enum types are integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000447 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
448 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000449 return false;
450}
451
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000452bool Type::isEnumeralType() const {
453 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000454 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000455 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
456 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000457 return false;
458}
459
460bool Type::isBooleanType() const {
461 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
462 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000463 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
464 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000465 return false;
466}
467
468bool Type::isCharType() const {
469 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
470 return BT->getKind() == BuiltinType::Char_U ||
471 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000472 BT->getKind() == BuiltinType::Char_S ||
473 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000474 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
475 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000476 return false;
477}
478
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000479/// isSignedIntegerType - Return true if this is an integer type that is
480/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
481/// an enum decl which has a signed representation, or a vector of signed
482/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000483bool Type::isSignedIntegerType() const {
484 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
485 return BT->getKind() >= BuiltinType::Char_S &&
486 BT->getKind() <= BuiltinType::LongLong;
487 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000488
Chris Lattner37c1b782008-04-06 22:29:16 +0000489 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
490 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000491
Steve Naroffc63b96a2007-07-12 21:46:55 +0000492 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
493 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000494 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
495 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 return false;
497}
498
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000499/// isUnsignedIntegerType - Return true if this is an integer type that is
500/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
501/// decl which has an unsigned representation, or a vector of unsigned integer
502/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000503bool Type::isUnsignedIntegerType() const {
504 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
505 return BT->getKind() >= BuiltinType::Bool &&
506 BT->getKind() <= BuiltinType::ULongLong;
507 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000508
Chris Lattner37c1b782008-04-06 22:29:16 +0000509 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
510 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000511
Steve Naroffc63b96a2007-07-12 21:46:55 +0000512 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
513 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000514 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
515 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 return false;
517}
518
519bool Type::isFloatingType() const {
520 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
521 return BT->getKind() >= BuiltinType::Float &&
522 BT->getKind() <= BuiltinType::LongDouble;
523 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000524 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000525 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
526 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000527 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
528 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 return false;
530}
531
532bool Type::isRealFloatingType() const {
533 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
534 return BT->getKind() >= BuiltinType::Float &&
535 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000536 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
537 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000538 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
539 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 return false;
541}
542
543bool Type::isRealType() const {
544 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
545 return BT->getKind() >= BuiltinType::Bool &&
546 BT->getKind() <= BuiltinType::LongDouble;
547 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000548 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000549 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
550 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000551 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
552 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 return false;
554}
555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556bool Type::isArithmeticType() const {
557 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
558 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000559 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
560 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
561 // If a body isn't seen by the time we get here, return false.
562 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000563 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
564 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
566}
567
568bool Type::isScalarType() const {
569 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
570 return BT->getKind() != BuiltinType::Void;
571 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000572 // Enums are scalar types, but only if they are defined. Incomplete enums
573 // are not treated as scalar types.
574 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 return true;
576 return false;
577 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000578 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
579 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000580 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000581 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000582}
583
584bool Type::isAggregateType() const {
585 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000586 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 return true;
588 return false;
589 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000590 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
591 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000592 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000593}
594
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000595/// isConstantSizeType - Return true if this is not a variable sized type,
596/// according to the rules of C99 6.7.5p3. It is not legal to call this on
597/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000598bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000599 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000600 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000601 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000602 // The VAT must have a size, as it is known to be complete.
603 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000604}
605
606/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
607/// - a type that can describe objects, but which lacks information needed to
608/// determine its size.
609bool Type::isIncompleteType() const {
610 switch (CanonicalType->getTypeClass()) {
611 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000612 case ASQual:
613 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 case Builtin:
615 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
616 // be completed.
617 return isVoidType();
618 case Tagged:
619 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
620 // forward declaration, but not a full definition (C99 6.2.5p22).
621 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000622 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000624 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 }
626}
627
628bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000629 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
630 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
632 if (!BT) return false;
633 switch (BT->getKind()) {
634 case BuiltinType::Bool:
635 case BuiltinType::Char_S:
636 case BuiltinType::Char_U:
637 case BuiltinType::SChar:
638 case BuiltinType::UChar:
639 case BuiltinType::Short:
640 case BuiltinType::UShort:
641 return true;
642 default:
643 return false;
644 }
645}
646
647const char *BuiltinType::getName() const {
648 switch (getKind()) {
649 default: assert(0 && "Unknown builtin type!");
650 case Void: return "void";
651 case Bool: return "_Bool";
652 case Char_S: return "char";
653 case Char_U: return "char";
654 case SChar: return "signed char";
655 case Short: return "short";
656 case Int: return "int";
657 case Long: return "long";
658 case LongLong: return "long long";
659 case UChar: return "unsigned char";
660 case UShort: return "unsigned short";
661 case UInt: return "unsigned int";
662 case ULong: return "unsigned long";
663 case ULongLong: return "unsigned long long";
664 case Float: return "float";
665 case Double: return "double";
666 case LongDouble: return "long double";
667 }
668}
669
Reid Spencer5f016e22007-07-11 17:01:13 +0000670void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000671 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 unsigned NumArgs, bool isVariadic) {
673 ID.AddPointer(Result.getAsOpaquePtr());
674 for (unsigned i = 0; i != NumArgs; ++i)
675 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
676 ID.AddInteger(isVariadic);
677}
678
679void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000680 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000681}
682
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000684 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000685 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000686 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000687 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000688 for (unsigned i = 0; i != NumProtocols; i++)
689 ID.AddPointer(protocols[i]);
690}
691
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000692void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000693 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000694}
695
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000696void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000697 ObjCProtocolDecl **protocols,
698 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000699 for (unsigned i = 0; i != NumProtocols; i++)
700 ID.AddPointer(protocols[i]);
701}
702
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000703void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000704 Profile(ID, &Protocols[0], getNumProtocols());
705}
706
Chris Lattnera2c77672007-07-16 22:05:22 +0000707/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
708/// potentially looking through *all* consequtive typedefs. This returns the
709/// sum of the type qualifiers, so if you have:
710/// typedef const int A;
711/// typedef volatile A B;
712/// looking through the typedefs for B will give you "const volatile A".
713///
714QualType TypedefType::LookThroughTypedefs() const {
715 // Usually, there is only a single level of typedefs, be fast in that case.
716 QualType FirstType = getDecl()->getUnderlyingType();
717 if (!isa<TypedefType>(FirstType))
718 return FirstType;
719
720 // Otherwise, do the fully general loop.
721 unsigned TypeQuals = 0;
722 const TypedefType *TDT = this;
723 while (1) {
724 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000725
726
727 /// FIXME:
728 /// FIXME: This is incorrect for ASQuals!
729 /// FIXME:
730 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000731
732 TDT = dyn_cast<TypedefType>(CurType);
733 if (TDT == 0)
734 return QualType(CurType.getTypePtr(), TypeQuals);
735 }
736}
Reid Spencer5f016e22007-07-11 17:01:13 +0000737
Chris Lattner2daa5df2008-04-06 22:04:54 +0000738bool RecordType::classof(const TagType *TT) {
739 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000740}
741
Chris Lattner2daa5df2008-04-06 22:04:54 +0000742bool EnumType::classof(const TagType *TT) {
743 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000744}
745
Reid Spencer5f016e22007-07-11 17:01:13 +0000746
747//===----------------------------------------------------------------------===//
748// Type Printing
749//===----------------------------------------------------------------------===//
750
751void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000752 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 getAsStringInternal(R);
754 if (msg)
755 fprintf(stderr, "%s: %s\n", msg, R.c_str());
756 else
757 fprintf(stderr, "%s\n", R.c_str());
758}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000759void QualType::dump() const {
760 dump("");
761}
762
763void Type::dump() const {
764 std::string S = "identifier";
765 getAsStringInternal(S);
766 fprintf(stderr, "%s\n", S.c_str());
767}
768
769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770
771static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
772 // Note: funkiness to ensure we get a space only between quals.
773 bool NonePrinted = true;
774 if (TypeQuals & QualType::Const)
775 S += "const", NonePrinted = false;
776 if (TypeQuals & QualType::Volatile)
777 S += (NonePrinted+" volatile"), NonePrinted = false;
778 if (TypeQuals & QualType::Restrict)
779 S += (NonePrinted+" restrict"), NonePrinted = false;
780}
781
782void QualType::getAsStringInternal(std::string &S) const {
783 if (isNull()) {
784 S += "NULL TYPE\n";
785 return;
786 }
787
788 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000789 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000791 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 if (!S.empty())
793 S = TQS + ' ' + S;
794 else
795 S = TQS;
796 }
797
798 getTypePtr()->getAsStringInternal(S);
799}
800
801void BuiltinType::getAsStringInternal(std::string &S) const {
802 if (S.empty()) {
803 S = getName();
804 } else {
805 // Prefix the basic type, e.g. 'int X'.
806 S = ' ' + S;
807 S = getName() + S;
808 }
809}
810
811void ComplexType::getAsStringInternal(std::string &S) const {
812 ElementType->getAsStringInternal(S);
813 S = "_Complex " + S;
814}
815
Christopher Lambebb97e92008-02-04 02:31:56 +0000816void ASQualType::getAsStringInternal(std::string &S) const {
817 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
818 BaseType->getAsStringInternal(S);
819}
820
Reid Spencer5f016e22007-07-11 17:01:13 +0000821void PointerType::getAsStringInternal(std::string &S) const {
822 S = '*' + S;
823
824 // Handle things like 'int (*A)[4];' correctly.
825 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000826 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 S = '(' + S + ')';
828
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000829 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000830}
831
832void ReferenceType::getAsStringInternal(std::string &S) const {
833 S = '&' + S;
834
835 // Handle things like 'int (&A)[4];' correctly.
836 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000837 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 S = '(' + S + ')';
839
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000840 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000841}
842
Steve Narofffb22d962007-08-30 01:06:46 +0000843void ConstantArrayType::getAsStringInternal(std::string &S) const {
844 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000845 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000846 S += ']';
847
848 getElementType().getAsStringInternal(S);
849}
850
Eli Friedmanc5773c42008-02-15 18:16:39 +0000851void IncompleteArrayType::getAsStringInternal(std::string &S) const {
852 S += "[]";
853
854 getElementType().getAsStringInternal(S);
855}
856
Steve Narofffb22d962007-08-30 01:06:46 +0000857void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 S += '[';
859
Steve Naroffc9406122007-08-30 18:10:14 +0000860 if (getIndexTypeQualifier()) {
861 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 S += ' ';
863 }
864
Steve Naroffc9406122007-08-30 18:10:14 +0000865 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000867 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 S += '*';
869
Steve Narofffb22d962007-08-30 01:06:46 +0000870 if (getSizeExpr()) {
871 std::ostringstream s;
872 getSizeExpr()->printPretty(s);
873 S += s.str();
874 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 S += ']';
876
Steve Narofffb22d962007-08-30 01:06:46 +0000877 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000878}
879
880void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000881 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000882 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000884 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 ElementType.getAsStringInternal(S);
886}
887
Nate Begeman213541a2008-04-18 23:10:10 +0000888void ExtVectorType::getAsStringInternal(std::string &S) const {
889 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000890 S += llvm::utostr_32(NumElements);
891 S += ")))";
892 ElementType.getAsStringInternal(S);
893}
894
Steve Naroffd1861fd2007-07-31 12:34:36 +0000895void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000896 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
897 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000898 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000899 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000900 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000901}
902
Steve Naroff363bcff2007-08-01 23:45:51 +0000903void TypeOfType::getAsStringInternal(std::string &InnerString) const {
904 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
905 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000906 std::string Tmp;
907 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000908 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000909}
910
Reid Spencer5f016e22007-07-11 17:01:13 +0000911void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
912 // If needed for precedence reasons, wrap the inner part in grouping parens.
913 if (!S.empty())
914 S = "(" + S + ")";
915
916 S += "()";
917 getResultType().getAsStringInternal(S);
918}
919
920void FunctionTypeProto::getAsStringInternal(std::string &S) const {
921 // If needed for precedence reasons, wrap the inner part in grouping parens.
922 if (!S.empty())
923 S = "(" + S + ")";
924
925 S += "(";
926 std::string Tmp;
927 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
928 if (i) S += ", ";
929 getArgType(i).getAsStringInternal(Tmp);
930 S += Tmp;
931 Tmp.clear();
932 }
933
934 if (isVariadic()) {
935 if (getNumArgs())
936 S += ", ";
937 S += "...";
938 } else if (getNumArgs() == 0) {
939 // Do not emit int() if we have a proto, emit 'int(void)'.
940 S += "void";
941 }
942
943 S += ")";
944 getResultType().getAsStringInternal(S);
945}
946
947
948void TypedefType::getAsStringInternal(std::string &InnerString) const {
949 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
950 InnerString = ' ' + InnerString;
951 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
952}
953
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000954void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000955 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
956 InnerString = ' ' + InnerString;
957 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
958}
959
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000960void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000961 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000962 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
963 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000964 std::string ObjCQIString = getDecl()->getName();
965 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +0000966 bool isFirst = true;
967 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
968 if (isFirst)
969 isFirst = false;
970 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971 ObjCQIString += ',';
Chris Lattnercdce6d12008-07-21 05:19:23 +0000972 ObjCQIString += (*I)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000973 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974 ObjCQIString += '>';
975 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000976}
977
Chris Lattnere8e4f922008-07-25 23:07:18 +0000978void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000979 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
980 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000981 std::string ObjCQIString = "id";
982 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000983 int num = getNumProtocols();
984 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000985 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000986 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000987 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000988 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989 ObjCQIString += '>';
990 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000991}
992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993void TagType::getAsStringInternal(std::string &InnerString) const {
994 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
995 InnerString = ' ' + InnerString;
996
997 const char *Kind = getDecl()->getKindName();
998 const char *ID;
999 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1000 ID = II->getName();
1001 else
1002 ID = "<anonymous>";
1003
1004 InnerString = std::string(Kind) + " " + ID + InnerString;
1005}