blob: 741d59bc7668dafa17512e11c86df6d844e0a370 [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 Lattner99dc9142008-04-13 18:59:07 +000063bool Type::isClassType() const {
64 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
65 if (RT->getDecl()->getKind() == Decl::Class)
66 return true;
67 return false;
68}
Chris Lattnerc8629632007-07-31 19:29:30 +000069bool Type::isStructureType() 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::Struct)
72 return true;
73 return false;
74}
75bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000076 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000077 if (RT->getDecl()->getKind() == Decl::Union)
78 return true;
79 return false;
80}
Chris Lattnerc8629632007-07-31 19:29:30 +000081
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000082bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +000083 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
84 return CT->getElementType()->isFloatingType();
85 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000086}
87
Steve Naroff4cdec1c2008-01-15 01:41:59 +000088bool Type::isComplexIntegerType() const {
89 // Check for GCC complex integer extension.
90 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
91 return CT->getElementType()->isIntegerType();
92 return false;
93}
94
95const ComplexType *Type::getAsComplexIntegerType() const {
96 // Are we directly a complex type?
97 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
98 if (CTy->getElementType()->isIntegerType())
99 return CTy;
100 }
101 // If the canonical form of this type isn't the right kind, reject it.
102 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
103 if (!CTy || !CTy->getElementType()->isIntegerType())
104 return 0;
105
106 // If this is a typedef for a complex type, strip the typedef off without
107 // losing all typedef information.
108 return getDesugaredType()->getAsComplexIntegerType();
109}
110
Chris Lattnerdea61462007-10-29 03:41:11 +0000111/// getDesugaredType - Return the specified type with any "sugar" removed from
112/// type type. This takes off typedefs, typeof's etc. If the outer level of
113/// the type is already concrete, it returns it unmodified. This is similar
114/// to getting the canonical type, but it doesn't remove *all* typedefs. For
115/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
116/// concrete.
117const Type *Type::getDesugaredType() const {
118 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
119 return TDT->LookThroughTypedefs().getTypePtr();
120 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
121 return TOE->getUnderlyingExpr()->getType().getTypePtr();
122 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
123 return TOT->getUnderlyingType().getTypePtr();
124 return this;
125}
126
127
Steve Naroff77878cc2007-08-27 04:08:11 +0000128const BuiltinType *Type::getAsBuiltinType() const {
129 // If this is directly a builtin type, return it.
130 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
131 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000132
133 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000134 if (!isa<BuiltinType>(CanonicalType)) {
135 // Look through type qualifiers
136 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
137 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000138 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000139 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000140
Steve Naroff77878cc2007-08-27 04:08:11 +0000141 // If this is a typedef for a builtin type, strip the typedef off without
142 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000143 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000144}
145
Chris Lattnerc8629632007-07-31 19:29:30 +0000146const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000147 // If this is directly a function type, return it.
148 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
149 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000150
Chris Lattnerdea61462007-10-29 03:41:11 +0000151 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000152 if (!isa<FunctionType>(CanonicalType)) {
153 // Look through type qualifiers
154 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
155 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000156 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000157 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000158
Steve Naroff7064f5c2007-07-26 18:32:01 +0000159 // If this is a typedef for a function type, strip the typedef off without
160 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000161 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000162}
163
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000164const PointerLikeType *Type::getAsPointerLikeType() const {
165 // If this is directly a pointer-like type, return it.
166 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
167 return PTy;
168
169 // If the canonical form of this type isn't the right kind, reject it.
170 if (!isa<PointerLikeType>(CanonicalType)) {
171 // Look through type qualifiers
172 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
173 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
174 return 0;
175 }
176
177 // If this is a typedef for a pointer type, strip the typedef off without
178 // losing all typedef information.
179 return getDesugaredType()->getAsPointerLikeType();
180}
181
Chris Lattnerbefee482007-07-31 16:53:04 +0000182const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000183 // If this is directly a pointer type, return it.
184 if (const PointerType *PTy = dyn_cast<PointerType>(this))
185 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000186
Chris Lattnerdea61462007-10-29 03:41:11 +0000187 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000188 if (!isa<PointerType>(CanonicalType)) {
189 // Look through type qualifiers
190 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
191 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000192 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000193 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000194
Chris Lattnera2c77672007-07-16 22:05:22 +0000195 // If this is a typedef for a pointer type, strip the typedef off without
196 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000197 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000200const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000201 // If this is directly a reference type, return it.
202 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
203 return RTy;
204
Chris Lattnerdea61462007-10-29 03:41:11 +0000205 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000206 if (!isa<ReferenceType>(CanonicalType)) {
207 // Look through type qualifiers
208 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
209 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000210 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000211 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000212
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000213 // If this is a typedef for a reference type, strip the typedef off without
214 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000215 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000216}
217
Chris Lattnerc8629632007-07-31 19:29:30 +0000218const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000219 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000220 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
221 return ATy;
222
Chris Lattnerdea61462007-10-29 03:41:11 +0000223 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000224 if (!isa<ArrayType>(CanonicalType)) {
225 // Look through type qualifiers
226 if (isa<ArrayType>(CanonicalType.getUnqualifiedType()))
227 return CanonicalType.getUnqualifiedType()->getAsArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000228 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000229 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000230
Steve Naroff700204c2007-07-24 21:46:40 +0000231 // If this is a typedef for an array type, strip the typedef off without
232 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000233 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000234}
235
Steve Naroffd7444aa2007-08-31 17:20:07 +0000236const ConstantArrayType *Type::getAsConstantArrayType() const {
237 // If this is directly a constant array type, return it.
238 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
239 return ATy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000240
Chris Lattnerdea61462007-10-29 03:41:11 +0000241 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000242 if (!isa<ConstantArrayType>(CanonicalType)) {
243 // Look through type qualifiers
244 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()))
245 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000246 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000247 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000248
249 // If this is a typedef for a constant array type, strip the typedef off
250 // without losing all typedef information.
251 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000252}
253
254const VariableArrayType *Type::getAsVariableArrayType() const {
255 // If this is directly a variable array type, return it.
256 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
257 return ATy;
258
Chris Lattnerdea61462007-10-29 03:41:11 +0000259 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000260 if (!isa<VariableArrayType>(CanonicalType)) {
261 // Look through type qualifiers
262 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType()))
263 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000264 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000265 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000266
267 // If this is a typedef for a variable array type, strip the typedef off
268 // without losing all typedef information.
269 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000270}
271
Eli Friedmand3f2f792008-02-17 00:59:11 +0000272/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
273/// array types and types that contain variable array types in their
274/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000275bool Type::isVariablyModifiedType() const {
Eli Friedmand3f2f792008-02-17 00:59:11 +0000276 // A VLA is a veriably modified type
277 if (getAsVariableArrayType())
278 return true;
279
280 // An array can contain a variably modified type
281 if (const ArrayType* AT = getAsArrayType())
282 return AT->getElementType()->isVariablyModifiedType();
283
284 // A pointer can point to a variably modified type
285 if (const PointerType* PT = getAsPointerType())
286 return PT->getPointeeType()->isVariablyModifiedType();
287
288 // A function can return a variably modified type
289 // This one isn't completely obvious, but it follows from the
290 // definition in C99 6.7.5p3. Because of this rule, it's
291 // illegal to declare a function returning a variably modified type.
292 if (const FunctionType* FT = getAsFunctionType())
293 return FT->getResultType()->isVariablyModifiedType();
294
Steve Naroffd7444aa2007-08-31 17:20:07 +0000295 return false;
296}
297
Steve Naroff5c06a692008-01-21 22:59:18 +0000298bool Type::isIncompleteArrayType() const {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000299 return isa<IncompleteArrayType>(CanonicalType);
Steve Naroff5c06a692008-01-21 22:59:18 +0000300}
301
Eli Friedmanc5773c42008-02-15 18:16:39 +0000302const IncompleteArrayType *Type::getAsIncompleteArrayType() const {
303 // If this is directly a variable array type, return it.
304 if (const IncompleteArrayType *ATy = dyn_cast<IncompleteArrayType>(this))
305 return ATy;
306
307 // If the canonical form of this type isn't the right kind, reject it.
308 if (!isa<IncompleteArrayType>(CanonicalType)) {
309 // Look through type qualifiers
310 if (isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()))
311 return CanonicalType.getUnqualifiedType()->getAsIncompleteArrayType();
312 return 0;
Steve Naroff5c06a692008-01-21 22:59:18 +0000313 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000314
315 // If this is a typedef for a variable array type, strip the typedef off
316 // without losing all typedef information.
317 return getDesugaredType()->getAsIncompleteArrayType();
Steve Naroff5c06a692008-01-21 22:59:18 +0000318}
319
Chris Lattnerc8629632007-07-31 19:29:30 +0000320const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000321 // If this is directly a reference type, return it.
322 if (const RecordType *RTy = dyn_cast<RecordType>(this))
323 return RTy;
324
Chris Lattnerdea61462007-10-29 03:41:11 +0000325 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000326 if (!isa<RecordType>(CanonicalType)) {
327 // Look through type qualifiers
328 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
329 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000330 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000331 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000332
333 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000334 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000335 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000336}
337
Chris Lattnerc8629632007-07-31 19:29:30 +0000338const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000339 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000340 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
341 if (RT->getDecl()->getKind() == Decl::Struct)
342 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000343 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000344
345 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000346 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000347 if (RT->getDecl()->getKind() != Decl::Struct)
348 return 0;
349
350 // If this is a typedef for a structure type, strip the typedef off without
351 // losing all typedef information.
352 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000354 // Look through type qualifiers
355 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
356 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000357 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000358}
359
Chris Lattnerc8629632007-07-31 19:29:30 +0000360const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000361 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000362 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
363 if (RT->getDecl()->getKind() == Decl::Union)
364 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000365 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000366
Chris Lattnerdea61462007-10-29 03:41:11 +0000367 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000368 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000369 if (RT->getDecl()->getKind() != Decl::Union)
370 return 0;
371
372 // If this is a typedef for a union type, strip the typedef off without
373 // losing all typedef information.
374 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000376
377 // Look through type qualifiers
378 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
379 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000380 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000381}
382
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000383const ComplexType *Type::getAsComplexType() const {
384 // Are we directly a complex type?
385 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
386 return CTy;
387
Chris Lattnerdea61462007-10-29 03:41:11 +0000388 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000389 if (!isa<ComplexType>(CanonicalType)) {
390 // Look through type qualifiers
391 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
392 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000393 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000394 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000395
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000396 // If this is a typedef for a complex type, strip the typedef off without
397 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000398 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000399}
400
Chris Lattnerc8629632007-07-31 19:29:30 +0000401const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000402 // Are we directly a vector type?
403 if (const VectorType *VTy = dyn_cast<VectorType>(this))
404 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000405
Chris Lattnerdea61462007-10-29 03:41:11 +0000406 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000407 if (!isa<VectorType>(CanonicalType)) {
408 // Look through type qualifiers
409 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
410 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000411 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000412 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000413
Chris Lattnera2c77672007-07-16 22:05:22 +0000414 // If this is a typedef for a vector type, strip the typedef off without
415 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000416 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000417}
418
Nate Begeman213541a2008-04-18 23:10:10 +0000419const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000420 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000421 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000422 return VTy;
423
Chris Lattnerdea61462007-10-29 03:41:11 +0000424 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000425 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000426 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000427 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
428 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000429 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000430 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000431
Nate Begeman213541a2008-04-18 23:10:10 +0000432 // If this is a typedef for an extended vector type, strip the typedef off
433 // without losing all typedef information.
434 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000435}
436
Chris Lattner368eefa2008-04-07 00:27:04 +0000437const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000438 // There is no sugar for ObjCInterfaceType's, just return the canonical
439 // type pointer if it is the right class.
440 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000441}
442
443const ObjCQualifiedInterfaceType *
444Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000445 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
446 // type pointer if it is the right class.
447 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
448}
449
450const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
451 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
452 // type pointer if it is the right class.
453 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000454}
455
456
Reid Spencer5f016e22007-07-11 17:01:13 +0000457bool Type::isIntegerType() const {
458 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
459 return BT->getKind() >= BuiltinType::Bool &&
460 BT->getKind() <= BuiltinType::LongLong;
461 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
462 if (TT->getDecl()->getKind() == Decl::Enum)
463 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000464 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
465 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000466 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
467 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 return false;
469}
470
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000471bool Type::isIntegralType() const {
472 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
473 return BT->getKind() >= BuiltinType::Bool &&
474 BT->getKind() <= BuiltinType::LongLong;
475 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
476 if (TT->getDecl()->getKind() == Decl::Enum)
477 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000478 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
479 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000480 return false;
481}
482
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000483bool Type::isEnumeralType() const {
484 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
485 return TT->getDecl()->getKind() == Decl::Enum;
Christopher Lambebb97e92008-02-04 02:31:56 +0000486 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
487 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000488 return false;
489}
490
491bool Type::isBooleanType() const {
492 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
493 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000494 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
495 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000496 return false;
497}
498
499bool Type::isCharType() const {
500 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
501 return BT->getKind() == BuiltinType::Char_U ||
502 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000503 BT->getKind() == BuiltinType::Char_S ||
504 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000505 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
506 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000507 return false;
508}
509
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000510/// isSignedIntegerType - Return true if this is an integer type that is
511/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
512/// an enum decl which has a signed representation, or a vector of signed
513/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000514bool Type::isSignedIntegerType() const {
515 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
516 return BT->getKind() >= BuiltinType::Char_S &&
517 BT->getKind() <= BuiltinType::LongLong;
518 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000519
Chris Lattner37c1b782008-04-06 22:29:16 +0000520 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
521 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000522
Steve Naroffc63b96a2007-07-12 21:46:55 +0000523 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
524 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000525 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
526 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 return false;
528}
529
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000530/// isUnsignedIntegerType - Return true if this is an integer type that is
531/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
532/// decl which has an unsigned representation, or a vector of unsigned integer
533/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000534bool Type::isUnsignedIntegerType() const {
535 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
536 return BT->getKind() >= BuiltinType::Bool &&
537 BT->getKind() <= BuiltinType::ULongLong;
538 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000539
Chris Lattner37c1b782008-04-06 22:29:16 +0000540 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
541 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000542
Steve Naroffc63b96a2007-07-12 21:46:55 +0000543 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
544 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000545 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
546 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 return false;
548}
549
550bool Type::isFloatingType() const {
551 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
552 return BT->getKind() >= BuiltinType::Float &&
553 BT->getKind() <= BuiltinType::LongDouble;
554 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000555 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000556 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
557 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000558 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
559 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 return false;
561}
562
563bool Type::isRealFloatingType() const {
564 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
565 return BT->getKind() >= BuiltinType::Float &&
566 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000567 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
568 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000569 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
570 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 return false;
572}
573
574bool Type::isRealType() const {
575 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
576 return BT->getKind() >= BuiltinType::Bool &&
577 BT->getKind() <= BuiltinType::LongDouble;
578 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
579 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000580 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
581 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000582 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
583 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 return false;
585}
586
Reid Spencer5f016e22007-07-11 17:01:13 +0000587bool Type::isArithmeticType() const {
588 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
589 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000590 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
591 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
592 // If a body isn't seen by the time we get here, return false.
593 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000594 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
595 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
597}
598
599bool Type::isScalarType() const {
600 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
601 return BT->getKind() != BuiltinType::Void;
602 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
603 if (TT->getDecl()->getKind() == Decl::Enum)
604 return true;
605 return false;
606 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000607 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
608 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000609 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000610 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000611}
612
613bool Type::isAggregateType() const {
614 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
615 if (TT->getDecl()->getKind() == Decl::Struct)
616 return true;
617 return false;
618 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000619 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
620 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000621 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000622}
623
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000624/// isConstantSizeType - Return true if this is not a variable sized type,
625/// according to the rules of C99 6.7.5p3. It is not legal to call this on
626/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000627bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000628 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000629 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000630 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000631 // The VAT must have a size, as it is known to be complete.
632 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633}
634
635/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
636/// - a type that can describe objects, but which lacks information needed to
637/// determine its size.
638bool Type::isIncompleteType() const {
639 switch (CanonicalType->getTypeClass()) {
640 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000641 case ASQual:
642 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 case Builtin:
644 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
645 // be completed.
646 return isVoidType();
647 case Tagged:
648 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
649 // forward declaration, but not a full definition (C99 6.2.5p22).
650 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000651 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000653 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 }
655}
656
657bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000658 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
659 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
661 if (!BT) return false;
662 switch (BT->getKind()) {
663 case BuiltinType::Bool:
664 case BuiltinType::Char_S:
665 case BuiltinType::Char_U:
666 case BuiltinType::SChar:
667 case BuiltinType::UChar:
668 case BuiltinType::Short:
669 case BuiltinType::UShort:
670 return true;
671 default:
672 return false;
673 }
674}
675
676const char *BuiltinType::getName() const {
677 switch (getKind()) {
678 default: assert(0 && "Unknown builtin type!");
679 case Void: return "void";
680 case Bool: return "_Bool";
681 case Char_S: return "char";
682 case Char_U: return "char";
683 case SChar: return "signed char";
684 case Short: return "short";
685 case Int: return "int";
686 case Long: return "long";
687 case LongLong: return "long long";
688 case UChar: return "unsigned char";
689 case UShort: return "unsigned short";
690 case UInt: return "unsigned int";
691 case ULong: return "unsigned long";
692 case ULongLong: return "unsigned long long";
693 case Float: return "float";
694 case Double: return "double";
695 case LongDouble: return "long double";
696 }
697}
698
Reid Spencer5f016e22007-07-11 17:01:13 +0000699void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000700 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 unsigned NumArgs, bool isVariadic) {
702 ID.AddPointer(Result.getAsOpaquePtr());
703 for (unsigned i = 0; i != NumArgs; ++i)
704 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
705 ID.AddInteger(isVariadic);
706}
707
708void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000709 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000710}
711
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000712void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000713 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000714 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000715 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000716 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000717 for (unsigned i = 0; i != NumProtocols; i++)
718 ID.AddPointer(protocols[i]);
719}
720
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000721void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000722 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000723}
724
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000725void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
726 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000727 unsigned NumProtocols) {
728 for (unsigned i = 0; i != NumProtocols; i++)
729 ID.AddPointer(protocols[i]);
730}
731
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000732void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000733 Profile(ID, &Protocols[0], getNumProtocols());
734}
735
Chris Lattnera2c77672007-07-16 22:05:22 +0000736/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
737/// potentially looking through *all* consequtive typedefs. This returns the
738/// sum of the type qualifiers, so if you have:
739/// typedef const int A;
740/// typedef volatile A B;
741/// looking through the typedefs for B will give you "const volatile A".
742///
743QualType TypedefType::LookThroughTypedefs() const {
744 // Usually, there is only a single level of typedefs, be fast in that case.
745 QualType FirstType = getDecl()->getUnderlyingType();
746 if (!isa<TypedefType>(FirstType))
747 return FirstType;
748
749 // Otherwise, do the fully general loop.
750 unsigned TypeQuals = 0;
751 const TypedefType *TDT = this;
752 while (1) {
753 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000754
755
756 /// FIXME:
757 /// FIXME: This is incorrect for ASQuals!
758 /// FIXME:
759 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000760
761 TDT = dyn_cast<TypedefType>(CurType);
762 if (TDT == 0)
763 return QualType(CurType.getTypePtr(), TypeQuals);
764 }
765}
Reid Spencer5f016e22007-07-11 17:01:13 +0000766
Chris Lattner2daa5df2008-04-06 22:04:54 +0000767bool RecordType::classof(const TagType *TT) {
768 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000769}
770
Chris Lattner2daa5df2008-04-06 22:04:54 +0000771bool EnumType::classof(const TagType *TT) {
772 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000773}
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775
776//===----------------------------------------------------------------------===//
777// Type Printing
778//===----------------------------------------------------------------------===//
779
780void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000781 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 getAsStringInternal(R);
783 if (msg)
784 fprintf(stderr, "%s: %s\n", msg, R.c_str());
785 else
786 fprintf(stderr, "%s\n", R.c_str());
787}
788
789static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
790 // Note: funkiness to ensure we get a space only between quals.
791 bool NonePrinted = true;
792 if (TypeQuals & QualType::Const)
793 S += "const", NonePrinted = false;
794 if (TypeQuals & QualType::Volatile)
795 S += (NonePrinted+" volatile"), NonePrinted = false;
796 if (TypeQuals & QualType::Restrict)
797 S += (NonePrinted+" restrict"), NonePrinted = false;
798}
799
800void QualType::getAsStringInternal(std::string &S) const {
801 if (isNull()) {
802 S += "NULL TYPE\n";
803 return;
804 }
805
806 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000807 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000809 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 if (!S.empty())
811 S = TQS + ' ' + S;
812 else
813 S = TQS;
814 }
815
816 getTypePtr()->getAsStringInternal(S);
817}
818
819void BuiltinType::getAsStringInternal(std::string &S) const {
820 if (S.empty()) {
821 S = getName();
822 } else {
823 // Prefix the basic type, e.g. 'int X'.
824 S = ' ' + S;
825 S = getName() + S;
826 }
827}
828
829void ComplexType::getAsStringInternal(std::string &S) const {
830 ElementType->getAsStringInternal(S);
831 S = "_Complex " + S;
832}
833
Christopher Lambebb97e92008-02-04 02:31:56 +0000834void ASQualType::getAsStringInternal(std::string &S) const {
835 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
836 BaseType->getAsStringInternal(S);
837}
838
Reid Spencer5f016e22007-07-11 17:01:13 +0000839void PointerType::getAsStringInternal(std::string &S) const {
840 S = '*' + S;
841
842 // Handle things like 'int (*A)[4];' correctly.
843 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000844 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 S = '(' + S + ')';
846
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000847 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848}
849
850void ReferenceType::getAsStringInternal(std::string &S) const {
851 S = '&' + S;
852
853 // Handle things like 'int (&A)[4];' correctly.
854 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000855 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 S = '(' + S + ')';
857
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000858 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859}
860
Steve Narofffb22d962007-08-30 01:06:46 +0000861void ConstantArrayType::getAsStringInternal(std::string &S) const {
862 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000863 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000864 S += ']';
865
866 getElementType().getAsStringInternal(S);
867}
868
Eli Friedmanc5773c42008-02-15 18:16:39 +0000869void IncompleteArrayType::getAsStringInternal(std::string &S) const {
870 S += "[]";
871
872 getElementType().getAsStringInternal(S);
873}
874
Steve Narofffb22d962007-08-30 01:06:46 +0000875void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 S += '[';
877
Steve Naroffc9406122007-08-30 18:10:14 +0000878 if (getIndexTypeQualifier()) {
879 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 S += ' ';
881 }
882
Steve Naroffc9406122007-08-30 18:10:14 +0000883 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000885 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 S += '*';
887
Steve Narofffb22d962007-08-30 01:06:46 +0000888 if (getSizeExpr()) {
889 std::ostringstream s;
890 getSizeExpr()->printPretty(s);
891 S += s.str();
892 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 S += ']';
894
Steve Narofffb22d962007-08-30 01:06:46 +0000895 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000896}
897
898void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000899 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000900 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000902 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 ElementType.getAsStringInternal(S);
904}
905
Nate Begeman213541a2008-04-18 23:10:10 +0000906void ExtVectorType::getAsStringInternal(std::string &S) const {
907 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000908 S += llvm::utostr_32(NumElements);
909 S += ")))";
910 ElementType.getAsStringInternal(S);
911}
912
Steve Naroffd1861fd2007-07-31 12:34:36 +0000913void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000914 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
915 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000916 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000917 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000918 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000919}
920
Steve Naroff363bcff2007-08-01 23:45:51 +0000921void TypeOfType::getAsStringInternal(std::string &InnerString) const {
922 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
923 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000924 std::string Tmp;
925 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000926 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000927}
928
Reid Spencer5f016e22007-07-11 17:01:13 +0000929void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
930 // If needed for precedence reasons, wrap the inner part in grouping parens.
931 if (!S.empty())
932 S = "(" + S + ")";
933
934 S += "()";
935 getResultType().getAsStringInternal(S);
936}
937
938void FunctionTypeProto::getAsStringInternal(std::string &S) const {
939 // If needed for precedence reasons, wrap the inner part in grouping parens.
940 if (!S.empty())
941 S = "(" + S + ")";
942
943 S += "(";
944 std::string Tmp;
945 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
946 if (i) S += ", ";
947 getArgType(i).getAsStringInternal(Tmp);
948 S += Tmp;
949 Tmp.clear();
950 }
951
952 if (isVariadic()) {
953 if (getNumArgs())
954 S += ", ";
955 S += "...";
956 } else if (getNumArgs() == 0) {
957 // Do not emit int() if we have a proto, emit 'int(void)'.
958 S += "void";
959 }
960
961 S += ")";
962 getResultType().getAsStringInternal(S);
963}
964
965
966void TypedefType::getAsStringInternal(std::string &InnerString) const {
967 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
968 InnerString = ' ' + InnerString;
969 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
970}
971
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000972void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000973 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
974 InnerString = ' ' + InnerString;
975 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
976}
977
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000979 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000980 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
981 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000982 std::string ObjCQIString = getDecl()->getName();
983 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000984 int num = getNumProtocols();
985 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000987 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000988 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000989 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000990 ObjCQIString += '>';
991 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000992}
993
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000995 std::string &InnerString) const {
996 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
997 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 std::string ObjCQIString = "id";
999 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001000 int num = getNumProtocols();
1001 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001002 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001003 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001005 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006 ObjCQIString += '>';
1007 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001008}
1009
Reid Spencer5f016e22007-07-11 17:01:13 +00001010void TagType::getAsStringInternal(std::string &InnerString) const {
1011 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1012 InnerString = ' ' + InnerString;
1013
1014 const char *Kind = getDecl()->getKindName();
1015 const char *ID;
1016 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1017 ID = II->getName();
1018 else
1019 ID = "<anonymous>";
1020
1021 InnerString = std::string(Kind) + " " + ID + InnerString;
1022}