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