TypeLoc.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #include "clang/AST/TypeLoc.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Expr.h"
  16. #include "clang/AST/TypeLocVisitor.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace clang;
  20. static const unsigned TypeLocMaxDataAlign = llvm::alignOf<void *>();
  21. //===----------------------------------------------------------------------===//
  22. // TypeLoc Implementation
  23. //===----------------------------------------------------------------------===//
  24. namespace {
  25. class TypeLocRanger : public TypeLocVisitor<TypeLocRanger, SourceRange> {
  26. public:
  27. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  28. #define TYPELOC(CLASS, PARENT) \
  29. SourceRange Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  30. return TyLoc.getLocalSourceRange(); \
  31. }
  32. #include "clang/AST/TypeLocNodes.def"
  33. };
  34. }
  35. SourceRange TypeLoc::getLocalSourceRangeImpl(TypeLoc TL) {
  36. if (TL.isNull()) return SourceRange();
  37. return TypeLocRanger().Visit(TL);
  38. }
  39. namespace {
  40. class TypeAligner : public TypeLocVisitor<TypeAligner, unsigned> {
  41. public:
  42. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  43. #define TYPELOC(CLASS, PARENT) \
  44. unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  45. return TyLoc.getLocalDataAlignment(); \
  46. }
  47. #include "clang/AST/TypeLocNodes.def"
  48. };
  49. }
  50. /// \brief Returns the alignment of the type source info data block.
  51. unsigned TypeLoc::getLocalAlignmentForType(QualType Ty) {
  52. if (Ty.isNull()) return 1;
  53. return TypeAligner().Visit(TypeLoc(Ty, nullptr));
  54. }
  55. namespace {
  56. class TypeSizer : public TypeLocVisitor<TypeSizer, unsigned> {
  57. public:
  58. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  59. #define TYPELOC(CLASS, PARENT) \
  60. unsigned Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  61. return TyLoc.getLocalDataSize(); \
  62. }
  63. #include "clang/AST/TypeLocNodes.def"
  64. };
  65. }
  66. /// \brief Returns the size of the type source info data block.
  67. unsigned TypeLoc::getFullDataSizeForType(QualType Ty) {
  68. unsigned Total = 0;
  69. TypeLoc TyLoc(Ty, nullptr);
  70. unsigned MaxAlign = 1;
  71. while (!TyLoc.isNull()) {
  72. unsigned Align = getLocalAlignmentForType(TyLoc.getType());
  73. MaxAlign = std::max(Align, MaxAlign);
  74. Total = llvm::alignTo(Total, Align);
  75. Total += TypeSizer().Visit(TyLoc);
  76. TyLoc = TyLoc.getNextTypeLoc();
  77. }
  78. Total = llvm::alignTo(Total, MaxAlign);
  79. return Total;
  80. }
  81. namespace {
  82. class NextLoc : public TypeLocVisitor<NextLoc, TypeLoc> {
  83. public:
  84. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  85. #define TYPELOC(CLASS, PARENT) \
  86. TypeLoc Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  87. return TyLoc.getNextTypeLoc(); \
  88. }
  89. #include "clang/AST/TypeLocNodes.def"
  90. };
  91. }
  92. /// \brief Get the next TypeLoc pointed by this TypeLoc, e.g for "int*" the
  93. /// TypeLoc is a PointerLoc and next TypeLoc is for "int".
  94. TypeLoc TypeLoc::getNextTypeLocImpl(TypeLoc TL) {
  95. return NextLoc().Visit(TL);
  96. }
  97. /// \brief Initializes a type location, and all of its children
  98. /// recursively, as if the entire tree had been written in the
  99. /// given location.
  100. void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL,
  101. SourceLocation Loc) {
  102. while (true) {
  103. switch (TL.getTypeLocClass()) {
  104. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  105. #define TYPELOC(CLASS, PARENT) \
  106. case CLASS: { \
  107. CLASS##TypeLoc TLCasted = TL.castAs<CLASS##TypeLoc>(); \
  108. TLCasted.initializeLocal(Context, Loc); \
  109. TL = TLCasted.getNextTypeLoc(); \
  110. if (!TL) return; \
  111. continue; \
  112. }
  113. #include "clang/AST/TypeLocNodes.def"
  114. }
  115. }
  116. }
  117. namespace {
  118. class TypeLocCopier : public TypeLocVisitor<TypeLocCopier> {
  119. TypeLoc Source;
  120. public:
  121. TypeLocCopier(TypeLoc source) : Source(source) { }
  122. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  123. #define TYPELOC(CLASS, PARENT) \
  124. void Visit##CLASS##TypeLoc(CLASS##TypeLoc dest) { \
  125. dest.copyLocal(Source.castAs<CLASS##TypeLoc>()); \
  126. }
  127. #include "clang/AST/TypeLocNodes.def"
  128. };
  129. }
  130. void TypeLoc::copy(TypeLoc other) {
  131. assert(getFullDataSize() == other.getFullDataSize());
  132. // If both data pointers are aligned to the maximum alignment, we
  133. // can memcpy because getFullDataSize() accurately reflects the
  134. // layout of the data.
  135. if (reinterpret_cast<uintptr_t>(Data) ==
  136. llvm::alignTo(reinterpret_cast<uintptr_t>(Data),
  137. TypeLocMaxDataAlign) &&
  138. reinterpret_cast<uintptr_t>(other.Data) ==
  139. llvm::alignTo(reinterpret_cast<uintptr_t>(other.Data),
  140. TypeLocMaxDataAlign)) {
  141. memcpy(Data, other.Data, getFullDataSize());
  142. return;
  143. }
  144. // Copy each of the pieces.
  145. TypeLoc TL(getType(), Data);
  146. do {
  147. TypeLocCopier(other).Visit(TL);
  148. other = other.getNextTypeLoc();
  149. } while ((TL = TL.getNextTypeLoc()));
  150. }
  151. SourceLocation TypeLoc::getBeginLoc() const {
  152. TypeLoc Cur = *this;
  153. TypeLoc LeftMost = Cur;
  154. while (true) {
  155. switch (Cur.getTypeLocClass()) {
  156. case Elaborated:
  157. LeftMost = Cur;
  158. break;
  159. case FunctionProto:
  160. if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()
  161. ->hasTrailingReturn()) {
  162. LeftMost = Cur;
  163. break;
  164. }
  165. /* Fall through */
  166. case FunctionNoProto:
  167. case ConstantArray:
  168. case DependentSizedArray:
  169. case IncompleteArray:
  170. case VariableArray:
  171. // FIXME: Currently QualifiedTypeLoc does not have a source range
  172. case Qualified:
  173. Cur = Cur.getNextTypeLoc();
  174. continue;
  175. default:
  176. if (Cur.getLocalSourceRange().getBegin().isValid())
  177. LeftMost = Cur;
  178. Cur = Cur.getNextTypeLoc();
  179. if (Cur.isNull())
  180. break;
  181. continue;
  182. } // switch
  183. break;
  184. } // while
  185. return LeftMost.getLocalSourceRange().getBegin();
  186. }
  187. SourceLocation TypeLoc::getEndLoc() const {
  188. TypeLoc Cur = *this;
  189. TypeLoc Last;
  190. while (true) {
  191. switch (Cur.getTypeLocClass()) {
  192. default:
  193. if (!Last)
  194. Last = Cur;
  195. return Last.getLocalSourceRange().getEnd();
  196. case Paren:
  197. case ConstantArray:
  198. case DependentSizedArray:
  199. case IncompleteArray:
  200. case VariableArray:
  201. case FunctionNoProto:
  202. Last = Cur;
  203. break;
  204. case FunctionProto:
  205. if (Cur.castAs<FunctionProtoTypeLoc>().getTypePtr()->hasTrailingReturn())
  206. Last = TypeLoc();
  207. else
  208. Last = Cur;
  209. break;
  210. case Pointer:
  211. case BlockPointer:
  212. case MemberPointer:
  213. case LValueReference:
  214. case RValueReference:
  215. case PackExpansion:
  216. if (!Last)
  217. Last = Cur;
  218. break;
  219. case Qualified:
  220. case Elaborated:
  221. break;
  222. }
  223. Cur = Cur.getNextTypeLoc();
  224. }
  225. }
  226. namespace {
  227. struct TSTChecker : public TypeLocVisitor<TSTChecker, bool> {
  228. // Overload resolution does the real work for us.
  229. static bool isTypeSpec(TypeSpecTypeLoc _) { return true; }
  230. static bool isTypeSpec(TypeLoc _) { return false; }
  231. #define ABSTRACT_TYPELOC(CLASS, PARENT)
  232. #define TYPELOC(CLASS, PARENT) \
  233. bool Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc) { \
  234. return isTypeSpec(TyLoc); \
  235. }
  236. #include "clang/AST/TypeLocNodes.def"
  237. };
  238. }
  239. /// \brief Determines if the given type loc corresponds to a
  240. /// TypeSpecTypeLoc. Since there is not actually a TypeSpecType in
  241. /// the type hierarchy, this is made somewhat complicated.
  242. ///
  243. /// There are a lot of types that currently use TypeSpecTypeLoc
  244. /// because it's a convenient base class. Ideally we would not accept
  245. /// those here, but ideally we would have better implementations for
  246. /// them.
  247. bool TypeSpecTypeLoc::isKind(const TypeLoc &TL) {
  248. if (TL.getType().hasLocalQualifiers()) return false;
  249. return TSTChecker().Visit(TL);
  250. }
  251. // Reimplemented to account for GNU/C++ extension
  252. // typeof unary-expression
  253. // where there are no parentheses.
  254. SourceRange TypeOfExprTypeLoc::getLocalSourceRange() const {
  255. if (getRParenLoc().isValid())
  256. return SourceRange(getTypeofLoc(), getRParenLoc());
  257. else
  258. return SourceRange(getTypeofLoc(),
  259. getUnderlyingExpr()->getSourceRange().getEnd());
  260. }
  261. TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const {
  262. if (needsExtraLocalData())
  263. return static_cast<TypeSpecifierType>(getWrittenBuiltinSpecs().Type);
  264. switch (getTypePtr()->getKind()) {
  265. case BuiltinType::Void:
  266. return TST_void;
  267. case BuiltinType::Bool:
  268. return TST_bool;
  269. case BuiltinType::Char_U:
  270. case BuiltinType::Char_S:
  271. return TST_char;
  272. case BuiltinType::Char16:
  273. return TST_char16;
  274. case BuiltinType::Char32:
  275. return TST_char32;
  276. case BuiltinType::WChar_S:
  277. case BuiltinType::WChar_U:
  278. return TST_wchar;
  279. case BuiltinType::UChar:
  280. case BuiltinType::UShort:
  281. case BuiltinType::UInt:
  282. case BuiltinType::ULong:
  283. case BuiltinType::ULongLong:
  284. case BuiltinType::UInt128:
  285. case BuiltinType::SChar:
  286. case BuiltinType::Short:
  287. case BuiltinType::Int:
  288. case BuiltinType::Long:
  289. case BuiltinType::LongLong:
  290. case BuiltinType::Int128:
  291. case BuiltinType::Half:
  292. case BuiltinType::Float:
  293. case BuiltinType::Double:
  294. case BuiltinType::LongDouble:
  295. llvm_unreachable("Builtin type needs extra local data!");
  296. // Fall through, if the impossible happens.
  297. case BuiltinType::NullPtr:
  298. case BuiltinType::Overload:
  299. case BuiltinType::Dependent:
  300. case BuiltinType::BoundMember:
  301. case BuiltinType::UnknownAny:
  302. case BuiltinType::ARCUnbridgedCast:
  303. case BuiltinType::PseudoObject:
  304. case BuiltinType::ObjCId:
  305. case BuiltinType::ObjCClass:
  306. case BuiltinType::ObjCSel:
  307. #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  308. case BuiltinType::Id:
  309. #include "clang/Basic/OpenCLImageTypes.def"
  310. case BuiltinType::OCLSampler:
  311. case BuiltinType::OCLEvent:
  312. case BuiltinType::OCLClkEvent:
  313. case BuiltinType::OCLQueue:
  314. case BuiltinType::OCLNDRange:
  315. case BuiltinType::OCLReserveID:
  316. case BuiltinType::BuiltinFn:
  317. case BuiltinType::OMPArraySection:
  318. return TST_unspecified;
  319. }
  320. llvm_unreachable("Invalid BuiltinType Kind!");
  321. }
  322. TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) {
  323. while (ParenTypeLoc PTL = TL.getAs<ParenTypeLoc>())
  324. TL = PTL.getInnerLoc();
  325. return TL;
  326. }
  327. SourceLocation TypeLoc::findNullabilityLoc() const {
  328. if (auto attributedLoc = getAs<AttributedTypeLoc>()) {
  329. if (attributedLoc.getAttrKind() == AttributedType::attr_nullable ||
  330. attributedLoc.getAttrKind() == AttributedType::attr_nonnull ||
  331. attributedLoc.getAttrKind() == AttributedType::attr_null_unspecified)
  332. return attributedLoc.getAttrNameLoc();
  333. }
  334. return SourceLocation();
  335. }
  336. TypeLoc TypeLoc::findExplicitQualifierLoc() const {
  337. // Qualified types.
  338. if (auto qual = getAs<QualifiedTypeLoc>())
  339. return qual;
  340. TypeLoc loc = IgnoreParens();
  341. // Attributed types.
  342. if (auto attr = loc.getAs<AttributedTypeLoc>()) {
  343. if (attr.isQualifier()) return attr;
  344. return attr.getModifiedLoc().findExplicitQualifierLoc();
  345. }
  346. // C11 _Atomic types.
  347. if (auto atomic = loc.getAs<AtomicTypeLoc>()) {
  348. return atomic;
  349. }
  350. return TypeLoc();
  351. }
  352. void ObjCObjectTypeLoc::initializeLocal(ASTContext &Context,
  353. SourceLocation Loc) {
  354. setHasBaseTypeAsWritten(true);
  355. setTypeArgsLAngleLoc(Loc);
  356. setTypeArgsRAngleLoc(Loc);
  357. for (unsigned i = 0, e = getNumTypeArgs(); i != e; ++i) {
  358. setTypeArgTInfo(i,
  359. Context.getTrivialTypeSourceInfo(
  360. getTypePtr()->getTypeArgsAsWritten()[i], Loc));
  361. }
  362. setProtocolLAngleLoc(Loc);
  363. setProtocolRAngleLoc(Loc);
  364. for (unsigned i = 0, e = getNumProtocols(); i != e; ++i)
  365. setProtocolLoc(i, Loc);
  366. }
  367. void TypeOfTypeLoc::initializeLocal(ASTContext &Context,
  368. SourceLocation Loc) {
  369. TypeofLikeTypeLoc<TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo>
  370. ::initializeLocal(Context, Loc);
  371. this->getLocalData()->UnderlyingTInfo = Context.getTrivialTypeSourceInfo(
  372. getUnderlyingType(), Loc);
  373. }
  374. void ElaboratedTypeLoc::initializeLocal(ASTContext &Context,
  375. SourceLocation Loc) {
  376. setElaboratedKeywordLoc(Loc);
  377. NestedNameSpecifierLocBuilder Builder;
  378. Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
  379. setQualifierLoc(Builder.getWithLocInContext(Context));
  380. }
  381. void DependentNameTypeLoc::initializeLocal(ASTContext &Context,
  382. SourceLocation Loc) {
  383. setElaboratedKeywordLoc(Loc);
  384. NestedNameSpecifierLocBuilder Builder;
  385. Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
  386. setQualifierLoc(Builder.getWithLocInContext(Context));
  387. setNameLoc(Loc);
  388. }
  389. void
  390. DependentTemplateSpecializationTypeLoc::initializeLocal(ASTContext &Context,
  391. SourceLocation Loc) {
  392. setElaboratedKeywordLoc(Loc);
  393. if (getTypePtr()->getQualifier()) {
  394. NestedNameSpecifierLocBuilder Builder;
  395. Builder.MakeTrivial(Context, getTypePtr()->getQualifier(), Loc);
  396. setQualifierLoc(Builder.getWithLocInContext(Context));
  397. } else {
  398. setQualifierLoc(NestedNameSpecifierLoc());
  399. }
  400. setTemplateKeywordLoc(Loc);
  401. setTemplateNameLoc(Loc);
  402. setLAngleLoc(Loc);
  403. setRAngleLoc(Loc);
  404. TemplateSpecializationTypeLoc::initializeArgLocs(Context, getNumArgs(),
  405. getTypePtr()->getArgs(),
  406. getArgInfos(), Loc);
  407. }
  408. void TemplateSpecializationTypeLoc::initializeArgLocs(ASTContext &Context,
  409. unsigned NumArgs,
  410. const TemplateArgument *Args,
  411. TemplateArgumentLocInfo *ArgInfos,
  412. SourceLocation Loc) {
  413. for (unsigned i = 0, e = NumArgs; i != e; ++i) {
  414. switch (Args[i].getKind()) {
  415. case TemplateArgument::Null:
  416. llvm_unreachable("Impossible TemplateArgument");
  417. case TemplateArgument::Integral:
  418. case TemplateArgument::Declaration:
  419. case TemplateArgument::NullPtr:
  420. ArgInfos[i] = TemplateArgumentLocInfo();
  421. break;
  422. case TemplateArgument::Expression:
  423. ArgInfos[i] = TemplateArgumentLocInfo(Args[i].getAsExpr());
  424. break;
  425. case TemplateArgument::Type:
  426. ArgInfos[i] = TemplateArgumentLocInfo(
  427. Context.getTrivialTypeSourceInfo(Args[i].getAsType(),
  428. Loc));
  429. break;
  430. case TemplateArgument::Template:
  431. case TemplateArgument::TemplateExpansion: {
  432. NestedNameSpecifierLocBuilder Builder;
  433. TemplateName Template = Args[i].getAsTemplateOrTemplatePattern();
  434. if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
  435. Builder.MakeTrivial(Context, DTN->getQualifier(), Loc);
  436. else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
  437. Builder.MakeTrivial(Context, QTN->getQualifier(), Loc);
  438. ArgInfos[i] = TemplateArgumentLocInfo(
  439. Builder.getWithLocInContext(Context), Loc,
  440. Args[i].getKind() == TemplateArgument::Template ? SourceLocation()
  441. : Loc);
  442. break;
  443. }
  444. case TemplateArgument::Pack:
  445. ArgInfos[i] = TemplateArgumentLocInfo();
  446. break;
  447. }
  448. }
  449. }