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