Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/Decl.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 20 | #include "llvm/Bitcode/Serialize.h" |
| 21 | #include "llvm/Bitcode/Deserialize.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 22 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | enum FloatingRank { |
| 26 | FloatRank, DoubleRank, LongDoubleRank |
| 27 | }; |
| 28 | |
| 29 | ASTContext::~ASTContext() { |
| 30 | // Deallocate all the types. |
| 31 | while (!Types.empty()) { |
| 32 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) { |
| 33 | // Destroy the object, but don't call delete. These are malloc'd. |
| 34 | FT->~FunctionTypeProto(); |
| 35 | free(FT); |
| 36 | } else { |
| 37 | delete Types.back(); |
| 38 | } |
| 39 | Types.pop_back(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void ASTContext::PrintStats() const { |
| 44 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 45 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 46 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 47 | unsigned NumVector = 0, NumComplex = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
| 49 | |
| 50 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 51 | unsigned NumObjcInterfaces = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | |
| 53 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 54 | Type *T = Types[i]; |
| 55 | if (isa<BuiltinType>(T)) |
| 56 | ++NumBuiltin; |
| 57 | else if (isa<PointerType>(T)) |
| 58 | ++NumPointer; |
| 59 | else if (isa<ReferenceType>(T)) |
| 60 | ++NumReference; |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 61 | else if (isa<ComplexType>(T)) |
| 62 | ++NumComplex; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | else if (isa<ArrayType>(T)) |
| 64 | ++NumArray; |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 65 | else if (isa<VectorType>(T)) |
| 66 | ++NumVector; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | else if (isa<FunctionTypeNoProto>(T)) |
| 68 | ++NumFunctionNP; |
| 69 | else if (isa<FunctionTypeProto>(T)) |
| 70 | ++NumFunctionP; |
| 71 | else if (isa<TypedefType>(T)) |
| 72 | ++NumTypeName; |
| 73 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
| 74 | ++NumTagged; |
| 75 | switch (TT->getDecl()->getKind()) { |
| 76 | default: assert(0 && "Unknown tagged type!"); |
| 77 | case Decl::Struct: ++NumTagStruct; break; |
| 78 | case Decl::Union: ++NumTagUnion; break; |
| 79 | case Decl::Class: ++NumTagClass; break; |
| 80 | case Decl::Enum: ++NumTagEnum; break; |
| 81 | } |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 82 | } else if (isa<ObjcInterfaceType>(T)) |
| 83 | ++NumObjcInterfaces; |
| 84 | else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | assert(0 && "Unknown type!"); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 90 | fprintf(stderr, " %d pointer types\n", NumPointer); |
| 91 | fprintf(stderr, " %d reference types\n", NumReference); |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 92 | fprintf(stderr, " %d complex types\n", NumComplex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | fprintf(stderr, " %d array types\n", NumArray); |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 94 | fprintf(stderr, " %d vector types\n", NumVector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 96 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 97 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 98 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 99 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 100 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 101 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 102 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
Steve Naroff | 3f128ad | 2007-09-17 14:16:13 +0000 | [diff] [blame] | 103 | fprintf(stderr, " %d interface types\n", NumObjcInterfaces); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
| 105 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
Chris Lattner | 6d87fc6 | 2007-07-18 05:50:59 +0000 | [diff] [blame] | 106 | NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 108 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
| 109 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType))); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 114 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
| 115 | } |
| 116 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | void ASTContext::InitBuiltinTypes() { |
| 118 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 119 | |
| 120 | // C99 6.2.5p19. |
| 121 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 122 | |
| 123 | // C99 6.2.5p2. |
| 124 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 125 | // C99 6.2.5p3. |
| 126 | if (Target.isCharSigned(SourceLocation())) |
| 127 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 128 | else |
| 129 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 130 | // C99 6.2.5p4. |
| 131 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 132 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 133 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 134 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 135 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 136 | |
| 137 | // C99 6.2.5p6. |
| 138 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 139 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 140 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 141 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 142 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 143 | |
| 144 | // C99 6.2.5p10. |
| 145 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 146 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 147 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
| 148 | |
| 149 | // C99 6.2.5p11. |
| 150 | FloatComplexTy = getComplexType(FloatTy); |
| 151 | DoubleComplexTy = getComplexType(DoubleTy); |
| 152 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 153 | |
| 154 | BuiltinVaListType = QualType(); |
| 155 | ObjcIdType = QualType(); |
| 156 | IdStructType = 0; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 157 | ObjcClassType = QualType(); |
| 158 | ClassStructType = 0; |
| 159 | |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 160 | ObjcConstantStringType = QualType(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 161 | |
| 162 | // void * type |
| 163 | VoidPtrTy = getPointerType(VoidTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 166 | //===----------------------------------------------------------------------===// |
| 167 | // Type Sizing and Analysis |
| 168 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 169 | |
| 170 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 171 | /// does not work on incomplete types. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 172 | std::pair<uint64_t, unsigned> |
| 173 | ASTContext::getTypeInfo(QualType T, SourceLocation L) { |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 174 | T = T.getCanonicalType(); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 175 | uint64_t Size; |
| 176 | unsigned Align; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 177 | switch (T->getTypeClass()) { |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 178 | case Type::TypeName: assert(0 && "Not a canonical type!"); |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 179 | case Type::FunctionNoProto: |
| 180 | case Type::FunctionProto: |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 181 | default: |
Chris Lattner | b1c2df9 | 2007-07-20 18:13:33 +0000 | [diff] [blame] | 182 | assert(0 && "Incomplete types have no size!"); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 183 | case Type::VariableArray: |
| 184 | assert(0 && "VLAs not implemented yet!"); |
| 185 | case Type::ConstantArray: { |
| 186 | ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
| 187 | |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 188 | std::pair<uint64_t, unsigned> EltInfo = |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 189 | getTypeInfo(CAT->getElementType(), L); |
| 190 | Size = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 191 | Align = EltInfo.second; |
| 192 | break; |
| 193 | } |
| 194 | case Type::Vector: { |
| 195 | std::pair<uint64_t, unsigned> EltInfo = |
| 196 | getTypeInfo(cast<VectorType>(T)->getElementType(), L); |
| 197 | Size = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
| 198 | // FIXME: Vector alignment is not the alignment of its elements. |
| 199 | Align = EltInfo.second; |
| 200 | break; |
| 201 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 202 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 203 | case Type::Builtin: { |
| 204 | // FIXME: need to use TargetInfo to derive the target specific sizes. This |
| 205 | // implementation will suffice for play with vector support. |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 206 | const llvm::fltSemantics *F; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 207 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 208 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 209 | case BuiltinType::Void: |
| 210 | assert(0 && "Incomplete types have no size!"); |
| 211 | case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 212 | case BuiltinType::Char_S: |
| 213 | case BuiltinType::Char_U: |
| 214 | case BuiltinType::UChar: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 215 | case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 216 | case BuiltinType::UShort: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 217 | case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 218 | case BuiltinType::UInt: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 219 | case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 220 | case BuiltinType::ULong: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 221 | case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 222 | case BuiltinType::ULongLong: |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 223 | case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break; |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 224 | case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break; |
| 225 | case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break; |
| 226 | case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 227 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 228 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 229 | } |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 230 | case Type::Pointer: Target.getPointerInfo(Size, Align, L); break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 231 | case Type::Reference: |
Chris Lattner | 7ab2ed8 | 2007-07-13 22:16:13 +0000 | [diff] [blame] | 232 | // "When applied to a reference or a reference type, the result is the size |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 233 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
| 234 | // FIXME: This is wrong for struct layout! |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 235 | return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L); |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 236 | |
| 237 | case Type::Complex: { |
| 238 | // Complex types have the same alignment as their elements, but twice the |
| 239 | // size. |
| 240 | std::pair<uint64_t, unsigned> EltInfo = |
| 241 | getTypeInfo(cast<ComplexType>(T)->getElementType(), L); |
| 242 | Size = EltInfo.first*2; |
| 243 | Align = EltInfo.second; |
| 244 | break; |
| 245 | } |
| 246 | case Type::Tagged: |
Chris Lattner | 6cd862c | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 247 | TagType *TT = cast<TagType>(T); |
| 248 | if (RecordType *RT = dyn_cast<RecordType>(TT)) { |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 249 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L); |
Chris Lattner | 6cd862c | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 250 | Size = Layout.getSize(); |
| 251 | Align = Layout.getAlignment(); |
| 252 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) { |
Chris Lattner | e00b18c | 2007-08-28 18:24:31 +0000 | [diff] [blame] | 253 | return getTypeInfo(ED->getIntegerType(), L); |
Chris Lattner | 6cd862c | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 254 | } else { |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 255 | assert(0 && "Unimplemented type sizes!"); |
Chris Lattner | 6cd862c | 2007-08-27 17:38:00 +0000 | [diff] [blame] | 256 | } |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 257 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 258 | } |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 260 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 261 | return std::make_pair(Size, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 264 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 265 | /// specified record (struct/union/class), which indicates its size and field |
| 266 | /// position information. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 267 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D, |
| 268 | SourceLocation L) { |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 269 | assert(D->isDefinition() && "Cannot get layout of forward declarations!"); |
| 270 | |
| 271 | // Look up this layout, if already laid out, return what we have. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 272 | const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 273 | if (Entry) return *Entry; |
| 274 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 275 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 276 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
| 277 | ASTRecordLayout *NewEntry = new ASTRecordLayout(); |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 278 | Entry = NewEntry; |
| 279 | |
| 280 | uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()]; |
| 281 | uint64_t RecordSize = 0; |
| 282 | unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits. |
| 283 | |
| 284 | if (D->getKind() != Decl::Union) { |
| 285 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 286 | // the future, this will need to be tweakable by targets. |
| 287 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 288 | const FieldDecl *FD = D->getMember(i); |
| 289 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 290 | uint64_t FieldSize = FieldInfo.first; |
| 291 | unsigned FieldAlign = FieldInfo.second; |
| 292 | |
| 293 | // Round up the current record size to the field's alignment boundary. |
| 294 | RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1); |
| 295 | |
| 296 | // Place this field at the current location. |
| 297 | FieldOffsets[i] = RecordSize; |
| 298 | |
| 299 | // Reserve space for this field. |
| 300 | RecordSize += FieldSize; |
| 301 | |
| 302 | // Remember max struct/class alignment. |
| 303 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 304 | } |
| 305 | |
| 306 | // Finally, round the size of the total struct up to the alignment of the |
| 307 | // struct itself. |
| 308 | RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1); |
| 309 | } else { |
| 310 | // Union layout just puts each member at the start of the record. |
| 311 | for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { |
| 312 | const FieldDecl *FD = D->getMember(i); |
| 313 | std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L); |
| 314 | uint64_t FieldSize = FieldInfo.first; |
| 315 | unsigned FieldAlign = FieldInfo.second; |
| 316 | |
| 317 | // Round up the current record size to the field's alignment boundary. |
| 318 | RecordSize = std::max(RecordSize, FieldSize); |
| 319 | |
| 320 | // Place this field at the start of the record. |
| 321 | FieldOffsets[i] = 0; |
| 322 | |
| 323 | // Remember max struct/class alignment. |
| 324 | RecordAlign = std::max(RecordAlign, FieldAlign); |
| 325 | } |
| 326 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 327 | |
| 328 | NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets); |
| 329 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 332 | //===----------------------------------------------------------------------===// |
| 333 | // Type creation/memoization methods |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
| 336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 338 | /// number with the specified element type. |
| 339 | QualType ASTContext::getComplexType(QualType T) { |
| 340 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 341 | // structure. |
| 342 | llvm::FoldingSetNodeID ID; |
| 343 | ComplexType::Profile(ID, T); |
| 344 | |
| 345 | void *InsertPos = 0; |
| 346 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 347 | return QualType(CT, 0); |
| 348 | |
| 349 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 350 | // so fill in the canonical type field. |
| 351 | QualType Canonical; |
| 352 | if (!T->isCanonical()) { |
| 353 | Canonical = getComplexType(T.getCanonicalType()); |
| 354 | |
| 355 | // Get the new insert position for the node we care about. |
| 356 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 357 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 358 | } |
| 359 | ComplexType *New = new ComplexType(T, Canonical); |
| 360 | Types.push_back(New); |
| 361 | ComplexTypes.InsertNode(New, InsertPos); |
| 362 | return QualType(New, 0); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 367 | /// the specified type. |
| 368 | QualType ASTContext::getPointerType(QualType T) { |
| 369 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 370 | // structure. |
| 371 | llvm::FoldingSetNodeID ID; |
| 372 | PointerType::Profile(ID, T); |
| 373 | |
| 374 | void *InsertPos = 0; |
| 375 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 376 | return QualType(PT, 0); |
| 377 | |
| 378 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 379 | // so fill in the canonical type field. |
| 380 | QualType Canonical; |
| 381 | if (!T->isCanonical()) { |
| 382 | Canonical = getPointerType(T.getCanonicalType()); |
| 383 | |
| 384 | // Get the new insert position for the node we care about. |
| 385 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 386 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 387 | } |
| 388 | PointerType *New = new PointerType(T, Canonical); |
| 389 | Types.push_back(New); |
| 390 | PointerTypes.InsertNode(New, InsertPos); |
| 391 | return QualType(New, 0); |
| 392 | } |
| 393 | |
| 394 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 395 | /// to the specified type. |
| 396 | QualType ASTContext::getReferenceType(QualType T) { |
| 397 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 398 | // structure. |
| 399 | llvm::FoldingSetNodeID ID; |
| 400 | ReferenceType::Profile(ID, T); |
| 401 | |
| 402 | void *InsertPos = 0; |
| 403 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 404 | return QualType(RT, 0); |
| 405 | |
| 406 | // If the referencee type isn't canonical, this won't be a canonical type |
| 407 | // either, so fill in the canonical type field. |
| 408 | QualType Canonical; |
| 409 | if (!T->isCanonical()) { |
| 410 | Canonical = getReferenceType(T.getCanonicalType()); |
| 411 | |
| 412 | // Get the new insert position for the node we care about. |
| 413 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 414 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 415 | } |
| 416 | |
| 417 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 418 | Types.push_back(New); |
| 419 | ReferenceTypes.InsertNode(New, InsertPos); |
| 420 | return QualType(New, 0); |
| 421 | } |
| 422 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 423 | /// getConstantArrayType - Return the unique reference to the type for an |
| 424 | /// array of the specified element type. |
| 425 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 426 | const llvm::APInt &ArySize, |
| 427 | ArrayType::ArraySizeModifier ASM, |
| 428 | unsigned EltTypeQuals) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 429 | llvm::FoldingSetNodeID ID; |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 430 | ConstantArrayType::Profile(ID, EltTy, ArySize); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 431 | |
| 432 | void *InsertPos = 0; |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 433 | if (ConstantArrayType *ATP = |
| 434 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | return QualType(ATP, 0); |
| 436 | |
| 437 | // If the element type isn't canonical, this won't be a canonical type either, |
| 438 | // so fill in the canonical type field. |
| 439 | QualType Canonical; |
| 440 | if (!EltTy->isCanonical()) { |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 441 | Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize, |
| 442 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | // Get the new insert position for the node we care about. |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 444 | ConstantArrayType *NewIP = |
| 445 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 446 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 447 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 448 | } |
| 449 | |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 450 | ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize, |
| 451 | ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 452 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | Types.push_back(New); |
| 454 | return QualType(New, 0); |
| 455 | } |
| 456 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 457 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 458 | /// variable array of the specified element type. |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 459 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
| 460 | ArrayType::ArraySizeModifier ASM, |
| 461 | unsigned EltTypeQuals) { |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 462 | if (NumElts) { |
| 463 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 464 | // that have an expression provided for their size. |
| 465 | |
Ted Kremenek | 347b9f3 | 2007-10-30 16:41:53 +0000 | [diff] [blame] | 466 | VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 467 | ASM, EltTypeQuals); |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 347b9f3 | 2007-10-30 16:41:53 +0000 | [diff] [blame] | 469 | CompleteVariableArrayTypes.push_back(New); |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 470 | Types.push_back(New); |
| 471 | return QualType(New, 0); |
| 472 | } |
| 473 | else { |
| 474 | // No size is provided for the VLA. These we can unique. |
| 475 | llvm::FoldingSetNodeID ID; |
| 476 | VariableArrayType::Profile(ID, EltTy); |
| 477 | |
| 478 | void *InsertPos = 0; |
| 479 | if (VariableArrayType *ATP = |
| 480 | IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 481 | return QualType(ATP, 0); |
| 482 | |
| 483 | // If the element type isn't canonical, this won't be a canonical type |
| 484 | // either, so fill in the canonical type field. |
| 485 | QualType Canonical; |
| 486 | |
| 487 | if (!EltTy->isCanonical()) { |
| 488 | Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts, |
| 489 | ASM, EltTypeQuals); |
| 490 | |
| 491 | // Get the new insert position for the node we care about. |
| 492 | VariableArrayType *NewIP = |
| 493 | IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 494 | |
| 495 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 496 | } |
| 497 | |
| 498 | VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts, |
| 499 | ASM, EltTypeQuals); |
| 500 | |
| 501 | IncompleteVariableArrayTypes.InsertNode(New, InsertPos); |
| 502 | Types.push_back(New); |
| 503 | return QualType(New, 0); |
| 504 | } |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 507 | /// getVectorType - Return the unique reference to a vector type of |
| 508 | /// the specified element type and size. VectorType must be a built-in type. |
| 509 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 510 | BuiltinType *baseType; |
| 511 | |
| 512 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 513 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | |
| 515 | // Check if we've already instantiated a vector of this type. |
| 516 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 517 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 518 | void *InsertPos = 0; |
| 519 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 520 | return QualType(VTP, 0); |
| 521 | |
| 522 | // If the element type isn't canonical, this won't be a canonical type either, |
| 523 | // so fill in the canonical type field. |
| 524 | QualType Canonical; |
| 525 | if (!vecType->isCanonical()) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 526 | Canonical = getVectorType(vecType.getCanonicalType(), NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 527 | |
| 528 | // Get the new insert position for the node we care about. |
| 529 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 530 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 531 | } |
| 532 | VectorType *New = new VectorType(vecType, NumElts, Canonical); |
| 533 | VectorTypes.InsertNode(New, InsertPos); |
| 534 | Types.push_back(New); |
| 535 | return QualType(New, 0); |
| 536 | } |
| 537 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 538 | /// getOCUVectorType - Return the unique reference to an OCU vector type of |
| 539 | /// the specified element type and size. VectorType must be a built-in type. |
| 540 | QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) { |
| 541 | BuiltinType *baseType; |
| 542 | |
| 543 | baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr()); |
| 544 | assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type"); |
| 545 | |
| 546 | // Check if we've already instantiated a vector of this type. |
| 547 | llvm::FoldingSetNodeID ID; |
| 548 | VectorType::Profile(ID, vecType, NumElts, Type::OCUVector); |
| 549 | void *InsertPos = 0; |
| 550 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 551 | return QualType(VTP, 0); |
| 552 | |
| 553 | // If the element type isn't canonical, this won't be a canonical type either, |
| 554 | // so fill in the canonical type field. |
| 555 | QualType Canonical; |
| 556 | if (!vecType->isCanonical()) { |
| 557 | Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts); |
| 558 | |
| 559 | // Get the new insert position for the node we care about. |
| 560 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 561 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 562 | } |
| 563 | OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical); |
| 564 | VectorTypes.InsertNode(New, InsertPos); |
| 565 | Types.push_back(New); |
| 566 | return QualType(New, 0); |
| 567 | } |
| 568 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 569 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 570 | /// |
| 571 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
| 572 | // Unique functions, to guarantee there is only one function of a particular |
| 573 | // structure. |
| 574 | llvm::FoldingSetNodeID ID; |
| 575 | FunctionTypeNoProto::Profile(ID, ResultTy); |
| 576 | |
| 577 | void *InsertPos = 0; |
| 578 | if (FunctionTypeNoProto *FT = |
| 579 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 580 | return QualType(FT, 0); |
| 581 | |
| 582 | QualType Canonical; |
| 583 | if (!ResultTy->isCanonical()) { |
| 584 | Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType()); |
| 585 | |
| 586 | // Get the new insert position for the node we care about. |
| 587 | FunctionTypeNoProto *NewIP = |
| 588 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 589 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 590 | } |
| 591 | |
| 592 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 593 | Types.push_back(New); |
| 594 | FunctionTypeProtos.InsertNode(New, InsertPos); |
| 595 | return QualType(New, 0); |
| 596 | } |
| 597 | |
| 598 | /// getFunctionType - Return a normal function type with a typed argument |
| 599 | /// list. isVariadic indicates whether the argument list includes '...'. |
| 600 | QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray, |
| 601 | unsigned NumArgs, bool isVariadic) { |
| 602 | // Unique functions, to guarantee there is only one function of a particular |
| 603 | // structure. |
| 604 | llvm::FoldingSetNodeID ID; |
| 605 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic); |
| 606 | |
| 607 | void *InsertPos = 0; |
| 608 | if (FunctionTypeProto *FTP = |
| 609 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
| 610 | return QualType(FTP, 0); |
| 611 | |
| 612 | // Determine whether the type being created is already canonical or not. |
| 613 | bool isCanonical = ResultTy->isCanonical(); |
| 614 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 615 | if (!ArgArray[i]->isCanonical()) |
| 616 | isCanonical = false; |
| 617 | |
| 618 | // If this type isn't canonical, get the canonical version of it. |
| 619 | QualType Canonical; |
| 620 | if (!isCanonical) { |
| 621 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 622 | CanonicalArgs.reserve(NumArgs); |
| 623 | for (unsigned i = 0; i != NumArgs; ++i) |
| 624 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 625 | |
| 626 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 627 | &CanonicalArgs[0], NumArgs, |
| 628 | isVariadic); |
| 629 | |
| 630 | // Get the new insert position for the node we care about. |
| 631 | FunctionTypeProto *NewIP = |
| 632 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 633 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 634 | } |
| 635 | |
| 636 | // FunctionTypeProto objects are not allocated with new because they have a |
| 637 | // variable size array (for parameter types) at the end of them. |
| 638 | FunctionTypeProto *FTP = |
| 639 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
Chris Lattner | 942cfd3 | 2007-07-20 18:48:28 +0000 | [diff] [blame] | 640 | NumArgs*sizeof(QualType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 642 | Canonical); |
| 643 | Types.push_back(FTP); |
| 644 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
| 645 | return QualType(FTP, 0); |
| 646 | } |
| 647 | |
| 648 | /// getTypedefType - Return the unique reference to the type for the |
| 649 | /// specified typename decl. |
| 650 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 651 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 652 | |
| 653 | QualType Canonical = Decl->getUnderlyingType().getCanonicalType(); |
| 654 | Decl->TypeForDecl = new TypedefType(Decl, Canonical); |
| 655 | Types.push_back(Decl->TypeForDecl); |
| 656 | return QualType(Decl->TypeForDecl, 0); |
| 657 | } |
| 658 | |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 659 | /// getObjcInterfaceType - Return the unique reference to the type for the |
| 660 | /// specified ObjC interface decl. |
| 661 | QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) { |
| 662 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 663 | |
| 664 | Decl->TypeForDecl = new ObjcInterfaceType(Decl); |
| 665 | Types.push_back(Decl->TypeForDecl); |
| 666 | return QualType(Decl->TypeForDecl, 0); |
| 667 | } |
| 668 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 669 | /// getObjcQualifiedInterfaceType - Return a |
| 670 | /// ObjcQualifiedInterfaceType type for the given interface decl and |
| 671 | /// the conforming protocol list. |
| 672 | QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl, |
| 673 | ObjcProtocolDecl **Protocols, unsigned NumProtocols) { |
| 674 | ObjcInterfaceType *IType = |
| 675 | cast<ObjcInterfaceType>(getObjcInterfaceType(Decl)); |
| 676 | |
| 677 | llvm::FoldingSetNodeID ID; |
| 678 | ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols); |
| 679 | |
| 680 | void *InsertPos = 0; |
| 681 | if (ObjcQualifiedInterfaceType *QT = |
| 682 | ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 683 | return QualType(QT, 0); |
| 684 | |
| 685 | // No Match; |
Chris Lattner | 00bb283 | 2007-10-11 03:36:41 +0000 | [diff] [blame] | 686 | ObjcQualifiedInterfaceType *QType = |
| 687 | new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 688 | Types.push_back(QType); |
| 689 | ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
| 690 | return QualType(QType, 0); |
| 691 | } |
| 692 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 693 | /// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique |
| 694 | /// TypeOfExpr AST's (since expression's are never shared). For example, |
| 695 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 696 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 697 | /// on canonical type's (which are always unique). |
Steve Naroff | 8d1a3b8 | 2007-08-01 17:20:42 +0000 | [diff] [blame] | 698 | QualType ASTContext::getTypeOfExpr(Expr *tofExpr) { |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 699 | QualType Canonical = tofExpr->getType().getCanonicalType(); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 700 | TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical); |
| 701 | Types.push_back(toe); |
| 702 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 705 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 706 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 707 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 708 | /// an issue. This doesn't effect the type checker, since it operates |
| 709 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 710 | QualType ASTContext::getTypeOfType(QualType tofType) { |
| 711 | QualType Canonical = tofType.getCanonicalType(); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 712 | TypeOfType *tot = new TypeOfType(tofType, Canonical); |
| 713 | Types.push_back(tot); |
| 714 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 717 | /// getTagDeclType - Return the unique reference to the type for the |
| 718 | /// specified TagDecl (struct/union/class/enum) decl. |
| 719 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
| 720 | // The decl stores the type cache. |
| 721 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 722 | |
| 723 | Decl->TypeForDecl = new TagType(Decl, QualType()); |
| 724 | Types.push_back(Decl->TypeForDecl); |
| 725 | return QualType(Decl->TypeForDecl, 0); |
| 726 | } |
| 727 | |
| 728 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 729 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 730 | /// needs to agree with the definition in <stddef.h>. |
| 731 | QualType ASTContext::getSizeType() const { |
| 732 | // On Darwin, size_t is defined as a "long unsigned int". |
| 733 | // FIXME: should derive from "Target". |
| 734 | return UnsignedLongTy; |
| 735 | } |
| 736 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 737 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 738 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 739 | QualType ASTContext::getPointerDiffType() const { |
| 740 | // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug... |
| 741 | // FIXME: should derive from "Target". |
| 742 | return IntTy; |
| 743 | } |
| 744 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 745 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 746 | /// routine will assert if passed a built-in type that isn't an integer or enum. |
| 747 | static int getIntegerRank(QualType t) { |
| 748 | if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) { |
| 749 | assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum"); |
| 750 | return 4; |
| 751 | } |
| 752 | |
| 753 | const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType()); |
| 754 | switch (BT->getKind()) { |
| 755 | default: |
| 756 | assert(0 && "getIntegerRank(): not a built-in integer"); |
| 757 | case BuiltinType::Bool: |
| 758 | return 1; |
| 759 | case BuiltinType::Char_S: |
| 760 | case BuiltinType::Char_U: |
| 761 | case BuiltinType::SChar: |
| 762 | case BuiltinType::UChar: |
| 763 | return 2; |
| 764 | case BuiltinType::Short: |
| 765 | case BuiltinType::UShort: |
| 766 | return 3; |
| 767 | case BuiltinType::Int: |
| 768 | case BuiltinType::UInt: |
| 769 | return 4; |
| 770 | case BuiltinType::Long: |
| 771 | case BuiltinType::ULong: |
| 772 | return 5; |
| 773 | case BuiltinType::LongLong: |
| 774 | case BuiltinType::ULongLong: |
| 775 | return 6; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /// getFloatingRank - Return a relative rank for floating point types. |
| 780 | /// This routine will assert if passed a built-in type that isn't a float. |
| 781 | static int getFloatingRank(QualType T) { |
| 782 | T = T.getCanonicalType(); |
| 783 | if (ComplexType *CT = dyn_cast<ComplexType>(T)) |
| 784 | return getFloatingRank(CT->getElementType()); |
| 785 | |
| 786 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 787 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 788 | case BuiltinType::Float: return FloatRank; |
| 789 | case BuiltinType::Double: return DoubleRank; |
| 790 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 791 | } |
| 792 | } |
| 793 | |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 794 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 795 | /// point or a complex type (based on typeDomain/typeSize). |
| 796 | /// 'typeDomain' is a real floating point or complex type. |
| 797 | /// 'typeSize' is a real floating point or complex type. |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 798 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain( |
| 799 | QualType typeSize, QualType typeDomain) const { |
| 800 | if (typeDomain->isComplexType()) { |
| 801 | switch (getFloatingRank(typeSize)) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 802 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 803 | case FloatRank: return FloatComplexTy; |
| 804 | case DoubleRank: return DoubleComplexTy; |
| 805 | case LongDoubleRank: return LongDoubleComplexTy; |
| 806 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 807 | } |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 808 | if (typeDomain->isRealFloatingType()) { |
| 809 | switch (getFloatingRank(typeSize)) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 810 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 811 | case FloatRank: return FloatTy; |
| 812 | case DoubleRank: return DoubleTy; |
| 813 | case LongDoubleRank: return LongDoubleTy; |
| 814 | } |
| 815 | } |
| 816 | assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain"); |
Chris Lattner | b1776cb | 2007-09-16 19:23:47 +0000 | [diff] [blame] | 817 | //an invalid return value, but the assert |
| 818 | //will ensure that this code is never reached. |
| 819 | return VoidTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 822 | /// compareFloatingType - Handles 3 different combos: |
| 823 | /// float/float, float/complex, complex/complex. |
| 824 | /// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1. |
| 825 | int ASTContext::compareFloatingType(QualType lt, QualType rt) { |
| 826 | if (getFloatingRank(lt) == getFloatingRank(rt)) |
| 827 | return 0; |
| 828 | if (getFloatingRank(lt) > getFloatingRank(rt)) |
| 829 | return 1; |
| 830 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | // maxIntegerType - Returns the highest ranked integer type. Handles 3 case: |
| 834 | // unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1. |
| 835 | QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) { |
| 836 | if (lhs == rhs) return lhs; |
| 837 | |
| 838 | bool t1Unsigned = lhs->isUnsignedIntegerType(); |
| 839 | bool t2Unsigned = rhs->isUnsignedIntegerType(); |
| 840 | |
| 841 | if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned)) |
| 842 | return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs; |
| 843 | |
| 844 | // We have two integer types with differing signs |
| 845 | QualType unsignedType = t1Unsigned ? lhs : rhs; |
| 846 | QualType signedType = t1Unsigned ? rhs : lhs; |
| 847 | |
| 848 | if (getIntegerRank(unsignedType) >= getIntegerRank(signedType)) |
| 849 | return unsignedType; |
| 850 | else { |
| 851 | // FIXME: Need to check if the signed type can represent all values of the |
| 852 | // unsigned type. If it can, then the result is the signed type. |
| 853 | // If it can't, then the result is the unsigned version of the signed type. |
| 854 | // Should probably add a helper that returns a signed integer type from |
| 855 | // an unsigned (and vice versa). C99 6.3.1.8. |
| 856 | return signedType; |
| 857 | } |
| 858 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 859 | |
| 860 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 861 | QualType ASTContext::getCFConstantStringType() { |
| 862 | if (!CFConstantStringTypeDecl) { |
| 863 | CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(), |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 864 | &Idents.get("NSConstantString"), |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 865 | 0); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 866 | QualType FieldTypes[3]; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 867 | |
| 868 | // const int *isa; |
| 869 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 870 | // const char *str; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 871 | FieldTypes[1] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 872 | // long length; |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 873 | FieldTypes[2] = LongTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 874 | // Create fields |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 875 | FieldDecl *FieldDecls[3]; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 876 | |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 877 | for (unsigned i = 0; i < 3; ++i) |
Steve Naroff | f38661e | 2007-09-14 02:20:46 +0000 | [diff] [blame] | 878 | FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 879 | |
| 880 | CFConstantStringTypeDecl->defineBody(FieldDecls, 4); |
| 881 | } |
| 882 | |
| 883 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 884 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 885 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 886 | // This returns true if a type has been typedefed to BOOL: |
| 887 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 888 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 889 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 890 | return !strcmp(TT->getDecl()->getName(), "BOOL"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 891 | |
| 892 | return false; |
| 893 | } |
| 894 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 895 | /// getObjcEncodingTypeSize returns size of type for objective-c encoding |
| 896 | /// purpose. |
| 897 | int ASTContext::getObjcEncodingTypeSize(QualType type) { |
| 898 | SourceLocation Loc; |
| 899 | uint64_t sz = getTypeSize(type, Loc); |
| 900 | |
| 901 | // Make all integer and enum types at least as large as an int |
| 902 | if (sz > 0 && type->isIntegralType()) |
| 903 | sz = std::max(sz, getTypeSize(IntTy, Loc)); |
| 904 | // Treat arrays as pointers, since that's how they're passed in. |
| 905 | else if (type->isArrayType()) |
| 906 | sz = getTypeSize(VoidPtrTy, Loc); |
| 907 | return sz / getTypeSize(CharTy, Loc); |
| 908 | } |
| 909 | |
| 910 | /// getObjcEncodingForMethodDecl - Return the encoded type for this method |
| 911 | /// declaration. |
| 912 | void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl, |
| 913 | std::string& S) |
| 914 | { |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 915 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
| 916 | getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 917 | // Encode result type. |
| 918 | getObjcEncodingForType(Decl->getResultType(), S); |
| 919 | // Compute size of all parameters. |
| 920 | // Start with computing size of a pointer in number of bytes. |
| 921 | // FIXME: There might(should) be a better way of doing this computation! |
| 922 | SourceLocation Loc; |
| 923 | int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc); |
| 924 | // The first two arguments (self and _cmd) are pointers; account for |
| 925 | // their size. |
| 926 | int ParmOffset = 2 * PtrSize; |
| 927 | int NumOfParams = Decl->getNumParams(); |
| 928 | for (int i = 0; i < NumOfParams; i++) { |
| 929 | QualType PType = Decl->getParamDecl(i)->getType(); |
| 930 | int sz = getObjcEncodingTypeSize (PType); |
| 931 | assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type"); |
| 932 | ParmOffset += sz; |
| 933 | } |
| 934 | S += llvm::utostr(ParmOffset); |
| 935 | S += "@0:"; |
| 936 | S += llvm::utostr(PtrSize); |
| 937 | |
| 938 | // Argument types. |
| 939 | ParmOffset = 2 * PtrSize; |
| 940 | for (int i = 0; i < NumOfParams; i++) { |
| 941 | QualType PType = Decl->getParamDecl(i)->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 942 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 943 | // 'in', 'inout', etc. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 944 | getObjcEncodingForTypeQualifier( |
| 945 | Decl->getParamDecl(i)->getObjcDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 946 | getObjcEncodingForType(PType, S); |
| 947 | S += llvm::utostr(ParmOffset); |
| 948 | ParmOffset += getObjcEncodingTypeSize(PType); |
| 949 | } |
| 950 | } |
| 951 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 952 | void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const |
| 953 | { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 954 | // FIXME: This currently doesn't encode: |
| 955 | // @ An object (whether statically typed or typed id) |
| 956 | // # A class object (Class) |
| 957 | // : A method selector (SEL) |
| 958 | // {name=type...} A structure |
| 959 | // (name=type...) A union |
| 960 | // bnum A bit field of num bits |
| 961 | |
| 962 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 963 | char encoding; |
| 964 | switch (BT->getKind()) { |
| 965 | case BuiltinType::Void: |
| 966 | encoding = 'v'; |
| 967 | break; |
| 968 | case BuiltinType::Bool: |
| 969 | encoding = 'B'; |
| 970 | break; |
| 971 | case BuiltinType::Char_U: |
| 972 | case BuiltinType::UChar: |
| 973 | encoding = 'C'; |
| 974 | break; |
| 975 | case BuiltinType::UShort: |
| 976 | encoding = 'S'; |
| 977 | break; |
| 978 | case BuiltinType::UInt: |
| 979 | encoding = 'I'; |
| 980 | break; |
| 981 | case BuiltinType::ULong: |
| 982 | encoding = 'L'; |
| 983 | break; |
| 984 | case BuiltinType::ULongLong: |
| 985 | encoding = 'Q'; |
| 986 | break; |
| 987 | case BuiltinType::Char_S: |
| 988 | case BuiltinType::SChar: |
| 989 | encoding = 'c'; |
| 990 | break; |
| 991 | case BuiltinType::Short: |
| 992 | encoding = 's'; |
| 993 | break; |
| 994 | case BuiltinType::Int: |
| 995 | encoding = 'i'; |
| 996 | break; |
| 997 | case BuiltinType::Long: |
| 998 | encoding = 'l'; |
| 999 | break; |
| 1000 | case BuiltinType::LongLong: |
| 1001 | encoding = 'q'; |
| 1002 | break; |
| 1003 | case BuiltinType::Float: |
| 1004 | encoding = 'f'; |
| 1005 | break; |
| 1006 | case BuiltinType::Double: |
| 1007 | encoding = 'd'; |
| 1008 | break; |
| 1009 | case BuiltinType::LongDouble: |
| 1010 | encoding = 'd'; |
| 1011 | break; |
| 1012 | default: |
| 1013 | assert(0 && "Unhandled builtin type kind"); |
| 1014 | } |
| 1015 | |
| 1016 | S += encoding; |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1017 | } else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1018 | QualType PointeeTy = PT->getPointeeType(); |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1019 | if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) { |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1020 | S += '@'; |
| 1021 | return; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1022 | } else if (isObjcClassType(PointeeTy)) { |
| 1023 | S += '#'; |
| 1024 | return; |
| 1025 | } else if (isObjcSelType(PointeeTy)) { |
| 1026 | S += ':'; |
| 1027 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 1028 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1029 | |
| 1030 | if (PointeeTy->isCharType()) { |
| 1031 | // char pointer types should be encoded as '*' unless it is a |
| 1032 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1033 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1034 | S += '*'; |
| 1035 | return; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | S += '^'; |
| 1040 | getObjcEncodingForType(PT->getPointeeType(), S); |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1041 | } else if (const ArrayType *AT = T->getAsArrayType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1042 | S += '['; |
| 1043 | |
| 1044 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1045 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 1046 | else |
| 1047 | assert(0 && "Unhandled array type!"); |
| 1048 | |
| 1049 | getObjcEncodingForType(AT->getElementType(), S); |
| 1050 | S += ']'; |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 1051 | } else if (T->getAsFunctionType()) { |
| 1052 | S += '?'; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1053 | } else |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 1054 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 1057 | void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT, |
| 1058 | std::string& S) const { |
| 1059 | if (QT & Decl::OBJC_TQ_In) |
| 1060 | S += 'n'; |
| 1061 | if (QT & Decl::OBJC_TQ_Inout) |
| 1062 | S += 'N'; |
| 1063 | if (QT & Decl::OBJC_TQ_Out) |
| 1064 | S += 'o'; |
| 1065 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 1066 | S += 'O'; |
| 1067 | if (QT & Decl::OBJC_TQ_Byref) |
| 1068 | S += 'R'; |
| 1069 | if (QT & Decl::OBJC_TQ_Oneway) |
| 1070 | S += 'V'; |
| 1071 | } |
| 1072 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 1073 | void ASTContext::setBuiltinVaListType(QualType T) |
| 1074 | { |
| 1075 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 1076 | |
| 1077 | BuiltinVaListType = T; |
| 1078 | } |
| 1079 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 1080 | void ASTContext::setObjcIdType(TypedefDecl *TD) |
| 1081 | { |
| 1082 | assert(ObjcIdType.isNull() && "'id' type already set!"); |
| 1083 | |
| 1084 | ObjcIdType = getTypedefType(TD); |
| 1085 | |
| 1086 | // typedef struct objc_object *id; |
| 1087 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1088 | assert(ptr && "'id' incorrectly typed"); |
| 1089 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1090 | assert(rec && "'id' incorrectly typed"); |
| 1091 | IdStructType = rec; |
| 1092 | } |
| 1093 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1094 | void ASTContext::setObjcSelType(TypedefDecl *TD) |
| 1095 | { |
| 1096 | assert(ObjcSelType.isNull() && "'SEL' type already set!"); |
| 1097 | |
| 1098 | ObjcSelType = getTypedefType(TD); |
| 1099 | |
| 1100 | // typedef struct objc_selector *SEL; |
| 1101 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1102 | assert(ptr && "'SEL' incorrectly typed"); |
| 1103 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1104 | assert(rec && "'SEL' incorrectly typed"); |
| 1105 | SelStructType = rec; |
| 1106 | } |
| 1107 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1108 | void ASTContext::setObjcProtoType(TypedefDecl *TD) |
| 1109 | { |
| 1110 | assert(ObjcProtoType.isNull() && "'Protocol' type already set!"); |
| 1111 | |
| 1112 | // typedef struct Protocol Protocol; |
| 1113 | ObjcProtoType = TD->getUnderlyingType(); |
| 1114 | // Protocol * type |
| 1115 | ObjcProtoType = getPointerType(ObjcProtoType); |
| 1116 | ProtoStructType = TD->getUnderlyingType()->getAsStructureType(); |
| 1117 | } |
| 1118 | |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 1119 | void ASTContext::setObjcClassType(TypedefDecl *TD) |
| 1120 | { |
| 1121 | assert(ObjcClassType.isNull() && "'Class' type already set!"); |
| 1122 | |
| 1123 | ObjcClassType = getTypedefType(TD); |
| 1124 | |
| 1125 | // typedef struct objc_class *Class; |
| 1126 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 1127 | assert(ptr && "'Class' incorrectly typed"); |
| 1128 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 1129 | assert(rec && "'Class' incorrectly typed"); |
| 1130 | ClassStructType = rec; |
| 1131 | } |
| 1132 | |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 1133 | void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) { |
| 1134 | assert(ObjcConstantStringType.isNull() && |
| 1135 | "'NSConstantString' type already set!"); |
| 1136 | |
| 1137 | ObjcConstantStringType = getObjcInterfaceType(Decl); |
| 1138 | } |
| 1139 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1140 | bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1141 | const BuiltinType *lBuiltin = lhs->getAsBuiltinType(); |
| 1142 | const BuiltinType *rBuiltin = rhs->getAsBuiltinType(); |
| 1143 | |
| 1144 | return lBuiltin->getKind() == rBuiltin->getKind(); |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1149 | if (lhs->isObjcInterfaceType() && isObjcIdType(rhs)) |
| 1150 | return true; |
| 1151 | else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType()) |
| 1152 | return true; |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
| 1156 | bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1157 | return true; // FIXME: IMPLEMENT. |
| 1158 | } |
| 1159 | |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1160 | bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1161 | const VectorType *lVector = lhs->getAsVectorType(); |
| 1162 | const VectorType *rVector = rhs->getAsVectorType(); |
| 1163 | |
| 1164 | if ((lVector->getElementType().getCanonicalType() == |
| 1165 | rVector->getElementType().getCanonicalType()) && |
| 1166 | (lVector->getNumElements() == rVector->getNumElements())) |
| 1167 | return true; |
| 1168 | return false; |
| 1169 | } |
| 1170 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1171 | // C99 6.2.7p1: If both are complete types, then the following additional |
| 1172 | // requirements apply...FIXME (handle compatibility across source files). |
| 1173 | bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1174 | TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl(); |
| 1175 | TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl(); |
| 1176 | |
| 1177 | if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) { |
| 1178 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1179 | return true; |
| 1180 | } |
| 1181 | if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) { |
| 1182 | if (ldecl->getIdentifier() == rdecl->getIdentifier()) |
| 1183 | return true; |
| 1184 | } |
Steve Naroff | ab37309 | 2007-11-07 06:03:51 +0000 | [diff] [blame] | 1185 | // "Class" and "id" are compatible built-in structure types. |
| 1186 | if (isObjcIdType(lhs) && isObjcClassType(rhs) || |
| 1187 | isObjcClassType(lhs) && isObjcIdType(rhs)) |
| 1188 | return true; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1193 | // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be |
| 1194 | // identically qualified and both shall be pointers to compatible types. |
| 1195 | if (lhs.getQualifiers() != rhs.getQualifiers()) |
| 1196 | return false; |
| 1197 | |
| 1198 | QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType(); |
| 1199 | QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType(); |
| 1200 | |
| 1201 | return typesAreCompatible(ltype, rtype); |
| 1202 | } |
| 1203 | |
| 1204 | // C++ 5.17p6: When the left opperand of an assignment operator denotes a |
| 1205 | // reference to T, the operation assigns to the object of type T denoted by the |
| 1206 | // reference. |
| 1207 | bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1208 | QualType ltype = lhs; |
| 1209 | |
| 1210 | if (lhs->isReferenceType()) |
| 1211 | ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType(); |
| 1212 | |
| 1213 | QualType rtype = rhs; |
| 1214 | |
| 1215 | if (rhs->isReferenceType()) |
| 1216 | rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType(); |
| 1217 | |
| 1218 | return typesAreCompatible(ltype, rtype); |
| 1219 | } |
| 1220 | |
| 1221 | bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1222 | const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType()); |
| 1223 | const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType()); |
| 1224 | const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase); |
| 1225 | const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase); |
| 1226 | |
| 1227 | // first check the return types (common between C99 and K&R). |
| 1228 | if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType())) |
| 1229 | return false; |
| 1230 | |
| 1231 | if (lproto && rproto) { // two C99 style function prototypes |
| 1232 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 1233 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 1234 | |
| 1235 | if (lproto_nargs != rproto_nargs) |
| 1236 | return false; |
| 1237 | |
| 1238 | // both prototypes have the same number of arguments. |
| 1239 | if ((lproto->isVariadic() && !rproto->isVariadic()) || |
| 1240 | (rproto->isVariadic() && !lproto->isVariadic())) |
| 1241 | return false; |
| 1242 | |
| 1243 | // The use of ellipsis agree...now check the argument types. |
| 1244 | for (unsigned i = 0; i < lproto_nargs; i++) |
| 1245 | if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i))) |
| 1246 | return false; |
| 1247 | return true; |
| 1248 | } |
| 1249 | if (!lproto && !rproto) // two K&R style function decls, nothing to do. |
| 1250 | return true; |
| 1251 | |
| 1252 | // we have a mixture of K&R style with C99 prototypes |
| 1253 | const FunctionTypeProto *proto = lproto ? lproto : rproto; |
| 1254 | |
| 1255 | if (proto->isVariadic()) |
| 1256 | return false; |
| 1257 | |
| 1258 | // FIXME: Each parameter type T in the prototype must be compatible with the |
| 1259 | // type resulting from applying the usual argument conversions to T. |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) { |
| 1264 | QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType(); |
| 1265 | QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType(); |
| 1266 | |
| 1267 | if (!typesAreCompatible(ltype, rtype)) |
| 1268 | return false; |
| 1269 | |
| 1270 | // FIXME: If both types specify constant sizes, then the sizes must also be |
| 1271 | // the same. Even if the sizes are the same, GCC produces an error. |
| 1272 | return true; |
| 1273 | } |
| 1274 | |
| 1275 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 1276 | /// both shall have the identically qualified version of a compatible type. |
| 1277 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 1278 | /// same. See 6.7.[2,3,5] for additional rules. |
| 1279 | bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) { |
| 1280 | QualType lcanon = lhs.getCanonicalType(); |
| 1281 | QualType rcanon = rhs.getCanonicalType(); |
| 1282 | |
| 1283 | // If two types are identical, they are are compatible |
| 1284 | if (lcanon == rcanon) |
| 1285 | return true; |
| 1286 | |
| 1287 | // If the canonical type classes don't match, they can't be compatible |
| 1288 | if (lcanon->getTypeClass() != rcanon->getTypeClass()) { |
| 1289 | // For Objective-C, it is possible for two types to be compatible |
| 1290 | // when their classes don't match (when dealing with "id"). If either type |
| 1291 | // is an interface, we defer to objcTypesAreCompatible(). |
| 1292 | if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType()) |
| 1293 | return objcTypesAreCompatible(lcanon, rcanon); |
| 1294 | return false; |
| 1295 | } |
| 1296 | switch (lcanon->getTypeClass()) { |
| 1297 | case Type::Pointer: |
| 1298 | return pointerTypesAreCompatible(lcanon, rcanon); |
| 1299 | case Type::Reference: |
| 1300 | return referenceTypesAreCompatible(lcanon, rcanon); |
| 1301 | case Type::ConstantArray: |
| 1302 | case Type::VariableArray: |
| 1303 | return arrayTypesAreCompatible(lcanon, rcanon); |
| 1304 | case Type::FunctionNoProto: |
| 1305 | case Type::FunctionProto: |
| 1306 | return functionTypesAreCompatible(lcanon, rcanon); |
| 1307 | case Type::Tagged: // handle structures, unions |
| 1308 | return tagTypesAreCompatible(lcanon, rcanon); |
| 1309 | case Type::Builtin: |
| 1310 | return builtinTypesAreCompatible(lcanon, rcanon); |
| 1311 | case Type::ObjcInterface: |
| 1312 | return interfaceTypesAreCompatible(lcanon, rcanon); |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 1313 | case Type::Vector: |
| 1314 | case Type::OCUVector: |
| 1315 | return vectorTypesAreCompatible(lcanon, rcanon); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1316 | default: |
| 1317 | assert(0 && "unexpected type"); |
| 1318 | } |
| 1319 | return true; // should never get here... |
| 1320 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1321 | |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1322 | /// Emit - Serialize an ASTContext object to Bitcode. |
| 1323 | void ASTContext::Emit(llvm::Serializer& S) const { |
Ted Kremenek | 5451350 | 2007-10-31 20:00:03 +0000 | [diff] [blame] | 1324 | S.EmitRef(SourceMgr); |
| 1325 | S.EmitRef(Target); |
| 1326 | S.EmitRef(Idents); |
| 1327 | S.EmitRef(Selectors); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1328 | |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1329 | // Emit the size of the type vector so that we can reserve that size |
| 1330 | // when we reconstitute the ASTContext object. |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1331 | S.EmitInt(Types.size()); |
| 1332 | |
Ted Kremenek | 03ed440 | 2007-11-13 22:02:55 +0000 | [diff] [blame^] | 1333 | for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end(); |
| 1334 | I!=E;++I) |
| 1335 | (*I)->Emit(S); |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1336 | |
Ted Kremenek | a9a4a24 | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 1337 | // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Ted Kremenek | 0f84c00 | 2007-11-13 00:25:37 +0000 | [diff] [blame] | 1340 | ASTContext* ASTContext::Create(llvm::Deserializer& D) { |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1341 | SourceManager &SM = D.ReadRef<SourceManager>(); |
| 1342 | TargetInfo &t = D.ReadRef<TargetInfo>(); |
| 1343 | IdentifierTable &idents = D.ReadRef<IdentifierTable>(); |
| 1344 | SelectorTable &sels = D.ReadRef<SelectorTable>(); |
| 1345 | |
| 1346 | unsigned size_reserve = D.ReadInt(); |
| 1347 | |
| 1348 | ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve); |
| 1349 | |
Ted Kremenek | 03ed440 | 2007-11-13 22:02:55 +0000 | [diff] [blame^] | 1350 | for (unsigned i = 0; i < size_reserve; ++i) |
| 1351 | Type::Create(*A,i,D); |
Ted Kremenek | a4559c3 | 2007-11-06 22:26:16 +0000 | [diff] [blame] | 1352 | |
Ted Kremenek | a9a4a24 | 2007-11-01 18:11:32 +0000 | [diff] [blame] | 1353 | // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>(); |
Ted Kremenek | fee0452 | 2007-10-31 22:44:07 +0000 | [diff] [blame] | 1354 | |
| 1355 | return A; |
| 1356 | } |