blob: 1ce5059777454c3ebfad1239491845b68471b96e [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:
Reid Spencer5f016e22007-07-11 17:01:13 +000049 case FunctionProto:
50 case FunctionNoProto:
51 case Reference:
52 return true;
53 case Tagged: {
54 const TagType *TT = cast<TagType>(CanonicalType);
55 const Decl::Kind Kind = TT->getDecl()->getKind();
56 return Kind == Decl::Struct || Kind == Decl::Union;
57 }
58 default:
59 return false;
60 }
61}
62
Chris Lattnerc8629632007-07-31 19:29:30 +000063bool Type::isStructureType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000064 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000065 if (RT->getDecl()->getKind() == Decl::Struct)
66 return true;
67 return false;
68}
69bool Type::isUnionType() const {
Seo Sanghyeon2de3b3a2007-12-02 16:57:27 +000070 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType))
Chris Lattnerc8629632007-07-31 19:29:30 +000071 if (RT->getDecl()->getKind() == Decl::Union)
72 return true;
73 return false;
74}
Chris Lattnerc8629632007-07-31 19:29:30 +000075
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000076bool Type::isComplexType() const {
Steve Naroff02f62a92008-01-15 19:36:10 +000077 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
78 return CT->getElementType()->isFloatingType();
79 return false;
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000080}
81
Steve Naroff4cdec1c2008-01-15 01:41:59 +000082bool Type::isComplexIntegerType() const {
83 // Check for GCC complex integer extension.
84 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
85 return CT->getElementType()->isIntegerType();
86 return false;
87}
88
89const ComplexType *Type::getAsComplexIntegerType() const {
90 // Are we directly a complex type?
91 if (const ComplexType *CTy = dyn_cast<ComplexType>(this)) {
92 if (CTy->getElementType()->isIntegerType())
93 return CTy;
94 }
95 // If the canonical form of this type isn't the right kind, reject it.
96 const ComplexType *CTy = dyn_cast<ComplexType>(CanonicalType);
97 if (!CTy || !CTy->getElementType()->isIntegerType())
98 return 0;
99
100 // If this is a typedef for a complex type, strip the typedef off without
101 // losing all typedef information.
102 return getDesugaredType()->getAsComplexIntegerType();
103}
104
Chris Lattnerdea61462007-10-29 03:41:11 +0000105/// getDesugaredType - Return the specified type with any "sugar" removed from
106/// type type. This takes off typedefs, typeof's etc. If the outer level of
107/// the type is already concrete, it returns it unmodified. This is similar
108/// to getting the canonical type, but it doesn't remove *all* typedefs. For
109/// example, it return "T*" as "T*", (not as "int*"), because the pointer is
110/// concrete.
111const Type *Type::getDesugaredType() const {
112 if (const TypedefType *TDT = dyn_cast<TypedefType>(this))
113 return TDT->LookThroughTypedefs().getTypePtr();
114 if (const TypeOfExpr *TOE = dyn_cast<TypeOfExpr>(this))
115 return TOE->getUnderlyingExpr()->getType().getTypePtr();
116 if (const TypeOfType *TOT = dyn_cast<TypeOfType>(this))
117 return TOT->getUnderlyingType().getTypePtr();
118 return this;
119}
120
121
Steve Naroff77878cc2007-08-27 04:08:11 +0000122const BuiltinType *Type::getAsBuiltinType() const {
123 // If this is directly a builtin type, return it.
124 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
125 return BTy;
Chris Lattnerdea61462007-10-29 03:41:11 +0000126
127 // If the canonical form of this type isn't a builtin type, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000128 if (!isa<BuiltinType>(CanonicalType)) {
129 // Look through type qualifiers
130 if (isa<BuiltinType>(CanonicalType.getUnqualifiedType()))
131 return CanonicalType.getUnqualifiedType()->getAsBuiltinType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000132 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000133 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000134
Steve Naroff77878cc2007-08-27 04:08:11 +0000135 // If this is a typedef for a builtin type, strip the typedef off without
136 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000137 return getDesugaredType()->getAsBuiltinType();
Steve Naroff77878cc2007-08-27 04:08:11 +0000138}
139
Chris Lattnerc8629632007-07-31 19:29:30 +0000140const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000141 // If this is directly a function type, return it.
142 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
143 return FTy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000144
Chris Lattnerdea61462007-10-29 03:41:11 +0000145 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000146 if (!isa<FunctionType>(CanonicalType)) {
147 // Look through type qualifiers
148 if (isa<FunctionType>(CanonicalType.getUnqualifiedType()))
149 return CanonicalType.getUnqualifiedType()->getAsFunctionType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000150 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000151 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000152
Steve Naroff7064f5c2007-07-26 18:32:01 +0000153 // If this is a typedef for a function type, strip the typedef off without
154 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000155 return getDesugaredType()->getAsFunctionType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000156}
157
Chris Lattnerbefee482007-07-31 16:53:04 +0000158const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000159 // If this is directly a pointer type, return it.
160 if (const PointerType *PTy = dyn_cast<PointerType>(this))
161 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000162
Chris Lattnerdea61462007-10-29 03:41:11 +0000163 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000164 if (!isa<PointerType>(CanonicalType)) {
165 // Look through type qualifiers
166 if (isa<PointerType>(CanonicalType.getUnqualifiedType()))
167 return CanonicalType.getUnqualifiedType()->getAsPointerType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000168 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000169 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000170
Chris Lattnera2c77672007-07-16 22:05:22 +0000171 // If this is a typedef for a pointer type, strip the typedef off without
172 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000173 return getDesugaredType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000176const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000177 // If this is directly a reference type, return it.
178 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
179 return RTy;
180
Chris Lattnerdea61462007-10-29 03:41:11 +0000181 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000182 if (!isa<ReferenceType>(CanonicalType)) {
183 // Look through type qualifiers
184 if (isa<ReferenceType>(CanonicalType.getUnqualifiedType()))
185 return CanonicalType.getUnqualifiedType()->getAsReferenceType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000186 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000187 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000188
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000189 // If this is a typedef for a reference type, strip the typedef off without
190 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000191 return getDesugaredType()->getAsReferenceType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000192}
193
Chris Lattnerc8629632007-07-31 19:29:30 +0000194const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000195 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000196 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
197 return ATy;
198
Chris Lattnerdea61462007-10-29 03:41:11 +0000199 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000200 if (!isa<ArrayType>(CanonicalType)) {
201 // Look through type qualifiers
202 if (isa<ArrayType>(CanonicalType.getUnqualifiedType()))
203 return CanonicalType.getUnqualifiedType()->getAsArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000204 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000205 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000206
Steve Naroff700204c2007-07-24 21:46:40 +0000207 // If this is a typedef for an array type, strip the typedef off without
208 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000209 return getDesugaredType()->getAsArrayType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000210}
211
Steve Naroffd7444aa2007-08-31 17:20:07 +0000212const ConstantArrayType *Type::getAsConstantArrayType() const {
213 // If this is directly a constant array type, return it.
214 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
215 return ATy;
Christopher Lambebb97e92008-02-04 02:31:56 +0000216
Chris Lattnerdea61462007-10-29 03:41:11 +0000217 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000218 if (!isa<ConstantArrayType>(CanonicalType)) {
219 // Look through type qualifiers
220 if (isa<ConstantArrayType>(CanonicalType.getUnqualifiedType()))
221 return CanonicalType.getUnqualifiedType()->getAsConstantArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000222 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000223 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000224
225 // If this is a typedef for a constant array type, strip the typedef off
226 // without losing all typedef information.
227 return getDesugaredType()->getAsConstantArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000228}
229
230const VariableArrayType *Type::getAsVariableArrayType() const {
231 // If this is directly a variable array type, return it.
232 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
233 return ATy;
234
Chris Lattnerdea61462007-10-29 03:41:11 +0000235 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000236 if (!isa<VariableArrayType>(CanonicalType)) {
237 // Look through type qualifiers
238 if (isa<VariableArrayType>(CanonicalType.getUnqualifiedType()))
239 return CanonicalType.getUnqualifiedType()->getAsVariableArrayType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000240 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000241 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000242
243 // If this is a typedef for a variable array type, strip the typedef off
244 // without losing all typedef information.
245 return getDesugaredType()->getAsVariableArrayType();
Steve Naroffd7444aa2007-08-31 17:20:07 +0000246}
247
248/// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
249/// types that have a non-constant expression. This does not include "[]".
250bool Type::isVariablyModifiedType() const {
251 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
252 if (VAT->getSizeExpr())
253 return true;
254 }
255 return false;
256}
257
258const VariableArrayType *Type::getAsVariablyModifiedType() const {
259 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
260 if (VAT->getSizeExpr())
261 return VAT;
262 }
263 return 0;
264}
265
Steve Naroff5c06a692008-01-21 22:59:18 +0000266bool Type::isIncompleteArrayType() const {
267 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
268 if (!VAT->getSizeExpr())
269 return true;
270 }
271 return false;
272}
273
274const VariableArrayType *Type::getAsIncompleteArrayType() const {
275 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
276 if (!VAT->getSizeExpr())
277 return VAT;
278 }
279 return 0;
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)) {
303 if (RT->getDecl()->getKind() == Decl::Struct)
304 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)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000309 if (RT->getDecl()->getKind() != Decl::Struct)
310 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)) {
325 if (RT->getDecl()->getKind() == Decl::Union)
326 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)) {
Chris Lattnerdea61462007-10-29 03:41:11 +0000331 if (RT->getDecl()->getKind() != Decl::Union)
332 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
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000345const ComplexType *Type::getAsComplexType() const {
346 // Are we directly a complex type?
347 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
348 return CTy;
349
Chris Lattnerdea61462007-10-29 03:41:11 +0000350 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000351 if (!isa<ComplexType>(CanonicalType)) {
352 // Look through type qualifiers
353 if (isa<ComplexType>(CanonicalType.getUnqualifiedType()))
354 return CanonicalType.getUnqualifiedType()->getAsComplexType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000355 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000356 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000357
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000358 // If this is a typedef for a complex type, strip the typedef off without
359 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000360 return getDesugaredType()->getAsComplexType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000361}
362
Chris Lattnerc8629632007-07-31 19:29:30 +0000363const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000364 // Are we directly a vector type?
365 if (const VectorType *VTy = dyn_cast<VectorType>(this))
366 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000367
Chris Lattnerdea61462007-10-29 03:41:11 +0000368 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000369 if (!isa<VectorType>(CanonicalType)) {
370 // Look through type qualifiers
371 if (isa<VectorType>(CanonicalType.getUnqualifiedType()))
372 return CanonicalType.getUnqualifiedType()->getAsVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000373 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000374 }
Chris Lattnerdea61462007-10-29 03:41:11 +0000375
Chris Lattnera2c77672007-07-16 22:05:22 +0000376 // If this is a typedef for a vector type, strip the typedef off without
377 // losing all typedef information.
Chris Lattnerdea61462007-10-29 03:41:11 +0000378 return getDesugaredType()->getAsVectorType();
Chris Lattner7a2e0472007-07-16 00:23:25 +0000379}
380
Chris Lattnerc8629632007-07-31 19:29:30 +0000381const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000382 // Are we directly an OpenCU vector type?
383 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
384 return VTy;
385
Chris Lattnerdea61462007-10-29 03:41:11 +0000386 // If the canonical form of this type isn't the right kind, reject it.
Christopher Lambebb97e92008-02-04 02:31:56 +0000387 if (!isa<OCUVectorType>(CanonicalType)) {
388 // Look through type qualifiers
389 if (isa<OCUVectorType>(CanonicalType.getUnqualifiedType()))
390 return CanonicalType.getUnqualifiedType()->getAsOCUVectorType();
Chris Lattnerdea61462007-10-29 03:41:11 +0000391 return 0;
Christopher Lambebb97e92008-02-04 02:31:56 +0000392 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000393
Chris Lattnerdea61462007-10-29 03:41:11 +0000394 // If this is a typedef for an ocuvector type, strip the typedef off without
395 // losing all typedef information.
396 return getDesugaredType()->getAsOCUVectorType();
Steve Naroff7064f5c2007-07-26 18:32:01 +0000397}
398
Reid Spencer5f016e22007-07-11 17:01:13 +0000399bool Type::isIntegerType() const {
400 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
401 return BT->getKind() >= BuiltinType::Bool &&
402 BT->getKind() <= BuiltinType::LongLong;
403 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
404 if (TT->getDecl()->getKind() == Decl::Enum)
405 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000406 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
407 return VT->getElementType()->isIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000408 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
409 return ASQT->getBaseType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 return false;
411}
412
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000413bool Type::isIntegralType() const {
414 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
415 return BT->getKind() >= BuiltinType::Bool &&
416 BT->getKind() <= BuiltinType::LongLong;
417 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
418 if (TT->getDecl()->getKind() == Decl::Enum)
419 return true;
Christopher Lambebb97e92008-02-04 02:31:56 +0000420 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
421 return ASQT->getBaseType()->isIntegralType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000422 return false;
423}
424
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000425bool Type::isEnumeralType() const {
426 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
427 return TT->getDecl()->getKind() == Decl::Enum;
Christopher Lambebb97e92008-02-04 02:31:56 +0000428 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
429 return ASQT->getBaseType()->isEnumeralType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000430 return false;
431}
432
433bool Type::isBooleanType() const {
434 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
435 return BT->getKind() == BuiltinType::Bool;
Christopher Lambebb97e92008-02-04 02:31:56 +0000436 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
437 return ASQT->getBaseType()->isBooleanType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000438 return false;
439}
440
441bool Type::isCharType() const {
442 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
443 return BT->getKind() == BuiltinType::Char_U ||
444 BT->getKind() == BuiltinType::UChar ||
Anders Carlssonc67ad5f2007-10-29 02:52:18 +0000445 BT->getKind() == BuiltinType::Char_S ||
446 BT->getKind() == BuiltinType::SChar;
Christopher Lambebb97e92008-02-04 02:31:56 +0000447 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
448 return ASQT->getBaseType()->isCharType();
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000449 return false;
450}
451
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000452/// isSignedIntegerType - Return true if this is an integer type that is
453/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
454/// an enum decl which has a signed representation, or a vector of signed
455/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000456bool Type::isSignedIntegerType() const {
457 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
458 return BT->getKind() >= BuiltinType::Char_S &&
459 BT->getKind() <= BuiltinType::LongLong;
460 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000461
462 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
463 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
464 return ED->getIntegerType()->isSignedIntegerType();
465
Steve Naroffc63b96a2007-07-12 21:46:55 +0000466 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
467 return VT->getElementType()->isSignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000468 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
469 return ASQT->getBaseType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 return false;
471}
472
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000473/// isUnsignedIntegerType - Return true if this is an integer type that is
474/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
475/// decl which has an unsigned representation, or a vector of unsigned integer
476/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000477bool Type::isUnsignedIntegerType() const {
478 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
479 return BT->getKind() >= BuiltinType::Bool &&
480 BT->getKind() <= BuiltinType::ULongLong;
481 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000482
483 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
484 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
485 return ED->getIntegerType()->isUnsignedIntegerType();
486
Steve Naroffc63b96a2007-07-12 21:46:55 +0000487 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
488 return VT->getElementType()->isUnsignedIntegerType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000489 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
490 return ASQT->getBaseType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 return false;
492}
493
494bool Type::isFloatingType() const {
495 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
496 return BT->getKind() >= BuiltinType::Float &&
497 BT->getKind() <= BuiltinType::LongDouble;
498 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000499 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000500 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
501 return VT->getElementType()->isFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000502 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
503 return ASQT->getBaseType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 return false;
505}
506
507bool Type::isRealFloatingType() const {
508 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
509 return BT->getKind() >= BuiltinType::Float &&
510 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000511 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
512 return VT->getElementType()->isRealFloatingType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000513 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
514 return ASQT->getBaseType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 return false;
516}
517
518bool Type::isRealType() const {
519 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
520 return BT->getKind() >= BuiltinType::Bool &&
521 BT->getKind() <= BuiltinType::LongDouble;
522 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
523 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000524 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
525 return VT->getElementType()->isRealType();
Christopher Lambebb97e92008-02-04 02:31:56 +0000526 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
527 return ASQT->getBaseType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 return false;
529}
530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531bool Type::isArithmeticType() const {
532 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
533 return BT->getKind() != BuiltinType::Void;
534 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
Steve Naroff67c49e82008-01-16 23:54:22 +0000535 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
536 // GCC allows forward declaration of enum types (forbid by C99 6.7.2.3p2).
Steve Naroff494783a2008-01-16 23:56:32 +0000537 // If a body isn't seen by the time we get here, return false.
Steve Naroff67c49e82008-01-16 23:54:22 +0000538 return ED->isDefinition();
Christopher Lambebb97e92008-02-04 02:31:56 +0000539 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
540 return ASQT->getBaseType()->isArithmeticType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
542}
543
544bool Type::isScalarType() const {
545 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
546 return BT->getKind() != BuiltinType::Void;
547 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
548 if (TT->getDecl()->getKind() == Decl::Enum)
549 return true;
550 return false;
551 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000552 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
553 return ASQT->getBaseType()->isScalarType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000554 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000555 isa<ObjCQualifiedIdType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000556}
557
558bool Type::isAggregateType() const {
559 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
560 if (TT->getDecl()->getKind() == Decl::Struct)
561 return true;
562 return false;
563 }
Christopher Lambebb97e92008-02-04 02:31:56 +0000564 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
565 return ASQT->getBaseType()->isAggregateType();
Steve Narofffb22d962007-08-30 01:06:46 +0000566 return CanonicalType->getTypeClass() == ConstantArray ||
567 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000568}
569
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000570/// isConstantSizeType - Return true if this is not a variable sized type,
571/// according to the rules of C99 6.7.5p3. It is not legal to call this on
572/// incomplete types.
Eli Friedman3c2b3172008-02-15 12:20:59 +0000573bool Type::isConstantSizeType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000574 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
Eli Friedman3c2b3172008-02-15 12:20:59 +0000575 return ASQT->getBaseType()->isConstantSizeType();
Chris Lattnerd52a4572007-12-18 07:03:30 +0000576 assert(!isIncompleteType() && "This doesn't make sense for incomplete types");
Chris Lattner9bfa73c2007-12-18 07:18:16 +0000577 // The VAT must have a size, as it is known to be complete.
578 return !isa<VariableArrayType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579}
580
581/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
582/// - a type that can describe objects, but which lacks information needed to
583/// determine its size.
584bool Type::isIncompleteType() const {
585 switch (CanonicalType->getTypeClass()) {
586 default: return false;
Christopher Lambebb97e92008-02-04 02:31:56 +0000587 case ASQual:
588 return cast<ASQualType>(CanonicalType)->getBaseType()->isIncompleteType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 case Builtin:
590 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
591 // be completed.
592 return isVoidType();
593 case Tagged:
594 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
595 // forward declaration, but not a full definition (C99 6.2.5p22).
596 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000597 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000599 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 }
601}
602
603bool Type::isPromotableIntegerType() const {
Christopher Lambebb97e92008-02-04 02:31:56 +0000604 if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
605 return ASQT->getBaseType()->isPromotableIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
607 if (!BT) return false;
608 switch (BT->getKind()) {
609 case BuiltinType::Bool:
610 case BuiltinType::Char_S:
611 case BuiltinType::Char_U:
612 case BuiltinType::SChar:
613 case BuiltinType::UChar:
614 case BuiltinType::Short:
615 case BuiltinType::UShort:
616 return true;
617 default:
618 return false;
619 }
620}
621
622const char *BuiltinType::getName() const {
623 switch (getKind()) {
624 default: assert(0 && "Unknown builtin type!");
625 case Void: return "void";
626 case Bool: return "_Bool";
627 case Char_S: return "char";
628 case Char_U: return "char";
629 case SChar: return "signed char";
630 case Short: return "short";
631 case Int: return "int";
632 case Long: return "long";
633 case LongLong: return "long long";
634 case UChar: return "unsigned char";
635 case UShort: return "unsigned short";
636 case UInt: return "unsigned int";
637 case ULong: return "unsigned long";
638 case ULongLong: return "unsigned long long";
639 case Float: return "float";
640 case Double: return "double";
641 case LongDouble: return "long double";
642 }
643}
644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000646 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 unsigned NumArgs, bool isVariadic) {
648 ID.AddPointer(Result.getAsOpaquePtr());
649 for (unsigned i = 0; i != NumArgs; ++i)
650 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
651 ID.AddInteger(isVariadic);
652}
653
654void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000655 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000656}
657
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000658void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID,
659 ObjCProtocolDecl **protocols,
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000660 unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000661 for (unsigned i = 0; i != NumProtocols; i++)
662 ID.AddPointer(protocols[i]);
663}
664
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000665void ObjCQualifiedInterfaceType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000666 Profile(ID, &Protocols[0], getNumProtocols());
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000667}
668
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000669void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID,
670 ObjCProtocolDecl **protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000671 unsigned NumProtocols) {
672 for (unsigned i = 0; i != NumProtocols; i++)
673 ID.AddPointer(protocols[i]);
674}
675
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000676void ObjCQualifiedIdType::Profile(llvm::FoldingSetNodeID &ID) {
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000677 Profile(ID, &Protocols[0], getNumProtocols());
678}
679
Chris Lattnera2c77672007-07-16 22:05:22 +0000680/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
681/// potentially looking through *all* consequtive typedefs. This returns the
682/// sum of the type qualifiers, so if you have:
683/// typedef const int A;
684/// typedef volatile A B;
685/// looking through the typedefs for B will give you "const volatile A".
686///
687QualType TypedefType::LookThroughTypedefs() const {
688 // Usually, there is only a single level of typedefs, be fast in that case.
689 QualType FirstType = getDecl()->getUnderlyingType();
690 if (!isa<TypedefType>(FirstType))
691 return FirstType;
692
693 // Otherwise, do the fully general loop.
694 unsigned TypeQuals = 0;
695 const TypedefType *TDT = this;
696 while (1) {
697 QualType CurType = TDT->getDecl()->getUnderlyingType();
698 TypeQuals |= CurType.getQualifiers();
699
700 TDT = dyn_cast<TypedefType>(CurType);
701 if (TDT == 0)
702 return QualType(CurType.getTypePtr(), TypeQuals);
703 }
704}
Reid Spencer5f016e22007-07-11 17:01:13 +0000705
706bool RecordType::classof(const Type *T) {
707 if (const TagType *TT = dyn_cast<TagType>(T))
708 return isa<RecordDecl>(TT->getDecl());
709 return false;
710}
711
712
713//===----------------------------------------------------------------------===//
714// Type Printing
715//===----------------------------------------------------------------------===//
716
717void QualType::dump(const char *msg) const {
Chris Lattner39caea92007-12-06 04:20:07 +0000718 std::string R = "identifier";
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 getAsStringInternal(R);
720 if (msg)
721 fprintf(stderr, "%s: %s\n", msg, R.c_str());
722 else
723 fprintf(stderr, "%s\n", R.c_str());
724}
725
726static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
727 // Note: funkiness to ensure we get a space only between quals.
728 bool NonePrinted = true;
729 if (TypeQuals & QualType::Const)
730 S += "const", NonePrinted = false;
731 if (TypeQuals & QualType::Volatile)
732 S += (NonePrinted+" volatile"), NonePrinted = false;
733 if (TypeQuals & QualType::Restrict)
734 S += (NonePrinted+" restrict"), NonePrinted = false;
735}
736
737void QualType::getAsStringInternal(std::string &S) const {
738 if (isNull()) {
739 S += "NULL TYPE\n";
740 return;
741 }
742
743 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000744 unsigned TQ = getQualifiers();
745 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 std::string TQS;
747 AppendTypeQualList(TQS, TQ);
748 if (!S.empty())
749 S = TQS + ' ' + S;
750 else
751 S = TQS;
752 }
753
754 getTypePtr()->getAsStringInternal(S);
755}
756
757void BuiltinType::getAsStringInternal(std::string &S) const {
758 if (S.empty()) {
759 S = getName();
760 } else {
761 // Prefix the basic type, e.g. 'int X'.
762 S = ' ' + S;
763 S = getName() + S;
764 }
765}
766
767void ComplexType::getAsStringInternal(std::string &S) const {
768 ElementType->getAsStringInternal(S);
769 S = "_Complex " + S;
770}
771
Christopher Lambebb97e92008-02-04 02:31:56 +0000772void ASQualType::getAsStringInternal(std::string &S) const {
773 S = "__attribute__((address_space("+llvm::utostr_32(AddressSpace)+")))" + S;
774 BaseType->getAsStringInternal(S);
775}
776
Reid Spencer5f016e22007-07-11 17:01:13 +0000777void PointerType::getAsStringInternal(std::string &S) const {
778 S = '*' + S;
779
780 // Handle things like 'int (*A)[4];' correctly.
781 // FIXME: this should include vectors, but vectors use attributes I guess.
782 if (isa<ArrayType>(PointeeType.getTypePtr()))
783 S = '(' + S + ')';
784
785 PointeeType.getAsStringInternal(S);
786}
787
788void ReferenceType::getAsStringInternal(std::string &S) const {
789 S = '&' + S;
790
791 // Handle things like 'int (&A)[4];' correctly.
792 // FIXME: this should include vectors, but vectors use attributes I guess.
793 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
794 S = '(' + S + ')';
795
796 ReferenceeType.getAsStringInternal(S);
797}
798
Steve Narofffb22d962007-08-30 01:06:46 +0000799void ConstantArrayType::getAsStringInternal(std::string &S) const {
800 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000801 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000802 S += ']';
803
804 getElementType().getAsStringInternal(S);
805}
806
807void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 S += '[';
809
Steve Naroffc9406122007-08-30 18:10:14 +0000810 if (getIndexTypeQualifier()) {
811 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 S += ' ';
813 }
814
Steve Naroffc9406122007-08-30 18:10:14 +0000815 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000817 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 S += '*';
819
Steve Narofffb22d962007-08-30 01:06:46 +0000820 if (getSizeExpr()) {
821 std::ostringstream s;
822 getSizeExpr()->printPretty(s);
823 S += s.str();
824 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 S += ']';
826
Steve Narofffb22d962007-08-30 01:06:46 +0000827 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000828}
829
830void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattner7ee261c2007-11-27 07:28:18 +0000831 S += " __attribute__((__vector_size__(";
Chris Lattnere107b5d2007-07-13 21:01:17 +0000832 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000833 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000834 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 ElementType.getAsStringInternal(S);
836}
837
Steve Naroff31a45842007-07-28 23:10:27 +0000838void OCUVectorType::getAsStringInternal(std::string &S) const {
839 S += " __attribute__((ocu_vector_type(";
840 S += llvm::utostr_32(NumElements);
841 S += ")))";
842 ElementType.getAsStringInternal(S);
843}
844
Steve Naroffd1861fd2007-07-31 12:34:36 +0000845void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000846 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
847 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000848 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000849 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000850 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000851}
852
Steve Naroff363bcff2007-08-01 23:45:51 +0000853void TypeOfType::getAsStringInternal(std::string &InnerString) const {
854 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
855 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000856 std::string Tmp;
857 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000858 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000859}
860
Reid Spencer5f016e22007-07-11 17:01:13 +0000861void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
862 // If needed for precedence reasons, wrap the inner part in grouping parens.
863 if (!S.empty())
864 S = "(" + S + ")";
865
866 S += "()";
867 getResultType().getAsStringInternal(S);
868}
869
870void FunctionTypeProto::getAsStringInternal(std::string &S) const {
871 // If needed for precedence reasons, wrap the inner part in grouping parens.
872 if (!S.empty())
873 S = "(" + S + ")";
874
875 S += "(";
876 std::string Tmp;
877 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
878 if (i) S += ", ";
879 getArgType(i).getAsStringInternal(Tmp);
880 S += Tmp;
881 Tmp.clear();
882 }
883
884 if (isVariadic()) {
885 if (getNumArgs())
886 S += ", ";
887 S += "...";
888 } else if (getNumArgs() == 0) {
889 // Do not emit int() if we have a proto, emit 'int(void)'.
890 S += "void";
891 }
892
893 S += ")";
894 getResultType().getAsStringInternal(S);
895}
896
897
898void TypedefType::getAsStringInternal(std::string &InnerString) const {
899 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
900 InnerString = ' ' + InnerString;
901 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
902}
903
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000904void ObjCInterfaceType::getAsStringInternal(std::string &InnerString) const {
Steve Naroff3536b442007-09-06 21:24:23 +0000905 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
906 InnerString = ' ' + InnerString;
907 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
908}
909
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000910void ObjCQualifiedInterfaceType::getAsStringInternal(
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000911 std::string &InnerString) const {
Fariborz Jahaniandfbcce22007-10-11 18:08:47 +0000912 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
913 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000914 std::string ObjCQIString = getDecl()->getName();
915 ObjCQIString += '<';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000916 int num = getNumProtocols();
917 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000919 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000920 ObjCQIString += ',';
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000921 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000922 ObjCQIString += '>';
923 InnerString = ObjCQIString + InnerString;
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000924}
925
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926void ObjCQualifiedIdType::getAsStringInternal(
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000927 std::string &InnerString) const {
928 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
929 InnerString = ' ' + InnerString;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000930 std::string ObjCQIString = "id";
931 ObjCQIString += '<';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000932 int num = getNumProtocols();
933 for (int i = 0; i < num; i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000934 ObjCQIString += getProtocols(i)->getName();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000935 if (i < num-1)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000936 ObjCQIString += ',';
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000937 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 ObjCQIString += '>';
939 InnerString = ObjCQIString + InnerString;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000940}
941
Reid Spencer5f016e22007-07-11 17:01:13 +0000942void TagType::getAsStringInternal(std::string &InnerString) const {
943 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
944 InnerString = ' ' + InnerString;
945
946 const char *Kind = getDecl()->getKindName();
947 const char *ID;
948 if (const IdentifierInfo *II = getDecl()->getIdentifier())
949 ID = II->getName();
950 else
951 ID = "<anonymous>";
952
953 InnerString = std::string(Kind) + " " + ID + InnerString;
954}