blob: f5f7f3d82c4ed4c4a6cae197dcc7624eca3e4f99 [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>
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26Type::~Type() {}
27
28/// isVoidType - Helper method to determine if this is the 'void' type.
29bool Type::isVoidType() const {
30 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
31 return BT->getKind() == BuiltinType::Void;
32 return false;
33}
34
35bool Type::isObjectType() const {
36 if (isa<FunctionType>(CanonicalType))
37 return false;
38 else if (CanonicalType->isIncompleteType())
39 return false;
40 else
41 return true;
42}
43
44bool Type::isDerivedType() const {
45 switch (CanonicalType->getTypeClass()) {
46 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000047 case VariableArray:
48 case ConstantArray:
Eli Friedmanc5773c42008-02-15 18:16:39 +000049 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000050 case FunctionProto:
51 case FunctionNoProto:
52 case Reference:
53 return true;
54 case Tagged: {
55 const TagType *TT = cast<TagType>(CanonicalType);
56 const Decl::Kind Kind = TT->getDecl()->getKind();
57 return Kind == Decl::Struct || Kind == Decl::Union;
58 }
59 default:
60 return false;
61 }
62}
63
Chris Lattnerc8629632007-07-31 19:29:30 +000064bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000065 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000066 if (RT->getDecl()->getKind() == Decl::Struct)
67 return true;
68 return false;
69}
70bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000071 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000072 if (RT->getDecl()->getKind() == Decl::Union)
73 return true;
74 return false;
75}
Chris Lattnerc8629632007-07-31 19:29:30 +000076
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000077bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +000078 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
79 return CT->getElementType()->isFloatingType();
80 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000081}
82
Steve Naroff4cdec1c2008-01-15 01:41:59 +000083bool Type::isComplexIntegerType() const {
84 // Check for GCC complex integer extension.
85 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
86 return CT->getElementType()->isIntegerType();
87 return false;
88}
89
90const ComplexType *Type::getAsComplexIntegerType() const {
91 // Are we directly a complex type?
92 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
93 if (CTy->getElementType()->isIntegerType())
94 return CTy;
95 }
96 // If the canonical form of this type isn't the right kind, reject it.
97 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
98 if (!CTy || !CTy->getElementType()->isIntegerType())
99 return 0;
100
101 // If this is a typedef for a complex type, strip the typedef off without
102 // losing all typedef information.
103 return getDesugaredType()->getAsComplexIntegerType();
104}
105
Chris Lattnerdea61462007-10-29 03:41:11 +0000106/// getDesugaredType - Return the specified type with any "sugar" removed from
107/// type type. This takes off typedefs, typeof's etc. If the outer level of
108/// the type is already concrete, it returns it unmodified. This is similar
109/// to getting the canonical type, but it doesn't remove *all* typedefs. For
110/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
111/// concrete.
112const Type *Type::getDesugaredType() const {
113 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
114 return TDT->LookThroughTypedefs().getTypePtr();
115 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
116 return TOE->getUnderlyingExpr()->getType().getTypePtr();
117 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
118 return TOT->getUnderlyingType().getTypePtr();
119 return this;
120}
121
122
Steve Naroff77878cc2007-08-27 04:08:11 +0000123const BuiltinType *Type::getAsBuiltinType() const {
124 // If this is directly a builtin type, return it.
125 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
126 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000127
128 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000129 if (!isa<BuiltinType>(CanonicalType)) {
130 // Look through type qualifiers
131 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
132 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000133 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000134 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000135
Steve Naroff77878cc2007-08-27 04:08:11 +0000136 // If this is a typedef for a builtin type, strip the typedef off without
137 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000138 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000139}
140
Chris Lattnerc8629632007-07-31 19:29:30 +0000141const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000142 // If this is directly a function type, return it.
143 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
144 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000145
Chris Lattnerdea61462007-10-29 03:41:11 +0000146 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000147 if (!isa<FunctionType>(CanonicalType)) {
148 // Look through type qualifiers
149 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
150 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000151 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000152 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000153
Steve Naroff7064f5c2007-07-26 18:32:01 +0000154 // If this is a typedef for a function type, strip the typedef off without
155 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000156 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000157}
158
Chris Lattnerbefee482007-07-31 16:53:04 +0000159const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000160 // If this is directly a pointer type, return it.
161 if (const PointerType *PTy = dyn_cast<PointerType>(this))
162 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000163
Chris Lattnerdea61462007-10-29 03:41:11 +0000164 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000165 if (!isa<PointerType>(CanonicalType)) {
166 // Look through type qualifiers
167 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
168 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000169 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000170 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000171
Chris Lattnera2c77672007-07-16 22:05:22 +0000172 // If this is a typedef for a pointer type, strip the typedef off without
173 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000174 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000175}
176
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000177const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000178 // If this is directly a reference type, return it.
179 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
180 return RTy;
181
Chris Lattnerdea61462007-10-29 03:41:11 +0000182 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000183 if (!isa<ReferenceType>(CanonicalType)) {
184 // Look through type qualifiers
185 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
186 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000187 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000188 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000189
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000190 // If this is a typedef for a reference type, strip the typedef off without
191 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000192 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000193}
194
Chris Lattnerc8629632007-07-31 19:29:30 +0000195const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000196 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000197 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
198 return ATy;
199
Chris Lattnerdea61462007-10-29 03:41:11 +0000200 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000201 if (!isa<ArrayType>(CanonicalType)) {
202 // Look through type qualifiers
203 if (isa<ArrayType>(CanonicalType.getUnqualifiedType()))
204 return CanonicalType.getUnqualifiedType()->getAsArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000205 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000206 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000207
Steve Naroff700204c2007-07-24 21:46:40 +0000208 // If this is a typedef for an array type, strip the typedef off without
209 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000210 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
Steve Naroffd7444aa2007-08-31 17:20:07 +0000213const ConstantArrayType *Type::getAsConstantArrayType() const {
214 // If this is directly a constant array type, return it.
215 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
216 return ATy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000217
Chris Lattnerdea61462007-10-29 03:41:11 +0000218 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000219 if (!isa<ConstantArrayType>(CanonicalType)) {
220 // Look through type qualifiers
221 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()))
222 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000223 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000224 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000225
226 // If this is a typedef for a constant array type, strip the typedef off
227 // without losing all typedef information.
228 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000229}
230
231const VariableArrayType *Type::getAsVariableArrayType() const {
232 // If this is directly a variable array type, return it.
233 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
234 return ATy;
235
Chris Lattnerdea61462007-10-29 03:41:11 +0000236 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000237 if (!isa<VariableArrayType>(CanonicalType)) {
238 // Look through type qualifiers
239 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType()))
240 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000241 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000242 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000243
244 // If this is a typedef for a variable array type, strip the typedef off
245 // without losing all typedef information.
246 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000247}
248
Eli Friedmand3f2f792008-02-17 00:59:11 +0000249/// isVariablyModifiedType (C99 6.7.5p3) - Return true for variable length
250/// array types and types that contain variable array types in their
251/// declarator
Steve Naroffd7444aa2007-08-31 17:20:07 +0000252bool Type::isVariablyModifiedType() const {
Eli Friedmand3f2f792008-02-17 00:59:11 +0000253 // A VLA is a veriably modified type
254 if (getAsVariableArrayType())
255 return true;
256
257 // An array can contain a variably modified type
258 if (const ArrayType* AT = getAsArrayType())
259 return AT->getElementType()->isVariablyModifiedType();
260
261 // A pointer can point to a variably modified type
262 if (const PointerType* PT = getAsPointerType())
263 return PT->getPointeeType()->isVariablyModifiedType();
264
265 // A function can return a variably modified type
266 // This one isn't completely obvious, but it follows from the
267 // definition in C99 6.7.5p3. Because of this rule, it's
268 // illegal to declare a function returning a variably modified type.
269 if (const FunctionType* FT = getAsFunctionType())
270 return FT->getResultType()->isVariablyModifiedType();
271
Steve Naroffd7444aa2007-08-31 17:20:07 +0000272 return false;
273}
274
Steve Naroff5c06a692008-01-21 22:59:18 +0000275bool Type::isIncompleteArrayType() const {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000276 return isa<IncompleteArrayType>(CanonicalType);
Steve Naroff5c06a692008-01-21 22:59:18 +0000277}
278
Eli Friedmanc5773c42008-02-15 18:16:39 +0000279const IncompleteArrayType *Type::getAsIncompleteArrayType() const {
280 // If this is directly a variable array type, return it.
281 if (const IncompleteArrayType *ATy = dyn_cast<IncompleteArrayType>(this))
282 return ATy;
283
284 // If the canonical form of this type isn't the right kind, reject it.
285 if (!isa<IncompleteArrayType>(CanonicalType)) {
286 // Look through type qualifiers
287 if (isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType()))
288 return CanonicalType.getUnqualifiedType()->getAsIncompleteArrayType();
289 return 0;
Steve Naroff5c06a692008-01-21 22:59:18 +0000290 }
Eli Friedmanc5773c42008-02-15 18:16:39 +0000291
292 // If this is a typedef for a variable array type, strip the typedef off
293 // without losing all typedef information.
294 return getDesugaredType()->getAsIncompleteArrayType();
Steve Naroff5c06a692008-01-21 22:59:18 +0000295}
296
Chris Lattnerc8629632007-07-31 19:29:30 +0000297const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000298 // If this is directly a reference type, return it.
299 if (const RecordType *RTy = dyn_cast<RecordType>(this))
300 return RTy;
301
Chris Lattnerdea61462007-10-29 03:41:11 +0000302 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000303 if (!isa<RecordType>(CanonicalType)) {
304 // Look through type qualifiers
305 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
306 return CanonicalType.getUnqualifiedType()->getAsRecordType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000307 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000308 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000309
310 // If this is a typedef for a record type, strip the typedef off without
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000311 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000312 return getDesugaredType()->getAsRecordType();
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000313}
314
Chris Lattnerc8629632007-07-31 19:29:30 +0000315const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000316 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000317 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
318 if (RT->getDecl()->getKind() == Decl::Struct)
319 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000320 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000321
322 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000323 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000324 if (RT->getDecl()->getKind() != Decl::Struct)
325 return 0;
326
327 // If this is a typedef for a structure type, strip the typedef off without
328 // losing all typedef information.
329 return getDesugaredType()->getAsStructureType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000331 // Look through type qualifiers
332 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
333 return CanonicalType.getUnqualifiedType()->getAsStructureType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000334 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000335}
336
Chris Lattnerc8629632007-07-31 19:29:30 +0000337const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000338 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000339 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
340 if (RT->getDecl()->getKind() == Decl::Union)
341 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000342 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000343
Chris Lattnerdea61462007-10-29 03:41:11 +0000344 // If the canonical form of this type isn't the right kind, reject it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000345 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000346 if (RT->getDecl()->getKind() != Decl::Union)
347 return 0;
348
349 // If this is a typedef for a union type, strip the typedef off without
350 // losing all typedef information.
351 return getDesugaredType()->getAsUnionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000353
354 // Look through type qualifiers
355 if (isa<RecordType>(CanonicalType.getUnqualifiedType()))
356 return CanonicalType.getUnqualifiedType()->getAsUnionType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000357 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000358}
359
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000360const ComplexType *Type::getAsComplexType() const {
361 // Are we directly a complex type?
362 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
363 return CTy;
364
Chris Lattnerdea61462007-10-29 03:41:11 +0000365 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000366 if (!isa<ComplexType>(CanonicalType)) {
367 // Look through type qualifiers
368 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
369 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000370 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000371 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000372
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000373 // If this is a typedef for a complex type, strip the typedef off without
374 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000375 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000376}
377
Chris Lattnerc8629632007-07-31 19:29:30 +0000378const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000379 // Are we directly a vector type?
380 if (const VectorType *VTy = dyn_cast<VectorType>(this))
381 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000382
Chris Lattnerdea61462007-10-29 03:41:11 +0000383 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000384 if (!isa<VectorType>(CanonicalType)) {
385 // Look through type qualifiers
386 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
387 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000388 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000389 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000390
Chris Lattnera2c77672007-07-16 22:05:22 +0000391 // If this is a typedef for a vector type, strip the typedef off without
392 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000393 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000394}
395
Chris Lattnerc8629632007-07-31 19:29:30 +0000396const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000397 // Are we directly an OpenCU vector type?
398 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
399 return VTy;
400
Chris Lattnerdea61462007-10-29 03:41:11 +0000401 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000402 if (!isa<OCUVectorType>(CanonicalType)) {
403 // Look through type qualifiers
404 if (isa<OCUVectorType>(CanonicalType.getUnqualifiedType()))
405 return CanonicalType.getUnqualifiedType()->getAsOCUVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000406 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000407 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000408
Chris Lattnerdea61462007-10-29 03:41:11 +0000409 // If this is a typedef for an ocuvector type, strip the typedef off without
410 // losing all typedef information.
411 return getDesugaredType()->getAsOCUVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000412}
413
Reid Spencer5f016e22007-07-11 17:01:13 +0000414bool Type::isIntegerType() const {
415 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
416 return BT->getKind() >= BuiltinType::Bool &&
417 BT->getKind() <= BuiltinType::LongLong;
418 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
419 if (TT->getDecl()->getKind() == Decl::Enum)
420 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000421 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
422 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000423 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
424 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 return false;
426}
427
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000428bool Type::isIntegralType() const {
429 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
430 return BT->getKind() >= BuiltinType::Bool &&
431 BT->getKind() <= BuiltinType::LongLong;
432 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
433 if (TT->getDecl()->getKind() == Decl::Enum)
434 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000435 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
436 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000437 return false;
438}
439
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000440bool Type::isEnumeralType() const {
441 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
442 return TT->getDecl()->getKind() == Decl::Enum;
Christopher Lambebb97e92008-02-04 02:31:56 +0000443 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
444 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000445 return false;
446}
447
448bool Type::isBooleanType() const {
449 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
450 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000451 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
452 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000453 return false;
454}
455
456bool Type::isCharType() const {
457 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
458 return BT->getKind() == BuiltinType::Char_U ||
459 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000460 BT->getKind() == BuiltinType::Char_S ||
461 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000462 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
463 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000464 return false;
465}
466
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000467/// isSignedIntegerType - Return true if this is an integer type that is
468/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
469/// an enum decl which has a signed representation, or a vector of signed
470/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000471bool Type::isSignedIntegerType() const {
472 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
473 return BT->getKind() >= BuiltinType::Char_S &&
474 BT->getKind() <= BuiltinType::LongLong;
475 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000476
477 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
478 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
479 return ED->getIntegerType()->isSignedIntegerType();
480
Steve Naroffc63b96a2007-07-12 21:46:55 +0000481 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
482 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000483 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
484 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 return false;
486}
487
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000488/// isUnsignedIntegerType - Return true if this is an integer type that is
489/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
490/// decl which has an unsigned representation, or a vector of unsigned integer
491/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000492bool Type::isUnsignedIntegerType() const {
493 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
494 return BT->getKind() >= BuiltinType::Bool &&
495 BT->getKind() <= BuiltinType::ULongLong;
496 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000497
498 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
499 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
500 return ED->getIntegerType()->isUnsignedIntegerType();
501
Steve Naroffc63b96a2007-07-12 21:46:55 +0000502 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
503 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000504 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
505 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 return false;
507}
508
509bool Type::isFloatingType() const {
510 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
511 return BT->getKind() >= BuiltinType::Float &&
512 BT->getKind() <= BuiltinType::LongDouble;
513 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000514 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000515 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
516 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000517 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
518 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 return false;
520}
521
522bool Type::isRealFloatingType() const {
523 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
524 return BT->getKind() >= BuiltinType::Float &&
525 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000526 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
527 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000528 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
529 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 return false;
531}
532
533bool Type::isRealType() const {
534 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
535 return BT->getKind() >= BuiltinType::Bool &&
536 BT->getKind() <= BuiltinType::LongDouble;
537 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
538 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000539 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
540 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000541 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
542 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 return false;
544}
545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546bool Type::isArithmeticType() const {
547 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
548 return BT->getKind() != BuiltinType::Void;
549 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Steve Naroff67c49e82008-01-16 23:54:22 +0000550 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
551 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
Steve Naroff494783a2008-01-16 23:56:32 +0000552 // If a body isn't seen by the time we get here, return false.
Steve Naroff67c49e82008-01-16 23:54:22 +0000553 return ED->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000554 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
555 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
557}
558
559bool Type::isScalarType() const {
560 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
561 return BT->getKind() != BuiltinType::Void;
562 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
563 if (TT->getDecl()->getKind() == Decl::Enum)
564 return true;
565 return false;
566 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000567 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
568 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000569 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000570 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000571}
572
573bool Type::isAggregateType() const {
574 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
575 if (TT->getDecl()->getKind() == Decl::Struct)
576 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()->isAggregateType();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000581 return isa<ArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000582}
583
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000584/// isConstantSizeType - Return true if this is not a variable sized type,
585/// according to the rules of C99 6.7.5p3. It is not legal to call this on
586/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000587bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000588 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000589 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000590 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000591 // The VAT must have a size, as it is known to be complete.
592 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000593}
594
595/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
596/// - a type that can describe objects, but which lacks information needed to
597/// determine its size.
598bool Type::isIncompleteType() const {
599 switch (CanonicalType->getTypeClass()) {
600 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000601 case ASQual:
602 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 case Builtin:
604 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
605 // be completed.
606 return isVoidType();
607 case Tagged:
608 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
609 // forward declaration, but not a full definition (C99 6.2.5p22).
610 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Eli Friedmanc5773c42008-02-15 18:16:39 +0000611 case IncompleteArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Eli Friedmanc5773c42008-02-15 18:16:39 +0000613 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 }
615}
616
617bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000618 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
619 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
621 if (!BT) return false;
622 switch (BT->getKind()) {
623 case BuiltinType::Bool:
624 case BuiltinType::Char_S:
625 case BuiltinType::Char_U:
626 case BuiltinType::SChar:
627 case BuiltinType::UChar:
628 case BuiltinType::Short:
629 case BuiltinType::UShort:
630 return true;
631 default:
632 return false;
633 }
634}
635
636const char *BuiltinType::getName() const {
637 switch (getKind()) {
638 default: assert(0 && "Unknown builtin type!");
639 case Void: return "void";
640 case Bool: return "_Bool";
641 case Char_S: return "char";
642 case Char_U: return "char";
643 case SChar: return "signed char";
644 case Short: return "short";
645 case Int: return "int";
646 case Long: return "long";
647 case LongLong: return "long long";
648 case UChar: return "unsigned char";
649 case UShort: return "unsigned short";
650 case UInt: return "unsigned int";
651 case ULong: return "unsigned long";
652 case ULongLong: return "unsigned long long";
653 case Float: return "float";
654 case Double: return "double";
655 case LongDouble: return "long double";
656 }
657}
658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000660 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 unsigned NumArgs, bool isVariadic) {
662 ID.AddPointer(Result.getAsOpaquePtr());
663 for (unsigned i = 0; i != NumArgs; ++i)
664 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
665 ID.AddInteger(isVariadic);
666}
667
668void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000669 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000670}
671
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000672void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
673 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000674 unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000675 for (unsigned i = 0; i != NumProtocols; i++)
676 ID.AddPointer(protocols[i]);
677}
678
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000679void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000680 Profile(ID, &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000681}
682
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000683void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
684 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000685 unsigned NumProtocols) {
686 for (unsigned i = 0; i != NumProtocols; i++)
687 ID.AddPointer(protocols[i]);
688}
689
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000690void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000691 Profile(ID, &Protocols[0], getNumProtocols());
692}
693
Chris Lattnera2c77672007-07-16 22:05:22 +0000694/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
695/// potentially looking through *all* consequtive typedefs. This returns the
696/// sum of the type qualifiers, so if you have:
697/// typedef const int A;
698/// typedef volatile A B;
699/// looking through the typedefs for B will give you "const volatile A".
700///
701QualType TypedefType::LookThroughTypedefs() const {
702 // Usually, there is only a single level of typedefs, be fast in that case.
703 QualType FirstType = getDecl()->getUnderlyingType();
704 if (!isa<TypedefType>(FirstType))
705 return FirstType;
706
707 // Otherwise, do the fully general loop.
708 unsigned TypeQuals = 0;
709 const TypedefType *TDT = this;
710 while (1) {
711 QualType CurType = TDT->getDecl()->getUnderlyingType();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000712
713
714 /// FIXME:
715 /// FIXME: This is incorrect for ASQuals!
716 /// FIXME:
717 TypeQuals |= CurType.getCVRQualifiers();
Chris Lattnera2c77672007-07-16 22:05:22 +0000718
719 TDT = dyn_cast<TypedefType>(CurType);
720 if (TDT == 0)
721 return QualType(CurType.getTypePtr(), TypeQuals);
722 }
723}
Reid Spencer5f016e22007-07-11 17:01:13 +0000724
725bool RecordType::classof(const Type *T) {
726 if (const TagType *TT = dyn_cast<TagType>(T))
727 return isa<RecordDecl>(TT->getDecl());
728 return false;
729}
730
731
732//===----------------------------------------------------------------------===//
733// Type Printing
734//===----------------------------------------------------------------------===//
735
736void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000737 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 getAsStringInternal(R);
739 if (msg)
740 fprintf(stderr, "%s: %s\n", msg, R.c_str());
741 else
742 fprintf(stderr, "%s\n", R.c_str());
743}
744
745static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
746 // Note: funkiness to ensure we get a space only between quals.
747 bool NonePrinted = true;
748 if (TypeQuals & QualType::Const)
749 S += "const", NonePrinted = false;
750 if (TypeQuals & QualType::Volatile)
751 S += (NonePrinted+" volatile"), NonePrinted = false;
752 if (TypeQuals & QualType::Restrict)
753 S += (NonePrinted+" restrict"), NonePrinted = false;
754}
755
756void QualType::getAsStringInternal(std::string &S) const {
757 if (isNull()) {
758 S += "NULL TYPE\n";
759 return;
760 }
761
762 // Print qualifiers as appropriate.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000763 if (unsigned TQ = getCVRQualifiers()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 std::string TQS;
765 AppendTypeQualList(TQS, TQ);
766 if (!S.empty())
767 S = TQS + ' ' + S;
768 else
769 S = TQS;
770 }
771
772 getTypePtr()->getAsStringInternal(S);
773}
774
775void BuiltinType::getAsStringInternal(std::string &S) const {
776 if (S.empty()) {
777 S = getName();
778 } else {
779 // Prefix the basic type, e.g. 'int X'.
780 S = ' ' + S;
781 S = getName() + S;
782 }
783}
784
785void ComplexType::getAsStringInternal(std::string &S) const {
786 ElementType->getAsStringInternal(S);
787 S = "_Complex " + S;
788}
789
Christopher Lambebb97e92008-02-04 02:31:56 +0000790void ASQualType::getAsStringInternal(std::string &S) const {
791 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
792 BaseType->getAsStringInternal(S);
793}
794
Reid Spencer5f016e22007-07-11 17:01:13 +0000795void PointerType::getAsStringInternal(std::string &S) const {
796 S = '*' + S;
797
798 // Handle things like 'int (*A)[4];' correctly.
799 // FIXME: this should include vectors, but vectors use attributes I guess.
800 if (isa<ArrayType>(PointeeType.getTypePtr()))
801 S = '(' + S + ')';
802
803 PointeeType.getAsStringInternal(S);
804}
805
806void ReferenceType::getAsStringInternal(std::string &S) const {
807 S = '&' + S;
808
809 // Handle things like 'int (&A)[4];' correctly.
810 // FIXME: this should include vectors, but vectors use attributes I guess.
811 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
812 S = '(' + S + ')';
813
814 ReferenceeType.getAsStringInternal(S);
815}
816
Steve Narofffb22d962007-08-30 01:06:46 +0000817void ConstantArrayType::getAsStringInternal(std::string &S) const {
818 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000819 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000820 S += ']';
821
822 getElementType().getAsStringInternal(S);
823}
824
Eli Friedmanc5773c42008-02-15 18:16:39 +0000825void IncompleteArrayType::getAsStringInternal(std::string &S) const {
826 S += "[]";
827
828 getElementType().getAsStringInternal(S);
829}
830
Steve Narofffb22d962007-08-30 01:06:46 +0000831void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 S += '[';
833
Steve Naroffc9406122007-08-30 18:10:14 +0000834 if (getIndexTypeQualifier()) {
835 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 S += ' ';
837 }
838
Steve Naroffc9406122007-08-30 18:10:14 +0000839 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000841 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 S += '*';
843
Steve Narofffb22d962007-08-30 01:06:46 +0000844 if (getSizeExpr()) {
845 std::ostringstream s;
846 getSizeExpr()->printPretty(s);
847 S += s.str();
848 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 S += ']';
850
Steve Narofffb22d962007-08-30 01:06:46 +0000851 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000852}
853
854void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000855 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000856 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000858 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 ElementType.getAsStringInternal(S);
860}
861
Steve Naroff31a45842007-07-28 23:10:27 +0000862void OCUVectorType::getAsStringInternal(std::string &S) const {
863 S += " __attribute__((ocu_vector_type(";
864 S += llvm::utostr_32(NumElements);
865 S += ")))";
866 ElementType.getAsStringInternal(S);
867}
868
Steve Naroffd1861fd2007-07-31 12:34:36 +0000869void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000870 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
871 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000872 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000873 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000874 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000875}
876
Steve Naroff363bcff2007-08-01 23:45:51 +0000877void TypeOfType::getAsStringInternal(std::string &InnerString) const {
878 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
879 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000880 std::string Tmp;
881 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000882 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000883}
884
Reid Spencer5f016e22007-07-11 17:01:13 +0000885void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
886 // If needed for precedence reasons, wrap the inner part in grouping parens.
887 if (!S.empty())
888 S = "(" + S + ")";
889
890 S += "()";
891 getResultType().getAsStringInternal(S);
892}
893
894void FunctionTypeProto::getAsStringInternal(std::string &S) const {
895 // If needed for precedence reasons, wrap the inner part in grouping parens.
896 if (!S.empty())
897 S = "(" + S + ")";
898
899 S += "(";
900 std::string Tmp;
901 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
902 if (i) S += ", ";
903 getArgType(i).getAsStringInternal(Tmp);
904 S += Tmp;
905 Tmp.clear();
906 }
907
908 if (isVariadic()) {
909 if (getNumArgs())
910 S += ", ";
911 S += "...";
912 } else if (getNumArgs() == 0) {
913 // Do not emit int() if we have a proto, emit 'int(void)'.
914 S += "void";
915 }
916
917 S += ")";
918 getResultType().getAsStringInternal(S);
919}
920
921
922void TypedefType::getAsStringInternal(std::string &InnerString) const {
923 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
924 InnerString = ' ' + InnerString;
925 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
926}
927
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000928void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000929 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
930 InnerString = ' ' + InnerString;
931 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
932}
933
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000935 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000936 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
937 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 std::string ObjCQIString = getDecl()->getName();
939 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000940 int num = getNumProtocols();
941 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000942 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000943 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000944 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000945 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000946 ObjCQIString += '>';
947 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000948}
949
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000950void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000951 std::string &InnerString) const {
952 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
953 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000954 std::string ObjCQIString = "id";
955 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000956 int num = getNumProtocols();
957 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000958 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000959 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000960 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000961 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000962 ObjCQIString += '>';
963 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000964}
965
Reid Spencer5f016e22007-07-11 17:01:13 +0000966void TagType::getAsStringInternal(std::string &InnerString) const {
967 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
968 InnerString = ' ' + InnerString;
969
970 const char *Kind = getDecl()->getKindName();
971 const char *ID;
972 if (const IdentifierInfo *II = getDecl()->getIdentifier())
973 ID = II->getName();
974 else
975 ID = "<anonymous>";
976
977 InnerString = std::string(Kind) + " " + ID + InnerString;
978}