blob: f80f3641da020508a0701fec46cbea009b0e44b2 [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
25Type::~Type() {}
26
27/// isVoidType - Helper method to determine if this is the 'void' type.
28bool Type::isVoidType() const {
29 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
30 return BT->getKind() == BuiltinType::Void;
31 return false;
32}
33
34bool Type::isObjectType() const {
35 if (isa<FunctionType>(CanonicalType))
36 return false;
37 else if (CanonicalType->isIncompleteType())
38 return false;
39 else
40 return true;
41}
42
43bool Type::isDerivedType() const {
44 switch (CanonicalType->getTypeClass()) {
45 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000046 case VariableArray:
47 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +000048 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000049 case FunctionProto:
50 case FunctionNoProto:
51 case Reference:
52 return true;
53 case Tagged: {
54 const TagType *TT = cast<TagType>(CanonicalType);
55 const Decl::Kind Kind = TT->getDecl()->getKind();
56 return Kind == Decl::Struct || Kind == Decl::Union;
57 }
58 default:
59 return false;
60 }
61}
62
Chris Lattnerc8629632007-07-31 19:29:30 +000063bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000064 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000065 if (RT->getDecl()->getKind() == Decl::Struct)
66 return true;
67 return false;
68}
69bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000070 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000071 if (RT->getDecl()->getKind() == Decl::Union)
72 return true;
73 return false;
74}
Chris Lattnerc8629632007-07-31 19:29:30 +000075
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000076bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +000077 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
78 return CT->getElementType()->isFloatingType();
79 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000080}
81
Steve Naroff4cdec1c2008-01-15 01:41:59 +000082bool Type::isComplexIntegerType() const {
83 // Check for GCC complex integer extension.
84 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
85 return CT->getElementType()->isIntegerType();
86 return false;
87}
88
89const ComplexType *Type::getAsComplexIntegerType() const {
90 // Are we directly a complex type?
91 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
92 if (CTy->getElementType()->isIntegerType())
93 return CTy;
94 }
95 // If the canonical form of this type isn't the right kind, reject it.
96 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
97 if (!CTy || !CTy->getElementType()->isIntegerType())
98 return 0;
99
100 // If this is a typedef for a complex type, strip the typedef off without
101 // losing all typedef information.
102 return getDesugaredType()->getAsComplexIntegerType();
103}
104
Chris Lattnerdea61462007-10-29 03:41:11 +0000105/// getDesugaredType - Return the specified type with any "sugar" removed from
106/// type type. This takes off typedefs, typeof's etc. If the outer level of
107/// the type is already concrete, it returns it unmodified. This is similar
108/// to getting the canonical type, but it doesn't remove *all* typedefs. For
109/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
110/// concrete.
111const Type *Type::getDesugaredType() const {
112 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
113 return TDT->LookThroughTypedefs().getTypePtr();
114 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
115 return TOE->getUnderlyingExpr()->getType().getTypePtr();
116 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
117 return TOT->getUnderlyingType().getTypePtr();
118 return this;
119}
120
121
Steve Naroff77878cc2007-08-27 04:08:11 +0000122const BuiltinType *Type::getAsBuiltinType() const {
123 // If this is directly a builtin type, return it.
124 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
125 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000126
127 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000128 if (!isa<BuiltinType>(CanonicalType)) {
129 // Look through type qualifiers
130 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
131 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000132 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000133 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000134
Steve Naroff77878cc2007-08-27 04:08:11 +0000135 // If this is a typedef for a builtin type, strip the typedef off without
136 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000137 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000138}
139
Chris Lattnerc8629632007-07-31 19:29:30 +0000140const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000141 // If this is directly a function type, return it.
142 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
143 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000144
Chris Lattnerdea61462007-10-29 03:41:11 +0000145 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000146 if (!isa<FunctionType>(CanonicalType)) {
147 // Look through type qualifiers
148 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
149 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000150 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000151 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000152
Steve Naroff7064f5c2007-07-26 18:32:01 +0000153 // If this is a typedef for a function type, strip the typedef off without
154 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000155 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000156}
157
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000158const PointerLikeType *Type::getAsPointerLikeType() const {
159 // If this is directly a pointer-like type, return it.
160 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
161 return PTy;
162
163 // If the canonical form of this type isn't the right kind, reject it.
164 if (!isa<PointerLikeType>(CanonicalType)) {
165 // Look through type qualifiers
166 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
167 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
168 return 0;
169 }
170
171 // If this is a typedef for a pointer type, strip the typedef off without
172 // losing all typedef information.
173 return getDesugaredType()->getAsPointerLikeType();
174}
175
Chris Lattnerbefee482007-07-31 16:53:04 +0000176const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000177 // If this is directly a pointer type, return it.
178 if (const PointerType *PTy = dyn_cast<PointerType>(this))
179 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000180
Chris Lattnerdea61462007-10-29 03:41:11 +0000181 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000182 if (!isa<PointerType>(CanonicalType)) {
183 // Look through type qualifiers
184 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
185 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000186 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000187 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000188
Chris Lattnera2c77672007-07-16 22:05:22 +0000189 // If this is a typedef for a pointer type, strip the typedef off without
190 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000191 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000192}
193
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000194const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000195 // If this is directly a reference type, return it.
196 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
197 return RTy;
198
Chris Lattnerdea61462007-10-29 03:41:11 +0000199 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000200 if (!isa<ReferenceType>(CanonicalType)) {
201 // Look through type qualifiers
202 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
203 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000204 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000205 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000206
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000207 // If this is a typedef for a reference type, strip the typedef off without
208 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000209 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Chris Lattnerc8629632007-07-31 19:29:30 +0000212const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000213 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000214 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
215 return ATy;
216
Chris Lattnerdea61462007-10-29 03:41:11 +0000217 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000218 if (!isa<ArrayType>(CanonicalType)) {
219 // Look through type qualifiers
220 if (isa<ArrayType>(CanonicalType.getUnqualifiedType()))
221 return CanonicalType.getUnqualifiedType()->getAsArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000222 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000223 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000224
Steve Naroff700204c2007-07-24 21:46:40 +0000225 // If this is a typedef for an array type, strip the typedef off without
226 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000227 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000228}
229
Steve Naroffd7444aa2007-08-31 17:20:07 +0000230const ConstantArrayType *Type::getAsConstantArrayType() const {
231 // If this is directly a constant array type, return it.
232 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
233 return ATy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000234
Chris Lattnerdea61462007-10-29 03:41:11 +0000235 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000236 if (!isa<ConstantArrayType>(CanonicalType)) {
237 // Look through type qualifiers
238 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()))
239 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000240 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000241 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000242
243 // If this is a typedef for a constant array type, strip the typedef off
244 // without losing all typedef information.
245 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000246}
247
248const VariableArrayType *Type::getAsVariableArrayType() const {
249 // If this is directly a variable array type, return it.
250 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
251 return ATy;
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<VariableArrayType>(CanonicalType)) {
255 // Look through type qualifiers
256 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType()))
257 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType();
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
261 // If this is a typedef for a variable array type, strip the typedef off
262 // without losing all typedef information.
263 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +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 {
Eli Friedmand3f2f792008-02-17 00:59:11 +0000270 // A VLA is a veriably modified type
271 if (getAsVariableArrayType())
272 return true;
273
274 // An array can contain a variably modified type
275 if (const ArrayType* AT = getAsArrayType())
276 return AT->getElementType()->isVariablyModifiedType();
277
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
Steve Naroff5c06a692008-01-21 22:59:18 +0000292bool Type::isIncompleteArrayType() const {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000293 return isa<IncompleteArrayType>(CanonicalType);
Steve Naroff5c06a692008-01-21 22:59:18 +0000294}
295
Eli Friedmanc5773c42008-02-15 18:16:39 +0000296const IncompleteArrayType *Type::getAsIncompleteArrayType() const {
297 // If this is directly a variable array type, return it.
298 if (const IncompleteArrayType *ATy = dyn_cast<IncompleteArrayType>(this))
299 return ATy;
300
301 // If the canonical form of this type isn't the right kind, reject it.
302 if (!isa<IncompleteArrayType>(CanonicalType)) {
303 // Look through type qualifiers
304 if (isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()))
305 return CanonicalType.getUnqualifiedType()->getAsIncompleteArrayType();
306 return 0;
Steve Naroff5c06a692008-01-21 22:59:18 +0000307 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000308
309 // If this is a typedef for a variable array type, strip the typedef off
310 // without losing all typedef information.
311 return getDesugaredType()->getAsIncompleteArrayType();
Steve Naroff5c06a692008-01-21 22:59:18 +0000312}
313
Chris Lattnerc8629632007-07-31 19:29:30 +0000314const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000315 // If this is directly a reference type, return it.
316 if (const RecordType *RTy = dyn_cast<RecordType>(this))
317 return RTy;
318
Chris Lattnerdea61462007-10-29 03:41:11 +0000319 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000320 if (!isa<RecordType>(CanonicalType)) {
321 // Look through type qualifiers
322 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
323 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000324 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000325 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000326
327 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000328 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000329 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000330}
331
Chris Lattnerc8629632007-07-31 19:29:30 +0000332const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000333 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000334 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
335 if (RT->getDecl()->getKind() == Decl::Struct)
336 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000337 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000338
339 // 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)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000341 if (RT->getDecl()->getKind() != Decl::Struct)
342 return 0;
343
344 // If this is a typedef for a structure type, strip the typedef off without
345 // losing all typedef information.
346 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000348 // Look through type qualifiers
349 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
350 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000351 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000352}
353
Chris Lattnerc8629632007-07-31 19:29:30 +0000354const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000355 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000356 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
357 if (RT->getDecl()->getKind() == Decl::Union)
358 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000359 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000360
Chris Lattnerdea61462007-10-29 03:41:11 +0000361 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000362 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000363 if (RT->getDecl()->getKind() != Decl::Union)
364 return 0;
365
366 // If this is a typedef for a union type, strip the typedef off without
367 // losing all typedef information.
368 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000370
371 // Look through type qualifiers
372 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
373 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000374 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000375}
376
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000377const ComplexType *Type::getAsComplexType() const {
378 // Are we directly a complex type?
379 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
380 return CTy;
381
Chris Lattnerdea61462007-10-29 03:41:11 +0000382 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000383 if (!isa<ComplexType>(CanonicalType)) {
384 // Look through type qualifiers
385 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
386 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000387 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000388 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000389
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000390 // If this is a typedef for a complex type, strip the typedef off without
391 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000392 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000393}
394
Chris Lattnerc8629632007-07-31 19:29:30 +0000395const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000396 // Are we directly a vector type?
397 if (const VectorType *VTy = dyn_cast<VectorType>(this))
398 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000399
Chris Lattnerdea61462007-10-29 03:41:11 +0000400 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000401 if (!isa<VectorType>(CanonicalType)) {
402 // Look through type qualifiers
403 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
404 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000405 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000406 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000407
Chris Lattnera2c77672007-07-16 22:05:22 +0000408 // If this is a typedef for a vector type, strip the typedef off without
409 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000410 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000411}
412
Chris Lattnerc8629632007-07-31 19:29:30 +0000413const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000414 // Are we directly an OpenCU vector type?
415 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
416 return VTy;
417
Chris Lattnerdea61462007-10-29 03:41:11 +0000418 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000419 if (!isa<OCUVectorType>(CanonicalType)) {
420 // Look through type qualifiers
421 if (isa<OCUVectorType>(CanonicalType.getUnqualifiedType()))
422 return CanonicalType.getUnqualifiedType()->getAsOCUVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000423 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000424 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000425
Chris Lattnerdea61462007-10-29 03:41:11 +0000426 // If this is a typedef for an ocuvector type, strip the typedef off without
427 // losing all typedef information.
428 return getDesugaredType()->getAsOCUVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000429}
430
Chris Lattner368eefa2008-04-07 00:27:04 +0000431const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000432 // There is no sugar for ObjCInterfaceType's, just return the canonical
433 // type pointer if it is the right class.
434 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000435}
436
437const ObjCQualifiedInterfaceType *
438Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000439 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
440 // type pointer if it is the right class.
441 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
442}
443
444const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
445 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
446 // type pointer if it is the right class.
447 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000448}
449
450
Reid Spencer5f016e22007-07-11 17:01:13 +0000451bool Type::isIntegerType() 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))
456 if (TT->getDecl()->getKind() == Decl::Enum)
457 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000458 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
459 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000460 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
461 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 return false;
463}
464
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000465bool Type::isIntegralType() const {
466 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
467 return BT->getKind() >= BuiltinType::Bool &&
468 BT->getKind() <= BuiltinType::LongLong;
469 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
470 if (TT->getDecl()->getKind() == Decl::Enum)
471 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000472 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
473 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000474 return false;
475}
476
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000477bool Type::isEnumeralType() const {
478 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
479 return TT->getDecl()->getKind() == Decl::Enum;
Christopher Lambebb97e92008-02-04 02:31:56 +0000480 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
481 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000482 return false;
483}
484
485bool Type::isBooleanType() const {
486 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
487 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000488 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
489 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000490 return false;
491}
492
493bool Type::isCharType() const {
494 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
495 return BT->getKind() == BuiltinType::Char_U ||
496 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000497 BT->getKind() == BuiltinType::Char_S ||
498 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000499 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
500 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000501 return false;
502}
503
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000504/// isSignedIntegerType - Return true if this is an integer type that is
505/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
506/// an enum decl which has a signed representation, or a vector of signed
507/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000508bool Type::isSignedIntegerType() const {
509 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
510 return BT->getKind() >= BuiltinType::Char_S &&
511 BT->getKind() <= BuiltinType::LongLong;
512 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000513
Chris Lattner37c1b782008-04-06 22:29:16 +0000514 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
515 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000516
Steve Naroffc63b96a2007-07-12 21:46:55 +0000517 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
518 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000519 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
520 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 return false;
522}
523
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000524/// isUnsignedIntegerType - Return true if this is an integer type that is
525/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
526/// decl which has an unsigned representation, or a vector of unsigned integer
527/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000528bool Type::isUnsignedIntegerType() const {
529 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
530 return BT->getKind() >= BuiltinType::Bool &&
531 BT->getKind() <= BuiltinType::ULongLong;
532 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000533
Chris Lattner37c1b782008-04-06 22:29:16 +0000534 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
535 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000536
Steve Naroffc63b96a2007-07-12 21:46:55 +0000537 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
538 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000539 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
540 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 return false;
542}
543
544bool Type::isFloatingType() const {
545 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
546 return BT->getKind() >= BuiltinType::Float &&
547 BT->getKind() <= BuiltinType::LongDouble;
548 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000549 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000550 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
551 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000552 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
553 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 return false;
555}
556
557bool Type::isRealFloatingType() const {
558 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
559 return BT->getKind() >= BuiltinType::Float &&
560 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000561 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
562 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000563 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
564 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 return false;
566}
567
568bool Type::isRealType() const {
569 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
570 return BT->getKind() >= BuiltinType::Bool &&
571 BT->getKind() <= BuiltinType::LongDouble;
572 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
573 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000574 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
575 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000576 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
577 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 return false;
579}
580
Reid Spencer5f016e22007-07-11 17:01:13 +0000581bool Type::isArithmeticType() const {
582 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
583 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000584 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
585 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
586 // If a body isn't seen by the time we get here, return false.
587 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000588 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
589 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
591}
592
593bool Type::isScalarType() const {
594 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
595 return BT->getKind() != BuiltinType::Void;
596 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
597 if (TT->getDecl()->getKind() == Decl::Enum)
598 return true;
599 return false;
600 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000601 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
602 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000603 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000604 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000605}
606
607bool Type::isAggregateType() const {
608 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
609 if (TT->getDecl()->getKind() == Decl::Struct)
610 return true;
611 return false;
612 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000613 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
614 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000615 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000616}
617
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000618/// isConstantSizeType - Return true if this is not a variable sized type,
619/// according to the rules of C99 6.7.5p3. It is not legal to call this on
620/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000621bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000622 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000623 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000624 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000625 // The VAT must have a size, as it is known to be complete.
626 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000627}
628
629/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
630/// - a type that can describe objects, but which lacks information needed to
631/// determine its size.
632bool Type::isIncompleteType() const {
633 switch (CanonicalType->getTypeClass()) {
634 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000635 case ASQual:
636 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 case Builtin:
638 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
639 // be completed.
640 return isVoidType();
641 case Tagged:
642 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
643 // forward declaration, but not a full definition (C99 6.2.5p22).
644 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000645 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000647 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 }
649}
650
651bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000652 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
653 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
655 if (!BT) return false;
656 switch (BT->getKind()) {
657 case BuiltinType::Bool:
658 case BuiltinType::Char_S:
659 case BuiltinType::Char_U:
660 case BuiltinType::SChar:
661 case BuiltinType::UChar:
662 case BuiltinType::Short:
663 case BuiltinType::UShort:
664 return true;
665 default:
666 return false;
667 }
668}
669
670const char *BuiltinType::getName() const {
671 switch (getKind()) {
672 default: assert(0 && "Unknown builtin type!");
673 case Void: return "void";
674 case Bool: return "_Bool";
675 case Char_S: return "char";
676 case Char_U: return "char";
677 case SChar: return "signed char";
678 case Short: return "short";
679 case Int: return "int";
680 case Long: return "long";
681 case LongLong: return "long long";
682 case UChar: return "unsigned char";
683 case UShort: return "unsigned short";
684 case UInt: return "unsigned int";
685 case ULong: return "unsigned long";
686 case ULongLong: return "unsigned long long";
687 case Float: return "float";
688 case Double: return "double";
689 case LongDouble: return "long double";
690 }
691}
692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000694 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 unsigned NumArgs, bool isVariadic) {
696 ID.AddPointer(Result.getAsOpaquePtr());
697 for (unsigned i = 0; i != NumArgs; ++i)
698 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
699 ID.AddInteger(isVariadic);
700}
701
702void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000703 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000704}
705
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000706void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
707 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000708 unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000709 for (unsigned i = 0; i != NumProtocols; i++)
710 ID.AddPointer(protocols[i]);
711}
712
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000713void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000714 Profile(ID, &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000715}
716
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000717void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
718 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000719 unsigned NumProtocols) {
720 for (unsigned i = 0; i != NumProtocols; i++)
721 ID.AddPointer(protocols[i]);
722}
723
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000724void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000725 Profile(ID, &Protocols[0], getNumProtocols());
726}
727
Chris Lattnera2c77672007-07-16 22:05:22 +0000728/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
729/// potentially looking through *all* consequtive typedefs. This returns the
730/// sum of the type qualifiers, so if you have:
731/// typedef const int A;
732/// typedef volatile A B;
733/// looking through the typedefs for B will give you "const volatile A".
734///
735QualType TypedefType::LookThroughTypedefs() const {
736 // Usually, there is only a single level of typedefs, be fast in that case.
737 QualType FirstType = getDecl()->getUnderlyingType();
738 if (!isa<TypedefType>(FirstType))
739 return FirstType;
740
741 // Otherwise, do the fully general loop.
742 unsigned TypeQuals = 0;
743 const TypedefType *TDT = this;
744 while (1) {
745 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000746
747
748 /// FIXME:
749 /// FIXME: This is incorrect for ASQuals!
750 /// FIXME:
751 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000752
753 TDT = dyn_cast<TypedefType>(CurType);
754 if (TDT == 0)
755 return QualType(CurType.getTypePtr(), TypeQuals);
756 }
757}
Reid Spencer5f016e22007-07-11 17:01:13 +0000758
Chris Lattner2daa5df2008-04-06 22:04:54 +0000759bool RecordType::classof(const TagType *TT) {
760 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000761}
762
Chris Lattner2daa5df2008-04-06 22:04:54 +0000763bool EnumType::classof(const TagType *TT) {
764 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000765}
766
Reid Spencer5f016e22007-07-11 17:01:13 +0000767
768//===----------------------------------------------------------------------===//
769// Type Printing
770//===----------------------------------------------------------------------===//
771
772void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000773 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 getAsStringInternal(R);
775 if (msg)
776 fprintf(stderr, "%s: %s\n", msg, R.c_str());
777 else
778 fprintf(stderr, "%s\n", R.c_str());
779}
780
781static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
782 // Note: funkiness to ensure we get a space only between quals.
783 bool NonePrinted = true;
784 if (TypeQuals & QualType::Const)
785 S += "const", NonePrinted = false;
786 if (TypeQuals & QualType::Volatile)
787 S += (NonePrinted+" volatile"), NonePrinted = false;
788 if (TypeQuals & QualType::Restrict)
789 S += (NonePrinted+" restrict"), NonePrinted = false;
790}
791
792void QualType::getAsStringInternal(std::string &S) const {
793 if (isNull()) {
794 S += "NULL TYPE\n";
795 return;
796 }
797
798 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000799 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000801 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 if (!S.empty())
803 S = TQS + ' ' + S;
804 else
805 S = TQS;
806 }
807
808 getTypePtr()->getAsStringInternal(S);
809}
810
811void BuiltinType::getAsStringInternal(std::string &S) const {
812 if (S.empty()) {
813 S = getName();
814 } else {
815 // Prefix the basic type, e.g. 'int X'.
816 S = ' ' + S;
817 S = getName() + S;
818 }
819}
820
821void ComplexType::getAsStringInternal(std::string &S) const {
822 ElementType->getAsStringInternal(S);
823 S = "_Complex " + S;
824}
825
Christopher Lambebb97e92008-02-04 02:31:56 +0000826void ASQualType::getAsStringInternal(std::string &S) const {
827 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
828 BaseType->getAsStringInternal(S);
829}
830
Reid Spencer5f016e22007-07-11 17:01:13 +0000831void PointerType::getAsStringInternal(std::string &S) const {
832 S = '*' + S;
833
834 // Handle things like 'int (*A)[4];' correctly.
835 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000836 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 S = '(' + S + ')';
838
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000839 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000840}
841
842void ReferenceType::getAsStringInternal(std::string &S) const {
843 S = '&' + S;
844
845 // Handle things like 'int (&A)[4];' correctly.
846 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000847 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 S = '(' + S + ')';
849
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000850 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000851}
852
Steve Narofffb22d962007-08-30 01:06:46 +0000853void ConstantArrayType::getAsStringInternal(std::string &S) const {
854 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000855 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000856 S += ']';
857
858 getElementType().getAsStringInternal(S);
859}
860
Eli Friedmanc5773c42008-02-15 18:16:39 +0000861void IncompleteArrayType::getAsStringInternal(std::string &S) const {
862 S += "[]";
863
864 getElementType().getAsStringInternal(S);
865}
866
Steve Narofffb22d962007-08-30 01:06:46 +0000867void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000868 S += '[';
869
Steve Naroffc9406122007-08-30 18:10:14 +0000870 if (getIndexTypeQualifier()) {
871 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 S += ' ';
873 }
874
Steve Naroffc9406122007-08-30 18:10:14 +0000875 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000877 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 S += '*';
879
Steve Narofffb22d962007-08-30 01:06:46 +0000880 if (getSizeExpr()) {
881 std::ostringstream s;
882 getSizeExpr()->printPretty(s);
883 S += s.str();
884 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 S += ']';
886
Steve Narofffb22d962007-08-30 01:06:46 +0000887 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000888}
889
890void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000891 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000892 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000894 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 ElementType.getAsStringInternal(S);
896}
897
Steve Naroff31a45842007-07-28 23:10:27 +0000898void OCUVectorType::getAsStringInternal(std::string &S) const {
899 S += " __attribute__((ocu_vector_type(";
900 S += llvm::utostr_32(NumElements);
901 S += ")))";
902 ElementType.getAsStringInternal(S);
903}
904
Steve Naroffd1861fd2007-07-31 12:34:36 +0000905void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000906 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
907 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000908 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000909 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000910 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000911}
912
Steve Naroff363bcff2007-08-01 23:45:51 +0000913void TypeOfType::getAsStringInternal(std::string &InnerString) const {
914 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
915 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000916 std::string Tmp;
917 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000918 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000919}
920
Reid Spencer5f016e22007-07-11 17:01:13 +0000921void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
922 // If needed for precedence reasons, wrap the inner part in grouping parens.
923 if (!S.empty())
924 S = "(" + S + ")";
925
926 S += "()";
927 getResultType().getAsStringInternal(S);
928}
929
930void FunctionTypeProto::getAsStringInternal(std::string &S) const {
931 // If needed for precedence reasons, wrap the inner part in grouping parens.
932 if (!S.empty())
933 S = "(" + S + ")";
934
935 S += "(";
936 std::string Tmp;
937 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
938 if (i) S += ", ";
939 getArgType(i).getAsStringInternal(Tmp);
940 S += Tmp;
941 Tmp.clear();
942 }
943
944 if (isVariadic()) {
945 if (getNumArgs())
946 S += ", ";
947 S += "...";
948 } else if (getNumArgs() == 0) {
949 // Do not emit int() if we have a proto, emit 'int(void)'.
950 S += "void";
951 }
952
953 S += ")";
954 getResultType().getAsStringInternal(S);
955}
956
957
958void TypedefType::getAsStringInternal(std::string &InnerString) const {
959 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
960 InnerString = ' ' + InnerString;
961 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
962}
963
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000964void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000965 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
966 InnerString = ' ' + InnerString;
967 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
968}
969
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000970void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000971 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000972 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
973 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974 std::string ObjCQIString = getDecl()->getName();
975 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000976 int num = getNumProtocols();
977 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000979 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000980 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000981 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000982 ObjCQIString += '>';
983 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000984}
985
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000987 std::string &InnerString) const {
988 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
989 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000990 std::string ObjCQIString = "id";
991 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000992 int num = getNumProtocols();
993 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000995 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000996 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000997 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 ObjCQIString += '>';
999 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001000}
1001
Reid Spencer5f016e22007-07-11 17:01:13 +00001002void TagType::getAsStringInternal(std::string &InnerString) const {
1003 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1004 InnerString = ' ' + InnerString;
1005
1006 const char *Kind = getDecl()->getKindName();
1007 const char *ID;
1008 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1009 ID = II->getName();
1010 else
1011 ID = "<anonymous>";
1012
1013 InnerString = std::string(Kind) + " " + ID + InnerString;
1014}