Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1 | //===--- TypeLoc.cpp - Type Source Info Wrapper -----------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the TypeLoc subclasses implementations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 34a0447 | 2009-10-15 03:50:32 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | 0c41180 | 2009-09-29 21:27:32 +0000 | [diff] [blame] | 15 | #include "clang/AST/TypeLocVisitor.h" |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // TypeLoc Implementation |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Argyrios Kyrtzidis | 68006af | 2009-09-29 19:40:46 +0000 | [diff] [blame] | 24 | namespace { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 25 | class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> { |
| 26 | public: |
| 27 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | 34a0447 | 2009-10-15 03:50:32 +0000 | [diff] [blame] | 28 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 29 | SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \ |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 30 | return TyLoc.getLocalSourceRange(); \ |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 31 | } |
Argyrios Kyrtzidis | 68006af | 2009-09-29 19:40:46 +0000 | [diff] [blame] | 32 | #include "clang/AST/TypeLocNodes.def" |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 33 | }; |
Argyrios Kyrtzidis | 68006af | 2009-09-29 19:40:46 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 36 | SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 37 | if (TL.isNull()) return SourceRange(); |
| 38 | return TypeLocRanger().Visit(TL); |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | namespace { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 42 | class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> { |
| 43 | public: |
| 44 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | 34a0447 | 2009-10-15 03:50:32 +0000 | [diff] [blame] | 45 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 46 | unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \ |
| 47 | return TyLoc.getFullDataSize(); \ |
| 48 | } |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 49 | #include "clang/AST/TypeLocNodes.def" |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 50 | }; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /// \brief Returns the size of the type source info data block. |
John McCall | 34a0447 | 2009-10-15 03:50:32 +0000 | [diff] [blame] | 54 | unsigned TypeLoc::getFullDataSizeForType(QualType Ty) { |
| 55 | if (Ty.isNull()) return 0; |
| 56 | return TypeSizer().Visit(TypeLoc(Ty, 0)); |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | namespace { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 60 | class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> { |
| 61 | public: |
| 62 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 63 | #define TYPELOC(CLASS, PARENT) \ |
| 64 | TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \ |
| 65 | return TyLoc.getNextTypeLoc(); \ |
| 66 | } |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 67 | #include "clang/AST/TypeLocNodes.def" |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 68 | }; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// \brief Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the |
| 72 | /// TypeLoc is a PointerLoc and next TypeLoc is for "int". |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 73 | TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) { |
| 74 | return NextLoc().Visit(TL); |
John McCall | 4ce74bd | 2009-10-16 22:31:57 +0000 | [diff] [blame] | 75 | } |
| 76 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 77 | /// \brief Initializes a type location, and all of its children |
| 78 | /// recursively, as if the entire tree had been written in the |
| 79 | /// given location. |
John McCall | 4ce74bd | 2009-10-16 22:31:57 +0000 | [diff] [blame] | 80 | void TypeLoc::initializeImpl(TypeLoc TL, SourceLocation Loc) { |
John McCall | 7eb914b | 2010-05-16 02:09:32 +0000 | [diff] [blame] | 81 | while (true) { |
| 82 | switch (TL.getTypeLocClass()) { |
| 83 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 84 | #define TYPELOC(CLASS, PARENT) \ |
| 85 | case CLASS: { \ |
| 86 | CLASS##TypeLoc TLCasted = cast<CLASS##TypeLoc>(TL); \ |
| 87 | TLCasted.initializeLocal(Loc); \ |
| 88 | TL = TLCasted.getNextTypeLoc(); \ |
| 89 | if (!TL) return; \ |
| 90 | continue; \ |
| 91 | } |
| 92 | #include "clang/AST/TypeLocNodes.def" |
| 93 | } |
| 94 | } |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 95 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 96 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 97 | /// \brief Initializes a type location by copying all its data from |
| 98 | /// another type location of the same type. |
| 99 | void TypeLoc::initializeFullCopyImpl(TypeLoc TL, TypeLoc Other) { |
| 100 | assert(TL.getType() == Other.getType() && "Must copy from same type"); |
| 101 | memcpy(TL.getOpaqueData(), Other.getOpaqueData(), TL.getFullDataSize()); |
| 102 | } |
| 103 | |
Abramo Bagnara | e4aec0e | 2010-05-21 21:12:12 +0000 | [diff] [blame] | 104 | SourceLocation TypeLoc::getBeginLoc() const { |
| 105 | TypeLoc Cur = *this; |
| 106 | while (true) { |
| 107 | switch (Cur.getTypeLocClass()) { |
| 108 | // FIXME: Currently QualifiedTypeLoc does not have a source range |
| 109 | // case Qualified: |
| 110 | case Elaborated: |
| 111 | break; |
| 112 | default: |
| 113 | TypeLoc Next = Cur.getNextTypeLoc(); |
| 114 | if (Next.isNull()) break; |
| 115 | Cur = Next; |
| 116 | continue; |
| 117 | } |
| 118 | break; |
| 119 | } |
| 120 | return Cur.getLocalSourceRange().getBegin(); |
| 121 | } |
| 122 | |
| 123 | SourceLocation TypeLoc::getEndLoc() const { |
| 124 | TypeLoc Cur = *this; |
| 125 | while (true) { |
| 126 | switch (Cur.getTypeLocClass()) { |
| 127 | default: |
| 128 | break; |
| 129 | case Qualified: |
| 130 | case Elaborated: |
| 131 | Cur = Cur.getNextTypeLoc(); |
| 132 | continue; |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | return Cur.getLocalSourceRange().getEnd(); |
| 137 | } |
| 138 | |
| 139 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 140 | namespace { |
| 141 | struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> { |
| 142 | // Overload resolution does the real work for us. |
| 143 | static bool isTypeSpec(TypeSpecTypeLoc _) { return true; } |
| 144 | static bool isTypeSpec(TypeLoc _) { return false; } |
| 145 | |
| 146 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
| 147 | #define TYPELOC(CLASS, PARENT) \ |
| 148 | bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \ |
| 149 | return isTypeSpec(TyLoc); \ |
| 150 | } |
| 151 | #include "clang/AST/TypeLocNodes.def" |
| 152 | }; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /// \brief Determines if the given type loc corresponds to a |
| 157 | /// TypeSpecTypeLoc. Since there is not actually a TypeSpecType in |
| 158 | /// the type hierarchy, this is made somewhat complicated. |
| 159 | /// |
| 160 | /// There are a lot of types that currently use TypeSpecTypeLoc |
| 161 | /// because it's a convenient base class. Ideally we would not accept |
| 162 | /// those here, but ideally we would have better implementations for |
| 163 | /// them. |
| 164 | bool TypeSpecTypeLoc::classof(const TypeLoc *TL) { |
| 165 | if (TL->getType().hasLocalQualifiers()) return false; |
| 166 | return TSTChecker().Visit(*TL); |
| 167 | } |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 168 | |
| 169 | // Reimplemented to account for GNU/C++ extension |
| 170 | // typeof unary-expression |
| 171 | // where there are no parentheses. |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 172 | SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const { |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 173 | if (getRParenLoc().isValid()) |
| 174 | return SourceRange(getTypeofLoc(), getRParenLoc()); |
| 175 | else |
| 176 | return SourceRange(getTypeofLoc(), |
| 177 | getUnderlyingExpr()->getSourceRange().getEnd()); |
| 178 | } |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const { |
| 182 | if (needsExtraLocalData()) |
| 183 | return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type); |
| 184 | else { |
| 185 | switch (getTypePtr()->getKind()) { |
| 186 | case BuiltinType::Void: |
| 187 | return TST_void; |
| 188 | case BuiltinType::Bool: |
| 189 | return TST_bool; |
| 190 | case BuiltinType::Char_U: |
| 191 | case BuiltinType::Char_S: |
| 192 | return TST_char; |
| 193 | case BuiltinType::Char16: |
| 194 | return TST_char16; |
| 195 | case BuiltinType::Char32: |
| 196 | return TST_char32; |
| 197 | case BuiltinType::WChar: |
| 198 | return TST_wchar; |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 199 | case BuiltinType::UndeducedAuto: |
| 200 | return TST_auto; |
| 201 | |
| 202 | case BuiltinType::UChar: |
| 203 | case BuiltinType::UShort: |
| 204 | case BuiltinType::UInt: |
| 205 | case BuiltinType::ULong: |
| 206 | case BuiltinType::ULongLong: |
| 207 | case BuiltinType::UInt128: |
| 208 | case BuiltinType::SChar: |
| 209 | case BuiltinType::Short: |
| 210 | case BuiltinType::Int: |
| 211 | case BuiltinType::Long: |
| 212 | case BuiltinType::LongLong: |
| 213 | case BuiltinType::Int128: |
Douglas Gregor | d038f36 | 2010-01-18 20:37:56 +0000 | [diff] [blame] | 214 | case BuiltinType::Float: |
| 215 | case BuiltinType::Double: |
| 216 | case BuiltinType::LongDouble: |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 217 | llvm_unreachable("Builtin type needs extra local data!"); |
| 218 | // Fall through, if the impossible happens. |
| 219 | |
| 220 | case BuiltinType::NullPtr: |
| 221 | case BuiltinType::Overload: |
| 222 | case BuiltinType::Dependent: |
| 223 | case BuiltinType::ObjCId: |
| 224 | case BuiltinType::ObjCClass: |
| 225 | case BuiltinType::ObjCSel: |
| 226 | return TST_unspecified; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return TST_unspecified; |
| 231 | } |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame^] | 232 | |
| 233 | TypeLoc TypeLoc::IgnoreParens() const { |
| 234 | TypeLoc TL = *this; |
| 235 | while (ParenTypeLoc* PTL = dyn_cast<ParenTypeLoc>(&TL)) |
| 236 | TL = PTL->getInnerLoc(); |
| 237 | return TL; |
| 238 | } |
| 239 | |