blob: c4b44afd329ed4a177a012b1f3826b47ef66d08a [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"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/Expr.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000019#include "clang/Basic/IdentifierTable.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
21#include "llvm/Support/Streams.h"
22#include "llvm/ADT/StringExtras.h"
Steve Naroff8d1a3b82007-08-01 17:20:42 +000023#include <sstream>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000026void Type::Destroy(ASTContext& C) { delete this; }
27
28void FunctionTypeProto::Destroy(ASTContext& C) {
29 // Destroy the object, but don't call delete. These are malloc'd.
30 this->~FunctionTypeProto();
31 free(this);
32}
33
34void VariableArrayType::Destroy(ASTContext& C) {
35 SizeExpr->Destroy(C);
36 delete this;
37}
Reid Spencer5f016e22007-07-11 17:01:13 +000038
Chris Lattnerc63a1f22008-08-04 07:31:14 +000039
40/// getArrayElementTypeNoTypeQual - If this is an array type, return the
41/// element type of the array, potentially with type qualifiers missing.
42/// This method should never be used when type qualifiers are meaningful.
43const Type *Type::getArrayElementTypeNoTypeQual() const {
44 // If this is directly an array type, return it.
45 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
46 return ATy->getElementType().getTypePtr();
47
48 // If the canonical form of this type isn't the right kind, reject it.
49 if (!isa<ArrayType>(CanonicalType)) {
50 // Look through type qualifiers
51 if (ArrayType *AT = dyn_cast<ArrayType>(CanonicalType.getUnqualifiedType()))
52 return AT->getElementType().getTypePtr();
53 return 0;
54 }
55
56 // If this is a typedef for an array type, strip the typedef off without
57 // losing all typedef information.
58 return getDesugaredType()->getArrayElementTypeNoTypeQual();
59}
60
61/// getDesugaredType - Return the specified type with any "sugar" removed from
62/// type type. This takes off typedefs, typeof's etc. If the outer level of
63/// the type is already concrete, it returns it unmodified. This is similar
64/// to getting the canonical type, but it doesn't remove *all* typedefs. For
65/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
66/// concrete.
67QualType Type::getDesugaredType() const {
68 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
69 return TDT->LookThroughTypedefs();
70 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
71 return TOE->getUnderlyingExpr()->getType();
72 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
73 return TOT->getUnderlyingType();
74 // FIXME: remove this cast.
75 return QualType(const_cast<Type*>(this), 0);
76}
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078/// isVoidType - Helper method to determine if this is the 'void' type.
79bool Type::isVoidType() const {
80 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
81 return BT->getKind() == BuiltinType::Void;
82 return false;
83}
84
85bool Type::isObjectType() const {
86 if (isa<FunctionType>(CanonicalType))
87 return false;
88 else if (CanonicalType->isIncompleteType())
89 return false;
90 else
91 return true;
92}
93
94bool Type::isDerivedType() const {
95 switch (CanonicalType->getTypeClass()) {
96 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000097 case VariableArray:
98 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +000099 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 case FunctionProto:
101 case FunctionNoProto:
102 case Reference:
103 return true;
104 case Tagged: {
105 const TagType *TT = cast<TagType>(CanonicalType);
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000106 return !TT->getDecl()->isEnum();
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 }
108 default:
109 return false;
110 }
111}
112
Chris Lattner99dc9142008-04-13 18:59:07 +0000113bool Type::isClassType() const {
114 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000115 if (RT->getDecl()->isClass())
Chris Lattner99dc9142008-04-13 18:59:07 +0000116 return true;
117 return false;
118}
Chris Lattnerc8629632007-07-31 19:29:30 +0000119bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000120 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000121 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000122 return true;
123 return false;
124}
125bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +0000126 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000127 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000128 return true;
129 return false;
130}
Chris Lattnerc8629632007-07-31 19:29:30 +0000131
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000132bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +0000133 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
134 return CT->getElementType()->isFloatingType();
135 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000136}
137
Steve Naroff4cdec1c2008-01-15 01:41:59 +0000138bool Type::isComplexIntegerType() const {
139 // Check for GCC complex integer extension.
140 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
141 return CT->getElementType()->isIntegerType();
142 return false;
143}
144
145const ComplexType *Type::getAsComplexIntegerType() const {
146 // Are we directly a complex type?
147 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
148 if (CTy->getElementType()->isIntegerType())
149 return CTy;
150 }
151 // If the canonical form of this type isn't the right kind, reject it.
152 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
153 if (!CTy || !CTy->getElementType()->isIntegerType())
154 return 0;
155
156 // If this is a typedef for a complex type, strip the typedef off without
157 // losing all typedef information.
158 return getDesugaredType()->getAsComplexIntegerType();
159}
160
Steve Naroff77878cc2007-08-27 04:08:11 +0000161const BuiltinType *Type::getAsBuiltinType() const {
162 // If this is directly a builtin type, return it.
163 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
164 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000165
166 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000167 if (!isa<BuiltinType>(CanonicalType)) {
168 // Look through type qualifiers
169 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
170 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000171 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000172 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000173
Steve Naroff77878cc2007-08-27 04:08:11 +0000174 // If this is a typedef for a builtin type, strip the typedef off without
175 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000176 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000177}
178
Chris Lattnerc8629632007-07-31 19:29:30 +0000179const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000180 // If this is directly a function type, return it.
181 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
182 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000183
Chris Lattnerdea61462007-10-29 03:41:11 +0000184 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000185 if (!isa<FunctionType>(CanonicalType)) {
186 // Look through type qualifiers
187 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
188 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000189 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000190 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000191
Steve Naroff7064f5c2007-07-26 18:32:01 +0000192 // If this is a typedef for a function type, strip the typedef off without
193 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000194 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000195}
196
Chris Lattnerb77792e2008-07-26 22:17:49 +0000197const FunctionTypeProto *Type::getAsFunctionTypeProto() const {
198 return dyn_cast_or_null<FunctionTypeProto>(getAsFunctionType());
199}
200
201
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000202const PointerLikeType *Type::getAsPointerLikeType() const {
203 // If this is directly a pointer-like type, return it.
204 if (const PointerLikeType *PTy = dyn_cast<PointerLikeType>(this))
205 return PTy;
206
207 // If the canonical form of this type isn't the right kind, reject it.
208 if (!isa<PointerLikeType>(CanonicalType)) {
209 // Look through type qualifiers
210 if (isa<PointerLikeType>(CanonicalType.getUnqualifiedType()))
211 return CanonicalType.getUnqualifiedType()->getAsPointerLikeType();
212 return 0;
213 }
214
215 // If this is a typedef for a pointer type, strip the typedef off without
216 // losing all typedef information.
217 return getDesugaredType()->getAsPointerLikeType();
218}
219
Chris Lattnerbefee482007-07-31 16:53:04 +0000220const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000221 // If this is directly a pointer type, return it.
222 if (const PointerType *PTy = dyn_cast<PointerType>(this))
223 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000224
Chris Lattnerdea61462007-10-29 03:41:11 +0000225 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000226 if (!isa<PointerType>(CanonicalType)) {
227 // Look through type qualifiers
228 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
229 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000230 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000231 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000232
Chris Lattnera2c77672007-07-16 22:05:22 +0000233 // If this is a typedef for a pointer type, strip the typedef off without
234 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000235 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000236}
237
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000238const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000239 // If this is directly a reference type, return it.
240 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
241 return RTy;
242
Chris Lattnerdea61462007-10-29 03:41:11 +0000243 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000244 if (!isa<ReferenceType>(CanonicalType)) {
245 // Look through type qualifiers
246 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
247 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000248 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000249 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000250
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000251 // If this is a typedef for a reference type, strip the typedef off without
252 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000253 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000254}
255
Eli Friedmand3f2f792008-02-17 00:59:11 +0000256/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
257/// array types and types that contain variable array types in their
258/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000259bool Type::isVariablyModifiedType() const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000260 // A VLA is a variably modified type.
261 if (isVariableArrayType())
Eli Friedmand3f2f792008-02-17 00:59:11 +0000262 return true;
263
264 // An array can contain a variably modified type
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000265 if (const Type *T = getArrayElementTypeNoTypeQual())
266 return T->isVariablyModifiedType();
Eli Friedmand3f2f792008-02-17 00:59:11 +0000267
268 // A pointer can point to a variably modified type
269 if (const PointerType* PT = getAsPointerType())
270 return PT->getPointeeType()->isVariablyModifiedType();
271
272 // A function can return a variably modified type
273 // This one isn't completely obvious, but it follows from the
274 // definition in C99 6.7.5p3. Because of this rule, it's
275 // illegal to declare a function returning a variably modified type.
276 if (const FunctionType* FT = getAsFunctionType())
277 return FT->getResultType()->isVariablyModifiedType();
278
Steve Naroffd7444aa2007-08-31 17:20:07 +0000279 return false;
280}
281
Chris Lattnerc8629632007-07-31 19:29:30 +0000282const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000283 // If this is directly a reference type, return it.
284 if (const RecordType *RTy = dyn_cast<RecordType>(this))
285 return RTy;
286
Chris Lattnerdea61462007-10-29 03:41:11 +0000287 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000288 if (!isa<RecordType>(CanonicalType)) {
289 // Look through type qualifiers
290 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
291 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000292 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000293 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000294
295 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000296 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000297 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000298}
299
Chris Lattnerc8629632007-07-31 19:29:30 +0000300const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000301 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000302 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000303 if (RT->getDecl()->isStruct())
Chris Lattnerc8629632007-07-31 19:29:30 +0000304 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000305 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000306
307 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000308 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000309 if (!RT->getDecl()->isStruct())
Chris Lattnerdea61462007-10-29 03:41:11 +0000310 return 0;
311
312 // If this is a typedef for a structure type, strip the typedef off without
313 // losing all typedef information.
314 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000316 // Look through type qualifiers
317 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
318 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000319 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000320}
321
Chris Lattnerc8629632007-07-31 19:29:30 +0000322const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000323 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000324 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000325 if (RT->getDecl()->isUnion())
Chris Lattnerc8629632007-07-31 19:29:30 +0000326 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000327 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000328
Chris Lattnerdea61462007-10-29 03:41:11 +0000329 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000330 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000331 if (!RT->getDecl()->isUnion())
Chris Lattnerdea61462007-10-29 03:41:11 +0000332 return 0;
333
334 // If this is a typedef for a union type, strip the typedef off without
335 // losing all typedef information.
336 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000338
339 // Look through type qualifiers
340 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
341 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000342 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000343}
344
Eli Friedmanad74a752008-06-28 06:23:08 +0000345const EnumType *Type::getAsEnumType() const {
346 // Check the canonicalized unqualified type directly; the more complex
347 // version is unnecessary because there isn't any typedef information
348 // to preserve.
349 return dyn_cast<EnumType>(CanonicalType.getUnqualifiedType());
350}
351
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000352const ComplexType *Type::getAsComplexType() const {
353 // Are we directly a complex type?
354 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
355 return CTy;
356
Chris Lattnerdea61462007-10-29 03:41:11 +0000357 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000358 if (!isa<ComplexType>(CanonicalType)) {
359 // Look through type qualifiers
360 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
361 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000362 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000363 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000364
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000365 // If this is a typedef for a complex type, strip the typedef off without
366 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000367 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000368}
369
Chris Lattnerc8629632007-07-31 19:29:30 +0000370const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000371 // Are we directly a vector type?
372 if (const VectorType *VTy = dyn_cast<VectorType>(this))
373 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000374
Chris Lattnerdea61462007-10-29 03:41:11 +0000375 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000376 if (!isa<VectorType>(CanonicalType)) {
377 // Look through type qualifiers
378 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
379 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000380 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000381 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000382
Chris Lattnera2c77672007-07-16 22:05:22 +0000383 // If this is a typedef for a vector type, strip the typedef off without
384 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000385 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000386}
387
Nate Begeman213541a2008-04-18 23:10:10 +0000388const ExtVectorType *Type::getAsExtVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000389 // Are we directly an OpenCU vector type?
Nate Begeman213541a2008-04-18 23:10:10 +0000390 if (const ExtVectorType *VTy = dyn_cast<ExtVectorType>(this))
Steve Naroff7064f5c2007-07-26 18:32:01 +0000391 return VTy;
392
Chris Lattnerdea61462007-10-29 03:41:11 +0000393 // If the canonical form of this type isn't the right kind, reject it.
Nate Begeman213541a2008-04-18 23:10:10 +0000394 if (!isa<ExtVectorType>(CanonicalType)) {
Christopher Lambebb97e92008-02-04 02:31:56 +0000395 // Look through type qualifiers
Nate Begeman213541a2008-04-18 23:10:10 +0000396 if (isa<ExtVectorType>(CanonicalType.getUnqualifiedType()))
397 return CanonicalType.getUnqualifiedType()->getAsExtVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000398 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000399 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000400
Nate Begeman213541a2008-04-18 23:10:10 +0000401 // If this is a typedef for an extended vector type, strip the typedef off
402 // without losing all typedef information.
403 return getDesugaredType()->getAsExtVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000404}
405
Chris Lattner368eefa2008-04-07 00:27:04 +0000406const ObjCInterfaceType *Type::getAsObjCInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000407 // There is no sugar for ObjCInterfaceType's, just return the canonical
408 // type pointer if it is the right class.
409 return dyn_cast<ObjCInterfaceType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000410}
411
412const ObjCQualifiedInterfaceType *
413Type::getAsObjCQualifiedInterfaceType() const {
Chris Lattnereca7be62008-04-07 05:30:13 +0000414 // There is no sugar for ObjCQualifiedInterfaceType's, just return the canonical
415 // type pointer if it is the right class.
416 return dyn_cast<ObjCQualifiedInterfaceType>(CanonicalType);
417}
418
419const ObjCQualifiedIdType *Type::getAsObjCQualifiedIdType() const {
420 // There is no sugar for ObjCQualifiedIdType's, just return the canonical
421 // type pointer if it is the right class.
422 return dyn_cast<ObjCQualifiedIdType>(CanonicalType);
Chris Lattner368eefa2008-04-07 00:27:04 +0000423}
424
425
Reid Spencer5f016e22007-07-11 17:01:13 +0000426bool Type::isIntegerType() const {
427 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
428 return BT->getKind() >= BuiltinType::Bool &&
429 BT->getKind() <= BuiltinType::LongLong;
430 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000431 // Incomplete enum types are not treated as integer types.
432 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000434 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
435 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000436 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
437 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 return false;
439}
440
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000441bool Type::isIntegralType() const {
442 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
443 return BT->getKind() >= BuiltinType::Bool &&
444 BT->getKind() <= BuiltinType::LongLong;
445 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000446 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
447 return true; // Complete enum types are integral.
Christopher Lambebb97e92008-02-04 02:31:56 +0000448 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
449 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000450 return false;
451}
452
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000453bool Type::isEnumeralType() const {
454 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000455 return TT->getDecl()->isEnum();
Christopher Lambebb97e92008-02-04 02:31:56 +0000456 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
457 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000458 return false;
459}
460
461bool Type::isBooleanType() const {
462 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
463 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000464 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
465 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000466 return false;
467}
468
469bool Type::isCharType() const {
470 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
471 return BT->getKind() == BuiltinType::Char_U ||
472 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000473 BT->getKind() == BuiltinType::Char_S ||
474 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000475 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
476 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000477 return false;
478}
479
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000480/// isSignedIntegerType - Return true if this is an integer type that is
481/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
482/// an enum decl which has a signed representation, or a vector of signed
483/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000484bool Type::isSignedIntegerType() const {
485 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
486 return BT->getKind() >= BuiltinType::Char_S &&
487 BT->getKind() <= BuiltinType::LongLong;
488 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000489
Chris Lattner37c1b782008-04-06 22:29:16 +0000490 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
491 return ET->getDecl()->getIntegerType()->isSignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000492
Steve Naroffc63b96a2007-07-12 21:46:55 +0000493 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
494 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000495 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
496 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 return false;
498}
499
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000500/// isUnsignedIntegerType - Return true if this is an integer type that is
501/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
502/// decl which has an unsigned representation, or a vector of unsigned integer
503/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000504bool Type::isUnsignedIntegerType() const {
505 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
506 return BT->getKind() >= BuiltinType::Bool &&
507 BT->getKind() <= BuiltinType::ULongLong;
508 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000509
Chris Lattner37c1b782008-04-06 22:29:16 +0000510 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
511 return ET->getDecl()->getIntegerType()->isUnsignedIntegerType();
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000512
Steve Naroffc63b96a2007-07-12 21:46:55 +0000513 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
514 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000515 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
516 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 return false;
518}
519
520bool Type::isFloatingType() const {
521 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
522 return BT->getKind() >= BuiltinType::Float &&
523 BT->getKind() <= BuiltinType::LongDouble;
524 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000525 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000526 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
527 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000528 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
529 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 return false;
531}
532
533bool Type::isRealFloatingType() const {
534 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
535 return BT->getKind() >= BuiltinType::Float &&
536 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000537 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
538 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000539 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
540 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 return false;
542}
543
544bool Type::isRealType() const {
545 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
546 return BT->getKind() >= BuiltinType::Bool &&
547 BT->getKind() <= BuiltinType::LongDouble;
548 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Chris Lattner834a72a2008-07-25 23:18:17 +0000549 return TT->getDecl()->isEnum() && TT->getDecl()->isDefinition();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000550 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
551 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000552 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
553 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 return false;
555}
556
Reid Spencer5f016e22007-07-11 17:01:13 +0000557bool Type::isArithmeticType() const {
558 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
559 return BT->getKind() != BuiltinType::Void;
Chris Lattner37c1b782008-04-06 22:29:16 +0000560 if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
561 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
562 // If a body isn't seen by the time we get here, return false.
563 return ET->getDecl()->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000564 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
565 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
567}
568
569bool Type::isScalarType() const {
570 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
571 return BT->getKind() != BuiltinType::Void;
572 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Chris Lattner834a72a2008-07-25 23:18:17 +0000573 // Enums are scalar types, but only if they are defined. Incomplete enums
574 // are not treated as scalar types.
575 if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 return true;
577 return false;
578 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000579 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
580 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000581 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000583}
584
585bool Type::isAggregateType() const {
586 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000587 if (TT->getDecl()->isStruct())
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 return true;
589 return false;
590 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000591 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
592 return ASQT->getBaseType()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000593 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594}
595
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000596/// isConstantSizeType - Return true if this is not a variable sized type,
597/// according to the rules of C99 6.7.5p3. It is not legal to call this on
598/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000599bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000600 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000601 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000602 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000603 // The VAT must have a size, as it is known to be complete.
604 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000605}
606
607/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
608/// - a type that can describe objects, but which lacks information needed to
609/// determine its size.
610bool Type::isIncompleteType() const {
611 switch (CanonicalType->getTypeClass()) {
612 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000613 case ASQual:
614 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 case Builtin:
616 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
617 // be completed.
618 return isVoidType();
619 case Tagged:
620 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
621 // forward declaration, but not a full definition (C99 6.2.5p22).
622 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000623 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000625 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 }
627}
628
629bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000630 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
631 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
633 if (!BT) return false;
634 switch (BT->getKind()) {
635 case BuiltinType::Bool:
636 case BuiltinType::Char_S:
637 case BuiltinType::Char_U:
638 case BuiltinType::SChar:
639 case BuiltinType::UChar:
640 case BuiltinType::Short:
641 case BuiltinType::UShort:
642 return true;
643 default:
644 return false;
645 }
646}
647
648const char *BuiltinType::getName() const {
649 switch (getKind()) {
650 default: assert(0 && "Unknown builtin type!");
651 case Void: return "void";
652 case Bool: return "_Bool";
653 case Char_S: return "char";
654 case Char_U: return "char";
655 case SChar: return "signed char";
656 case Short: return "short";
657 case Int: return "int";
658 case Long: return "long";
659 case LongLong: return "long long";
660 case UChar: return "unsigned char";
661 case UShort: return "unsigned short";
662 case UInt: return "unsigned int";
663 case ULong: return "unsigned long";
664 case ULongLong: return "unsigned long long";
665 case Float: return "float";
666 case Double: return "double";
667 case LongDouble: return "long double";
668 }
669}
670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000672 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 unsigned NumArgs, bool isVariadic) {
674 ID.AddPointer(Result.getAsOpaquePtr());
675 for (unsigned i = 0; i != NumArgs; ++i)
676 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
677 ID.AddInteger(isVariadic);
678}
679
680void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000681 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000682}
683
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000684void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner1ee07002008-04-07 06:37:47 +0000685 const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000686 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000687 unsigned NumProtocols) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000688 ID.AddPointer(Decl);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000689 for (unsigned i = 0; i != NumProtocols; i++)
690 ID.AddPointer(protocols[i]);
691}
692
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000693void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner1ee07002008-04-07 06:37:47 +0000694 Profile(ID, getDecl(), &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000695}
696
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000697void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000698 ObjCProtocolDecl **protocols,
699 unsigned NumProtocols) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000700 for (unsigned i = 0; i != NumProtocols; i++)
701 ID.AddPointer(protocols[i]);
702}
703
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000704void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000705 Profile(ID, &Protocols[0], getNumProtocols());
706}
707
Chris Lattnera2c77672007-07-16 22:05:22 +0000708/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
709/// potentially looking through *all* consequtive typedefs. This returns the
710/// sum of the type qualifiers, so if you have:
711/// typedef const int A;
712/// typedef volatile A B;
713/// looking through the typedefs for B will give you "const volatile A".
714///
715QualType TypedefType::LookThroughTypedefs() const {
716 // Usually, there is only a single level of typedefs, be fast in that case.
717 QualType FirstType = getDecl()->getUnderlyingType();
718 if (!isa<TypedefType>(FirstType))
719 return FirstType;
720
721 // Otherwise, do the fully general loop.
722 unsigned TypeQuals = 0;
723 const TypedefType *TDT = this;
724 while (1) {
725 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000726
727
728 /// FIXME:
729 /// FIXME: This is incorrect for ASQuals!
730 /// FIXME:
731 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000732
733 TDT = dyn_cast<TypedefType>(CurType);
734 if (TDT == 0)
735 return QualType(CurType.getTypePtr(), TypeQuals);
736 }
737}
Reid Spencer5f016e22007-07-11 17:01:13 +0000738
Chris Lattner2daa5df2008-04-06 22:04:54 +0000739bool RecordType::classof(const TagType *TT) {
740 return isa<RecordDecl>(TT->getDecl());
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}
742
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +0000743bool CXXRecordType::classof(const TagType *TT) {
744 return isa<CXXRecordDecl>(TT->getDecl());
745}
746
Chris Lattner2daa5df2008-04-06 22:04:54 +0000747bool EnumType::classof(const TagType *TT) {
748 return isa<EnumDecl>(TT->getDecl());
Chris Lattner5edb8bf2008-04-06 21:58:47 +0000749}
750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751
752//===----------------------------------------------------------------------===//
753// Type Printing
754//===----------------------------------------------------------------------===//
755
756void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000757 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 getAsStringInternal(R);
759 if (msg)
760 fprintf(stderr, "%s: %s\n", msg, R.c_str());
761 else
762 fprintf(stderr, "%s\n", R.c_str());
763}
Chris Lattnerc36d4052008-07-27 00:48:22 +0000764void QualType::dump() const {
765 dump("");
766}
767
768void Type::dump() const {
769 std::string S = "identifier";
770 getAsStringInternal(S);
771 fprintf(stderr, "%s\n", S.c_str());
772}
773
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775
776static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
777 // Note: funkiness to ensure we get a space only between quals.
778 bool NonePrinted = true;
779 if (TypeQuals & QualType::Const)
780 S += "const", NonePrinted = false;
781 if (TypeQuals & QualType::Volatile)
782 S += (NonePrinted+" volatile"), NonePrinted = false;
783 if (TypeQuals & QualType::Restrict)
784 S += (NonePrinted+" restrict"), NonePrinted = false;
785}
786
787void QualType::getAsStringInternal(std::string &S) const {
788 if (isNull()) {
789 S += "NULL TYPE\n";
790 return;
791 }
792
793 // Print qualifiers as appropriate.
Gabor Greif3513e132008-02-21 17:40:55 +0000794 if (unsigned Tq = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 std::string TQS;
Gabor Greif3513e132008-02-21 17:40:55 +0000796 AppendTypeQualList(TQS, Tq);
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 if (!S.empty())
798 S = TQS + ' ' + S;
799 else
800 S = TQS;
801 }
802
803 getTypePtr()->getAsStringInternal(S);
804}
805
806void BuiltinType::getAsStringInternal(std::string &S) const {
807 if (S.empty()) {
808 S = getName();
809 } else {
810 // Prefix the basic type, e.g. 'int X'.
811 S = ' ' + S;
812 S = getName() + S;
813 }
814}
815
816void ComplexType::getAsStringInternal(std::string &S) const {
817 ElementType->getAsStringInternal(S);
818 S = "_Complex " + S;
819}
820
Christopher Lambebb97e92008-02-04 02:31:56 +0000821void ASQualType::getAsStringInternal(std::string &S) const {
822 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
823 BaseType->getAsStringInternal(S);
824}
825
Reid Spencer5f016e22007-07-11 17:01:13 +0000826void PointerType::getAsStringInternal(std::string &S) const {
827 S = '*' + S;
828
829 // Handle things like 'int (*A)[4];' correctly.
830 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000831 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 S = '(' + S + ')';
833
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000834 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835}
836
837void ReferenceType::getAsStringInternal(std::string &S) const {
838 S = '&' + S;
839
840 // Handle things like 'int (&A)[4];' correctly.
841 // FIXME: this should include vectors, but vectors use attributes I guess.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000842 if (isa<ArrayType>(getPointeeType()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 S = '(' + S + ')';
844
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000845 getPointeeType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000846}
847
Steve Narofffb22d962007-08-30 01:06:46 +0000848void ConstantArrayType::getAsStringInternal(std::string &S) const {
849 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000850 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000851 S += ']';
852
853 getElementType().getAsStringInternal(S);
854}
855
Eli Friedmanc5773c42008-02-15 18:16:39 +0000856void IncompleteArrayType::getAsStringInternal(std::string &S) const {
857 S += "[]";
858
859 getElementType().getAsStringInternal(S);
860}
861
Steve Narofffb22d962007-08-30 01:06:46 +0000862void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 S += '[';
864
Steve Naroffc9406122007-08-30 18:10:14 +0000865 if (getIndexTypeQualifier()) {
866 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 S += ' ';
868 }
869
Steve Naroffc9406122007-08-30 18:10:14 +0000870 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000872 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 S += '*';
874
Steve Narofffb22d962007-08-30 01:06:46 +0000875 if (getSizeExpr()) {
876 std::ostringstream s;
877 getSizeExpr()->printPretty(s);
878 S += s.str();
879 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 S += ']';
881
Steve Narofffb22d962007-08-30 01:06:46 +0000882 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883}
884
885void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000886 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000887 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000889 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 ElementType.getAsStringInternal(S);
891}
892
Nate Begeman213541a2008-04-18 23:10:10 +0000893void ExtVectorType::getAsStringInternal(std::string &S) const {
894 S += " __attribute__((ext_vector_type(";
Steve Naroff31a45842007-07-28 23:10:27 +0000895 S += llvm::utostr_32(NumElements);
896 S += ")))";
897 ElementType.getAsStringInternal(S);
898}
899
Steve Naroffd1861fd2007-07-31 12:34:36 +0000900void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000901 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
902 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000903 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000904 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000905 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000906}
907
Steve Naroff363bcff2007-08-01 23:45:51 +0000908void TypeOfType::getAsStringInternal(std::string &InnerString) const {
909 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
910 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000911 std::string Tmp;
912 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000913 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000914}
915
Reid Spencer5f016e22007-07-11 17:01:13 +0000916void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
917 // If needed for precedence reasons, wrap the inner part in grouping parens.
918 if (!S.empty())
919 S = "(" + S + ")";
920
921 S += "()";
922 getResultType().getAsStringInternal(S);
923}
924
925void FunctionTypeProto::getAsStringInternal(std::string &S) const {
926 // If needed for precedence reasons, wrap the inner part in grouping parens.
927 if (!S.empty())
928 S = "(" + S + ")";
929
930 S += "(";
931 std::string Tmp;
932 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
933 if (i) S += ", ";
934 getArgType(i).getAsStringInternal(Tmp);
935 S += Tmp;
936 Tmp.clear();
937 }
938
939 if (isVariadic()) {
940 if (getNumArgs())
941 S += ", ";
942 S += "...";
943 } else if (getNumArgs() == 0) {
944 // Do not emit int() if we have a proto, emit 'int(void)'.
945 S += "void";
946 }
947
948 S += ")";
949 getResultType().getAsStringInternal(S);
950}
951
952
953void TypedefType::getAsStringInternal(std::string &InnerString) const {
954 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
955 InnerString = ' ' + InnerString;
956 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
957}
958
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000959void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000960 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
961 InnerString = ' ' + InnerString;
962 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
963}
964
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000965void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000966 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000967 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
968 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000969 std::string ObjCQIString = getDecl()->getName();
970 ObjCQIString += '<';
Chris Lattnercdce6d12008-07-21 05:19:23 +0000971 bool isFirst = true;
972 for (qual_iterator I = qual_begin(), E = qual_end(); I != E; ++I) {
973 if (isFirst)
974 isFirst = false;
975 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000976 ObjCQIString += ',';
Chris Lattnercdce6d12008-07-21 05:19:23 +0000977 ObjCQIString += (*I)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000978 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000979 ObjCQIString += '>';
980 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000981}
982
Chris Lattnere8e4f922008-07-25 23:07:18 +0000983void ObjCQualifiedIdType::getAsStringInternal(std::string &InnerString) const {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000984 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
985 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 std::string ObjCQIString = "id";
987 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000988 int num = getNumProtocols();
989 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000990 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000991 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000992 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000993 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000994 ObjCQIString += '>';
995 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000996}
997
Reid Spencer5f016e22007-07-11 17:01:13 +0000998void TagType::getAsStringInternal(std::string &InnerString) const {
999 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
1000 InnerString = ' ' + InnerString;
1001
1002 const char *Kind = getDecl()->getKindName();
1003 const char *ID;
1004 if (const IdentifierInfo *II = getDecl()->getIdentifier())
1005 ID = II->getName();
1006 else
1007 ID = "<anonymous>";
1008
1009 InnerString = std::string(Kind) + " " + ID + InnerString;
1010}