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 | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | using namespace clang; |
| 20 | |
| 21 | ASTContext::ASTContext(Preprocessor &pp) |
| 22 | : PP(pp), Target(pp.getTargetInfo()) { |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 23 | InitBuiltinTypes(); |
| 24 | } |
| 25 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 26 | ASTContext::~ASTContext() { |
| 27 | // Deallocate all the types. |
| 28 | while (!Types.empty()) { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 29 | if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) { |
| 30 | // Destroy the object, but don't call delete. These are malloc'd. |
| 31 | FT->~FunctionTypeProto(); |
| 32 | free(FT); |
| 33 | } else { |
| 34 | delete Types.back(); |
| 35 | } |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 36 | Types.pop_back(); |
| 37 | } |
| 38 | } |
| 39 | |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 40 | void ASTContext::InitBuiltinType(TypeRef &R, BuiltinType::Kind K) { |
| 41 | Types.push_back((R = new BuiltinType(K)).getTypePtr()); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 45 | void ASTContext::InitBuiltinTypes() { |
| 46 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 47 | |
| 48 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 49 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 50 | |
| 51 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 52 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 53 | // C99 6.2.5p3. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 54 | InitBuiltinType(CharTy, BuiltinType::Char); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 55 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 56 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 57 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 58 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 59 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 60 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 61 | |
| 62 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 63 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 64 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 65 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 66 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 67 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 68 | |
| 69 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 70 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 71 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 72 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 73 | |
| 74 | // C99 6.2.5p11. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 75 | InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex); |
| 76 | InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex); |
| 77 | InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 81 | /// the specified type. |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 82 | TypeRef ASTContext::getPointerType(TypeRef T) { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 83 | // FIXME: This is obviously braindead! |
| 84 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 85 | // structure. |
| 86 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
Chris Lattner | 4781466 | 2006-11-12 00:56:20 +0000 | [diff] [blame] | 87 | if (PointerType *PTy = dyn_cast<PointerType>(Types[i])) |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 88 | if (PTy->getPointeeType() == T) |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 89 | return Types[i]; |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 91 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 92 | // so fill in the canonical type field. |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 93 | Type *Canonical = 0; |
| 94 | if (!T->isCanonical()) |
| 95 | Canonical = getPointerType(T.getCanonicalType()).getTypePtr(); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 96 | |
| 97 | Types.push_back(new PointerType(T, Canonical)); |
| 98 | return Types.back(); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 101 | /// getArrayType - Return the unique reference to the type for an array of the |
| 102 | /// specified element type. |
| 103 | TypeRef ASTContext::getArrayType(TypeRef EltTy,ArrayType::ArraySizeModifier ASM, |
| 104 | unsigned EltTypeQuals, void *NumElts) { |
| 105 | #warning "IGNORING SIZE" |
| 106 | |
| 107 | // FIXME: This is obviously braindead! |
| 108 | // Unique array, to guarantee there is only one array of a particular |
| 109 | // structure. |
| 110 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 111 | if (ArrayType *ATy = dyn_cast<ArrayType>(Types[i])) |
| 112 | if (ATy->getElementType() == EltTy && |
| 113 | ATy->getSizeModifier() == ASM && |
| 114 | ATy->getIndexTypeQualifier() == EltTypeQuals) |
| 115 | return Types[i]; |
| 116 | |
| 117 | // If the element type isn't canonical, this won't be a canonical type either, |
| 118 | // so fill in the canonical type field. |
| 119 | Type *Canonical = 0; |
| 120 | if (!EltTy->isCanonical()) |
| 121 | Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals, |
| 122 | NumElts).getTypePtr(); |
| 123 | |
| 124 | Types.push_back(new ArrayType(EltTy, ASM, EltTypeQuals, Canonical)); |
| 125 | return Types.back(); |
| 126 | } |
| 127 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 128 | /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. |
| 129 | /// |
| 130 | TypeRef ASTContext::getFunctionTypeNoProto(TypeRef ResultTy) { |
| 131 | // FIXME: This is obviously braindead! |
| 132 | // Unique functions, to guarantee there is only one function of a particular |
| 133 | // structure. |
| 134 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 135 | if (FunctionTypeNoProto *FTy = dyn_cast<FunctionTypeNoProto>(Types[i])) |
| 136 | if (FTy->getResultType() == ResultTy) |
| 137 | return Types[i]; |
| 138 | |
| 139 | Type *Canonical = 0; |
| 140 | if (!ResultTy->isCanonical()) |
| 141 | Canonical =getFunctionTypeNoProto(ResultTy.getCanonicalType()).getTypePtr(); |
| 142 | |
| 143 | Types.push_back(new FunctionTypeNoProto(ResultTy, Canonical)); |
| 144 | return Types.back(); |
| 145 | } |
| 146 | |
| 147 | /// getFunctionType - Return a normal function type with a typed argument |
| 148 | /// list. isVariadic indicates whether the argument list includes '...'. |
| 149 | TypeRef ASTContext::getFunctionType(TypeRef ResultTy, TypeRef *ArgArray, |
| 150 | unsigned NumArgs, bool isVariadic) { |
| 151 | // FIXME: This is obviously braindead! |
| 152 | // Unique functions, to guarantee there is only one function of a particular |
| 153 | // structure. |
| 154 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 155 | if (FunctionTypeProto *FTy = dyn_cast<FunctionTypeProto>(Types[i])) |
| 156 | if (FTy->getResultType() == ResultTy && |
| 157 | FTy->getNumArgs() == NumArgs && |
| 158 | FTy->isVariadic() == isVariadic) { |
| 159 | bool Match = true; |
| 160 | for (unsigned arg = 0; arg != NumArgs; ++arg) { |
| 161 | if (FTy->getArgType(arg) != ArgArray[arg]) { |
| 162 | Match = false; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | if (Match) |
| 167 | return Types[i]; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Determine whether the type being created is already canonical or not. |
| 172 | bool isCanonical = ResultTy->isCanonical(); |
| 173 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 174 | if (!ArgArray[i]->isCanonical()) |
| 175 | isCanonical = false; |
| 176 | |
| 177 | // If this type isn't canonical, get the canonical version of it. |
| 178 | Type *Canonical = 0; |
| 179 | if (!isCanonical) { |
| 180 | SmallVector<TypeRef, 16> CanonicalArgs; |
| 181 | CanonicalArgs.reserve(NumArgs); |
| 182 | for (unsigned i = 0; i != NumArgs; ++i) |
| 183 | CanonicalArgs.push_back(ArgArray[i].getCanonicalType()); |
| 184 | |
| 185 | Canonical = getFunctionType(ResultTy.getCanonicalType(), |
| 186 | &CanonicalArgs[0], NumArgs, |
| 187 | isVariadic).getTypePtr(); |
| 188 | } |
| 189 | |
| 190 | // FunctionTypeProto objects are not allocated with new because they have a |
| 191 | // variable size array (for parameter types) at the end of them. |
| 192 | FunctionTypeProto *FTP = |
| 193 | (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + |
| 194 | (NumArgs-1)*sizeof(TypeRef)); |
| 195 | new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, |
| 196 | Canonical); |
| 197 | |
| 198 | Types.push_back(FTP); |
| 199 | return FTP; |
| 200 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 201 | |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 202 | /// getTypeDeclType - Return the unique reference to the type for the |
| 203 | /// specified typename decl. |
| 204 | TypeRef ASTContext::getTypeDeclType(TypeDecl *Decl) { |
| 205 | // FIXME: This is obviously braindead! |
| 206 | // Unique TypeDecl, to guarantee there is only one TypeDeclType. |
| 207 | for (unsigned i = 0, e = Types.size(); i != e; ++i) |
| 208 | if (TypeNameType *Ty = dyn_cast<TypeNameType>(Types[i])) |
| 209 | if (Ty->getDecl() == Decl) |
| 210 | return Types[i]; |
| 211 | |
| 212 | // FIXME: does this lose qualifiers from the typedef?? |
| 213 | |
Chris Lattner | d0ee402 | 2007-01-22 07:39:30 +0000 | [diff] [blame] | 214 | Type *Canonical = Decl->getUnderlyingType().getTypePtr(); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 215 | Types.push_back(new TypeNameType(Decl, Canonical)); |
| 216 | return Types.back(); |
| 217 | } |
| 218 | |