Chris Lattner | ddc135e | 2006-11-10 06:34:16 +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" |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 21 | enum FloatingRank { |
| 22 | FloatRank, DoubleRank, LongDoubleRank |
| 23 | }; |
| 24 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 25 | ASTContext::~ASTContext() { |
| 26 | // Deallocate all the types. |
| 27 | while (!Types.empty()) { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 28 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) { |
| 29 | // Destroy the object, but don't call delete. These are malloc'd. |
| 30 | FT->~FunctionTypeProto(); |
| 31 | free(FT); |
| 32 | } else { |
| 33 | delete Types.back(); |
| 34 | } |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 35 | Types.pop_back(); |
| 36 | } |
| 37 | } |
| 38 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 39 | void ASTContext::PrintStats() const { |
| 40 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 41 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
| 42 | unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 43 | unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 44 | |
| 45 | unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0; |
| 46 | |
| 47 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 48 | Type *T = Types[i]; |
| 49 | if (isa<BuiltinType>(T)) |
| 50 | ++NumBuiltin; |
| 51 | else if (isa<PointerType>(T)) |
| 52 | ++NumPointer; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 53 | else if (isa<ReferenceType>(T)) |
| 54 | ++NumReference; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 55 | else if (isa<ArrayType>(T)) |
| 56 | ++NumArray; |
| 57 | else if (isa<FunctionTypeNoProto>(T)) |
| 58 | ++NumFunctionNP; |
| 59 | else if (isa<FunctionTypeProto>(T)) |
| 60 | ++NumFunctionP; |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 61 | else if (isa<TypedefType>(T)) |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 62 | ++NumTypeName; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 63 | else if (TagType *TT = dyn_cast<TagType>(T)) { |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 64 | ++NumTagged; |
| 65 | switch (TT->getDecl()->getKind()) { |
| 66 | default: assert(0 && "Unknown tagged type!"); |
| 67 | case Decl::Struct: ++NumTagStruct; break; |
| 68 | case Decl::Union: ++NumTagUnion; break; |
| 69 | case Decl::Class: ++NumTagClass; break; |
| 70 | case Decl::Enum: ++NumTagEnum; break; |
| 71 | } |
| 72 | } else { |
| 73 | assert(0 && "Unknown type!"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fprintf(stderr, " %d builtin types\n", NumBuiltin); |
| 78 | fprintf(stderr, " %d pointer types\n", NumPointer); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 79 | fprintf(stderr, " %d reference types\n", NumReference); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 80 | fprintf(stderr, " %d array types\n", NumArray); |
| 81 | fprintf(stderr, " %d function types with proto\n", NumFunctionP); |
| 82 | fprintf(stderr, " %d function types with no proto\n", NumFunctionNP); |
| 83 | fprintf(stderr, " %d typename (typedef) types\n", NumTypeName); |
| 84 | fprintf(stderr, " %d tagged types\n", NumTagged); |
| 85 | fprintf(stderr, " %d struct types\n", NumTagStruct); |
| 86 | fprintf(stderr, " %d union types\n", NumTagUnion); |
| 87 | fprintf(stderr, " %d class types\n", NumTagClass); |
| 88 | fprintf(stderr, " %d enum types\n", NumTagEnum); |
Chris Lattner | fc234de | 2007-05-24 00:40:54 +0000 | [diff] [blame] | 89 | fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+ |
Steve Naroff | f84d11f | 2007-05-23 21:48:04 +0000 | [diff] [blame] | 90 | NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+ |
Chris Lattner | fc234de | 2007-05-24 00:40:54 +0000 | [diff] [blame] | 91 | NumFunctionP*sizeof(FunctionTypeProto)+ |
| 92 | NumFunctionNP*sizeof(FunctionTypeNoProto)+ |
| 93 | NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType))); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 97 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
| 98 | Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr()); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 102 | void ASTContext::InitBuiltinTypes() { |
| 103 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 104 | |
| 105 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 106 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 107 | |
| 108 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 109 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 110 | // C99 6.2.5p3. |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 111 | if (Target.isCharSigned(SourceLocation())) |
| 112 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 113 | else |
| 114 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 115 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 116 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 117 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 118 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 119 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 120 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 121 | |
| 122 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 123 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 124 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 125 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 126 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 127 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 128 | |
| 129 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 130 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 131 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 132 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 133 | |
| 134 | // C99 6.2.5p11. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 135 | InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex); |
| 136 | InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex); |
| 137 | InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 141 | /// the specified type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 142 | QualType ASTContext::getPointerType(QualType T) { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 143 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 144 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame^] | 145 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 146 | PointerType::Profile(ID, T); |
| 147 | |
| 148 | void *InsertPos = 0; |
| 149 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 150 | return QualType(PT, 0); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 152 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 153 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 154 | QualType Canonical; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 155 | if (!T->isCanonical()) { |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 156 | Canonical = getPointerType(T.getCanonicalType()); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 157 | |
| 158 | // Get the new insert position for the node we care about. |
| 159 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 160 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 161 | } |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 162 | PointerType *New = new PointerType(T, Canonical); |
| 163 | Types.push_back(New); |
| 164 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 165 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 168 | /// getReferenceType - Return the uniqued reference to the type for a reference |
| 169 | /// to the specified type. |
| 170 | QualType ASTContext::getReferenceType(QualType T) { |
| 171 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 172 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame^] | 173 | llvm::FoldingSetNodeID ID; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 174 | ReferenceType::Profile(ID, T); |
| 175 | |
| 176 | void *InsertPos = 0; |
| 177 | if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 178 | return QualType(RT, 0); |
| 179 | |
| 180 | // If the referencee type isn't canonical, this won't be a canonical type |
| 181 | // either, so fill in the canonical type field. |
| 182 | QualType Canonical; |
| 183 | if (!T->isCanonical()) { |
| 184 | Canonical = getReferenceType(T.getCanonicalType()); |
| 185 | |
| 186 | // Get the new insert position for the node we care about. |
| 187 | ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 188 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 189 | } |
| 190 | |
| 191 | ReferenceType *New = new ReferenceType(T, Canonical); |
| 192 | Types.push_back(New); |
| 193 | ReferenceTypes.InsertNode(New, InsertPos); |
| 194 | return QualType(New, 0); |
| 195 | } |
| 196 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 197 | /// getArrayType - Return the unique reference to the type for an array of the |
| 198 | /// specified element type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 199 | QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM, |
| 200 | unsigned EltTypeQuals, Expr *NumElts) { |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 201 | // Unique array types, to guarantee there is only one array of a particular |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 202 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame^] | 203 | llvm::FoldingSetNodeID ID; |
Steve Naroff | b7d4924 | 2007-03-14 19:55:17 +0000 | [diff] [blame] | 204 | ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts); |
Steve Naroff | 408451b | 2007-02-26 22:17:12 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 206 | void *InsertPos = 0; |
| 207 | if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 208 | return QualType(ATP, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 209 | |
| 210 | // If the element type isn't canonical, this won't be a canonical type either, |
| 211 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 212 | QualType Canonical; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 213 | if (!EltTy->isCanonical()) { |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 214 | Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals, |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 215 | NumElts); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 216 | |
| 217 | // Get the new insert position for the node we care about. |
| 218 | ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 219 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 220 | } |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 221 | |
Steve Naroff | b7d4924 | 2007-03-14 19:55:17 +0000 | [diff] [blame] | 222 | ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 223 | ArrayTypes.InsertNode(New, InsertPos); |
| 224 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 225 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 228 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 229 | /// |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 230 | QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 231 | // Unique functions, to guarantee there is only one function of a particular |
| 232 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame^] | 233 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 234 | FunctionTypeNoProto::Profile(ID, ResultTy); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 236 | void *InsertPos = 0; |
| 237 | if (FunctionTypeNoProto *FT = |
| 238 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 239 | return QualType(FT, 0); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 240 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 241 | QualType Canonical; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 242 | if (!ResultTy->isCanonical()) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 243 | Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType()); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 244 | |
| 245 | // Get the new insert position for the node we care about. |
| 246 | FunctionTypeNoProto *NewIP = |
| 247 | FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 248 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
| 249 | } |
| 250 | |
| 251 | FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical); |
| 252 | Types.push_back(New); |
| 253 | FunctionTypeProtos.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 254 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /// getFunctionType - Return a normal function type with a typed argument |
| 258 | /// list. isVariadic indicates whether the argument list includes '...'. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 259 | QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray, |
| 260 | unsigned NumArgs, bool isVariadic) { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 261 | // Unique functions, to guarantee there is only one function of a particular |
| 262 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame^] | 263 | llvm::FoldingSetNodeID ID; |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 264 | FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic); |
| 265 | |
| 266 | void *InsertPos = 0; |
| 267 | if (FunctionTypeProto *FTP = |
| 268 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 269 | return QualType(FTP, 0); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 270 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 271 | // Determine whether the type being created is already canonical or not. |
| 272 | bool isCanonical = ResultTy->isCanonical(); |
| 273 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 274 | if (!ArgArray[i]->isCanonical()) |
| 275 | isCanonical = false; |
| 276 | |
| 277 | // If this type isn't canonical, get the canonical version of it. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 278 | QualType Canonical; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 279 | if (!isCanonical) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame^] | 280 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 281 | CanonicalArgs.reserve(NumArgs); |
| 282 | for (unsigned i = 0; i != NumArgs; ++i) |
| 283 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 284 | |
| 285 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 286 | &CanonicalArgs[0], NumArgs, |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 287 | isVariadic); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 288 | |
| 289 | // Get the new insert position for the node we care about. |
| 290 | FunctionTypeProto *NewIP = |
| 291 | FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos); |
| 292 | assert(NewIP == 0 && "Shouldn't be in the map!"); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // FunctionTypeProto objects are not allocated with new because they have a |
| 296 | // variable size array (for parameter types) at the end of them. |
| 297 | FunctionTypeProto *FTP = |
| 298 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 299 | (NumArgs-1)*sizeof(QualType)); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 300 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 301 | Canonical); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 302 | Types.push_back(FTP); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 303 | FunctionTypeProtos.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 304 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 307 | /// getTypedefType - Return the unique reference to the type for the |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 308 | /// specified typename decl. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 309 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 310 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 311 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 312 | QualType Canonical = Decl->getUnderlyingType().getCanonicalType(); |
Chris Lattner | cceab1a | 2007-03-26 20:16:44 +0000 | [diff] [blame] | 313 | Decl->TypeForDecl = new TypedefType(Decl, Canonical); |
| 314 | Types.push_back(Decl->TypeForDecl); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 315 | return QualType(Decl->TypeForDecl, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 318 | /// getTagDeclType - Return the unique reference to the type for the |
| 319 | /// specified TagDecl (struct/union/class/enum) decl. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 320 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
Chris Lattner | 733067d | 2007-01-26 01:42:24 +0000 | [diff] [blame] | 321 | // The decl stores the type cache. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 322 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 323 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 324 | Decl->TypeForDecl = new TagType(Decl, QualType()); |
Chris Lattner | cceab1a | 2007-03-26 20:16:44 +0000 | [diff] [blame] | 325 | Types.push_back(Decl->TypeForDecl); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 326 | return QualType(Decl->TypeForDecl, 0); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 329 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
Steve Naroff | 0f6256d | 2007-04-02 23:01:44 +0000 | [diff] [blame] | 330 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 331 | /// needs to agree with the definition in <stddef.h>. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 332 | QualType ASTContext::getSizeType() const { |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 333 | // On Darwin, size_t is defined as a "long unsigned int". |
| 334 | // FIXME: should derive from "Target". |
| 335 | return UnsignedLongTy; |
| 336 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 338 | /// getIntegerBitwidth - Return the bitwidth of the specified integer type |
| 339 | /// according to the target. 'Loc' specifies the source location that |
| 340 | /// requires evaluation of this property. |
| 341 | unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) { |
| 342 | if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) { |
| 343 | assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum"); |
| 344 | assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!"); |
| 345 | } |
| 346 | |
| 347 | const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType()); |
| 348 | switch (BT->getKind()) { |
| 349 | default: assert(0 && "getIntegerBitwidth(): not a built-in integer"); |
| 350 | case BuiltinType::Bool: return Target.getBoolWidth(Loc); |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 351 | case BuiltinType::Char_S: |
| 352 | case BuiltinType::Char_U: |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 353 | case BuiltinType::SChar: |
| 354 | case BuiltinType::UChar: return Target.getCharWidth(Loc); |
| 355 | case BuiltinType::Short: |
| 356 | case BuiltinType::UShort: return Target.getShortWidth(Loc); |
| 357 | case BuiltinType::Int: |
| 358 | case BuiltinType::UInt: return Target.getIntWidth(Loc); |
| 359 | case BuiltinType::Long: |
| 360 | case BuiltinType::ULong: return Target.getLongWidth(Loc); |
| 361 | case BuiltinType::LongLong: |
| 362 | case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc); |
| 363 | } |
| 364 | } |
| 365 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 366 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 367 | /// routine will assert if passed a built-in type that isn't an integer or enum. |
| 368 | static int getIntegerRank(QualType t) { |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 369 | if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) { |
| 370 | assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum"); |
| 371 | return 4; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 372 | } |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 373 | |
| 374 | const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType()); |
| 375 | switch (BT->getKind()) { |
| 376 | default: |
| 377 | assert(0 && "getIntegerRank(): not a built-in integer"); |
| 378 | case BuiltinType::Bool: |
| 379 | return 1; |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 380 | case BuiltinType::Char_S: |
| 381 | case BuiltinType::Char_U: |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 382 | case BuiltinType::SChar: |
| 383 | case BuiltinType::UChar: |
| 384 | return 2; |
| 385 | case BuiltinType::Short: |
| 386 | case BuiltinType::UShort: |
| 387 | return 3; |
| 388 | case BuiltinType::Int: |
| 389 | case BuiltinType::UInt: |
| 390 | return 4; |
| 391 | case BuiltinType::Long: |
| 392 | case BuiltinType::ULong: |
| 393 | return 5; |
| 394 | case BuiltinType::LongLong: |
| 395 | case BuiltinType::ULongLong: |
| 396 | return 6; |
| 397 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 400 | /// getFloatingRank - Return a relative rank for floating point types. |
| 401 | /// This routine will assert if passed a built-in type that isn't a float. |
| 402 | static int getFloatingRank(QualType t) { |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 403 | switch (cast<BuiltinType>(t.getCanonicalType())->getKind()) { |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 404 | default: |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 405 | assert(0 && "getFloatingPointRank(): not a floating type"); |
| 406 | case BuiltinType::Float: |
| 407 | case BuiltinType::FloatComplex: |
| 408 | return FloatRank; |
| 409 | case BuiltinType::Double: |
| 410 | case BuiltinType::DoubleComplex: |
| 411 | return DoubleRank; |
| 412 | case BuiltinType::LongDouble: |
| 413 | case BuiltinType::LongDoubleComplex: |
| 414 | return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | // maxComplexType - the following code handles 3 different combinations: |
| 419 | // complex/complex, complex/float, float/complex. |
| 420 | // When both operands are complex, the shorter operand is converted to the |
| 421 | // type of the longer, and that is the type of the result. This corresponds |
| 422 | // to what is done when combining two real floating-point operands. |
| 423 | // The fun begins when size promotion occur across type domains. g |
| 424 | // getFloatingRank & convertFloatingRankToComplexType handle this without |
| 425 | // enumerating all permutations. |
| 426 | // It also allows us to add new types without breakage. |
| 427 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 428 | // floating-point type, the less precise type is converted, within it's |
| 429 | // real or complex domain, to the precision of the other type. For example, |
| 430 | // when combining a "long double" with a "double _Complex", the |
| 431 | // "double _Complex" is promoted to "long double _Complex". |
| 432 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 433 | QualType ASTContext::maxComplexType(QualType lt, QualType rt) const { |
| 434 | switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) { |
| 435 | default: |
| 436 | assert(0 && "convertRankToComplex(): illegal value for rank"); |
| 437 | case FloatRank: |
| 438 | return FloatComplexTy; |
| 439 | case DoubleRank: |
| 440 | return DoubleComplexTy; |
| 441 | case LongDoubleRank: |
| 442 | return LongDoubleComplexTy; |
| 443 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // maxFloatingType - handles the simple case, both operands are floats. |
| 447 | QualType ASTContext::maxFloatingType(QualType lt, QualType rt) { |
| 448 | return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt; |
| 449 | } |
| 450 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 451 | // maxIntegerType - Returns the highest ranked integer type. Handles 3 case: |
| 452 | // unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1. |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 453 | QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) { |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 454 | if (lhs == rhs) return lhs; |
| 455 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 456 | bool t1Unsigned = lhs->isUnsignedIntegerType(); |
| 457 | bool t2Unsigned = rhs->isUnsignedIntegerType(); |
| 458 | |
| 459 | if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned)) |
| 460 | return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs; |
| 461 | |
| 462 | // We have two integer types with differing signs |
| 463 | QualType unsignedType = t1Unsigned ? lhs : rhs; |
| 464 | QualType signedType = t1Unsigned ? rhs : lhs; |
| 465 | |
| 466 | if (getIntegerRank(unsignedType) >= getIntegerRank(signedType)) |
| 467 | return unsignedType; |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 468 | else { |
| 469 | // FIXME: Need to check if the signed type can represent all values of the |
| 470 | // unsigned type. If it can, then the result is the signed type. |
| 471 | // If it can't, then the result is the unsigned version of the signed type. |
| 472 | // Should probably add a helper that returns a signed integer type from |
| 473 | // an unsigned (and vice versa). C99 6.3.1.8. |
| 474 | return signedType; |
| 475 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 476 | } |